2011-12-12 22:23:24 +00:00
|
|
|
{- git ref stuff
|
|
|
|
-
|
2024-05-07 18:22:04 +00:00
|
|
|
- Copyright 2011-2024 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.
|
reinject: Fix crash when reinjecting a file from outside the repository
Commit 4bf7940d6b912fbf692b268f621ebd41ed871125 introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.
Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.
In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.
Sponsored-by: Brock Spratlen on Patreon
2021-10-01 18:04:18 +00:00
|
|
|
-
|
|
|
|
- If the input file is located outside the repository, returns Nothing.
|
2013-11-07 17:55:36 +00:00
|
|
|
-}
|
reinject: Fix crash when reinjecting a file from outside the repository
Commit 4bf7940d6b912fbf692b268f621ebd41ed871125 introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.
Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.
In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.
Sponsored-by: Brock Spratlen on Patreon
2021-10-01 18:04:18 +00:00
|
|
|
fileRef :: RawFilePath -> Repo -> IO (Maybe Ref)
|
|
|
|
fileRef f repo = do
|
2021-05-07 17:25:59 +00:00
|
|
|
-- The filename could be absolute, or contain eg "../repo/file",
|
|
|
|
-- neither of which work in a ref, so convert it to a minimal
|
|
|
|
-- relative path.
|
|
|
|
f' <- relPathCwdToFile f
|
reinject: Fix crash when reinjecting a file from outside the repository
Commit 4bf7940d6b912fbf692b268f621ebd41ed871125 introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.
Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.
In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.
Sponsored-by: Brock Spratlen on Patreon
2021-10-01 18:04:18 +00:00
|
|
|
return $ if repoPath repo `dirContains` f'
|
|
|
|
-- Prefixing the file with ./ makes this work even when in a
|
|
|
|
-- subdirectory of a repo. Eg, ./foo in directory bar refers
|
|
|
|
-- to bar/foo, not to foo in the top of the repository.
|
|
|
|
then Just $ Ref $ ":./" <> toInternalGitPath f'
|
|
|
|
else Nothing
|
2020-07-10 17:28:16 +00:00
|
|
|
|
|
|
|
{- 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
|
2021-08-11 00:45:02 +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
|
reinject: Fix crash when reinjecting a file from outside the repository
Commit 4bf7940d6b912fbf692b268f621ebd41ed871125 introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.
Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.
In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.
Sponsored-by: Brock Spratlen on Patreon
2021-10-01 18:04:18 +00:00
|
|
|
- appears in a given Ref.
|
|
|
|
-
|
|
|
|
- If the file path is located outside the repository, returns Nothing.
|
|
|
|
-}
|
|
|
|
fileFromRef :: Ref -> RawFilePath -> Repo -> IO (Maybe Ref)
|
|
|
|
fileFromRef r f repo = fileRef f repo >>= return . \case
|
|
|
|
Just (Ref fr) -> Just (Ref (fromRef' r <> fr))
|
|
|
|
Nothing -> Nothing
|
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
|
|
|
|
2024-05-07 18:22:04 +00:00
|
|
|
{- Lists refs using for-each-ref. -}
|
|
|
|
forEachRef :: [CommandParam] -> Repo -> IO [(Sha, Branch)]
|
|
|
|
forEachRef ps repo = map gen . S8.lines <$>
|
|
|
|
pipeReadStrict (Param "for-each-ref" : ps ++ [format]) repo
|
|
|
|
where
|
|
|
|
format = Param "--format=%(objectname) %(refname)"
|
|
|
|
gen l = let (r, b) = separate' (== fromIntegral (ord ' ')) l
|
|
|
|
in (Ref r, Ref b)
|
|
|
|
|
2024-05-09 20:11:16 +00:00
|
|
|
{- Deletes a ref when it contains the specified sha.
|
|
|
|
- This can delete refs that are not branches, which
|
|
|
|
- git branch --delete refuses to delete. -}
|
2017-09-28 18:14:07 +00:00
|
|
|
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
|
|
|
]
|
|
|
|
|
2024-05-09 20:11:16 +00:00
|
|
|
{- Deletes a ref no matter what it contains. -}
|
|
|
|
delete' :: Ref -> Repo -> IO ()
|
|
|
|
delete' ref = run
|
|
|
|
[ Param "update-ref"
|
|
|
|
, Param "-d"
|
|
|
|
, Param $ fromRef ref
|
|
|
|
]
|
|
|
|
|
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"
|
2021-08-11 00:45:02 +00:00
|
|
|
, Param (decodeBS ref')
|
2020-04-06 21:14:49 +00:00
|
|
|
]
|
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
|
|
|
|
2024-05-09 20:11:16 +00:00
|
|
|
{- Check if the first ref is an ancestor of the second ref.
|
|
|
|
-
|
|
|
|
- Note that if the two refs point to the same commit, it is considered
|
|
|
|
- to be an ancestor of itself.
|
|
|
|
-}
|
|
|
|
isAncestor :: Ref -> Ref -> Repo -> IO Bool
|
|
|
|
isAncestor r1 r2 = runBool
|
|
|
|
[ Param "merge-base"
|
2024-05-13 17:52:58 +00:00
|
|
|
, Param "--is-ancestor"
|
2024-05-09 20:11:16 +00:00
|
|
|
, Param (fromRef r1)
|
|
|
|
, Param (fromRef r2)
|
|
|
|
]
|
|
|
|
|
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)]
|