summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-06-21 19:52:40 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-06-21 20:12:53 -0400
commit7a693394f4acd05d1dedbaffec0762007a954d9e (patch)
tree6ceb79326b176b7b94531b44746a2eea0e9a39bd
parent5e0adb26375413aaeef7c41dd72e8761e3cc1ada (diff)
allow for union merges between a tree and the content in the index
This is needed for robust handling of the git-annex branch. Since changes are staged to its index as git-annex runs, and committed at the end, it's possible that git-annex is interrupted, and leaves a dirty index. When it next runs, it needs to be able to merge the git-annex branch as necessary, without losing the existing changes in the index. Note that this assumes that the git-annex branch is only modified by git-annex. Any changes to it will be lost when git-annex updates the branch. I don't see a good, inexpensive way to find changes in the git-annex branch that arn't in the index, and union merging the git-annex branch into the index every time would likewise be expensive.
-rw-r--r--Branch.hs31
-rw-r--r--GitUnionMerge.hs79
-rw-r--r--git-union-merge.hs3
3 files changed, 70 insertions, 43 deletions
diff --git a/Branch.hs b/Branch.hs
index 82ff5fcad..9d7b1b094 100644
--- a/Branch.hs
+++ b/Branch.hs
@@ -12,12 +12,13 @@ module Branch (
commit
) where
-import Control.Monad (unless)
+import Control.Monad (unless, liftM)
import Control.Monad.State (liftIO)
import System.FilePath
import System.Directory
import Data.String.Utils
import System.Cmd.Utils
+import Data.Maybe
import qualified GitRepo as Git
import qualified GitUnionMerge
@@ -72,14 +73,18 @@ update = do
updated <- Annex.getState Annex.updated
unless updated $ withIndex $ do
g <- Annex.gitRepo
- refs <- liftIO $ Git.pipeRead g [Param "show-ref", Param name]
- mapM_ updateRef $ map (last . words) (lines refs)
+ r <- liftIO $ Git.pipeRead g [Param "show-ref", Param name]
+ let refs = map (last . words) (lines r)
+ updated <- catMaybes `liftM` mapM updateRef refs
+ unless (null updated) $ liftIO $
+ GitUnionMerge.commit g "update" fullname
+ (fullname:updated)
Annex.changeState $ \s -> s { Annex.updated = True }
-{- Ensures that a given ref has been merged into the local git-annex branch. -}
-updateRef :: String -> Annex ()
+{- Ensures that a given ref has been merged into the index. -}
+updateRef :: String -> Annex (Maybe String)
updateRef ref
- | ref == fullname = return ()
+ | ref == fullname = return Nothing
| otherwise = do
g <- Annex.gitRepo
diffs <- liftIO $ Git.pipeRead g [
@@ -87,9 +92,15 @@ updateRef ref
Param (name++".."++ref),
Params "--oneline -n1"
]
- unless (null diffs) $ do
- showSideAction $ "merging " ++ ref ++ " into " ++ name ++ "..."
- liftIO $ unionMerge g fullname ref fullname True
+ if (null diffs)
+ then return Nothing
+ else do
+ showSideAction $ "merging " ++ ref ++ " into " ++ name ++ "..."
+ -- By passing only one ref, it is actually
+ -- merged into the index, preserving any
+ -- changes that may already be staged.
+ liftIO $ GitUnionMerge.merge g [ref]
+ return $ Just ref
{- Stages the content of a file into the branch's index. -}
change :: FilePath -> String -> Annex ()
@@ -104,7 +115,7 @@ change file content = do
commit :: String -> Annex ()
commit message = withIndex $ do
g <- Annex.gitRepo
- liftIO $ GitUnionMerge.commit g message branch []
+ liftIO $ GitUnionMerge.commit g message fullname []
{- Gets the content of a file on the branch, or content staged in the index
- if it's newer. Returns an empty string if the file didn't exist yet. -}
diff --git a/GitUnionMerge.hs b/GitUnionMerge.hs
index bc12cbe27..82f01cc0f 100644
--- a/GitUnionMerge.hs
+++ b/GitUnionMerge.hs
@@ -7,7 +7,6 @@
module GitUnionMerge (
merge,
- stage,
commit
) where
@@ -19,38 +18,54 @@ import Data.String.Utils
import qualified GitRepo as Git
import Utility
-{- Performs a union merge. Should be run with a temporary index file
- - configured by Git.useIndex.
+{- Performs a union merge between two branches, staging it in the index.
+ - Any previously staged changes in the index will be lost.
-
- - Use indexpopulated only if the index file already contains exactly the
- - contents of aref.
+ - When only one branch is specified, it is merged into the index.
+ - In this case, previously staged changes in the index are preserved.
+ -
+ - Should be run with a temporary index file configured by Git.useIndex.
-}
-merge :: Git.Repo -> String -> String -> String -> Bool -> IO ()
-merge g aref bref newref indexpopulated = do
- stage g aref bref indexpopulated
- commit g "union merge" newref [aref, bref]
+merge :: Git.Repo -> [String] -> IO ()
+merge g (x:y:[]) = do
+ a <- ls_tree g x
+ b <- merge_trees g x y
+ update_index g (a++b)
+merge g [x] = merge_tree_index g x >>= update_index g
+merge _ _ = error "wrong number of branches to merge"
-{- Stages the content of both refs into the index. -}
-stage :: Git.Repo -> String -> String -> Bool -> IO ()
-stage g aref bref indexpopulated = do
- -- Get the contents of aref, as a starting point, unless
- -- the index is already populated with it.
- ls <- if indexpopulated
- then return []
- else fromgit ["ls-tree", "-z", "-r", "--full-tree", aref]
- -- Identify files that are different between aref and bref, and
- -- inject merged versions into git.
- diff <- fromgit
- ["diff-tree", "--raw", "-z", "-r", "--no-renames", "-l0", aref, bref]
- ls' <- mapM mergefile (pairs diff)
- -- Populate the index file. Later lines override earlier ones.
- togit ["update-index", "-z", "--index-info"]
- (join "\0" $ ls++catMaybes ls')
+{- Feeds a list into update-index. Later items in the list can override
+ - earlier ones, so the list can be generated from any combination of
+ - ls_tree, merge_trees, and merge_tree. -}
+update_index :: Git.Repo -> [String] -> IO ()
+update_index g l = togit ["update-index", "-z", "--index-info"] (join "\0" l)
where
- fromgit l = Git.pipeNullSplit g (map Param l)
- togit l content = Git.pipeWrite g (map Param l) content
+ togit ps content = Git.pipeWrite g (map Param ps) content
>>= forceSuccess
+{- Gets the contents of a tree in a format suitable for update_index. -}
+ls_tree :: Git.Repo -> String -> IO [String]
+ls_tree g x = Git.pipeNullSplit g $
+ map Param ["ls-tree", "-z", "-r", "--full-tree", x]
+
+{- For merging two trees. -}
+merge_trees :: Git.Repo -> String -> String -> IO [String]
+merge_trees g x y = calc_merge g
+ ["diff-tree", "--raw", "-z", "-r", "--no-renames", "-l0", x, y]
+
+{- For merging a single tree into the index. -}
+merge_tree_index :: Git.Repo -> String -> IO [String]
+merge_tree_index g x = calc_merge g
+ ["diff-index", "--raw", "-z", "-r", "--no-renames", "-l0", x]
+
+{- Calculates how to perform a merge, using git to get a raw diff,
+ - and returning a list suitable for update_index. -}
+calc_merge :: Git.Repo -> [String] -> IO [String]
+calc_merge g differ = do
+ diff <- Git.pipeNullSplit g $ map Param differ
+ l <- mapM mergefile (pairs diff)
+ return $ catMaybes l
+ where
pairs [] = []
pairs (_:[]) = error "parse error"
pairs (a:b:rest) = (a,b):pairs rest
@@ -62,7 +77,7 @@ stage g aref bref indexpopulated = do
mergefile (info, file) = do
let [_colonamode, _bmode, asha, bsha, _status] = words info
if bsha == nullsha
- then return Nothing -- already staged from aref
+ then return Nothing -- already staged
else mergefile' file asha bsha
mergefile' file asha bsha = do
let shas = filter (/= nullsha) [asha, bsha]
@@ -70,10 +85,10 @@ stage g aref bref indexpopulated = do
sha <- Git.hashObject g $ unionmerge content
return $ Just $ ls_tree_line sha file
-{- Commits the index into the specified branch. If refs are specified,
- - commits a merge. -}
+{- Commits the index into the specified branch,
+ - with the specified parent refs. -}
commit :: Git.Repo -> String -> String -> [String] -> IO ()
-commit g message newref mergedrefs = do
+commit g message newref parentrefs = do
tree <- Git.getSha "write-tree" $ ignorehandle $
pipeFrom "git" ["write-tree"]
sha <- Git.getSha "commit-tree" $ ignorehandle $
@@ -81,4 +96,4 @@ commit g message newref mergedrefs = do
Git.run g "update-ref" [Param newref, Param sha]
where
ignorehandle a = return . snd =<< a
- ps = concatMap (\r -> ["-p", r]) mergedrefs
+ ps = concatMap (\r -> ["-p", r]) parentrefs
diff --git a/git-union-merge.hs b/git-union-merge.hs
index f02db6be3..7c0c1cd84 100644
--- a/git-union-merge.hs
+++ b/git-union-merge.hs
@@ -44,5 +44,6 @@ main = do
g <- Git.configRead =<< Git.repoFromCwd
Git.useIndex (tmpIndex g)
setup g
- GitUnionMerge.merge g aref bref newref False
+ GitUnionMerge.merge g [aref, bref]
+ GitUnionMerge.commit g "union merge" newref [aref, bref]
cleanup g