1{- git-union-merge library 2 - 3 - Copyright 2011 Joey Hess <id@joeyh.name> 4 - 5 - Licensed under the GNU AGPL version 3 or higher. 6 -} 7 8module Git.UnionMerge ( 9 merge, 10 mergeIndex 11) where 12 13import qualified Data.ByteString as S 14import qualified Data.ByteString.Lazy as L 15import qualified Data.ByteString.Lazy.Char8 as L8 16import qualified Data.ByteString.Char8 as S8 17import qualified Data.Set as S 18 19import Common 20import Git 21import Git.Sha 22import Git.CatFile 23import Git.Command 24import Git.UpdateIndex 25import Git.HashObject 26import Git.Types 27import Git.FilePath 28 29{- Performs a union merge between two branches, staging it in the index. 30 - Any previously staged changes in the index will be lost. 31 - 32 - Should be run with a temporary index file configured by useIndex. 33 -} 34merge :: Ref -> Ref -> Repo -> IO () 35merge x y repo = do 36 hashhandle <- hashObjectStart True repo 37 ch <- catFileStart repo 38 streamUpdateIndex repo 39 [ lsTree x repo 40 , mergeTrees x y hashhandle ch repo 41 ] 42 catFileStop ch 43 hashObjectStop hashhandle 44 45{- Merges a list of branches into the index. Previously staged changes in 46 - the index are preserved (and participate in the merge). 47 - 48 - update-index is run once per ref in turn, so that each ref is merged on 49 - top of the merge for the previous ref. It would be more efficient, but 50 - harder to calculate a single union merge involving all the refs, as well 51 - as the index. 52 -} 53mergeIndex :: HashObjectHandle -> CatFileHandle -> Repo -> [Ref] -> IO () 54mergeIndex hashhandle ch repo bs = forM_ bs $ \b -> 55 streamUpdateIndex repo [mergeTreeIndex b hashhandle ch repo] 56 57{- For merging two trees. -} 58mergeTrees :: Ref -> Ref -> HashObjectHandle -> CatFileHandle -> Repo -> Streamer 59mergeTrees x y hashhandle ch = doMerge hashhandle ch 60 ("diff-tree":diffOpts ++ [fromRef x, fromRef y, "--"]) 61 62{- For merging a single tree into the index. -} 63mergeTreeIndex :: Ref -> HashObjectHandle -> CatFileHandle -> Repo -> Streamer 64mergeTreeIndex r hashhandle ch = doMerge hashhandle ch $ 65 "diff-index" : diffOpts ++ ["--cached", fromRef r, "--"] 66 67diffOpts :: [String] 68diffOpts = ["--raw", "-z", "-r", "--no-renames", "-l0"] 69 70{- Streams update-index changes to perform a merge, 71 - using git to get a raw diff. -} 72doMerge :: HashObjectHandle -> CatFileHandle -> [String] -> Repo -> Streamer 73doMerge hashhandle ch differ repo streamer = do 74 (diff, cleanup) <- pipeNullSplit' (map Param differ) repo 75 go diff 76 void $ cleanup 77 where 78 go [] = noop 79 go (info:file:rest) = mergeFile info file hashhandle ch >>= 80 maybe (go rest) (\l -> streamer l >> go rest) 81 go (_:[]) = error $ "parse error " ++ show differ 82 83{- Given an info line from a git raw diff, and the filename, generates 84 - a line suitable for update-index that union merges the two sides of the 85 - diff. -} 86mergeFile :: S.ByteString -> RawFilePath -> HashObjectHandle -> CatFileHandle -> IO (Maybe L.ByteString) 87mergeFile info file hashhandle h = case S8.words info of 88 [_colonmode, _bmode, asha, bsha, _status] -> 89 case filter (`notElem` nullShas) [Ref asha, Ref bsha] of 90 [] -> return Nothing 91 (sha:[]) -> use sha 92 shas -> use 93 =<< either return (hashBlob hashhandle . L8.unlines) 94 =<< calcMerge . zip shas <$> mapM getcontents shas 95 _ -> return Nothing 96 where 97 use sha = return $ Just $ 98 updateIndexLine sha TreeFile $ asTopFilePath file 99 -- Get file and split into lines to union merge. 100 -- The encoding of the file is assumed to be either ASCII or utf-8; 101 -- in either case it's safe to split on \n 102 getcontents s = L8.lines <$> catObject h s 103 104{- Calculates a union merge between a list of refs, with contents. 105 - 106 - When possible, reuses the content of an existing ref, rather than 107 - generating new content. 108 -} 109calcMerge :: [(Ref, [L8.ByteString])] -> Either Ref [L8.ByteString] 110calcMerge shacontents 111 | null reuseable = Right new 112 | otherwise = Left $ fst $ Prelude.head reuseable 113 where 114 reuseable = filter (\c -> sorteduniq (snd c) == new) shacontents 115 new = sorteduniq $ concat $ map snd shacontents 116 sorteduniq = S.toList . S.fromList 117