git-annex/Git/UnionMerge.hs

114 lines
3.8 KiB
Haskell
Raw Normal View History

2011-06-21 18:09:06 +00:00
{- git-union-merge library
-
- Copyright 2011 Joey Hess <id@joeyh.name>
2011-06-21 18:09:06 +00:00
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Git.UnionMerge (
merge,
mergeIndex
2011-06-21 18:09:06 +00:00
) where
import qualified Data.ByteString.Lazy as L
import qualified Data.Set as S
2011-06-21 18:09:06 +00:00
import Common
import Git
2011-12-14 19:30:14 +00:00
import Git.Sha
import Git.CatFile
2011-12-14 19:56:11 +00:00
import Git.Command
import Git.UpdateIndex
import Git.HashObject
import Git.Types
import Git.FilePath
import Utility.FileSystemEncoding
{- Performs a union merge between two branches, staging it in the index.
- Any previously staged changes in the index will be lost.
-
2011-12-14 19:30:14 +00:00
- Should be run with a temporary index file configured by useIndex.
-}
merge :: Ref -> Ref -> Repo -> IO ()
merge x y repo = do
hashhandle <- hashObjectStart repo
ch <- catFileStart repo
streamUpdateIndex repo
[ lsTree x repo
, mergeTrees x y hashhandle ch repo
]
catFileStop ch
hashObjectStop hashhandle
{- Merges a list of branches into the index. Previously staged changes in
- the index are preserved (and participate in the merge).
-
- update-index is run once per ref in turn, so that each ref is merged on
- top of the merge for the previous ref. It would be more efficient, but
- harder to calculate a single union merge involving all the refs, as well
- as the index.
-}
mergeIndex :: HashObjectHandle -> CatFileHandle -> Repo -> [Ref] -> IO ()
mergeIndex hashhandle ch repo bs = forM_ bs $ \b ->
streamUpdateIndex repo [mergeTreeIndex b hashhandle ch repo]
2011-06-21 18:09:06 +00:00
{- For merging two trees. -}
mergeTrees :: Ref -> Ref -> HashObjectHandle -> CatFileHandle -> Repo -> Streamer
mergeTrees (Ref x) (Ref y) hashhandle ch = doMerge hashhandle ch
("diff-tree":diffOpts ++ [x, y, "--"])
{- For merging a single tree into the index. -}
mergeTreeIndex :: Ref -> HashObjectHandle -> CatFileHandle -> Repo -> Streamer
mergeTreeIndex (Ref r) hashhandle ch = doMerge hashhandle ch $
"diff-index" : diffOpts ++ ["--cached", r, "--"]
diffOpts :: [String]
diffOpts = ["--raw", "-z", "-r", "--no-renames", "-l0"]
{- Streams update-index changes to perform a merge,
- using git to get a raw diff. -}
doMerge :: HashObjectHandle -> CatFileHandle -> [String] -> Repo -> Streamer
doMerge hashhandle ch differ repo streamer = do
(diff, cleanup) <- pipeNullSplit (map Param differ) repo
go diff
void $ cleanup
2012-12-13 04:24:19 +00:00
where
go [] = noop
go (info:file:rest) = mergeFile info file hashhandle ch >>=
2012-12-13 04:24:19 +00:00
maybe (go rest) (\l -> streamer l >> go rest)
go (_:[]) = error $ "parse error " ++ show differ
{- Given an info line from a git raw diff, and the filename, generates
2012-02-09 21:35:36 +00:00
- a line suitable for update-index that union merges the two sides of the
- diff. -}
mergeFile :: String -> FilePath -> HashObjectHandle -> CatFileHandle -> IO (Maybe String)
mergeFile info file hashhandle h = case filter (/= nullSha) [Ref asha, Ref bsha] of
[] -> return Nothing
(sha:[]) -> use sha
shas -> use
=<< either return (\s -> hashBlob hashhandle (unlines s))
=<< calcMerge . zip shas <$> mapM getcontents shas
2012-12-13 04:24:19 +00:00
where
[_colonmode, _bmode, asha, bsha, _status] = words info
use sha = return $ Just $
updateIndexLine sha TreeFile $ asTopFilePath file
2012-12-13 04:24:19 +00:00
-- We don't know how the file is encoded, but need to
-- split it into lines to union merge. Using the
-- FileSystemEncoding for this is a hack, but ensures there
-- are no decoding errors.
getcontents s = lines . encodeW8NUL . L.unpack <$> catObject h s
2011-12-12 04:48:25 +00:00
{- Calculates a union merge between a list of refs, with contents.
-
- When possible, reuses the content of an existing ref, rather than
- generating new content.
-}
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
calcMerge :: [(Ref, [String])] -> Either Ref [String]
calcMerge shacontents
| null reuseable = Right $ new
| otherwise = Left $ fst $ Prelude.head reuseable
2012-12-13 04:24:19 +00:00
where
reuseable = filter (\c -> sorteduniq (snd c) == new) shacontents
new = sorteduniq $ concat $ map snd shacontents
sorteduniq = S.toList . S.fromList