2011-12-12 22:23:24 +00:00
|
|
|
{- git ref stuff
|
|
|
|
-
|
2013-05-21 22:24:29 +00:00
|
|
|
- Copyright 2011-2013 Joey Hess <joey@kitenet.net>
|
2011-12-12 22:23:24 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Git.Ref where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2012-04-11 16:21:54 +00:00
|
|
|
import Data.Char (chr)
|
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
headRef :: Ref
|
|
|
|
headRef = Ref "HEAD"
|
|
|
|
|
2011-12-30 20:48:26 +00:00
|
|
|
{- Converts a fully qualified git ref into a user-visible string. -}
|
2011-12-12 22:23:24 +00:00
|
|
|
describe :: Ref -> String
|
2011-12-30 20:48:26 +00:00
|
|
|
describe = show . base
|
|
|
|
|
|
|
|
{- Often git refs are fully qualified (eg: refs/heads/master).
|
|
|
|
- Converts such a fully qualified ref into a base ref (eg: master). -}
|
|
|
|
base :: Ref -> Ref
|
|
|
|
base = Ref . remove "refs/heads/" . remove "refs/remotes/" . show
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
remove prefix s
|
|
|
|
| prefix `isPrefixOf` s = drop (length prefix) s
|
|
|
|
| otherwise = s
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2011-12-30 20:48:26 +00:00
|
|
|
{- Given a directory such as "refs/remotes/origin", and a ref such as
|
|
|
|
- refs/heads/master, yields a version of that ref under the directory,
|
|
|
|
- such as refs/remotes/origin/master. -}
|
|
|
|
under :: String -> Ref -> Ref
|
|
|
|
under dir r = Ref $ dir </> show (base r)
|
|
|
|
|
2011-12-12 22:23:24 +00:00
|
|
|
{- Checks if a ref exists. -}
|
|
|
|
exists :: Ref -> Repo -> IO Bool
|
2013-03-03 17:39:07 +00:00
|
|
|
exists ref = runBool
|
2013-03-07 15:09:30 +00:00
|
|
|
[Param "show-ref", Param "--verify", Param "-q", Param $ show ref]
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2013-02-06 16:40:59 +00:00
|
|
|
{- Checks if HEAD exists. It generally will, except for in a repository
|
|
|
|
- that was just created. -}
|
|
|
|
headExists :: Repo -> IO Bool
|
|
|
|
headExists repo = do
|
|
|
|
ls <- lines <$> pipeReadStrict [Param "show-ref", Param "--head"] repo
|
|
|
|
return $ any (" HEAD" `isSuffixOf`) ls
|
|
|
|
|
2011-12-12 22:23:24 +00:00
|
|
|
{- Get the sha of a fully qualified git ref, if it exists. -}
|
|
|
|
sha :: Branch -> Repo -> IO (Maybe Sha)
|
support all filename encodings with ghc 7.4
Under ghc 7.4, this seems to be able to handle all filename encodings
again. Including filename encodings that do not match the LANG setting.
I think this will not work with earlier versions of ghc, it uses some ghc
internals.
Turns out that ghc 7.4 has a special filesystem encoding that it uses when
reading/writing filenames (as FilePaths). This encoding is documented
to allow "arbitrary undecodable bytes to be round-tripped through it".
So, to get FilePaths from eg, git ls-files, set the Handle that is reading
from git to use this encoding. Then things basically just work.
However, I have not found a way to make Text read using this encoding.
Text really does assume unicode. So I had to switch back to using String
when reading/writing data to git. Which is a pity, because it's some
percent slower, but at least it works.
Note that stdout and stderr also have to be set to this encoding, or
printing out filenames that contain undecodable bytes causes a crash.
IMHO this is a misfeature in ghc, that the user can pass you a filename,
which you can readFile, etc, but that default, putStr of filename may
cause a crash!
Git.CheckAttr gave me special trouble, because the filenames I got back
from git, after feeding them in, had further encoding breakage.
Rather than try to deal with that, I just zip up the input filenames
with the attributes. Which must be returned in the same order queried
for this to work.
Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It
used to forkProcess and feed git from the child process. Unfortunatly,
after this forkProcess, accessing the `files` variable from the parent
returns []. Not the value that was passed into the function. This screams
of a bad bug, that's clobbering a variable, but for now I just avoid
forkProcess there to work around it. That forkProcess was itself only added
because of a ghc bug, #624389. I've confirmed that the test case for that
bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc
bug I have not isolated and reported. Why does this simple bit of code
magnet the ghc bugs? :)
Also, the symlink touching code is currently broken, when used on utf-8
filenames in a non-utf-8 locale, or probably on any filename containing
undecodable bytes, and I temporarily commented it out.
2012-02-03 19:12:41 +00:00
|
|
|
sha branch repo = process <$> showref repo
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
showref = pipeReadStrict [Param "show-ref",
|
|
|
|
Param "--hash", -- get the hash
|
|
|
|
Param $ show branch]
|
|
|
|
process [] = Nothing
|
|
|
|
process s = Just $ Ref $ firstLine s
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
{- List of (shas, branches) matching a given ref or refs. -}
|
|
|
|
matching :: [Ref] -> Repo -> IO [(Sha, Branch)]
|
2013-05-22 00:04:38 +00:00
|
|
|
matching refs repo = matching' (map show refs) repo
|
|
|
|
|
|
|
|
{- Includes HEAD in the output, if asked for it. -}
|
|
|
|
matchingWithHEAD :: [Ref] -> Repo -> IO [(Sha, Branch)]
|
|
|
|
matchingWithHEAD refs repo = matching' ("--head" : map show refs) repo
|
|
|
|
|
|
|
|
{- List of (shas, branches) matching a given ref or refs. -}
|
|
|
|
matching' :: [String] -> Repo -> IO [(Sha, Branch)]
|
|
|
|
matching' ps repo = map gen . lines <$>
|
|
|
|
pipeReadStrict (Param "show-ref" : map Param ps) repo
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
gen l = let (r, b) = separate (== ' ') l
|
|
|
|
in (Ref r, Ref b)
|
2011-12-30 22:36:40 +00:00
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
{- List of (shas, branches) matching a given ref spec.
|
|
|
|
- Duplicate shas are filtered out. -}
|
|
|
|
matchingUniq :: [Ref] -> Repo -> IO [(Sha, Branch)]
|
|
|
|
matchingUniq refs repo = nubBy uniqref <$> matching refs repo
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
uniqref (a, _) (b, _) = a == b
|
2012-04-11 16:21:54 +00:00
|
|
|
|
|
|
|
{- Checks if a String is a legal git ref name.
|
|
|
|
-
|
|
|
|
- The rules for this are complex; see git-check-ref-format(1) -}
|
2012-04-11 16:45:05 +00:00
|
|
|
legal :: Bool -> String -> Bool
|
|
|
|
legal allowonelevel s = all (== False) illegal
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
illegal =
|
|
|
|
[ any ("." `isPrefixOf`) pathbits
|
|
|
|
, any (".lock" `isSuffixOf`) pathbits
|
|
|
|
, not allowonelevel && length pathbits < 2
|
|
|
|
, contains ".."
|
|
|
|
, any (\c -> contains [c]) illegalchars
|
|
|
|
, begins "/"
|
|
|
|
, ends "/"
|
|
|
|
, contains "//"
|
|
|
|
, ends "."
|
|
|
|
, contains "@{"
|
|
|
|
, null s
|
|
|
|
]
|
|
|
|
contains v = v `isInfixOf` s
|
|
|
|
ends v = v `isSuffixOf` s
|
|
|
|
begins v = v `isPrefixOf` s
|
2012-04-11 16:29:31 +00:00
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
pathbits = split "/" s
|
|
|
|
illegalchars = " ~^:?*[\\" ++ controlchars
|
|
|
|
controlchars = chr 0o177 : [chr 0 .. chr (0o40-1)]
|