2011-12-12 22:23:24 +00:00
|
|
|
{- git ref stuff
|
|
|
|
-
|
2020-07-10 17:28:16 +00:00
|
|
|
- Copyright 2011-2020 Joey Hess <id@joeyh.name>
|
2011-12-12 22:23:24 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-12-12 22:23:24 +00:00
|
|
|
-}
|
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2011-12-12 22:23:24 +00:00
|
|
|
module Git.Ref where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2013-12-01 17:59:39 +00:00
|
|
|
import Git.Sha
|
2014-02-06 16:43:56 +00:00
|
|
|
import Git.Types
|
2020-07-10 17:28:16 +00:00
|
|
|
import Git.FilePath
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
import Data.Char (chr, ord)
|
|
|
|
import qualified Data.ByteString as S
|
2020-04-07 15:54:27 +00:00
|
|
|
import qualified Data.ByteString.Char8 as S8
|
2012-04-11 16:21:54 +00:00
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
headRef :: Ref
|
|
|
|
headRef = Ref "HEAD"
|
|
|
|
|
2016-04-04 17:17:24 +00:00
|
|
|
headFile :: Repo -> FilePath
|
2019-12-09 17:49:05 +00:00
|
|
|
headFile r = fromRawFilePath (localGitDir r) </> "HEAD"
|
2016-04-04 17:17:24 +00:00
|
|
|
|
|
|
|
setHeadRef :: Ref -> Repo -> IO ()
|
2020-04-07 21:41:09 +00:00
|
|
|
setHeadRef ref r = S.writeFile (headFile r) ("ref: " <> fromRef' ref)
|
2016-04-04 17:17:24 +00:00
|
|
|
|
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
|
2014-02-19 05:09:17 +00:00
|
|
|
describe = fromRef . base
|
2011-12-30 20:48:26 +00:00
|
|
|
|
2016-09-21 18:57:44 +00:00
|
|
|
{- Often git refs are fully qualified
|
|
|
|
- (eg refs/heads/master or refs/remotes/origin/master).
|
|
|
|
- Converts such a fully qualified ref into a base ref
|
|
|
|
- (eg: master or origin/master). -}
|
2011-12-30 20:48:26 +00:00
|
|
|
base :: Ref -> Ref
|
2019-03-01 20:08:18 +00:00
|
|
|
base = removeBase "refs/heads/" . removeBase "refs/remotes/"
|
|
|
|
|
|
|
|
{- Removes a directory such as "refs/heads/master" from a
|
|
|
|
- fully qualified ref. Any ref not starting with it is left as-is. -}
|
|
|
|
removeBase :: String -> Ref -> Ref
|
2020-04-07 15:54:27 +00:00
|
|
|
removeBase dir r
|
|
|
|
| prefix `isPrefixOf` rs = Ref $ encodeBS $ drop (length prefix) rs
|
|
|
|
| otherwise = r
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2020-04-07 15:54:27 +00:00
|
|
|
rs = fromRef r
|
2019-03-01 20:08:18 +00:00
|
|
|
prefix = case end dir of
|
|
|
|
['/'] -> dir
|
|
|
|
_ -> dir ++ "/"
|
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. -}
|
work around lack of receive.denyCurrentBranch in direct mode
Now that direct mode sets core.bare=true, git's normal prohibition about
pushing into the currently checked out branch doesn't work.
A simple fix for this would be an update hook which blocks the pushes..
but git hooks must be executable, and git-annex needs to be usable on eg,
FAT, which lacks x bits.
Instead, enabling direct mode switches the branch (eg master) to a special
purpose branch (eg annex/direct/master). This branch is not pushed when
syncing; instead any changes that git annex sync commits get written to
master, and it's pushed (along with synced/master) to the remote.
Note that initialization has been changed to always call setDirect,
even if it's just setDirect False for indirect mode. This is needed because
if the user has just cloned a direct mode repo, that nothing has synced
with before, it may have no master branch, and only a annex/direct/master.
Resulting in that branch being checked out locally too. Calling setDirect False
for indirect mode moves back out of this branch, to a new master branch,
and ensures that a manual "git push" doesn't push changes directly to
the annex/direct/master of the remote. (It's possible that the user
makes a commit w/o using git-annex and pushes it, but nothing I can do
about that really.)
This commit was sponsored by Jonathan Harrington.
2013-11-06 01:08:31 +00:00
|
|
|
underBase :: String -> Ref -> Ref
|
2020-04-07 21:41:09 +00:00
|
|
|
underBase dir r = Ref $ encodeBS dir <> "/" <> fromRef' (base r)
|
2011-12-30 20:48:26 +00:00
|
|
|
|
2017-09-28 18:14:07 +00:00
|
|
|
{- Convert a branch such as "master" into a fully qualified ref. -}
|
|
|
|
branchRef :: Branch -> Ref
|
|
|
|
branchRef = underBase "refs/heads"
|
|
|
|
|
2013-11-07 17:55:36 +00:00
|
|
|
{- A Ref that can be used to refer to a file in the repository, as staged
|
|
|
|
- in the index.
|
|
|
|
-
|
|
|
|
- Prefixing the file with ./ makes this work even if in a subdirectory
|
|
|
|
- of a repo.
|
|
|
|
-}
|
2019-11-26 19:27:22 +00:00
|
|
|
fileRef :: RawFilePath -> Ref
|
2020-07-10 17:28:16 +00:00
|
|
|
fileRef f = Ref $ ":./" <> toInternalGitPath f
|
|
|
|
|
|
|
|
{- A Ref that can be used to refer to a file in a particular branch. -}
|
|
|
|
branchFileRef :: Branch -> RawFilePath -> Ref
|
|
|
|
branchFileRef branch f = Ref $ fromRef' branch <> ":" <> toInternalGitPath f
|
2013-11-07 17:55:36 +00:00
|
|
|
|
2014-02-06 16:43:56 +00:00
|
|
|
{- Converts a Ref to refer to the content of the Ref on a given date. -}
|
|
|
|
dateRef :: Ref -> RefDate -> Ref
|
2020-04-07 15:54:27 +00:00
|
|
|
dateRef r (RefDate d) = Ref $ fromRef' r <> "@" <> encodeBS' d
|
2014-02-06 16:43:56 +00:00
|
|
|
|
2013-11-07 17:55:36 +00:00
|
|
|
{- A Ref that can be used to refer to a file in the repository as it
|
|
|
|
- appears in a given Ref. -}
|
2019-11-26 19:27:22 +00:00
|
|
|
fileFromRef :: Ref -> RawFilePath -> Ref
|
2020-04-07 15:54:27 +00:00
|
|
|
fileFromRef r f = let (Ref fr) = fileRef f in Ref (fromRef' r <> fr)
|
2013-11-07 17:55:36 +00:00
|
|
|
|
2021-03-23 19:22:51 +00:00
|
|
|
{- Checks if a ref exists. Note that it must be fully qualified,
|
|
|
|
- eg refs/heads/master rather than master. -}
|
2011-12-12 22:23:24 +00:00
|
|
|
exists :: Ref -> Repo -> IO Bool
|
2013-03-03 17:39:07 +00:00
|
|
|
exists ref = runBool
|
2020-04-07 15:54:27 +00:00
|
|
|
[ Param "show-ref"
|
|
|
|
, Param "--verify"
|
|
|
|
, Param "-q"
|
|
|
|
, Param $ fromRef ref
|
|
|
|
]
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2013-11-05 20:42:59 +00:00
|
|
|
{- The file used to record a ref. (Git also stores some refs in a
|
|
|
|
- packed-refs file.) -}
|
|
|
|
file :: Ref -> Repo -> FilePath
|
2019-12-09 17:49:05 +00:00
|
|
|
file ref repo = fromRawFilePath (localGitDir repo) </> fromRef ref
|
2013-11-05 20:42:59 +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
|
2019-11-25 20:18:19 +00:00
|
|
|
ls <- S.split nl <$> pipeReadStrict [Param "show-ref", Param "--head"] repo
|
|
|
|
return $ any (" HEAD" `S.isSuffixOf`) ls
|
|
|
|
where
|
|
|
|
nl = fromIntegral (ord '\n')
|
2013-02-06 16:40:59 +00:00
|
|
|
|
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
|
2019-02-23 19:48:25 +00:00
|
|
|
showref = pipeReadStrict
|
|
|
|
[ Param "show-ref"
|
|
|
|
, Param "--hash" -- get the hash
|
|
|
|
, Param $ fromRef branch
|
|
|
|
]
|
2019-11-25 20:18:19 +00:00
|
|
|
process s
|
|
|
|
| S.null s = Nothing
|
2020-04-07 15:54:27 +00:00
|
|
|
| otherwise = Just $ Ref $ firstLine' s
|
2011-12-12 22:23:24 +00:00
|
|
|
|
2015-03-04 19:25:13 +00:00
|
|
|
headSha :: Repo -> IO (Maybe Sha)
|
|
|
|
headSha = sha headRef
|
|
|
|
|
2013-05-21 22:24:29 +00:00
|
|
|
{- List of (shas, branches) matching a given ref or refs. -}
|
|
|
|
matching :: [Ref] -> Repo -> IO [(Sha, Branch)]
|
2020-04-07 15:54:27 +00:00
|
|
|
matching = matching' []
|
2013-05-22 00:04:38 +00:00
|
|
|
|
|
|
|
{- Includes HEAD in the output, if asked for it. -}
|
|
|
|
matchingWithHEAD :: [Ref] -> Repo -> IO [(Sha, Branch)]
|
2020-04-07 15:54:27 +00:00
|
|
|
matchingWithHEAD = matching' [Param "--head"]
|
2013-05-22 00:04:38 +00:00
|
|
|
|
2020-04-07 15:54:27 +00:00
|
|
|
matching' :: [CommandParam] -> [Ref] -> Repo -> IO [(Sha, Branch)]
|
|
|
|
matching' ps rs repo = map gen . S8.lines <$>
|
|
|
|
pipeReadStrict (Param "show-ref" : ps ++ rps) repo
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2020-04-07 15:54:27 +00:00
|
|
|
gen l = let (r, b) = separate' (== fromIntegral (ord ' ')) l
|
2012-12-13 04:24:19 +00:00
|
|
|
in (Ref r, Ref b)
|
2020-04-07 15:54:27 +00:00
|
|
|
rps = map (Param . fromRef) rs
|
2011-12-30 22:36:40 +00:00
|
|
|
|
2017-09-28 18:14:07 +00:00
|
|
|
{- List of (shas, branches) matching a given ref.
|
2013-05-21 22:24:29 +00:00
|
|
|
- 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
|
|
|
|
2017-09-28 18:14:07 +00:00
|
|
|
{- List of all refs. -}
|
|
|
|
list :: Repo -> IO [(Sha, Ref)]
|
2020-04-07 15:54:27 +00:00
|
|
|
list = matching' [] []
|
2017-09-28 18:14:07 +00:00
|
|
|
|
|
|
|
{- Deletes a ref. This can delete refs that are not branches,
|
|
|
|
- which git branch --delete refuses to delete. -}
|
|
|
|
delete :: Sha -> Ref -> Repo -> IO ()
|
|
|
|
delete oldvalue ref = run
|
|
|
|
[ Param "update-ref"
|
|
|
|
, Param "-d"
|
2020-04-07 15:54:27 +00:00
|
|
|
, Param $ fromRef ref
|
|
|
|
, Param $ fromRef oldvalue
|
2017-09-28 18:14:07 +00:00
|
|
|
]
|
|
|
|
|
2019-05-06 20:41:01 +00:00
|
|
|
{- Gets the sha of the tree a ref uses.
|
|
|
|
-
|
|
|
|
- The ref may be something like a branch name, and it could contain
|
|
|
|
- ":subdir" if a subtree is wanted. -}
|
2013-12-01 17:59:39 +00:00
|
|
|
tree :: Ref -> Repo -> IO (Maybe Sha)
|
2020-04-06 21:14:49 +00:00
|
|
|
tree (Ref ref) = extractSha <$$> pipeReadStrict
|
|
|
|
[ Param "rev-parse"
|
|
|
|
, Param "--verify"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param (decodeBS' ref')
|
|
|
|
]
|
2017-10-30 16:02:18 +00:00
|
|
|
where
|
2020-04-06 21:14:49 +00:00
|
|
|
ref' = if ":" `S.isInfixOf` ref
|
2017-10-30 16:02:18 +00:00
|
|
|
then ref
|
|
|
|
-- de-reference commit objects to the tree
|
2020-04-06 21:14:49 +00:00
|
|
|
else ref <> ":"
|
2013-12-01 17:59:39 +00:00
|
|
|
|
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
|
|
|
|
2017-01-31 22:40:42 +00:00
|
|
|
pathbits = splitc '/' s
|
2012-12-13 04:24:19 +00:00
|
|
|
illegalchars = " ~^:?*[\\" ++ controlchars
|
|
|
|
controlchars = chr 0o177 : [chr 0 .. chr (0o40-1)]
|