2016-02-23 20:36:08 +00:00
|
|
|
{- git trees
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2016-02-24 00:25:31 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2016-02-23 20:36:08 +00:00
|
|
|
module Git.Tree (
|
|
|
|
Tree(..),
|
|
|
|
TreeContent(..),
|
|
|
|
getTree,
|
|
|
|
recordTree,
|
2016-02-24 01:35:16 +00:00
|
|
|
TreeItem(..),
|
|
|
|
adjustTree,
|
2016-02-23 20:36:08 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
import Git.FilePath
|
|
|
|
import Git.Types
|
|
|
|
import Git.Command
|
2016-02-23 22:30:11 +00:00
|
|
|
import Git.Sha
|
2016-02-23 20:36:08 +00:00
|
|
|
import qualified Git.LsTree as LsTree
|
|
|
|
import qualified Utility.CoProcess as CoProcess
|
|
|
|
|
|
|
|
import Numeric
|
|
|
|
import System.Posix.Types
|
2016-02-24 01:35:16 +00:00
|
|
|
import Control.Monad.IO.Class
|
2016-02-23 20:36:08 +00:00
|
|
|
|
|
|
|
newtype Tree = Tree [TreeContent]
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
data TreeContent
|
|
|
|
= TreeBlob TopFilePath FileMode Sha
|
|
|
|
-- A subtree that is already recorded in git, with a known sha.
|
|
|
|
| RecordedSubTree TopFilePath Sha [TreeContent]
|
|
|
|
-- A subtree that has not yet been recorded in git.
|
|
|
|
| NewSubTree TopFilePath [TreeContent]
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
{- Gets the Tree for a Ref. -}
|
2016-02-24 00:25:31 +00:00
|
|
|
getTree :: Ref -> Repo -> IO Tree
|
2016-02-23 20:36:08 +00:00
|
|
|
getTree r repo = do
|
2016-02-24 01:35:16 +00:00
|
|
|
(l, cleanup) <- lsTreeWithObjects r repo
|
2016-02-24 00:25:31 +00:00
|
|
|
let !t = either (\e -> error ("ls-tree parse error:" ++ e)) id
|
|
|
|
(extractTree l)
|
|
|
|
void cleanup
|
|
|
|
return t
|
2016-02-23 20:36:08 +00:00
|
|
|
|
2016-02-24 01:35:16 +00:00
|
|
|
lsTreeWithObjects :: Ref -> Repo -> IO ([LsTree.TreeItem], IO Bool)
|
|
|
|
lsTreeWithObjects = LsTree.lsTree' [Param "-t"]
|
2016-02-23 20:36:08 +00:00
|
|
|
|
2016-02-24 02:21:25 +00:00
|
|
|
newtype MkTreeHandle = MkTreeHandle CoProcess.CoProcessHandle
|
|
|
|
|
|
|
|
withMkTreeHandle :: (MonadIO m, MonadMask m) => Repo -> (MkTreeHandle -> m a) -> m a
|
|
|
|
withMkTreeHandle repo a = bracketIO setup cleanup (a . MkTreeHandle)
|
|
|
|
where
|
|
|
|
setup = CoProcess.rawMode =<< gitCoProcessStart False ps repo
|
|
|
|
ps = [Param "mktree", Param "--batch", Param "-z"]
|
|
|
|
cleanup = CoProcess.stop
|
|
|
|
|
2016-02-23 20:36:08 +00:00
|
|
|
{- Records a Tree in the Repo, returning its Sha.
|
|
|
|
-
|
|
|
|
- Efficiently handles subtrees, by only recording ones that have not
|
|
|
|
- already been recorded before. And even when many subtrees need to be
|
|
|
|
- recorded, it's done with a single call to git mktree, using its batch
|
|
|
|
- interface.
|
|
|
|
-}
|
2016-02-24 01:35:16 +00:00
|
|
|
recordTree :: Tree -> Repo -> IO Sha
|
2016-02-24 02:21:25 +00:00
|
|
|
recordTree t repo = withMkTreeHandle repo $ \h -> recordTree' h t
|
2016-02-23 20:36:08 +00:00
|
|
|
|
2016-02-24 02:21:25 +00:00
|
|
|
recordTree' :: MkTreeHandle -> Tree -> IO Sha
|
2016-02-23 21:21:42 +00:00
|
|
|
recordTree' h (Tree l) = mkTree h =<< mapM (recordSubTree h) l
|
2016-02-23 20:36:08 +00:00
|
|
|
|
2016-02-24 01:35:16 +00:00
|
|
|
{- Note that the returned RecordedSubTree does not have its [TreeContent]
|
|
|
|
- list populated. This is a memory optimisation, since the list is not
|
|
|
|
- used. -}
|
2016-02-24 02:21:25 +00:00
|
|
|
recordSubTree :: MkTreeHandle -> TreeContent -> IO TreeContent
|
2016-02-23 20:36:08 +00:00
|
|
|
recordSubTree h (NewSubTree d l) = do
|
2016-02-24 01:35:16 +00:00
|
|
|
sha <- mkTree h =<< mapM (recordSubTree h) l
|
|
|
|
return (RecordedSubTree d sha [])
|
2016-02-23 20:36:08 +00:00
|
|
|
recordSubTree _ alreadyrecorded = return alreadyrecorded
|
|
|
|
|
2016-02-24 02:21:25 +00:00
|
|
|
mkTree :: MkTreeHandle -> [TreeContent] -> IO Sha
|
|
|
|
mkTree (MkTreeHandle cp) l = CoProcess.query cp send receive
|
2016-02-23 20:36:08 +00:00
|
|
|
where
|
|
|
|
send h = do
|
|
|
|
forM_ l $ \i -> hPutStr h $ case i of
|
|
|
|
TreeBlob f fm s -> mkTreeOutput fm BlobObject s f
|
|
|
|
RecordedSubTree f s _ -> mkTreeOutput 0o040000 TreeObject s f
|
|
|
|
NewSubTree _ _ -> error "recordSubTree internal error; unexpected NewSubTree"
|
|
|
|
hPutStr h "\NUL" -- signal end of tree to --batch
|
2016-02-23 22:30:11 +00:00
|
|
|
receive h = getSha "mktree" (hGetLine h)
|
2016-02-23 20:36:08 +00:00
|
|
|
|
|
|
|
mkTreeOutput :: FileMode -> ObjectType -> Sha -> TopFilePath -> String
|
2016-02-23 21:21:42 +00:00
|
|
|
mkTreeOutput fm ot s f = concat
|
|
|
|
[ showOct fm ""
|
|
|
|
, " "
|
|
|
|
, show ot
|
|
|
|
, " "
|
|
|
|
, fromRef s
|
|
|
|
, "\t"
|
|
|
|
, takeFileName (getTopFilePath f)
|
|
|
|
, "\NUL"
|
|
|
|
]
|
2016-02-24 01:35:16 +00:00
|
|
|
|
|
|
|
data TreeItem = TreeItem TopFilePath FileMode Sha
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
{- Applies an adjustment to items in a tree.
|
|
|
|
-
|
|
|
|
- While less flexible than using getTree and recordTree, this avoids
|
|
|
|
- buffering the whole tree in memory.
|
|
|
|
-}
|
2016-02-24 02:21:25 +00:00
|
|
|
adjustTree :: (MonadIO m, MonadMask m) => (TreeItem -> m (Maybe TreeItem)) -> Ref -> Repo -> m Sha
|
|
|
|
adjustTree adjust r repo = withMkTreeHandle repo $ \h -> do
|
2016-02-24 01:35:16 +00:00
|
|
|
(l, cleanup) <- liftIO $ lsTreeWithObjects r repo
|
2016-02-24 01:56:03 +00:00
|
|
|
(l', _, _) <- go h False [] topTree l
|
|
|
|
sha <- liftIO $ mkTree h l'
|
2016-02-24 01:35:16 +00:00
|
|
|
void $ liftIO cleanup
|
|
|
|
return sha
|
|
|
|
where
|
|
|
|
go _ wasmodified c _ [] = return (c, wasmodified, [])
|
2016-02-24 01:56:03 +00:00
|
|
|
go h wasmodified c intree (i:is)
|
|
|
|
| intree i =
|
2016-02-24 01:35:16 +00:00
|
|
|
case readObjectType (LsTree.typeobj i) of
|
|
|
|
Just BlobObject -> do
|
|
|
|
let ti = TreeItem (LsTree.file i) (LsTree.mode i) (LsTree.sha i)
|
|
|
|
v <- adjust ti
|
|
|
|
case v of
|
2016-02-24 01:56:03 +00:00
|
|
|
Nothing -> go h True c intree is
|
2016-02-24 01:35:16 +00:00
|
|
|
Just ti'@(TreeItem f m s) ->
|
2016-02-24 02:03:47 +00:00
|
|
|
let !modified = wasmodified || ti' /= ti
|
2016-02-24 01:35:16 +00:00
|
|
|
blob = TreeBlob f m s
|
2016-02-24 02:03:47 +00:00
|
|
|
in go h modified (blob:c) intree is
|
2016-02-24 01:35:16 +00:00
|
|
|
Just TreeObject -> do
|
2016-02-24 01:56:03 +00:00
|
|
|
(sl, modified, is') <- go h False [] (subTree i) is
|
2016-02-24 01:35:16 +00:00
|
|
|
subtree <- if modified
|
|
|
|
then liftIO $ recordSubTree h $ NewSubTree (LsTree.file i) sl
|
|
|
|
else return $ RecordedSubTree (LsTree.file i) (LsTree.sha i) []
|
2016-02-24 02:03:47 +00:00
|
|
|
let !modified' = modified || wasmodified
|
|
|
|
go h modified' (subtree : c) intree is'
|
2016-02-24 01:35:16 +00:00
|
|
|
_ -> error ("unexpected object type \"" ++ LsTree.typeobj i ++ "\"")
|
|
|
|
| otherwise = return (c, wasmodified, i:is)
|
|
|
|
|
|
|
|
{- Assumes the list is ordered, with tree objects coming right before their
|
|
|
|
- contents. -}
|
|
|
|
extractTree :: [LsTree.TreeItem] -> Either String Tree
|
2016-02-24 01:56:03 +00:00
|
|
|
extractTree l = case go [] topTree l of
|
2016-02-24 01:35:16 +00:00
|
|
|
Right (t, []) -> Right (Tree t)
|
|
|
|
Right _ -> parseerr "unexpected tree form"
|
|
|
|
Left e -> parseerr e
|
|
|
|
where
|
|
|
|
go t _ [] = Right (t, [])
|
2016-02-24 01:56:03 +00:00
|
|
|
go t intree (i:is)
|
|
|
|
| intree i =
|
2016-02-24 01:35:16 +00:00
|
|
|
case readObjectType (LsTree.typeobj i) of
|
|
|
|
Just BlobObject ->
|
|
|
|
let b = TreeBlob (LsTree.file i) (LsTree.mode i) (LsTree.sha i)
|
2016-02-24 01:56:03 +00:00
|
|
|
in go (b:t) intree is
|
|
|
|
Just TreeObject -> case go [] (subTree i) is of
|
2016-02-24 01:35:16 +00:00
|
|
|
Right (subtree, is') ->
|
|
|
|
let st = RecordedSubTree (LsTree.file i) (LsTree.sha i) subtree
|
2016-02-24 01:56:03 +00:00
|
|
|
in go (st:t) intree is'
|
2016-02-24 01:35:16 +00:00
|
|
|
Left e -> Left e
|
|
|
|
_ -> parseerr ("unexpected object type \"" ++ LsTree.typeobj i ++ "\"")
|
|
|
|
| otherwise = Right (t, i:is)
|
|
|
|
parseerr = Left
|
2016-02-24 01:56:03 +00:00
|
|
|
|
|
|
|
type InTree = LsTree.TreeItem -> Bool
|
|
|
|
|
|
|
|
topTree :: InTree
|
|
|
|
topTree = notElem '/' . getTopFilePath . LsTree.file
|
|
|
|
|
|
|
|
subTree :: LsTree.TreeItem -> InTree
|
2016-02-24 02:03:47 +00:00
|
|
|
subTree t =
|
|
|
|
let prefix = getTopFilePath (LsTree.file t) ++ "/"
|
2016-02-24 01:56:03 +00:00
|
|
|
in (\i -> prefix `isPrefixOf` getTopFilePath (LsTree.file i))
|