2011-12-13 01:12:51 +00:00
|
|
|
{- git branch stuff
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-08-05 19:45:47 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
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
|
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
|
|
|
|
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
|
|
|
|
- branch is not created yet. So, this also looks at show-ref HEAD
|
|
|
|
- to double-check.
|
|
|
|
-}
|
2011-12-31 07:38:58 +00:00
|
|
|
current :: Repo -> IO (Maybe Git.Ref)
|
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 ->
|
2014-02-19 05:09:17 +00:00
|
|
|
ifM (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. -}
|
|
|
|
currentUnsafe :: Repo -> IO (Maybe Git.Ref)
|
|
|
|
currentUnsafe r = parse . firstLine
|
2014-02-19 05:09:17 +00:00
|
|
|
<$> pipeReadStrict [Param "symbolic-ref", Param $ fromRef Git.Ref.headRef] r
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
parse l
|
|
|
|
| null l = Nothing
|
|
|
|
| otherwise = Just $ Git.Ref l
|
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
|
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
|
|
|
| otherwise = not . null <$> diffs
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
diffs = pipeReadStrict
|
|
|
|
[ Param "log"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param (fromRef origbranch ++ ".." ++ fromRef newbranch)
|
2012-12-13 04:24:19 +00:00
|
|
|
, Params "--oneline -n1"
|
|
|
|
] 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
|
2014-02-19 05:09:17 +00:00
|
|
|
run [Param "update-ref", Param $ fromRef branch, Param $ fromRef 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
|
|
|
|
|
|
|
{- 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.
|
|
|
|
-}
|
|
|
|
commit :: Bool -> String -> Branch -> [Ref] -> Repo -> IO (Maybe Sha)
|
|
|
|
commit allowempty message branch parentrefs repo = do
|
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
|
|
|
tree <- getSha "write-tree" $
|
2012-10-04 22:04:09 +00:00
|
|
|
pipeReadStrict [Param "write-tree"] repo
|
2013-12-01 17:59:39 +00:00
|
|
|
ifM (cancommit tree)
|
|
|
|
( do
|
|
|
|
sha <- getSha "commit-tree" $ pipeWriteRead
|
2014-02-19 05:09:17 +00:00
|
|
|
(map Param $ ["commit-tree", fromRef tree] ++ ps)
|
2013-12-01 17:59:39 +00:00
|
|
|
(Just $ flip hPutStr message) repo
|
|
|
|
update branch sha repo
|
|
|
|
return $ Just sha
|
|
|
|
, return Nothing
|
|
|
|
)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2014-02-19 05:09:17 +00:00
|
|
|
ps = concatMap (\r -> ["-p", fromRef r]) parentrefs
|
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
|
|
|
|
|
|
|
|
commitAlways :: String -> Branch -> [Ref] -> Repo -> IO Sha
|
|
|
|
commitAlways message branch parentrefs repo = fromJust
|
|
|
|
<$> commit True message branch parentrefs repo
|
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
|
|
|
|
|
|
|
{- Updates a branch (or other ref) to a new Sha. -}
|
|
|
|
update :: Branch -> Sha -> Repo -> IO ()
|
|
|
|
update branch sha = run
|
|
|
|
[ Param "update-ref"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param $ fromRef branch
|
|
|
|
, Param $ fromRef sha
|
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
|
|
|
]
|