2011-12-13 01:12:51 +00:00
|
|
|
{- git branch stuff
|
|
|
|
-
|
2021-05-17 17:24:58 +00:00
|
|
|
- Copyright 2011-2021 Joey Hess <id@joeyh.name>
|
2011-12-13 01:12:51 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-12-13 01:12:51 +00:00
|
|
|
-}
|
|
|
|
|
2012-08-05 19:45:47 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
2019-11-27 20:54:11 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2012-08-05 19:45:47 +00:00
|
|
|
|
2011-12-13 01:12:51 +00:00
|
|
|
module Git.Branch where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2011-12-14 19:30:14 +00:00
|
|
|
import Git.Sha
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2016-06-02 19:07:20 +00:00
|
|
|
import qualified Git.Config
|
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
|
|
|
import qualified Git.Ref
|
2011-12-13 01:12:51 +00:00
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
import qualified Data.ByteString as B
|
2020-04-07 17:27:11 +00:00
|
|
|
import qualified Data.ByteString.Char8 as B8
|
2019-11-25 20:18:19 +00:00
|
|
|
|
2012-08-05 19:45:47 +00:00
|
|
|
{- The currently checked out branch.
|
|
|
|
-
|
|
|
|
- In a just initialized git repo before the first commit,
|
|
|
|
- symbolic-ref will show the master branch, even though that
|
2019-07-16 16:28:44 +00:00
|
|
|
- branch is not created yet. So, this also looks at show-ref
|
2012-08-05 19:45:47 +00:00
|
|
|
- to double-check.
|
|
|
|
-}
|
2016-02-25 19:33:50 +00:00
|
|
|
current :: Repo -> IO (Maybe Branch)
|
2012-08-05 19:45:47 +00:00
|
|
|
current r = do
|
2012-08-05 20:35:30 +00:00
|
|
|
v <- currentUnsafe r
|
|
|
|
case v of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just branch ->
|
2019-11-25 20:18:19 +00:00
|
|
|
ifM (B.null <$> pipeReadStrict [Param "show-ref", Param $ fromRef branch] r)
|
2012-08-05 20:35:30 +00:00
|
|
|
( return Nothing
|
|
|
|
, return v
|
|
|
|
)
|
|
|
|
|
|
|
|
{- The current branch, which may not really exist yet. -}
|
2016-02-25 19:33:50 +00:00
|
|
|
currentUnsafe :: Repo -> IO (Maybe Branch)
|
2020-04-07 17:27:11 +00:00
|
|
|
currentUnsafe r = parse . firstLine' <$> pipeReadStrict
|
|
|
|
[ Param "symbolic-ref"
|
|
|
|
, Param "-q"
|
|
|
|
, Param $ fromRef Git.Ref.headRef
|
|
|
|
] r
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2019-11-25 20:18:19 +00:00
|
|
|
parse b
|
|
|
|
| B.null b = Nothing
|
2020-04-07 17:27:11 +00:00
|
|
|
| otherwise = Just $ Git.Ref b
|
2011-12-31 07:38:58 +00:00
|
|
|
|
2011-12-13 01:12:51 +00:00
|
|
|
{- Checks if the second branch has any commits not present on the first
|
|
|
|
- branch. -}
|
|
|
|
changed :: Branch -> Branch -> Repo -> IO Bool
|
|
|
|
changed origbranch newbranch repo
|
|
|
|
| origbranch == newbranch = return False
|
2020-04-07 17:27:11 +00:00
|
|
|
| otherwise = not . B.null
|
2016-03-03 20:19:09 +00:00
|
|
|
<$> changed' origbranch newbranch [Param "-n1"] repo
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2016-03-03 20:19:09 +00:00
|
|
|
|
2020-04-07 17:27:11 +00:00
|
|
|
changed' :: Branch -> Branch -> [CommandParam] -> Repo -> IO B.ByteString
|
|
|
|
changed' origbranch newbranch extraps repo = pipeReadStrict ps repo
|
2016-03-03 20:19:09 +00:00
|
|
|
where
|
|
|
|
ps =
|
2012-12-13 04:24:19 +00:00
|
|
|
[ Param "log"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param (fromRef origbranch ++ ".." ++ fromRef newbranch)
|
2014-06-09 22:01:30 +00:00
|
|
|
, Param "--pretty=%H"
|
2016-03-03 20:19:09 +00:00
|
|
|
] ++ extraps
|
2014-06-09 22:01:30 +00:00
|
|
|
|
2016-03-03 20:19:09 +00:00
|
|
|
{- Lists commits that are in the second branch and not in the first branch. -}
|
|
|
|
changedCommits :: Branch -> Branch -> [CommandParam] -> Repo -> IO [Sha]
|
|
|
|
changedCommits origbranch newbranch extraps repo =
|
2020-04-07 17:27:11 +00:00
|
|
|
catMaybes . map extractSha . B8.lines
|
2016-03-03 20:19:09 +00:00
|
|
|
<$> changed' origbranch newbranch extraps repo
|
|
|
|
|
2014-06-09 22:01:30 +00:00
|
|
|
{- Check if it's possible to fast-forward from the old
|
|
|
|
- ref to the new ref.
|
|
|
|
-
|
|
|
|
- This requires there to be a path from the old to the new. -}
|
|
|
|
fastForwardable :: Ref -> Ref -> Repo -> IO Bool
|
2019-11-25 20:18:19 +00:00
|
|
|
fastForwardable old new repo = not . B.null <$>
|
2014-06-09 22:01:30 +00:00
|
|
|
pipeReadStrict
|
|
|
|
[ Param "log"
|
|
|
|
, Param $ fromRef old ++ ".." ++ fromRef new
|
|
|
|
, Param "-n1"
|
|
|
|
, Param "--pretty=%H"
|
|
|
|
, Param "--ancestry-path"
|
2012-12-13 04:24:19 +00:00
|
|
|
] repo
|
2011-12-13 01:12:51 +00:00
|
|
|
|
|
|
|
{- Given a set of refs that are all known to have commits not
|
|
|
|
- on the branch, tries to update the branch by a fast-forward.
|
|
|
|
-
|
|
|
|
- In order for that to be possible, one of the refs must contain
|
|
|
|
- every commit present in all the other refs.
|
|
|
|
-}
|
|
|
|
fastForward :: Branch -> [Ref] -> Repo -> IO Bool
|
|
|
|
fastForward _ [] _ = return True
|
2012-03-16 05:59:07 +00:00
|
|
|
fastForward branch (first:rest) repo =
|
2011-12-13 01:12:51 +00:00
|
|
|
-- First, check that the branch does not contain any
|
|
|
|
-- new commits that are not in the first ref. If it does,
|
|
|
|
-- cannot fast-forward.
|
2012-03-16 05:59:07 +00:00
|
|
|
ifM (changed first branch repo)
|
|
|
|
( no_ff
|
|
|
|
, maybe no_ff do_ff =<< findbest first rest
|
|
|
|
)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
no_ff = return False
|
|
|
|
do_ff to = do
|
2016-03-31 16:27:48 +00:00
|
|
|
update' branch to repo
|
2012-12-13 04:24:19 +00:00
|
|
|
return True
|
|
|
|
findbest c [] = return $ Just c
|
|
|
|
findbest c (r:rs)
|
|
|
|
| c == r = findbest c rs
|
|
|
|
| otherwise = do
|
|
|
|
better <- changed c r repo
|
|
|
|
worse <- changed r c repo
|
|
|
|
case (better, worse) of
|
|
|
|
(True, True) -> return Nothing -- divergent fail
|
|
|
|
(True, False) -> findbest r rs -- better
|
|
|
|
(False, True) -> findbest c rs -- worse
|
|
|
|
(False, False) -> findbest c rs -- same
|
2011-12-13 19:08:44 +00:00
|
|
|
|
2016-06-02 19:07:20 +00:00
|
|
|
{- The user may have set commit.gpgsign, intending all their manual
|
2014-07-04 15:36:59 +00:00
|
|
|
- commits to be signed. But signing automatic/background commits could
|
|
|
|
- easily lead to unwanted gpg prompts or failures.
|
|
|
|
-}
|
|
|
|
data CommitMode = ManualCommit | AutomaticCommit
|
|
|
|
deriving (Eq)
|
|
|
|
|
2016-06-02 19:07:20 +00:00
|
|
|
{- Prevent signing automatic commits. -}
|
2014-07-04 17:48:55 +00:00
|
|
|
applyCommitMode :: CommitMode -> [CommandParam] -> [CommandParam]
|
|
|
|
applyCommitMode commitmode ps
|
2019-09-11 20:10:25 +00:00
|
|
|
| commitmode == AutomaticCommit = Param "--no-gpg-sign" : ps
|
2014-07-04 17:48:55 +00:00
|
|
|
| otherwise = ps
|
|
|
|
|
2016-06-02 19:07:20 +00:00
|
|
|
{- Some versions of git commit-tree honor commit.gpgsign themselves,
|
|
|
|
- but others need -S to be passed to enable gpg signing of manual commits. -}
|
|
|
|
applyCommitModeForCommitTree :: CommitMode -> [CommandParam] -> Repo -> [CommandParam]
|
|
|
|
applyCommitModeForCommitTree commitmode ps r
|
|
|
|
| commitmode == ManualCommit =
|
2019-11-27 20:54:11 +00:00
|
|
|
case Git.Config.getMaybe "commit.gpgsign" r of
|
2019-12-20 19:01:34 +00:00
|
|
|
Just s | Git.Config.isTrueFalse' s == Just True ->
|
2016-06-02 19:07:20 +00:00
|
|
|
Param "-S":ps
|
|
|
|
_ -> ps'
|
|
|
|
| otherwise = ps'
|
|
|
|
where
|
|
|
|
ps' = applyCommitMode commitmode ps
|
|
|
|
|
2014-07-04 15:36:59 +00:00
|
|
|
{- Commit via the usual git command. -}
|
|
|
|
commitCommand :: CommitMode -> [CommandParam] -> Repo -> IO Bool
|
|
|
|
commitCommand = commitCommand' runBool
|
|
|
|
|
|
|
|
commitCommand' :: ([CommandParam] -> Repo -> IO a) -> CommitMode -> [CommandParam] -> Repo -> IO a
|
2014-07-04 17:48:55 +00:00
|
|
|
commitCommand' runner commitmode ps = runner $
|
|
|
|
Param "commit" : applyCommitMode commitmode ps
|
2014-07-04 15:36:59 +00:00
|
|
|
|
2011-12-13 19:08:44 +00:00
|
|
|
{- Commits the index into the specified branch (or other ref),
|
2013-12-01 17:59:39 +00:00
|
|
|
- with the specified parent refs, and returns the committed sha.
|
|
|
|
-
|
|
|
|
- Without allowempy set, avoids making a commit if there is exactly
|
|
|
|
- one parent, and it has the same tree that would be committed.
|
|
|
|
-
|
|
|
|
- Unlike git-commit, does not run any hooks, or examine the work tree
|
|
|
|
- in any way.
|
|
|
|
-}
|
2014-07-04 15:36:59 +00:00
|
|
|
commit :: CommitMode -> Bool -> String -> Branch -> [Ref] -> Repo -> IO (Maybe Sha)
|
|
|
|
commit commitmode allowempty message branch parentrefs repo = do
|
2021-05-17 17:24:58 +00:00
|
|
|
tree <- writeTree repo
|
2013-12-01 17:59:39 +00:00
|
|
|
ifM (cancommit tree)
|
|
|
|
( do
|
2016-02-25 19:33:50 +00:00
|
|
|
sha <- commitTree commitmode message parentrefs tree repo
|
2016-03-31 16:27:48 +00:00
|
|
|
update' branch sha repo
|
2013-12-01 17:59:39 +00:00
|
|
|
return $ Just sha
|
|
|
|
, return Nothing
|
|
|
|
)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2013-12-01 17:59:39 +00:00
|
|
|
cancommit tree
|
|
|
|
| allowempty = return True
|
|
|
|
| otherwise = case parentrefs of
|
|
|
|
[p] -> maybe False (tree /=) <$> Git.Ref.tree p repo
|
|
|
|
_ -> return True
|
|
|
|
|
2014-07-04 15:36:59 +00:00
|
|
|
commitAlways :: CommitMode -> String -> Branch -> [Ref] -> Repo -> IO Sha
|
|
|
|
commitAlways commitmode message branch parentrefs repo = fromJust
|
|
|
|
<$> commit commitmode True message branch parentrefs repo
|
2013-08-29 18:15:32 +00:00
|
|
|
|
2021-05-24 15:33:23 +00:00
|
|
|
-- Throws exception if the index is locked, with an error message output by
|
|
|
|
-- git on stderr.
|
2021-05-17 17:24:58 +00:00
|
|
|
writeTree :: Repo -> IO Sha
|
2021-05-24 15:33:23 +00:00
|
|
|
writeTree repo = getSha "write-tree" $
|
|
|
|
pipeReadStrict [Param "write-tree"] repo
|
|
|
|
|
|
|
|
-- Avoids error output if the command fails due to eg, the index being locked.
|
|
|
|
writeTreeQuiet :: Repo -> IO (Maybe Sha)
|
|
|
|
writeTreeQuiet repo = extractSha <$> withNullHandle go
|
|
|
|
where
|
|
|
|
go nullh = pipeReadStrict' (\p -> p { std_err = UseHandle nullh })
|
|
|
|
[Param "write-tree"] repo
|
2021-05-17 17:24:58 +00:00
|
|
|
|
2016-02-25 19:33:50 +00:00
|
|
|
commitTree :: CommitMode -> String -> [Ref] -> Ref -> Repo -> IO Sha
|
|
|
|
commitTree commitmode message parentrefs tree repo =
|
|
|
|
getSha "commit-tree" $
|
|
|
|
pipeWriteRead ([Param "commit-tree", Param (fromRef tree)] ++ ps)
|
|
|
|
sendmsg repo
|
|
|
|
where
|
|
|
|
sendmsg = Just $ flip hPutStr message
|
2016-06-02 19:07:20 +00:00
|
|
|
ps = applyCommitModeForCommitTree commitmode parentparams repo
|
|
|
|
parentparams = map Param $ concatMap (\r -> ["-p", fromRef r]) parentrefs
|
2016-02-25 19:33:50 +00:00
|
|
|
|
2013-08-29 18:15:32 +00:00
|
|
|
{- A leading + makes git-push force pushing a branch. -}
|
|
|
|
forcePush :: String -> String
|
|
|
|
forcePush b = "+" ++ b
|
2013-11-05 22:20:52 +00:00
|
|
|
|
2016-04-04 17:17:24 +00:00
|
|
|
{- Updates a branch (or other ref) to a new Sha or branch Ref. -}
|
|
|
|
update :: String -> Branch -> Ref -> Repo -> IO ()
|
|
|
|
update message branch r = run
|
2016-03-31 16:27:48 +00:00
|
|
|
[ Param "update-ref"
|
|
|
|
, Param "-m"
|
|
|
|
, Param message
|
|
|
|
, Param $ fromRef branch
|
2016-04-04 17:17:24 +00:00
|
|
|
, Param $ fromRef r
|
2016-03-31 16:27:48 +00:00
|
|
|
]
|
|
|
|
|
2016-04-04 17:17:24 +00:00
|
|
|
update' :: Branch -> Ref -> Repo -> IO ()
|
|
|
|
update' branch r = run
|
2013-11-05 22:20:52 +00:00
|
|
|
[ Param "update-ref"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param $ fromRef branch
|
2016-04-04 17:17:24 +00:00
|
|
|
, Param $ fromRef r
|
2013-11-05 22:20:52 +00:00
|
|
|
]
|
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
|
|
|
|
|
|
|
{- Checks out a branch, creating it if necessary. -}
|
|
|
|
checkout :: Branch -> Repo -> IO ()
|
|
|
|
checkout branch = run
|
|
|
|
[ Param "checkout"
|
|
|
|
, Param "-q"
|
|
|
|
, Param "-B"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param $ fromRef $ Git.Ref.base branch
|
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
|
|
|
]
|
|
|
|
|
|
|
|
{- Removes a branch. -}
|
|
|
|
delete :: Branch -> Repo -> IO ()
|
|
|
|
delete branch = run
|
|
|
|
[ Param "branch"
|
|
|
|
, Param "-q"
|
|
|
|
, Param "-D"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param $ fromRef $ Git.Ref.base branch
|
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
|
|
|
]
|