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-10-07 17:17:54 +00:00
|
|
|
merge_index,
|
2011-06-23 15:37:26 +00:00
|
|
|
update_index,
|
2011-11-16 05:46:46 +00:00
|
|
|
stream_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
|
2011-09-29 23:19:28 +00:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
2011-06-21 18:09:06 +00:00
|
|
|
|
2011-10-07 17:17:54 +00:00
|
|
|
import Common
|
2011-06-30 17:32:47 +00:00
|
|
|
import Git
|
2011-11-12 21:45:12 +00:00
|
|
|
import Git.CatFile
|
2011-06-21 18:09:06 +00:00
|
|
|
|
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
|
|
|
- Should be run with a temporary index file configured by Git.useIndex.
|
2011-06-21 21:39:45 +00:00
|
|
|
-}
|
2011-11-08 19:34:10 +00:00
|
|
|
merge :: String -> String -> Repo -> IO ()
|
|
|
|
merge x y repo = do
|
2011-11-12 21:45:12 +00:00
|
|
|
h <- catFileStart repo
|
2011-11-16 05:46:46 +00:00
|
|
|
stream_update_index repo
|
2011-11-16 03:28:01 +00:00
|
|
|
[ ls_tree x repo
|
|
|
|
, merge_trees x y h repo
|
|
|
|
]
|
2011-11-12 21:45:12 +00:00
|
|
|
catFileStop h
|
2011-10-07 17:17:54 +00:00
|
|
|
|
|
|
|
{- Merges a list of branches into the index. Previously staged changed in
|
|
|
|
- the index are preserved (and participate in the merge). -}
|
2011-11-12 21:45:12 +00:00
|
|
|
merge_index :: CatFileHandle -> Repo -> [String] -> IO ()
|
|
|
|
merge_index h repo bs =
|
2011-11-16 05:46:46 +00:00
|
|
|
stream_update_index repo $ map (\b -> merge_tree_index b h repo) bs
|
2011-06-21 18:09:06 +00:00
|
|
|
|
2011-11-16 03:28:01 +00:00
|
|
|
{- Feeds content into update-index. Later items in the list can override
|
2011-06-21 23:52:40 +00:00
|
|
|
- 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-11-16 03:48:50 +00:00
|
|
|
update_index :: Repo -> [String] -> IO ()
|
2011-11-16 05:46:46 +00:00
|
|
|
update_index repo ls = stream_update_index repo [\s -> mapM_ s ls]
|
2011-11-16 03:48:50 +00:00
|
|
|
|
|
|
|
type Streamer = (String -> IO ()) -> IO ()
|
|
|
|
|
|
|
|
{- Streams content into update-index. -}
|
2011-11-16 05:46:46 +00:00
|
|
|
stream_update_index :: Repo -> [Streamer] -> IO ()
|
|
|
|
stream_update_index repo as = do
|
2011-11-16 02:01:32 +00:00
|
|
|
(p, h) <- hPipeTo "git" (toCommand $ Git.gitCommandLine params repo)
|
2011-11-16 03:48:50 +00:00
|
|
|
forM_ as (stream h)
|
2011-11-16 02:01:32 +00:00
|
|
|
hClose h
|
|
|
|
forceSuccess p
|
2011-06-21 18:09:06 +00:00
|
|
|
where
|
2011-11-16 02:01:32 +00:00
|
|
|
params = map Param ["update-index", "-z", "--index-info"]
|
2011-11-16 03:48:50 +00:00
|
|
|
stream h a = a (streamer h)
|
|
|
|
streamer h s = do
|
|
|
|
hPutStr h s
|
|
|
|
hPutStr h "\0"
|
2011-11-16 03:28:01 +00:00
|
|
|
|
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-11-16 03:28:01 +00:00
|
|
|
{- Gets the contents of a tree. -}
|
2011-11-16 03:48:50 +00:00
|
|
|
ls_tree :: String -> Repo -> Streamer
|
|
|
|
ls_tree x repo streamer = mapM_ streamer =<< pipeNullSplit params repo
|
2011-11-08 19:34:10 +00:00
|
|
|
where
|
|
|
|
params = map Param ["ls-tree", "-z", "-r", "--full-tree", x]
|
2011-06-21 23:52:40 +00:00
|
|
|
|
|
|
|
{- For merging two trees. -}
|
2011-11-16 03:48:50 +00:00
|
|
|
merge_trees :: String -> String -> CatFileHandle -> Repo -> Streamer
|
2011-11-12 21:45:12 +00:00
|
|
|
merge_trees x y h = calc_merge h $ "diff-tree":diff_opts ++ [x, y]
|
2011-06-21 23:52:40 +00:00
|
|
|
|
|
|
|
{- For merging a single tree into the index. -}
|
2011-11-16 03:48:50 +00:00
|
|
|
merge_tree_index :: String -> CatFileHandle -> Repo -> Streamer
|
2011-11-12 21:45:12 +00:00
|
|
|
merge_tree_index x h = calc_merge h $ "diff-index":diff_opts ++ ["--cached", x]
|
2011-06-23 21:38:27 +00:00
|
|
|
|
|
|
|
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-11-16 03:48:50 +00:00
|
|
|
calc_merge :: CatFileHandle -> [String] -> Repo -> Streamer
|
|
|
|
calc_merge ch differ repo streamer = gendiff >>= go
|
2011-06-21 23:52:40 +00:00
|
|
|
where
|
2011-11-16 03:48:50 +00:00
|
|
|
gendiff = pipeNullSplit (map Param differ) repo
|
2011-11-16 03:28:01 +00:00
|
|
|
go [] = return ()
|
|
|
|
go (info:file:rest) = mergeFile info file ch repo >>=
|
2011-11-16 03:48:50 +00:00
|
|
|
maybe (go rest) (\l -> streamer l >> go rest)
|
2011-11-16 03:28:01 +00:00
|
|
|
go (_:[]) = error "calc_merge parse error"
|
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-11-16 03:28:01 +00:00
|
|
|
mergeFile :: String -> FilePath -> CatFileHandle -> Repo -> IO (Maybe String)
|
|
|
|
mergeFile info file h repo = case filter (/= nullsha) [asha, bsha] of
|
2011-06-22 17:59:42 +00:00
|
|
|
[] -> 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-11-12 21:45:12 +00:00
|
|
|
content <- L.concat <$> mapM (catObject h) shas
|
2011-11-08 19:34:10 +00:00
|
|
|
sha <- hashObject (unionmerge content) repo
|
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-07-15 07:12:05 +00:00
|
|
|
nullsha = replicate shaSize '0'
|
2011-09-29 23:19:28 +00:00
|
|
|
unionmerge = L.unlines . nub . L.lines
|
2011-11-16 02:40:40 +00:00
|
|
|
|
|
|
|
{- Injects some content into git, returning its hash. -}
|
|
|
|
hashObject :: L.ByteString -> Repo -> IO String
|
|
|
|
hashObject content repo = getSha subcmd $ do
|
|
|
|
(h, s) <- pipeWriteRead (map Param params) content repo
|
|
|
|
L.length s `seq` do
|
|
|
|
forceSuccess h
|
|
|
|
reap -- XXX unsure why this is needed
|
|
|
|
return $ L.unpack s
|
|
|
|
where
|
|
|
|
subcmd = "hash-object"
|
|
|
|
params = [subcmd, "-w", "--stdin"]
|