rename GitUnionMerge to Git.UnionMerge

Also, moved commit function into Git proper, it's not union merge specific.
This commit is contained in:
Joey Hess 2011-06-30 13:32:47 -04:00
parent f0497312a7
commit 896726cde4
4 changed files with 40 additions and 42 deletions

View file

@ -31,7 +31,7 @@ import qualified Data.ByteString.Char8 as B
import Types.BranchState
import qualified Git
import qualified GitUnionMerge
import qualified Git.UnionMerge
import qualified Annex
import Utility
import Types
@ -72,7 +72,7 @@ index g = gitAnnexDir g </> "index"
- and merge in changes from other branches.
-}
genIndex :: Git.Repo -> IO ()
genIndex g = GitUnionMerge.ls_tree g fullname >>= GitUnionMerge.update_index g
genIndex g = Git.UnionMerge.ls_tree g fullname >>= Git.UnionMerge.update_index g
{- Runs an action using the branch's index file. -}
withIndex :: Annex a -> Annex a
@ -128,14 +128,13 @@ create = unlessM (refExists fullname) $ do
if e
then liftIO $ Git.run g "branch" [Param name, Param originname]
else withIndex' True $
liftIO $ GitUnionMerge.commit g "branch created" fullname []
liftIO $ Git.commit g "branch created" fullname []
{- Stages the journal, and commits staged changes to the branch. -}
commit :: String -> Annex ()
commit message = whenM stageJournalFiles $ do
g <- Annex.gitRepo
withIndex $ liftIO $
GitUnionMerge.commit g message fullname [fullname]
withIndex $ liftIO $ Git.commit g message fullname [fullname]
{- Ensures that the branch is up-to-date; should be called before
- data is read from it. Runs only once per git-annex run. -}
@ -158,8 +157,7 @@ update = do
let refs = map (last . words) (lines r)
updated <- catMaybes `liftM` mapM updateRef refs
unless (null updated && not staged) $ liftIO $
GitUnionMerge.commit g "update" fullname
(fullname:updated)
Git.commit g "update" fullname (fullname:updated)
Annex.changeState $ \s -> s { Annex.branchstate = state { branchUpdated = True } }
invalidateCache
@ -200,7 +198,7 @@ updateRef ref
-- index will be removed. So, documentation
-- advises users not to directly modify the
-- branch.
liftIO $ GitUnionMerge.merge g [ref]
liftIO $ Git.UnionMerge.merge g [ref]
return $ Just ref
{- Records changed content of a file into the journal. -}
@ -335,12 +333,12 @@ stageJournalFiles = do
(h, s) <- Git.pipeWriteRead g [Param "hash-object",
Param "-w", Param "--stdin-paths"] $ unlines paths
-- update the index, also in just one command
GitUnionMerge.update_index g $
Git.UnionMerge.update_index g $
index_lines (lines s) $ map fileJournal fs
forceSuccess h
mapM_ removeFile paths
index_lines shas fs = map genline $ zip shas fs
genline (sha, file) = GitUnionMerge.update_index_line sha file
genline (sha, file) = Git.UnionMerge.update_index_line sha file
{- Produces a filename to use in the journal for a file on the branch.
-

14
Git.hs
View file

@ -56,6 +56,7 @@ module Git (
hashObject,
getSha,
shaSize,
commit,
prop_idempotent_deencode
) where
@ -425,6 +426,19 @@ getSha subcommand a = do
shaSize :: Int
shaSize = 40
{- Commits the index into the specified branch,
- with the specified parent refs. -}
commit :: Repo -> String -> String -> [String] -> IO ()
commit g message newref parentrefs = do
tree <- getSha "write-tree" $
pipeRead g [Param "write-tree"]
sha <- getSha "commit-tree" $ ignorehandle $
pipeWriteRead g (map Param $ ["commit-tree", tree] ++ ps) message
run g "update-ref" [Param newref, Param sha]
where
ignorehandle a = return . snd =<< a
ps = concatMap (\r -> ["-p", r]) parentrefs
{- Reads null terminated output of a git command (as enabled by the -z
- parameter), and splits it into a list of files/lines/whatever. -}
pipeNullSplit :: Repo -> [CommandParam] -> IO [FilePath]

View file

@ -5,9 +5,8 @@
- Licensed under the GNU GPL version 3 or higher.
-}
module GitUnionMerge (
module Git.UnionMerge (
merge,
commit,
update_index,
update_index_line,
ls_tree
@ -18,7 +17,7 @@ import Data.List
import Data.Maybe
import Data.String.Utils
import qualified Git
import Git
import Utility
{- Performs a union merge between two branches, staging it in the index.
@ -29,7 +28,7 @@ import Utility
-
- Should be run with a temporary index file configured by Git.useIndex.
-}
merge :: Git.Repo -> [String] -> IO ()
merge :: Repo -> [String] -> IO ()
merge g (x:y:[]) = do
a <- ls_tree g x
b <- merge_trees g x y
@ -40,10 +39,10 @@ merge _ _ = error "wrong number of branches to merge"
{- 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_index. -}
update_index :: Git.Repo -> [String] -> IO ()
update_index :: Repo -> [String] -> IO ()
update_index g l = togit ["update-index", "-z", "--index-info"] (join "\0" l)
where
togit ps content = Git.pipeWrite g (map Param ps) content
togit ps content = pipeWrite g (map Param ps) content
>>= forceSuccess
{- Generates a line suitable to be fed into update-index, to add
@ -52,16 +51,16 @@ update_index_line :: String -> FilePath -> String
update_index_line sha file = "100644 blob " ++ sha ++ "\t" ++ file
{- 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 $
ls_tree :: Repo -> String -> IO [String]
ls_tree g x = 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 :: Repo -> String -> String -> IO [String]
merge_trees g x y = calc_merge g $ "diff-tree":diff_opts ++ [x, y]
{- For merging a single tree into the index. -}
merge_tree_index :: Git.Repo -> String -> IO [String]
merge_tree_index :: Repo -> String -> IO [String]
merge_tree_index g x = calc_merge g $ "diff-index":diff_opts ++ ["--cached", x]
diff_opts :: [String]
@ -69,9 +68,9 @@ diff_opts = ["--raw", "-z", "-r", "--no-renames", "-l0"]
{- 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 :: Repo -> [String] -> IO [String]
calc_merge g differ = do
diff <- Git.pipeNullSplit g $ map Param differ
diff <- pipeNullSplit g $ map Param differ
l <- mapM (mergeFile g) (pairs diff)
return $ catMaybes l
where
@ -82,28 +81,15 @@ calc_merge g differ = do
{- 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. -}
mergeFile :: Git.Repo -> (String, FilePath) -> IO (Maybe String)
mergeFile :: Repo -> (String, FilePath) -> IO (Maybe String)
mergeFile g (info, file) = case filter (/= nullsha) [asha, bsha] of
[] -> return Nothing
(sha:[]) -> return $ Just $ update_index_line sha file
shas -> do
content <- Git.pipeRead g $ map Param ("show":shas)
sha <- Git.hashObject g $ unionmerge content
content <- pipeRead g $ map Param ("show":shas)
sha <- hashObject g $ unionmerge content
return $ Just $ update_index_line sha file
where
[_colonamode, _bmode, asha, bsha, _status] = words info
nullsha = take Git.shaSize $ repeat '0'
nullsha = take shaSize $ repeat '0'
unionmerge = unlines . nub . lines
{- Commits the index into the specified branch,
- with the specified parent refs. -}
commit :: Git.Repo -> String -> String -> [String] -> IO ()
commit g message newref parentrefs = do
tree <- Git.getSha "write-tree" $
Git.pipeRead g [Param "write-tree"]
sha <- Git.getSha "commit-tree" $ ignorehandle $
Git.pipeWriteRead g (map Param $ ["commit-tree", tree] ++ ps) message
Git.run g "update-ref" [Param newref, Param sha]
where
ignorehandle a = return . snd =<< a
ps = concatMap (\r -> ["-p", r]) parentrefs

View file

@ -10,7 +10,7 @@ import System.FilePath
import System.Directory
import Control.Monad (when)
import qualified GitUnionMerge
import qualified Git.UnionMerge
import qualified Git
header :: String
@ -44,6 +44,6 @@ main = do
g <- Git.configRead =<< Git.repoFromCwd
_ <- Git.useIndex (tmpIndex g)
setup g
GitUnionMerge.merge g [aref, bref]
GitUnionMerge.commit g "union merge" newref [aref, bref]
Git.UnionMerge.merge g [aref, bref]
Git.commit g "union merge" newref [aref, bref]
cleanup g