2011-06-21 18:09:06 +00:00
|
|
|
{- git-union-merge library
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-06-30 17:32:47 +00:00
|
|
|
module Git.UnionMerge (
|
2011-06-21 23:09:20 +00:00
|
|
|
merge,
|
2011-06-23 15:37:26 +00:00
|
|
|
update_index,
|
2011-06-23 21:38:27 +00:00
|
|
|
update_index_line,
|
|
|
|
ls_tree
|
2011-06-21 18:09:06 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import System.Cmd.Utils
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.String.Utils
|
|
|
|
|
2011-06-30 17:32:47 +00:00
|
|
|
import Git
|
2011-06-21 18:09:06 +00:00
|
|
|
import Utility
|
|
|
|
|
2011-06-21 23:52:40 +00:00
|
|
|
{- Performs a union merge between two branches, staging it in the index.
|
|
|
|
- Any previously staged changes in the index will be lost.
|
2011-06-21 21:39:45 +00:00
|
|
|
-
|
2011-06-21 23:52:40 +00:00
|
|
|
- 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.
|
2011-06-21 21:39:45 +00:00
|
|
|
-}
|
2011-06-30 17:32:47 +00:00
|
|
|
merge :: Repo -> [String] -> IO ()
|
2011-06-21 23:52:40 +00:00
|
|
|
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"
|
2011-06-21 18:09:06 +00:00
|
|
|
|
2011-06-21 23:52:40 +00:00
|
|
|
{- 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
|
2011-06-22 17:59:42 +00:00
|
|
|
- ls_tree, merge_trees, and merge_tree_index. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
update_index :: Repo -> [String] -> IO ()
|
2011-06-23 21:38:27 +00:00
|
|
|
update_index g l = togit ["update-index", "-z", "--index-info"] (join "\0" l)
|
2011-06-21 18:09:06 +00:00
|
|
|
where
|
2011-06-30 17:32:47 +00:00
|
|
|
togit ps content = pipeWrite g (map Param ps) content
|
2011-06-21 18:09:06 +00:00
|
|
|
>>= forceSuccess
|
|
|
|
|
2011-06-23 15:37:26 +00:00
|
|
|
{- Generates a line suitable to be fed into update-index, to add
|
|
|
|
- a given file with a given sha. -}
|
|
|
|
update_index_line :: String -> FilePath -> String
|
|
|
|
update_index_line sha file = "100644 blob " ++ sha ++ "\t" ++ file
|
|
|
|
|
2011-06-21 23:52:40 +00:00
|
|
|
{- Gets the contents of a tree in a format suitable for update_index. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
ls_tree :: Repo -> String -> IO [String]
|
|
|
|
ls_tree g x = pipeNullSplit g $
|
2011-06-21 23:52:40 +00:00
|
|
|
map Param ["ls-tree", "-z", "-r", "--full-tree", x]
|
|
|
|
|
|
|
|
{- For merging two trees. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
merge_trees :: Repo -> String -> String -> IO [String]
|
2011-06-23 21:38:27 +00:00
|
|
|
merge_trees g x y = calc_merge g $ "diff-tree":diff_opts ++ [x, y]
|
2011-06-21 23:52:40 +00:00
|
|
|
|
|
|
|
{- For merging a single tree into the index. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
merge_tree_index :: Repo -> String -> IO [String]
|
2011-06-23 21:38:27 +00:00
|
|
|
merge_tree_index g x = calc_merge g $ "diff-index":diff_opts ++ ["--cached", x]
|
|
|
|
|
|
|
|
diff_opts :: [String]
|
|
|
|
diff_opts = ["--raw", "-z", "-r", "--no-renames", "-l0"]
|
2011-06-21 23:52:40 +00:00
|
|
|
|
|
|
|
{- Calculates how to perform a merge, using git to get a raw diff,
|
|
|
|
- and returning a list suitable for update_index. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
calc_merge :: Repo -> [String] -> IO [String]
|
2011-06-21 23:52:40 +00:00
|
|
|
calc_merge g differ = do
|
2011-06-30 17:32:47 +00:00
|
|
|
diff <- pipeNullSplit g $ map Param differ
|
2011-06-22 17:59:42 +00:00
|
|
|
l <- mapM (mergeFile g) (pairs diff)
|
2011-06-21 23:52:40 +00:00
|
|
|
return $ catMaybes l
|
|
|
|
where
|
2011-06-21 18:09:06 +00:00
|
|
|
pairs [] = []
|
2011-06-22 17:59:42 +00:00
|
|
|
pairs (_:[]) = error "calc_merge parse error"
|
2011-06-21 18:09:06 +00:00
|
|
|
pairs (a:b:rest) = (a,b):pairs rest
|
2011-06-22 17:59:42 +00:00
|
|
|
|
|
|
|
{- Given an info line from a git raw diff, and the filename, generates
|
|
|
|
- a line suitable for update_index that union merges the two sides of the
|
|
|
|
- diff. -}
|
2011-06-30 17:32:47 +00:00
|
|
|
mergeFile :: Repo -> (String, FilePath) -> IO (Maybe String)
|
2011-06-22 17:59:42 +00:00
|
|
|
mergeFile g (info, file) = case filter (/= nullsha) [asha, bsha] of
|
|
|
|
[] -> return Nothing
|
2011-06-23 15:37:26 +00:00
|
|
|
(sha:[]) -> return $ Just $ update_index_line sha file
|
2011-06-22 17:59:42 +00:00
|
|
|
shas -> do
|
2011-06-30 17:32:47 +00:00
|
|
|
content <- pipeRead g $ map Param ("show":shas)
|
|
|
|
sha <- hashObject g $ unionmerge content
|
2011-06-23 15:37:26 +00:00
|
|
|
return $ Just $ update_index_line sha file
|
2011-06-22 17:59:42 +00:00
|
|
|
where
|
|
|
|
[_colonamode, _bmode, asha, bsha, _status] = words info
|
2011-06-30 17:32:47 +00:00
|
|
|
nullsha = take shaSize $ repeat '0'
|
2011-06-21 18:09:06 +00:00
|
|
|
unionmerge = unlines . nub . lines
|