2010-10-12 03:22:38 +00:00
|
|
|
{- git repository handling
|
|
|
|
-
|
|
|
|
- This is written to be completely independant of git-annex and should be
|
|
|
|
- suitable for other uses.
|
|
|
|
-
|
2010-10-27 20:53:54 +00:00
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2010-10-14 06:36:41 +00:00
|
|
|
-}
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2010-10-11 21:52:46 +00:00
|
|
|
module GitRepo (
|
2010-10-14 06:36:41 +00:00
|
|
|
Repo,
|
|
|
|
repoFromCwd,
|
2011-03-04 01:02:29 +00:00
|
|
|
repoFromAbsPath,
|
2011-03-28 01:43:25 +00:00
|
|
|
repoFromUnknown,
|
2010-10-14 06:36:41 +00:00
|
|
|
repoFromUrl,
|
2011-02-04 05:56:45 +00:00
|
|
|
localToUrl,
|
2010-10-22 18:05:30 +00:00
|
|
|
repoIsUrl,
|
2010-10-22 17:40:19 +00:00
|
|
|
repoIsSsh,
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare,
|
2010-10-14 06:36:41 +00:00
|
|
|
repoDescribe,
|
2011-02-03 22:47:14 +00:00
|
|
|
repoLocation,
|
2010-10-14 06:36:41 +00:00
|
|
|
workTree,
|
2011-04-25 18:54:24 +00:00
|
|
|
workTreeFile,
|
2010-10-31 19:38:47 +00:00
|
|
|
gitDir,
|
2010-10-22 19:06:14 +00:00
|
|
|
urlPath,
|
|
|
|
urlHost,
|
2011-03-05 19:13:16 +00:00
|
|
|
urlPort,
|
|
|
|
urlHostUser,
|
|
|
|
urlAuthority,
|
2011-02-03 22:47:14 +00:00
|
|
|
urlScheme,
|
2010-10-14 06:36:41 +00:00
|
|
|
configGet,
|
|
|
|
configMap,
|
|
|
|
configRead,
|
2010-12-31 19:46:33 +00:00
|
|
|
hConfigRead,
|
|
|
|
configStore,
|
2010-10-28 16:15:21 +00:00
|
|
|
configTrue,
|
2010-10-31 19:25:55 +00:00
|
|
|
gitCommandLine,
|
2010-10-14 06:36:41 +00:00
|
|
|
run,
|
2010-10-16 18:20:43 +00:00
|
|
|
pipeRead,
|
2010-10-14 06:36:41 +00:00
|
|
|
attributes,
|
|
|
|
remotes,
|
|
|
|
remotesAdd,
|
2010-10-16 18:20:43 +00:00
|
|
|
repoRemoteName,
|
2011-03-28 01:43:25 +00:00
|
|
|
repoRemoteNameSet,
|
2010-10-16 18:58:14 +00:00
|
|
|
inRepo,
|
2010-10-29 21:26:26 +00:00
|
|
|
notInRepo,
|
2010-11-01 22:24:19 +00:00
|
|
|
stagedFiles,
|
2011-02-02 01:58:47 +00:00
|
|
|
stagedFilesNotDeleted,
|
2011-01-11 20:00:40 +00:00
|
|
|
changedUnstagedFiles,
|
2010-11-02 19:54:43 +00:00
|
|
|
checkAttr,
|
|
|
|
decodeGitFile,
|
|
|
|
encodeGitFile,
|
2010-11-10 18:01:41 +00:00
|
|
|
typeChangedFiles,
|
2010-11-11 22:21:54 +00:00
|
|
|
typeChangedStagedFiles,
|
2011-03-04 01:02:29 +00:00
|
|
|
repoAbsPath,
|
2011-02-13 04:50:09 +00:00
|
|
|
reap,
|
2010-11-02 19:54:43 +00:00
|
|
|
|
2010-11-02 20:49:35 +00:00
|
|
|
prop_idempotent_deencode
|
2010-10-11 21:52:46 +00:00
|
|
|
) where
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2011-04-25 18:54:24 +00:00
|
|
|
import Control.Monad (unless)
|
2010-11-06 21:07:11 +00:00
|
|
|
import System.Directory
|
2011-03-04 01:02:29 +00:00
|
|
|
import System.FilePath
|
2010-10-12 16:47:11 +00:00
|
|
|
import System.Posix.Directory
|
2011-01-01 01:22:03 +00:00
|
|
|
import System.Posix.User
|
2011-02-13 04:50:09 +00:00
|
|
|
import System.Posix.Process
|
2010-10-10 02:09:10 +00:00
|
|
|
import System.Path
|
2010-10-11 04:19:38 +00:00
|
|
|
import System.Cmd.Utils
|
2010-10-12 16:47:11 +00:00
|
|
|
import IO (bracket_)
|
2010-10-10 01:06:46 +00:00
|
|
|
import Data.String.Utils
|
2010-10-31 19:38:47 +00:00
|
|
|
import System.IO
|
2010-10-29 21:26:26 +00:00
|
|
|
import qualified Data.Map as Map hiding (map, split)
|
2010-10-12 04:53:42 +00:00
|
|
|
import Network.URI
|
2010-11-06 21:07:11 +00:00
|
|
|
import Data.Maybe
|
|
|
|
import Data.Char
|
2010-11-02 19:54:43 +00:00
|
|
|
import Data.Word (Word8)
|
|
|
|
import Codec.Binary.UTF8.String (encode)
|
2010-11-06 21:07:11 +00:00
|
|
|
import Text.Printf
|
2011-05-22 18:12:16 +00:00
|
|
|
import Data.List (isInfixOf, isPrefixOf, isSuffixOf)
|
Avoid pipeline stall when running git annex drop or fsck on a lot of files.
When it's stalled, there are 3 processes:
git annex
git ls-files
git check-attr
git-annex stalls trying to write to git check-attr, which stalls trying to
write to stdout (read by git-annex).
git ls-files does not seem to be involved directly; I've seen the stall when
it was still streaming out the file list, and after it had exited and
zombified.
The read and write are supposed to be handled by two different threads,
which pipeBoth forks off, thus avoiding deadlock. But it does deadlock.
(Certian signals unblock the deadlock for a while, then it stalls again.)
So, this is another case of WTF is the ghc IO manager doing today?
I avoid the issue by converting the writer to a separate process.
Possibly this was caused by some change in ghc 7 -- I'm offline and cannot
verify now, but I'm sure I used to be able to run git annex drop w/o it
hanging! And the code does not seem to have changed, except for commit
c1dc4079419cff94cca72441d5e67a866110ec7e, which I tried reverting without
success. In fact, I reverted all the way back to 0.20110316 and still
saw the stall.
Update: Minimal test case:
import System.Cmd.Utils
main = do
as <- checkAttr "blah" $ map show [1..100000]
sequence $ map (putStrLn . show) as
checkAttr attr files = do
(_, s) <- pipeBoth "git" params $ unlines files
return $ lines s
where
params = ["check-attr", attr, "--stdin"]
Bug filed on ghc in debian, #624389
2011-04-28 02:10:57 +00:00
|
|
|
import System.Exit
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-10 03:35:05 +00:00
|
|
|
import Utility
|
2010-10-11 21:19:55 +00:00
|
|
|
|
2010-10-22 18:05:30 +00:00
|
|
|
{- There are two types of repositories; those on local disk and those
|
|
|
|
- accessed via an URL. -}
|
2011-03-28 01:43:25 +00:00
|
|
|
data RepoLocation = Dir FilePath | Url URI | Unknown
|
2010-10-28 17:40:10 +00:00
|
|
|
deriving (Show, Eq)
|
2010-10-12 04:53:42 +00:00
|
|
|
|
2010-10-28 17:40:10 +00:00
|
|
|
data Repo = Repo {
|
|
|
|
location :: RepoLocation,
|
2010-10-29 21:26:26 +00:00
|
|
|
config :: Map.Map String String,
|
2010-10-28 17:40:10 +00:00
|
|
|
remotes :: [Repo],
|
|
|
|
-- remoteName holds the name used for this repo in remotes
|
|
|
|
remoteName :: Maybe String
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
newFrom :: RepoLocation -> Repo
|
2010-10-28 17:40:10 +00:00
|
|
|
newFrom l =
|
2010-10-22 18:05:30 +00:00
|
|
|
Repo {
|
2010-10-28 17:40:10 +00:00
|
|
|
location = l,
|
2010-10-13 18:01:17 +00:00
|
|
|
config = Map.empty,
|
2010-10-14 02:59:43 +00:00
|
|
|
remotes = [],
|
2010-10-13 18:01:17 +00:00
|
|
|
remoteName = Nothing
|
2010-10-10 22:05:37 +00:00
|
|
|
}
|
2010-10-10 06:22:47 +00:00
|
|
|
|
2011-03-04 01:02:29 +00:00
|
|
|
{- Local Repo constructor, requires an absolute path to the repo be
|
|
|
|
- specified. -}
|
|
|
|
repoFromAbsPath :: FilePath -> IO Repo
|
|
|
|
repoFromAbsPath dir
|
|
|
|
| "/" `isPrefixOf` dir = do
|
|
|
|
-- Git always looks for "dir.git" in preference to
|
|
|
|
-- to "dir", even if dir ends in a "/".
|
2011-05-22 18:12:16 +00:00
|
|
|
let canondir = dropTrailingPathSeparator dir
|
|
|
|
let dir' = canondir ++ ".git"
|
2011-03-04 01:02:29 +00:00
|
|
|
e <- doesDirectoryExist dir'
|
2011-05-22 18:12:16 +00:00
|
|
|
if e
|
|
|
|
then ret dir'
|
|
|
|
else if "/.git" `isSuffixOf` canondir
|
|
|
|
then do
|
|
|
|
-- When dir == "foo/.git", git looks
|
|
|
|
-- for "foo/.git/.git", and failing
|
|
|
|
-- that, uses "foo" as the repository.
|
|
|
|
e' <- doesDirectoryExist $ dir </> ".git"
|
|
|
|
if e'
|
|
|
|
then ret dir
|
|
|
|
else ret $ takeDirectory canondir
|
|
|
|
else ret dir
|
2011-03-04 01:02:29 +00:00
|
|
|
| otherwise = error $ "internal error, " ++ dir ++ " is not absolute"
|
2011-05-22 18:12:16 +00:00
|
|
|
where
|
|
|
|
ret = return . newFrom . Dir
|
2010-10-28 17:40:10 +00:00
|
|
|
|
2010-10-14 06:36:41 +00:00
|
|
|
{- Remote Repo constructor. Throws exception on invalid url. -}
|
2011-03-04 01:02:29 +00:00
|
|
|
repoFromUrl :: String -> IO Repo
|
2010-10-28 17:47:10 +00:00
|
|
|
repoFromUrl url
|
2011-03-04 01:02:29 +00:00
|
|
|
| startswith "file://" url = repoFromAbsPath $ uriPath u
|
|
|
|
| otherwise = return $ newFrom $ Url u
|
2011-02-03 22:47:14 +00:00
|
|
|
where
|
2011-05-15 06:49:43 +00:00
|
|
|
u = maybe bad id $ parseURI url
|
|
|
|
bad = error $ "bad url " ++ url
|
2010-10-12 04:53:42 +00:00
|
|
|
|
2011-03-28 01:43:25 +00:00
|
|
|
{- Creates a repo that has an unknown location. -}
|
|
|
|
repoFromUnknown :: Repo
|
|
|
|
repoFromUnknown = newFrom Unknown
|
|
|
|
|
2011-02-04 05:56:45 +00:00
|
|
|
{- Converts a Local Repo into a remote repo, using the reference repo
|
|
|
|
- which is assumed to be on the same host. -}
|
|
|
|
localToUrl :: Repo -> Repo -> Repo
|
|
|
|
localToUrl reference r
|
|
|
|
| not $ repoIsUrl reference = error "internal error; reference repo not url"
|
|
|
|
| repoIsUrl r = r
|
|
|
|
| otherwise = r { location = Url $ fromJust $ parseURI absurl }
|
|
|
|
where
|
|
|
|
absurl =
|
|
|
|
urlScheme reference ++ "//" ++
|
2011-03-05 19:13:16 +00:00
|
|
|
urlAuthority reference ++
|
2011-02-04 05:56:45 +00:00
|
|
|
workTree r
|
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- User-visible description of a git repo. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
repoDescribe :: Repo -> String
|
2010-10-28 17:40:10 +00:00
|
|
|
repoDescribe Repo { remoteName = Just name } = name
|
|
|
|
repoDescribe Repo { location = Url url } = show url
|
|
|
|
repoDescribe Repo { location = Dir dir } = dir
|
2011-03-28 01:43:25 +00:00
|
|
|
repoDescribe Repo { location = Unknown } = "UNKNOWN"
|
2010-10-13 18:40:56 +00:00
|
|
|
|
2011-02-03 22:47:14 +00:00
|
|
|
{- Location of the repo, either as a path or url. -}
|
|
|
|
repoLocation :: Repo -> String
|
|
|
|
repoLocation Repo { location = Url url } = show url
|
|
|
|
repoLocation Repo { location = Dir dir } = dir
|
2011-03-28 01:43:25 +00:00
|
|
|
repoLocation Repo { location = Unknown } = undefined
|
2011-02-03 22:47:14 +00:00
|
|
|
|
2010-10-14 02:59:43 +00:00
|
|
|
{- Constructs and returns an updated version of a repo with
|
|
|
|
- different remotes list. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
remotesAdd :: Repo -> [Repo] -> Repo
|
|
|
|
remotesAdd repo rs = repo { remotes = rs }
|
2010-10-14 02:59:43 +00:00
|
|
|
|
2011-02-04 02:20:55 +00:00
|
|
|
{- Returns the name of the remote that corresponds to the repo, if
|
|
|
|
- it is a remote. -}
|
|
|
|
repoRemoteName :: Repo -> Maybe String
|
|
|
|
repoRemoteName Repo { remoteName = Just name } = Just name
|
|
|
|
repoRemoteName _ = Nothing
|
2010-10-12 16:23:34 +00:00
|
|
|
|
2011-03-28 01:43:25 +00:00
|
|
|
{- Sets the name of a remote based on the git config key, such as
|
|
|
|
"remote.foo.url". -}
|
|
|
|
repoRemoteNameSet :: Repo -> String -> Repo
|
|
|
|
repoRemoteNameSet r k = r { remoteName = Just basename }
|
|
|
|
where
|
|
|
|
basename = join "." $ reverse $ drop 1 $
|
|
|
|
reverse $ drop 1 $ split "." k
|
|
|
|
|
2010-10-22 18:05:30 +00:00
|
|
|
{- Some code needs to vary between URL and normal repos,
|
2010-10-22 16:38:20 +00:00
|
|
|
- or bare and non-bare, these functions help with that. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
repoIsUrl :: Repo -> Bool
|
2010-10-28 17:40:10 +00:00
|
|
|
repoIsUrl Repo { location = Url _ } = True
|
|
|
|
repoIsUrl _ = False
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
repoIsSsh :: Repo -> Bool
|
2010-10-28 17:40:10 +00:00
|
|
|
repoIsSsh Repo { location = Url url }
|
|
|
|
| uriScheme url == "ssh:" = True
|
2010-12-14 16:46:09 +00:00
|
|
|
-- git treats these the same as ssh
|
2010-12-14 15:37:11 +00:00
|
|
|
| uriScheme url == "git+ssh:" = True
|
2010-12-14 16:46:09 +00:00
|
|
|
| uriScheme url == "ssh+git:" = True
|
2010-10-28 17:40:10 +00:00
|
|
|
| otherwise = False
|
|
|
|
repoIsSsh _ = False
|
|
|
|
|
2011-03-03 21:33:15 +00:00
|
|
|
configAvail ::Repo -> Bool
|
|
|
|
configAvail Repo { config = c } = c /= Map.empty
|
|
|
|
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare :: Repo -> Bool
|
2011-03-03 21:33:15 +00:00
|
|
|
repoIsLocalBare r@(Repo { location = Dir _ }) = configAvail r && configBare r
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare _ = False
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
assertLocal :: Repo -> a -> a
|
2010-10-22 18:05:30 +00:00
|
|
|
assertLocal repo action =
|
2010-11-22 19:46:57 +00:00
|
|
|
if not $ repoIsUrl repo
|
2010-10-12 04:53:42 +00:00
|
|
|
then action
|
2010-11-06 21:07:11 +00:00
|
|
|
else error $ "acting on URL git repo " ++ repoDescribe repo ++
|
2010-10-12 06:51:44 +00:00
|
|
|
" not supported"
|
2010-10-31 19:38:47 +00:00
|
|
|
assertUrl :: Repo -> a -> a
|
2010-10-22 18:05:30 +00:00
|
|
|
assertUrl repo action =
|
2010-11-22 19:46:57 +00:00
|
|
|
if repoIsUrl repo
|
2010-10-22 17:40:19 +00:00
|
|
|
then action
|
2010-11-06 21:07:11 +00:00
|
|
|
else error $ "acting on local git repo " ++ repoDescribe repo ++
|
2010-10-22 17:40:19 +00:00
|
|
|
" not supported"
|
2010-12-31 19:46:33 +00:00
|
|
|
|
2011-03-03 18:51:57 +00:00
|
|
|
configBare :: Repo -> Bool
|
2011-05-15 16:25:58 +00:00
|
|
|
configBare repo = maybe unknown configTrue $ Map.lookup "core.bare" $ config repo
|
|
|
|
where
|
|
|
|
unknown = error $ "it is not known if git repo " ++
|
2010-11-06 21:07:11 +00:00
|
|
|
repoDescribe repo ++
|
2010-10-12 16:23:34 +00:00
|
|
|
" is a bare repository; config not read"
|
2010-10-11 21:52:46 +00:00
|
|
|
|
2010-10-10 16:35:28 +00:00
|
|
|
{- Path to a repository's gitattributes file. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
attributes :: Repo -> String
|
2010-10-28 17:40:10 +00:00
|
|
|
attributes repo
|
2011-03-03 18:51:57 +00:00
|
|
|
| configBare repo = workTree repo ++ "/info/.gitattributes"
|
2010-11-06 21:07:11 +00:00
|
|
|
| otherwise = workTree repo ++ "/.gitattributes"
|
2010-10-10 06:29:58 +00:00
|
|
|
|
2010-10-22 19:06:14 +00:00
|
|
|
{- Path to a repository's .git directory, relative to its workTree. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
gitDir :: Repo -> String
|
|
|
|
gitDir repo
|
2011-03-03 18:51:57 +00:00
|
|
|
| configBare repo = ""
|
2010-10-28 17:40:10 +00:00
|
|
|
| otherwise = ".git"
|
2010-10-10 06:29:58 +00:00
|
|
|
|
2010-10-22 19:06:14 +00:00
|
|
|
{- Path to a repository's --work-tree, that is, its top.
|
|
|
|
-
|
2010-10-28 17:40:10 +00:00
|
|
|
- Note that for URL repositories, this is the path on the remote host. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
workTree :: Repo -> FilePath
|
2010-10-28 17:40:10 +00:00
|
|
|
workTree r@(Repo { location = Url _ }) = urlPath r
|
|
|
|
workTree (Repo { location = Dir d }) = d
|
2011-03-28 01:43:25 +00:00
|
|
|
workTree Repo { location = Unknown } = undefined
|
2010-10-12 04:53:42 +00:00
|
|
|
|
2011-04-25 18:54:24 +00:00
|
|
|
{- Given a relative or absolute filename inside a git repository's
|
|
|
|
- workTree, calculates the name to use to refer to that file to git.
|
|
|
|
-
|
|
|
|
- This is complicated because the best choice can vary depending on
|
|
|
|
- whether the cwd is in a subdirectory of the git repository, or not.
|
|
|
|
-
|
|
|
|
- For example, when adding a file "/tmp/repo/foo", it's best to refer
|
|
|
|
- to it as "foo" if the cwd is outside the repository entirely
|
|
|
|
- (this avoids a gotcha with using the full path name when /tmp/repo
|
|
|
|
- is itself a symlink). But, if the cwd is "/tmp/repo/subdir",
|
|
|
|
- it's best to refer to "../foo".
|
|
|
|
-}
|
|
|
|
workTreeFile :: Repo -> FilePath -> IO FilePath
|
|
|
|
workTreeFile repo@(Repo { location = Dir d }) file = do
|
2010-11-28 21:17:18 +00:00
|
|
|
cwd <- getCurrentDirectory
|
2011-04-25 17:02:54 +00:00
|
|
|
let file' = absfile cwd
|
2011-04-25 18:54:24 +00:00
|
|
|
unless (inrepo file') $
|
2011-04-25 17:02:54 +00:00
|
|
|
error $ file ++ " is not located inside git repository " ++ absrepo
|
2011-04-25 18:54:24 +00:00
|
|
|
if (inrepo $ addTrailingPathSeparator cwd)
|
|
|
|
then return $ relPathDirToFile cwd file'
|
|
|
|
else return $ drop (length absrepo) file'
|
2010-10-10 02:09:10 +00:00
|
|
|
where
|
|
|
|
-- normalize both repo and file, so that repo
|
|
|
|
-- will be substring of file
|
2011-05-15 16:25:58 +00:00
|
|
|
absrepo = maybe bad addTrailingPathSeparator $ absNormPath "/" d
|
2011-05-15 06:49:43 +00:00
|
|
|
absfile c = maybe file id $ secureAbsNormPath c file
|
2011-04-25 18:54:24 +00:00
|
|
|
inrepo f = absrepo `isPrefixOf` f
|
2011-05-15 16:25:58 +00:00
|
|
|
bad = error $ "bad repo" ++ repoDescribe repo
|
2011-04-25 18:54:24 +00:00
|
|
|
workTreeFile repo _ = assertLocal repo $ error "internal"
|
2010-10-10 02:09:10 +00:00
|
|
|
|
2011-03-05 19:13:16 +00:00
|
|
|
{- Path of an URL repo. -}
|
|
|
|
urlPath :: Repo -> String
|
|
|
|
urlPath Repo { location = Url u } = uriPath u
|
|
|
|
urlPath repo = assertUrl repo $ error "internal"
|
|
|
|
|
2011-02-03 22:47:14 +00:00
|
|
|
{- Scheme of an URL repo. -}
|
|
|
|
urlScheme :: Repo -> String
|
|
|
|
urlScheme Repo { location = Url u } = uriScheme u
|
|
|
|
urlScheme repo = assertUrl repo $ error "internal"
|
|
|
|
|
2011-05-06 19:21:30 +00:00
|
|
|
{- Work around a bug in the real uriRegName
|
|
|
|
- <http://trac.haskell.org/network/ticket/40> -}
|
|
|
|
uriRegName' :: URIAuth -> String
|
|
|
|
uriRegName' a = fixup $ uriRegName a
|
|
|
|
where
|
|
|
|
fixup x@('[':rest)
|
|
|
|
| rest !! len == ']' = take len rest
|
|
|
|
| otherwise = x
|
|
|
|
where
|
|
|
|
len = (length rest) - 1
|
|
|
|
fixup x = x
|
|
|
|
|
2011-03-05 19:13:16 +00:00
|
|
|
{- Hostname of an URL repo. -}
|
2010-10-22 19:06:14 +00:00
|
|
|
urlHost :: Repo -> String
|
2011-05-06 19:21:30 +00:00
|
|
|
urlHost = urlAuthPart uriRegName'
|
2010-10-22 19:06:14 +00:00
|
|
|
|
2011-03-05 19:13:16 +00:00
|
|
|
{- Port of an URL repo, if it has a nonstandard one. -}
|
|
|
|
urlPort :: Repo -> Maybe Integer
|
|
|
|
urlPort r =
|
|
|
|
case urlAuthPart uriPort r of
|
|
|
|
":" -> Nothing
|
|
|
|
(':':p) -> Just (read p)
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
{- Hostname of an URL repo, including any username (ie, "user@host") -}
|
|
|
|
urlHostUser :: Repo -> String
|
2011-05-06 19:21:30 +00:00
|
|
|
urlHostUser r = urlAuthPart uriUserInfo r ++ urlAuthPart uriRegName' r
|
2011-03-05 19:13:16 +00:00
|
|
|
|
|
|
|
{- The full authority portion an URL repo. (ie, "user@host:port") -}
|
|
|
|
urlAuthority :: Repo -> String
|
2011-05-06 19:21:30 +00:00
|
|
|
urlAuthority Repo { location = Url u } = uriUserInfo a ++ uriRegName' a ++ uriPort a
|
2011-02-04 02:20:55 +00:00
|
|
|
where
|
|
|
|
a = fromMaybe (error $ "bad url " ++ show u) (uriAuthority u)
|
2011-03-05 19:13:16 +00:00
|
|
|
urlAuthority repo = assertUrl repo $ error "internal"
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-03-05 19:13:16 +00:00
|
|
|
{- Applies a function to extract part of the uriAuthority of an URL repo. -}
|
|
|
|
urlAuthPart :: (URIAuth -> a) -> Repo -> a
|
|
|
|
urlAuthPart a Repo { location = Url u } = a auth
|
|
|
|
where
|
|
|
|
auth = fromMaybe (error $ "bad url " ++ show u) (uriAuthority u)
|
|
|
|
urlAuthPart _ repo = assertUrl repo $ error "internal"
|
2010-10-22 19:06:14 +00:00
|
|
|
|
2010-10-12 03:22:38 +00:00
|
|
|
{- Constructs a git command line operating on the specified repo. -}
|
2011-02-28 20:25:31 +00:00
|
|
|
gitCommandLine :: Repo -> [CommandParam] -> [CommandParam]
|
2010-10-28 17:40:10 +00:00
|
|
|
gitCommandLine repo@(Repo { location = Dir d} ) params =
|
2010-10-12 03:22:38 +00:00
|
|
|
-- force use of specified repo via --git-dir and --work-tree
|
2011-02-28 20:10:16 +00:00
|
|
|
[ Param ("--git-dir=" ++ d ++ "/" ++ gitDir repo)
|
|
|
|
, Param ("--work-tree=" ++ d)
|
|
|
|
] ++ params
|
2010-10-28 17:40:10 +00:00
|
|
|
gitCommandLine repo _ = assertLocal repo $ error "internal"
|
2010-10-12 03:22:38 +00:00
|
|
|
|
2010-10-29 18:07:26 +00:00
|
|
|
{- Runs git in the specified repo, throwing an error if it fails. -}
|
2011-02-28 20:25:31 +00:00
|
|
|
run :: Repo -> String -> [CommandParam] -> IO ()
|
2011-05-17 07:10:13 +00:00
|
|
|
run repo subcommand params = assertLocal repo $
|
|
|
|
boolSystem "git" (gitCommandLine repo ((Param subcommand):params))
|
2011-05-17 15:44:13 +00:00
|
|
|
>>! error $ "git " ++ show params ++ " failed"
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2011-02-13 04:50:09 +00:00
|
|
|
{- Runs a git subcommand and returns it output, lazily.
|
|
|
|
-
|
|
|
|
- Note that this leaves the git process running, and so zombies will
|
|
|
|
- result unless reap is called.
|
|
|
|
-}
|
2011-02-28 20:25:31 +00:00
|
|
|
pipeRead :: Repo -> [CommandParam] -> IO String
|
2010-10-22 18:05:30 +00:00
|
|
|
pipeRead repo params = assertLocal repo $ do
|
2011-02-28 20:25:31 +00:00
|
|
|
(_, s) <- pipeFrom "git" $ toCommand $ gitCommandLine repo params
|
2011-02-13 04:50:09 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
{- Reaps any zombie git processes. -}
|
|
|
|
reap :: IO ()
|
|
|
|
reap = do
|
|
|
|
-- throws an exception when there are no child processes
|
|
|
|
r <- catch (getAnyProcessStatus False True) (\_ -> return Nothing)
|
2011-05-15 06:49:43 +00:00
|
|
|
maybe (return ()) (const reap) r
|
2010-10-31 03:19:33 +00:00
|
|
|
|
2010-12-24 01:53:32 +00:00
|
|
|
{- Scans for files that are checked into git at the specified locations. -}
|
|
|
|
inRepo :: Repo -> [FilePath] -> IO [FilePath]
|
|
|
|
inRepo repo l = pipeNullSplit repo $
|
2011-02-28 20:10:16 +00:00
|
|
|
[Params "ls-files --cached --exclude-standard -z --"] ++ map File l
|
2010-12-24 01:53:32 +00:00
|
|
|
|
|
|
|
{- Scans for files at the specified locations that are not checked into git,
|
|
|
|
- and not gitignored. -}
|
|
|
|
notInRepo :: Repo -> [FilePath] -> IO [FilePath]
|
|
|
|
notInRepo repo l = pipeNullSplit repo $
|
2011-02-28 20:10:16 +00:00
|
|
|
[Params "ls-files --others --exclude-standard -z --"] ++ map File l
|
2010-12-24 01:53:32 +00:00
|
|
|
|
2011-02-02 01:58:47 +00:00
|
|
|
{- Returns a list of all files that are staged for commit. -}
|
|
|
|
stagedFiles :: Repo -> [FilePath] -> IO [FilePath]
|
|
|
|
stagedFiles repo l = stagedFiles' repo l []
|
|
|
|
|
2010-12-24 01:53:32 +00:00
|
|
|
{- Returns a list of the files, staged for commit, that are being added,
|
|
|
|
- moved, or changed (but not deleted), from the specified locations. -}
|
2011-02-02 01:58:47 +00:00
|
|
|
stagedFilesNotDeleted :: Repo -> [FilePath] -> IO [FilePath]
|
2011-02-28 20:10:16 +00:00
|
|
|
stagedFilesNotDeleted repo l = stagedFiles' repo l [Param "--diff-filter=ACMRT"]
|
2011-02-02 01:58:47 +00:00
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
stagedFiles' :: Repo -> [FilePath] -> [CommandParam] -> IO [FilePath]
|
2011-02-02 01:58:47 +00:00
|
|
|
stagedFiles' repo l middle = pipeNullSplit repo $ start ++ middle ++ end
|
|
|
|
where
|
2011-02-28 20:10:16 +00:00
|
|
|
start = [Params "diff --cached --name-only -z"]
|
|
|
|
end = [Param "--"] ++ map File l
|
2010-11-10 18:01:41 +00:00
|
|
|
|
2011-01-11 20:00:40 +00:00
|
|
|
{- Returns a list of files that have unstaged changes. -}
|
|
|
|
changedUnstagedFiles :: Repo -> [FilePath] -> IO [FilePath]
|
|
|
|
changedUnstagedFiles repo l = pipeNullSplit repo $
|
2011-02-28 20:10:16 +00:00
|
|
|
[Params "diff --name-only -z --"] ++ map File l
|
2011-01-11 20:00:40 +00:00
|
|
|
|
2010-12-24 01:53:32 +00:00
|
|
|
{- Returns a list of the files in the specified locations that are staged
|
|
|
|
- for commit, and whose type has changed. -}
|
|
|
|
typeChangedStagedFiles :: Repo -> [FilePath] -> IO [FilePath]
|
2011-02-28 20:10:16 +00:00
|
|
|
typeChangedStagedFiles repo l = typeChangedFiles' repo l [Param "--cached"]
|
2010-11-11 22:21:54 +00:00
|
|
|
|
2010-12-24 01:53:32 +00:00
|
|
|
{- Returns a list of the files in the specified locations whose type has
|
|
|
|
- changed. Files only staged for commit will not be included. -}
|
|
|
|
typeChangedFiles :: Repo -> [FilePath] -> IO [FilePath]
|
2010-11-11 22:21:54 +00:00
|
|
|
typeChangedFiles repo l = typeChangedFiles' repo l []
|
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
typeChangedFiles' :: Repo -> [FilePath] -> [CommandParam] -> IO [FilePath]
|
2010-11-11 22:21:54 +00:00
|
|
|
typeChangedFiles' repo l middle = pipeNullSplit repo $ start ++ middle ++ end
|
2010-11-10 18:01:41 +00:00
|
|
|
where
|
2011-02-28 20:10:16 +00:00
|
|
|
start = [Params "diff --name-only --diff-filter=T -z"]
|
|
|
|
end = [Param "--"] ++ map File l
|
2010-10-29 21:37:05 +00:00
|
|
|
|
|
|
|
{- Reads null terminated output of a git command (as enabled by the -z
|
|
|
|
- parameter), and splits it into a list of files. -}
|
2011-02-28 20:25:31 +00:00
|
|
|
pipeNullSplit :: Repo -> [CommandParam] -> IO [FilePath]
|
2010-10-29 21:37:05 +00:00
|
|
|
pipeNullSplit repo params = do
|
2011-02-13 04:50:09 +00:00
|
|
|
fs0 <- pipeRead repo params
|
2010-10-29 21:37:05 +00:00
|
|
|
return $ split0 fs0
|
|
|
|
where
|
|
|
|
split0 s = filter (not . null) $ split "\0" s
|
2010-10-29 21:26:26 +00:00
|
|
|
|
2010-12-31 19:46:33 +00:00
|
|
|
{- Runs git config and populates a repo with its config. -}
|
|
|
|
configRead :: Repo -> IO Repo
|
|
|
|
configRead repo@(Repo { location = Dir d }) = do
|
2010-10-28 17:40:10 +00:00
|
|
|
{- Cannot use pipeRead because it relies on the config having
|
|
|
|
been already read. Instead, chdir to the repo. -}
|
|
|
|
cwd <- getCurrentDirectory
|
|
|
|
bracket_ (changeWorkingDirectory d)
|
|
|
|
(\_ -> changeWorkingDirectory cwd) $
|
|
|
|
pOpen ReadFromPipe "git" ["config", "--list"] $
|
|
|
|
hConfigRead repo
|
2010-12-31 19:46:33 +00:00
|
|
|
configRead r = assertLocal r $ error "internal"
|
|
|
|
|
|
|
|
{- Reads git config from a handle and populates a repo with it. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
hConfigRead :: Repo -> Handle -> IO Repo
|
2010-10-28 17:40:10 +00:00
|
|
|
hConfigRead repo h = do
|
|
|
|
val <- hGetContentsStrict h
|
2011-03-04 01:02:29 +00:00
|
|
|
configStore repo val
|
2010-12-31 19:46:33 +00:00
|
|
|
|
|
|
|
{- Parses a git config and returns a version of the repo using it. -}
|
2011-03-04 01:02:29 +00:00
|
|
|
configStore :: Repo -> String -> IO Repo
|
|
|
|
configStore repo s = do
|
|
|
|
rs <- configRemotes r
|
|
|
|
return $ r { remotes = rs }
|
|
|
|
where
|
|
|
|
r = repo { config = configParse s }
|
2010-10-14 02:59:43 +00:00
|
|
|
|
|
|
|
{- Calculates a list of a repo's configured remotes, by parsing its config. -}
|
2011-03-04 01:02:29 +00:00
|
|
|
configRemotes :: Repo -> IO [Repo]
|
|
|
|
configRemotes repo = mapM construct remotepairs
|
2010-10-14 02:59:43 +00:00
|
|
|
where
|
2010-10-31 19:38:47 +00:00
|
|
|
remotepairs = Map.toList $ filterremotes $ config repo
|
|
|
|
filterremotes = Map.filterWithKey (\k _ -> isremote k)
|
2010-11-22 19:46:57 +00:00
|
|
|
isremote k = startswith "remote." k && endswith ".url" k
|
2011-03-04 01:02:29 +00:00
|
|
|
construct (k,v) = do
|
|
|
|
r <- gen v
|
2011-03-28 01:43:25 +00:00
|
|
|
return $ repoRemoteNameSet r k
|
2010-12-28 17:48:11 +00:00
|
|
|
gen v | scpstyle v = repoFromUrl $ scptourl v
|
|
|
|
| isURI v = repoFromUrl v
|
2011-03-04 01:02:29 +00:00
|
|
|
| otherwise = repoFromRemotePath v repo
|
2010-12-28 17:48:11 +00:00
|
|
|
-- git remotes can be written scp style -- [user@]host:dir
|
2011-01-27 21:58:30 +00:00
|
|
|
scpstyle v = ":" `isInfixOf` v && (not $ "//" `isInfixOf` v)
|
2010-12-28 17:48:11 +00:00
|
|
|
scptourl v = "ssh://" ++ host ++ slash dir
|
|
|
|
where
|
|
|
|
bits = split ":" v
|
|
|
|
host = bits !! 0
|
|
|
|
dir = join ":" $ drop 1 bits
|
|
|
|
slash d | d == "" = "/~/" ++ dir
|
|
|
|
| d !! 0 == '/' = dir
|
2011-03-04 01:02:29 +00:00
|
|
|
| d !! 0 == '~' = '/':dir
|
2010-12-28 17:48:11 +00:00
|
|
|
| otherwise = "/~/" ++ dir
|
2010-10-12 03:41:12 +00:00
|
|
|
|
2011-03-28 01:43:25 +00:00
|
|
|
{- Checks if a string from git config is a true value. -}
|
|
|
|
configTrue :: String -> Bool
|
|
|
|
configTrue s = map toLower s == "true"
|
|
|
|
|
2010-10-12 03:41:12 +00:00
|
|
|
{- Parses git config --list output into a config map. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
configParse :: String -> Map.Map String String
|
|
|
|
configParse s = Map.fromList $ map pair $ lines s
|
2010-10-12 03:41:12 +00:00
|
|
|
where
|
|
|
|
pair l = (key l, val l)
|
2010-11-22 19:46:57 +00:00
|
|
|
key l = head $ keyval l
|
2010-10-12 03:41:12 +00:00
|
|
|
val l = join sep $ drop 1 $ keyval l
|
|
|
|
keyval l = split sep l :: [String]
|
|
|
|
sep = "="
|
2010-10-12 03:22:38 +00:00
|
|
|
|
|
|
|
{- Returns a single git config setting, or a default value if not set. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
configGet :: Repo -> String -> String -> String
|
|
|
|
configGet repo key defaultValue =
|
2010-10-12 16:31:19 +00:00
|
|
|
Map.findWithDefault defaultValue key (config repo)
|
2010-10-12 05:35:32 +00:00
|
|
|
|
2010-10-14 02:59:43 +00:00
|
|
|
{- Access to raw config Map -}
|
2010-10-29 21:26:26 +00:00
|
|
|
configMap :: Repo -> Map.Map String String
|
2010-10-14 06:36:41 +00:00
|
|
|
configMap repo = config repo
|
2010-10-11 04:19:38 +00:00
|
|
|
|
2010-11-28 21:17:18 +00:00
|
|
|
{- Efficiently looks up a gitattributes value for each file in a list. -}
|
2010-11-02 16:43:34 +00:00
|
|
|
checkAttr :: Repo -> String -> [FilePath] -> IO [(FilePath, String)]
|
|
|
|
checkAttr repo attr files = do
|
2010-11-28 21:17:18 +00:00
|
|
|
-- git check-attr wants files that are absolute (or relative to the
|
|
|
|
-- top of the repo). But we're passed files relative to the current
|
|
|
|
-- directory. Convert to absolute, and then convert the filenames
|
|
|
|
-- in its output back to relative.
|
|
|
|
cwd <- getCurrentDirectory
|
2011-03-23 01:00:18 +00:00
|
|
|
let absfiles = map (absPathFrom cwd) files
|
Avoid pipeline stall when running git annex drop or fsck on a lot of files.
When it's stalled, there are 3 processes:
git annex
git ls-files
git check-attr
git-annex stalls trying to write to git check-attr, which stalls trying to
write to stdout (read by git-annex).
git ls-files does not seem to be involved directly; I've seen the stall when
it was still streaming out the file list, and after it had exited and
zombified.
The read and write are supposed to be handled by two different threads,
which pipeBoth forks off, thus avoiding deadlock. But it does deadlock.
(Certian signals unblock the deadlock for a while, then it stalls again.)
So, this is another case of WTF is the ghc IO manager doing today?
I avoid the issue by converting the writer to a separate process.
Possibly this was caused by some change in ghc 7 -- I'm offline and cannot
verify now, but I'm sure I used to be able to run git annex drop w/o it
hanging! And the code does not seem to have changed, except for commit
c1dc4079419cff94cca72441d5e67a866110ec7e, which I tried reverting without
success. In fact, I reverted all the way back to 0.20110316 and still
saw the stall.
Update: Minimal test case:
import System.Cmd.Utils
main = do
as <- checkAttr "blah" $ map show [1..100000]
sequence $ map (putStrLn . show) as
checkAttr attr files = do
(_, s) <- pipeBoth "git" params $ unlines files
return $ lines s
where
params = ["check-attr", attr, "--stdin"]
Bug filed on ghc in debian, #624389
2011-04-28 02:10:57 +00:00
|
|
|
(_, fromh, toh) <- hPipeBoth "git" (toCommand params)
|
|
|
|
_ <- forkProcess $ do
|
|
|
|
hClose fromh
|
|
|
|
hPutStr toh $ join "\0" absfiles
|
|
|
|
hClose toh
|
|
|
|
exitSuccess
|
|
|
|
hClose toh
|
|
|
|
s <- hGetContents fromh
|
2010-11-28 21:17:18 +00:00
|
|
|
return $ map (topair $ cwd++"/") $ lines s
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
2011-02-28 20:10:16 +00:00
|
|
|
params = gitCommandLine repo [Param "check-attr", Param attr, Params "-z --stdin"]
|
2010-11-28 21:17:18 +00:00
|
|
|
topair cwd l = (relfile, value)
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
2010-11-28 21:17:18 +00:00
|
|
|
relfile
|
|
|
|
| startswith cwd file = drop (length cwd) file
|
|
|
|
| otherwise = file
|
2010-11-02 16:43:34 +00:00
|
|
|
file = decodeGitFile $ join sep $ take end bits
|
|
|
|
value = bits !! end
|
|
|
|
end = length bits - 1
|
|
|
|
bits = split sep l
|
|
|
|
sep = ": " ++ attr ++ ": "
|
|
|
|
|
2010-11-02 20:00:55 +00:00
|
|
|
{- Some git commands output encoded filenames. Decode that (annoyingly
|
|
|
|
- complex) encoding. -}
|
2010-11-02 16:43:34 +00:00
|
|
|
decodeGitFile :: String -> FilePath
|
|
|
|
decodeGitFile [] = []
|
|
|
|
decodeGitFile f@(c:s)
|
2010-11-02 20:00:55 +00:00
|
|
|
-- encoded strings will be inside double quotes
|
2010-11-02 19:54:43 +00:00
|
|
|
| c == '"' = unescape ("", middle)
|
2010-11-02 16:43:34 +00:00
|
|
|
| otherwise = f
|
|
|
|
where
|
2010-11-02 20:00:55 +00:00
|
|
|
e = '\\'
|
2010-11-02 16:43:34 +00:00
|
|
|
middle = take (length s - 1) s
|
2010-11-02 19:54:43 +00:00
|
|
|
unescape (b, []) = b
|
2010-11-02 20:00:55 +00:00
|
|
|
-- look for escapes starting with '\'
|
2010-11-02 19:54:43 +00:00
|
|
|
unescape (b, v) = b ++ beginning ++ unescape (decode rest)
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
2010-11-02 20:00:55 +00:00
|
|
|
pair = span (/= e) v
|
2010-11-02 16:43:34 +00:00
|
|
|
beginning = fst pair
|
|
|
|
rest = snd pair
|
2010-11-02 20:49:35 +00:00
|
|
|
isescape x = x == e
|
2010-11-02 20:00:55 +00:00
|
|
|
-- \NNN is an octal encoded character
|
2010-11-02 20:49:35 +00:00
|
|
|
decode (x:n1:n2:n3:rest)
|
|
|
|
| isescape x && alloctal = (fromoctal, rest)
|
2010-11-02 19:54:43 +00:00
|
|
|
where
|
|
|
|
alloctal = isOctDigit n1 &&
|
|
|
|
isOctDigit n2 &&
|
|
|
|
isOctDigit n3
|
2010-11-22 19:46:57 +00:00
|
|
|
fromoctal = [chr $ readoctal [n1, n2, n3]]
|
2010-11-02 19:54:43 +00:00
|
|
|
readoctal o = read $ "0o" ++ o :: Int
|
2010-11-02 20:00:55 +00:00
|
|
|
-- \C is used for a few special characters
|
2010-11-02 20:49:35 +00:00
|
|
|
decode (x:nc:rest)
|
|
|
|
| isescape x = ([echar nc], rest)
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
2010-11-02 19:54:43 +00:00
|
|
|
echar 'a' = '\a'
|
|
|
|
echar 'b' = '\b'
|
|
|
|
echar 'f' = '\f'
|
|
|
|
echar 'n' = '\n'
|
|
|
|
echar 'r' = '\r'
|
|
|
|
echar 't' = '\t'
|
|
|
|
echar 'v' = '\v'
|
2010-11-02 20:49:35 +00:00
|
|
|
echar a = a
|
2010-11-02 19:54:43 +00:00
|
|
|
decode n = ("", n)
|
|
|
|
|
|
|
|
{- Should not need to use this, except for testing decodeGitFile. -}
|
|
|
|
encodeGitFile :: FilePath -> String
|
2010-11-22 19:46:57 +00:00
|
|
|
encodeGitFile s = foldl (++) "\"" (map echar s) ++ "\""
|
2010-11-02 19:54:43 +00:00
|
|
|
where
|
2010-11-22 19:46:57 +00:00
|
|
|
e c = '\\' : [c]
|
2010-11-02 19:54:43 +00:00
|
|
|
echar '\a' = e 'a'
|
|
|
|
echar '\b' = e 'b'
|
|
|
|
echar '\f' = e 'f'
|
|
|
|
echar '\n' = e 'n'
|
|
|
|
echar '\r' = e 'r'
|
|
|
|
echar '\t' = e 't'
|
|
|
|
echar '\v' = e 'v'
|
|
|
|
echar '\\' = e '\\'
|
|
|
|
echar '"' = e '"'
|
|
|
|
echar x
|
|
|
|
| ord x < 0x20 = e_num x -- low ascii
|
|
|
|
| ord x >= 256 = e_utf x
|
|
|
|
| ord x > 0x7E = e_num x -- high ascii
|
|
|
|
| otherwise = [x] -- printable ascii
|
|
|
|
where
|
2010-11-22 19:46:57 +00:00
|
|
|
showoctal i = '\\' : printf "%03o" i
|
2010-11-02 19:54:43 +00:00
|
|
|
e_num c = showoctal $ ord c
|
2010-11-02 20:02:43 +00:00
|
|
|
-- unicode character is decomposed to
|
|
|
|
-- Word8s and each is shown in octal
|
2011-05-16 18:49:28 +00:00
|
|
|
e_utf c = showoctal =<< (encode [c] :: [Word8])
|
2010-11-02 19:54:43 +00:00
|
|
|
|
|
|
|
{- for quickcheck -}
|
2010-11-02 20:49:35 +00:00
|
|
|
prop_idempotent_deencode :: String -> Bool
|
2010-11-22 19:46:57 +00:00
|
|
|
prop_idempotent_deencode s = s == decodeGitFile (encodeGitFile s)
|
2010-11-02 16:43:34 +00:00
|
|
|
|
2011-03-04 01:02:29 +00:00
|
|
|
{- Constructs a Repo from the path specified in the git remotes of
|
|
|
|
- another Repo. -}
|
|
|
|
repoFromRemotePath :: FilePath -> Repo -> IO Repo
|
|
|
|
repoFromRemotePath dir repo = do
|
|
|
|
dir' <- expandTilde dir
|
|
|
|
repoFromAbsPath $ workTree repo </> dir'
|
|
|
|
|
|
|
|
{- Git remotes can have a directory that is specified relative
|
|
|
|
- to the user's home directory, or that contains tilde expansions.
|
|
|
|
- This converts such a directory to an absolute path.
|
|
|
|
- Note that it has to run on the system where the remote is.
|
2011-01-01 01:22:03 +00:00
|
|
|
-}
|
2011-03-04 01:02:29 +00:00
|
|
|
repoAbsPath :: FilePath -> IO FilePath
|
|
|
|
repoAbsPath d = do
|
|
|
|
d' <- expandTilde d
|
|
|
|
h <- myHomeDir
|
|
|
|
return $ h </> d'
|
|
|
|
|
|
|
|
expandTilde :: FilePath -> IO FilePath
|
|
|
|
expandTilde = expandt True
|
2011-01-01 01:22:03 +00:00
|
|
|
where
|
2011-03-04 01:02:29 +00:00
|
|
|
expandt _ [] = return ""
|
|
|
|
expandt _ ('/':cs) = do
|
|
|
|
v <- expandt True cs
|
|
|
|
return ('/':v)
|
|
|
|
expandt True ('~':'/':cs) = do
|
|
|
|
h <- myHomeDir
|
|
|
|
return $ h </> cs
|
|
|
|
expandt True ('~':cs) = do
|
2011-01-01 01:22:03 +00:00
|
|
|
let (name, rest) = findname "" cs
|
|
|
|
u <- getUserEntryForName name
|
2011-03-04 01:02:29 +00:00
|
|
|
return $ homeDirectory u </> rest
|
|
|
|
expandt _ (c:cs) = do
|
|
|
|
v <- expandt False cs
|
2011-01-01 01:22:03 +00:00
|
|
|
return (c:v)
|
|
|
|
findname n [] = (n, "")
|
|
|
|
findname n (c:cs)
|
|
|
|
| c == '/' = (n, cs)
|
|
|
|
| otherwise = findname (n++[c]) cs
|
|
|
|
|
2010-10-10 19:54:02 +00:00
|
|
|
{- Finds the current git repository, which may be in a parent directory. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
repoFromCwd :: IO Repo
|
2011-05-15 16:25:58 +00:00
|
|
|
repoFromCwd = getCurrentDirectory >>= seekUp isRepoTop >>= maybe norepo makerepo
|
|
|
|
where
|
|
|
|
makerepo = return . newFrom . Dir
|
|
|
|
norepo = error "Not in a git repository."
|
|
|
|
|
|
|
|
seekUp :: (FilePath -> IO Bool) -> FilePath -> IO (Maybe FilePath)
|
|
|
|
seekUp want dir = do
|
2010-10-10 01:06:46 +00:00
|
|
|
ok <- want dir
|
|
|
|
if ok
|
|
|
|
then return (Just dir)
|
|
|
|
else case (parentDir dir) of
|
2010-10-10 03:35:05 +00:00
|
|
|
"" -> return Nothing
|
2011-05-15 16:25:58 +00:00
|
|
|
d -> seekUp want d
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
isRepoTop :: FilePath -> IO Bool
|
2010-10-10 01:06:46 +00:00
|
|
|
isRepoTop dir = do
|
2010-10-31 19:38:47 +00:00
|
|
|
r <- isRepo
|
|
|
|
b <- isBareRepo
|
2010-10-10 01:06:46 +00:00
|
|
|
return (r || b)
|
2010-10-12 16:23:34 +00:00
|
|
|
where
|
2010-10-31 19:38:47 +00:00
|
|
|
isRepo = gitSignature ".git" ".git/config"
|
|
|
|
isBareRepo = gitSignature "objects" "config"
|
|
|
|
gitSignature subdir file = do
|
2010-10-12 16:23:34 +00:00
|
|
|
s <- (doesDirectoryExist (dir ++ "/" ++ subdir))
|
|
|
|
f <- (doesFileExist (dir ++ "/" ++ file))
|
|
|
|
return (s && f)
|