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.
|
|
|
|
-
|
2011-12-13 19:05:07 +00:00
|
|
|
- Copyright 2010, 2011 Joey Hess <joey@kitenet.net>
|
2010-10-27 20:53:54 +00:00
|
|
|
-
|
|
|
|
- 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
|
|
|
|
2011-06-30 17:16:57 +00:00
|
|
|
module Git (
|
2010-10-14 06:36:41 +00:00
|
|
|
Repo,
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
Ref(..),
|
|
|
|
Branch,
|
|
|
|
Sha,
|
|
|
|
Tag,
|
2010-10-22 18:05:30 +00:00
|
|
|
repoIsUrl,
|
2010-10-22 17:40:19 +00:00
|
|
|
repoIsSsh,
|
2011-08-16 23:23:56 +00:00
|
|
|
repoIsHttp,
|
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
|
|
|
configMap,
|
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,
|
2011-06-22 20:02:07 +00:00
|
|
|
runBool,
|
2010-10-16 18:20:43 +00:00
|
|
|
pipeRead,
|
2011-06-21 01:35:39 +00:00
|
|
|
pipeWrite,
|
|
|
|
pipeWriteRead,
|
add git-union-merge
This is a new git subcommand, that does a generic union merge operation
between two refs, storing the result in a branch. It operates efficiently
without touching the working tree. It does need to write out a temporary
index file, and may need to write out some other temp files as well.
This could be useful for anything that stores data in a branch,
and needs to merge changes into that branch without actually checking the
branch out. Since conflict handling can't be done without a working copy,
the merge type is always a union merge, which is fine for data stored in
log format (as git-annex does), or in non-conflicting files
(as pristine-tar does).
This probably belongs in git proper, but it will live in git-annex for now.
---
Plan is to move .git-annex/ to a git-annex branch, and use git-union-merge
to handle merging changes when pulling from remotes.
Some preliminary benchmarking using real .git-annex/ data indicates
that it's quite fast, except for the "git add" call, which is as slow
as "git add" tends to be with a big index.
2011-06-20 23:44:45 +00:00
|
|
|
pipeNullSplit,
|
2011-09-30 03:43:42 +00:00
|
|
|
pipeNullSplitB,
|
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,
|
2011-10-14 22:17:46 +00:00
|
|
|
repoRemoteNameFromKey,
|
2010-11-02 19:54:43 +00:00
|
|
|
checkAttr,
|
|
|
|
decodeGitFile,
|
|
|
|
encodeGitFile,
|
2011-02-13 04:50:09 +00:00
|
|
|
reap,
|
2011-06-21 21:39:45 +00:00
|
|
|
useIndex,
|
|
|
|
getSha,
|
|
|
|
shaSize,
|
2011-09-29 23:04:24 +00:00
|
|
|
assertLocal,
|
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-07-15 07:12:05 +00:00
|
|
|
import qualified Data.Map as M hiding (map, split)
|
2010-10-12 04:53:42 +00:00
|
|
|
import Network.URI
|
2010-11-06 21:07:11 +00:00
|
|
|
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
|
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
|
2011-06-23 02:56:27 +00:00
|
|
|
import System.Posix.Env (setEnv, unsetEnv, getEnv)
|
2011-09-30 03:43:42 +00:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2011-10-04 02:24:57 +00:00
|
|
|
import Common
|
2011-12-13 19:05:07 +00:00
|
|
|
import Git.Types
|
2011-02-04 05:56:45 +00:00
|
|
|
|
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-10-14 22:17:46 +00:00
|
|
|
{- Sets the name of a remote. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
repoRemoteNameSet :: String -> Repo -> Repo
|
|
|
|
repoRemoteNameSet n r = r { remoteName = Just n }
|
2011-10-14 22:17:46 +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". -}
|
2011-11-08 19:34:10 +00:00
|
|
|
repoRemoteNameFromKey :: String -> Repo -> Repo
|
|
|
|
repoRemoteNameFromKey k = repoRemoteNameSet basename
|
2011-03-28 01:43:25 +00:00
|
|
|
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-08-16 23:23:56 +00:00
|
|
|
repoIsHttp :: Repo -> Bool
|
|
|
|
repoIsHttp Repo { location = Url url }
|
|
|
|
| uriScheme url == "http:" = True
|
|
|
|
| uriScheme url == "https:" = True
|
|
|
|
| otherwise = False
|
|
|
|
repoIsHttp _ = False
|
|
|
|
|
2011-03-03 21:33:15 +00:00
|
|
|
configAvail ::Repo -> Bool
|
2011-07-15 07:12:05 +00:00
|
|
|
configAvail Repo { config = c } = c /= M.empty
|
2011-03-03 21:33:15 +00:00
|
|
|
|
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-07-15 07:12:05 +00:00
|
|
|
configBare repo = maybe unknown configTrue $ M.lookup "core.bare" $ config repo
|
2011-05-15 16:25:58 +00:00
|
|
|
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
|
|
|
|
2011-08-19 16:59:07 +00:00
|
|
|
{- Path to a repository's .git directory. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
gitDir :: Repo -> String
|
|
|
|
gitDir repo
|
2011-08-19 16:59:07 +00:00
|
|
|
| configBare repo = workTree repo
|
|
|
|
| otherwise = workTree repo </> ".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".
|
|
|
|
-}
|
2011-11-08 19:34:10 +00:00
|
|
|
workTreeFile :: FilePath -> Repo -> IO FilePath
|
|
|
|
workTreeFile file repo@(Repo { location = Dir d }) = 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-07-15 07:12:05 +00:00
|
|
|
if inrepo $ addTrailingPathSeparator cwd
|
2011-04-25 18:54:24 +00:00
|
|
|
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-07-15 07:12:05 +00:00
|
|
|
absfile c = fromMaybe file $ 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-11-08 19:34:10 +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
|
2011-07-15 07:12:05 +00:00
|
|
|
len = length rest - 1
|
2011-05-06 19:21:30 +00:00
|
|
|
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
|
2011-12-09 22:57:09 +00:00
|
|
|
(':':p) -> readMaybe p
|
2011-03-05 19:13:16 +00:00
|
|
|
_ -> 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-10-15 20:21:08 +00:00
|
|
|
urlAuthority = urlAuthPart assemble
|
2011-10-15 05:37:55 +00:00
|
|
|
where
|
2011-10-15 20:21:08 +00:00
|
|
|
assemble a = uriUserInfo a ++ uriRegName' a ++ uriPort a
|
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-11-08 19:34:10 +00:00
|
|
|
gitCommandLine :: [CommandParam] -> Repo -> [CommandParam]
|
|
|
|
gitCommandLine params repo@(Repo { location = Dir _ } ) =
|
2010-10-12 03:22:38 +00:00
|
|
|
-- force use of specified repo via --git-dir and --work-tree
|
2011-08-19 16:59:07 +00:00
|
|
|
[ Param ("--git-dir=" ++ gitDir repo)
|
|
|
|
, Param ("--work-tree=" ++ workTree repo)
|
2011-02-28 20:10:16 +00:00
|
|
|
] ++ params
|
2011-11-08 19:34:10 +00:00
|
|
|
gitCommandLine _ repo = assertLocal repo $ error "internal"
|
2010-10-12 03:22:38 +00:00
|
|
|
|
2011-06-22 20:02:07 +00:00
|
|
|
{- Runs git in the specified repo. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
runBool :: String -> [CommandParam] -> Repo -> IO Bool
|
|
|
|
runBool subcommand params repo = assertLocal repo $
|
|
|
|
boolSystem "git" $ gitCommandLine (Param subcommand : params) repo
|
2011-06-22 20:02:07 +00:00
|
|
|
|
2010-10-29 18:07:26 +00:00
|
|
|
{- Runs git in the specified repo, throwing an error if it fails. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
run :: String -> [CommandParam] -> Repo -> IO ()
|
|
|
|
run subcommand params repo = assertLocal repo $
|
|
|
|
runBool subcommand params repo
|
2011-05-17 15:44:13 +00:00
|
|
|
>>! error $ "git " ++ show params ++ " failed"
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2011-06-21 01:35:39 +00:00
|
|
|
{- Runs a git subcommand and returns its output, lazily.
|
2011-02-13 04:50:09 +00:00
|
|
|
-
|
|
|
|
- Note that this leaves the git process running, and so zombies will
|
|
|
|
- result unless reap is called.
|
|
|
|
-}
|
2011-11-08 19:34:10 +00:00
|
|
|
pipeRead :: [CommandParam] -> Repo -> IO L.ByteString
|
|
|
|
pipeRead params repo = assertLocal repo $ do
|
|
|
|
(_, h) <- hPipeFrom "git" $ toCommand $ gitCommandLine params repo
|
2011-09-30 03:43:42 +00:00
|
|
|
hSetBinaryMode h True
|
|
|
|
L.hGetContents h
|
2011-02-13 04:50:09 +00:00
|
|
|
|
2011-06-21 01:35:39 +00:00
|
|
|
{- Runs a git subcommand, feeding it input.
|
|
|
|
- You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
pipeWrite :: [CommandParam] -> L.ByteString -> Repo -> IO PipeHandle
|
|
|
|
pipeWrite params s repo = assertLocal repo $ do
|
|
|
|
(p, h) <- hPipeTo "git" (toCommand $ gitCommandLine params repo)
|
2011-09-30 03:43:42 +00:00
|
|
|
L.hPut h s
|
|
|
|
hClose h
|
|
|
|
return p
|
2011-06-21 01:35:39 +00:00
|
|
|
|
|
|
|
{- Runs a git subcommand, feeding it input, and returning its output.
|
|
|
|
- You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
pipeWriteRead :: [CommandParam] -> L.ByteString -> Repo -> IO (PipeHandle, L.ByteString)
|
|
|
|
pipeWriteRead params s repo = assertLocal repo $ do
|
|
|
|
(p, from, to) <- hPipeBoth "git" (toCommand $ gitCommandLine params repo)
|
2011-09-30 03:43:42 +00:00
|
|
|
hSetBinaryMode from True
|
|
|
|
L.hPut to s
|
|
|
|
hClose to
|
|
|
|
c <- L.hGetContents from
|
|
|
|
return (p, c)
|
|
|
|
|
|
|
|
{- Reads null terminated output of a git command (as enabled by the -z
|
|
|
|
- parameter), and splits it. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
pipeNullSplit :: [CommandParam] -> Repo -> IO [String]
|
|
|
|
pipeNullSplit params repo = map L.unpack <$> pipeNullSplitB params repo
|
2011-09-30 03:43:42 +00:00
|
|
|
|
|
|
|
{- For when Strings are not needed. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
pipeNullSplitB ::[CommandParam] -> Repo -> IO [L.ByteString]
|
|
|
|
pipeNullSplitB params repo = filter (not . L.null) . L.split '\0' <$>
|
|
|
|
pipeRead params repo
|
2011-06-21 01:35:39 +00:00
|
|
|
|
2011-02-13 04:50:09 +00:00
|
|
|
{- Reaps any zombie git processes. -}
|
|
|
|
reap :: IO ()
|
|
|
|
reap = do
|
|
|
|
-- throws an exception when there are no child processes
|
2011-11-11 00:24:24 +00:00
|
|
|
r <- catchDefaultIO (getAnyProcessStatus False True) Nothing
|
2011-05-15 06:49:43 +00:00
|
|
|
maybe (return ()) (const reap) r
|
2010-10-31 03:19:33 +00:00
|
|
|
|
2011-06-23 02:56:27 +00:00
|
|
|
{- Forces git to use the specified index file.
|
|
|
|
- Returns an action that will reset back to the default
|
|
|
|
- index file. -}
|
|
|
|
useIndex :: FilePath -> IO (IO ())
|
|
|
|
useIndex index = do
|
2011-11-26 16:03:01 +00:00
|
|
|
res <- getEnv var
|
2011-06-23 02:56:27 +00:00
|
|
|
setEnv var index True
|
|
|
|
return $ reset res
|
|
|
|
where
|
|
|
|
var = "GIT_INDEX_FILE"
|
2011-11-26 16:03:01 +00:00
|
|
|
reset (Just v) = setEnv var v True
|
2011-06-23 02:56:27 +00:00
|
|
|
reset _ = unsetEnv var
|
2011-06-21 21:39:45 +00:00
|
|
|
|
|
|
|
{- Runs an action that causes a git subcommand to emit a sha, and strips
|
|
|
|
any trailing newline, returning the sha. -}
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
getSha :: String -> IO String -> IO Sha
|
2011-06-21 21:39:45 +00:00
|
|
|
getSha subcommand a = do
|
|
|
|
t <- a
|
|
|
|
let t' = if last t == '\n'
|
2011-09-21 03:24:48 +00:00
|
|
|
then init t
|
2011-06-21 21:39:45 +00:00
|
|
|
else t
|
|
|
|
when (length t' /= shaSize) $
|
|
|
|
error $ "failed to read sha from git " ++ subcommand ++ " (" ++ t' ++ ")"
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
return $ Ref t'
|
2011-06-21 21:39:45 +00:00
|
|
|
|
|
|
|
{- Size of a git sha. -}
|
|
|
|
shaSize :: Int
|
|
|
|
shaSize = 40
|
2011-06-21 20:08:09 +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-14 02:59:43 +00:00
|
|
|
{- Access to raw config Map -}
|
2011-07-15 07:12:05 +00:00
|
|
|
configMap :: Repo -> M.Map String String
|
|
|
|
configMap = config
|
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. -}
|
2011-11-08 19:34:10 +00:00
|
|
|
checkAttr :: String -> [FilePath] -> Repo -> IO [(FilePath, String)]
|
|
|
|
checkAttr attr files repo = do
|
2011-10-12 03:49:58 +00:00
|
|
|
-- git check-attr needs relative filenames input; it will choke
|
|
|
|
-- on some absolute filenames. This also means it will output
|
|
|
|
-- all relative filenames.
|
2011-10-12 03:39:29 +00:00
|
|
|
cwd <- getCurrentDirectory
|
|
|
|
let relfiles = map (relPathDirToFile cwd . 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
|
2011-10-12 03:39:29 +00:00
|
|
|
hPutStr toh $ join "\0" relfiles
|
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
|
|
|
hClose toh
|
|
|
|
exitSuccess
|
|
|
|
hClose toh
|
2011-10-12 02:52:55 +00:00
|
|
|
(map topair . lines) <$> hGetContents fromh
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
2011-11-08 19:34:10 +00:00
|
|
|
params = gitCommandLine
|
|
|
|
[ Param "check-attr"
|
|
|
|
, Param attr
|
|
|
|
, Params "-z --stdin"
|
|
|
|
] repo
|
2011-10-12 02:52:55 +00:00
|
|
|
topair l = (file, value)
|
2010-11-02 16:43:34 +00:00
|
|
|
where
|
|
|
|
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 = '\\'
|
2011-09-21 03:24:48 +00:00
|
|
|
middle = init 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)
|