2012-12-10 18:36:57 +00:00
|
|
|
{- git diff-tree interface
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-12-10 18:36:57 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Git.DiffTree (
|
|
|
|
DiffTreeItem(..),
|
2014-11-13 20:41:21 +00:00
|
|
|
isDiffOf,
|
2012-12-10 18:36:57 +00:00
|
|
|
diffTree,
|
2012-12-18 19:04:44 +00:00
|
|
|
diffTreeRecursive,
|
2013-02-06 16:40:59 +00:00
|
|
|
diffIndex,
|
2013-08-26 17:01:48 +00:00
|
|
|
diffWorkTree,
|
2014-11-13 20:41:21 +00:00
|
|
|
diffLog,
|
2012-12-10 18:36:57 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Numeric
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
import Git.Sha
|
|
|
|
import Git.Command
|
2013-10-17 18:51:19 +00:00
|
|
|
import Git.FilePath
|
2014-12-22 19:32:51 +00:00
|
|
|
import Git.DiffTreeItem
|
2012-12-10 18:36:57 +00:00
|
|
|
import qualified Git.Filename
|
2013-02-06 16:40:59 +00:00
|
|
|
import qualified Git.Ref
|
2012-12-10 18:36:57 +00:00
|
|
|
|
2014-11-13 20:41:21 +00:00
|
|
|
{- Checks if the DiffTreeItem modifies a file with a given name
|
|
|
|
- or under a directory by that name. -}
|
|
|
|
isDiffOf :: DiffTreeItem -> TopFilePath -> Bool
|
|
|
|
isDiffOf diff f = case getTopFilePath f of
|
|
|
|
"" -> True -- top of repo contains all
|
|
|
|
d -> d `dirContains` getTopFilePath (file diff)
|
|
|
|
|
2012-12-10 18:36:57 +00:00
|
|
|
{- Diffs two tree Refs. -}
|
|
|
|
diffTree :: Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
|
2013-02-06 16:40:59 +00:00
|
|
|
diffTree src dst = getdiff (Param "diff-tree")
|
2014-02-19 05:09:17 +00:00
|
|
|
[Param (fromRef src), Param (fromRef dst)]
|
2012-12-18 19:04:44 +00:00
|
|
|
|
|
|
|
{- Diffs two tree Refs, recursing into sub-trees -}
|
|
|
|
diffTreeRecursive :: Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
|
2013-02-06 16:40:59 +00:00
|
|
|
diffTreeRecursive src dst = getdiff (Param "diff-tree")
|
2014-02-19 05:09:17 +00:00
|
|
|
[Param "-r", Param (fromRef src), Param (fromRef dst)]
|
2012-12-18 19:04:44 +00:00
|
|
|
|
2013-08-26 00:47:49 +00:00
|
|
|
{- Diffs between a tree and the index. Does nothing if there is not yet a
|
|
|
|
- commit in the repository. -}
|
|
|
|
diffIndex :: Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
|
2013-08-26 17:01:48 +00:00
|
|
|
diffIndex ref = diffIndex' ref [Param "--cached"]
|
|
|
|
|
|
|
|
{- Diffs between a tree and the working tree. Does nothing if there is not
|
2014-06-09 22:01:30 +00:00
|
|
|
- yet a commit in the repository, or if the repository is bare. -}
|
2013-08-26 17:01:48 +00:00
|
|
|
diffWorkTree :: Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
|
|
|
|
diffWorkTree ref repo =
|
|
|
|
ifM (Git.Ref.headExists repo)
|
2014-10-09 19:09:26 +00:00
|
|
|
( diffIndex' ref [] repo
|
2013-08-26 17:01:48 +00:00
|
|
|
, return ([], return True)
|
|
|
|
)
|
|
|
|
|
|
|
|
diffIndex' :: Ref -> [CommandParam] -> Repo -> IO ([DiffTreeItem], IO Bool)
|
|
|
|
diffIndex' ref params repo =
|
2013-02-06 16:40:59 +00:00
|
|
|
ifM (Git.Ref.headExists repo)
|
2013-05-21 22:24:29 +00:00
|
|
|
( getdiff (Param "diff-index")
|
2015-01-07 01:41:21 +00:00
|
|
|
( params ++ [Param $ fromRef ref] ++ [Param "--"] )
|
2013-08-26 17:01:48 +00:00
|
|
|
repo
|
2013-02-06 16:40:59 +00:00
|
|
|
, return ([], return True)
|
|
|
|
)
|
|
|
|
|
2014-11-13 20:41:21 +00:00
|
|
|
{- Runs git log in --raw mode to get the changes that were made in
|
|
|
|
- a particular commit. The output format is adjusted to be the same
|
|
|
|
- as diff-tree --raw._-}
|
|
|
|
diffLog :: [CommandParam] -> Repo -> IO ([DiffTreeItem], IO Bool)
|
|
|
|
diffLog params = getdiff (Param "log")
|
|
|
|
(Param "-n1" : Param "--abbrev=40" : Param "--pretty=format:" : params)
|
|
|
|
|
2013-02-06 16:40:59 +00:00
|
|
|
getdiff :: CommandParam -> [CommandParam] -> Repo -> IO ([DiffTreeItem], IO Bool)
|
|
|
|
getdiff command params repo = do
|
2012-12-18 19:04:44 +00:00
|
|
|
(diff, cleanup) <- pipeNullSplit ps repo
|
2014-11-13 20:41:21 +00:00
|
|
|
return (parseDiffRaw diff, cleanup)
|
2012-12-18 19:04:44 +00:00
|
|
|
where
|
2013-02-06 16:40:59 +00:00
|
|
|
ps = command : Params "-z --raw --no-renames -l0" : params
|
2012-12-10 18:36:57 +00:00
|
|
|
|
2014-11-13 20:41:21 +00:00
|
|
|
{- Parses --raw output used by diff-tree and git-log. -}
|
|
|
|
parseDiffRaw :: [String] -> [DiffTreeItem]
|
|
|
|
parseDiffRaw l = go l []
|
2012-12-10 18:36:57 +00:00
|
|
|
where
|
|
|
|
go [] c = c
|
|
|
|
go (info:f:rest) c = go rest (mk info f : c)
|
|
|
|
go (s:[]) _ = error $ "diff-tree parse error " ++ s
|
|
|
|
|
|
|
|
mk info f = DiffTreeItem
|
|
|
|
{ srcmode = readmode srcm
|
|
|
|
, dstmode = readmode dstm
|
|
|
|
, srcsha = fromMaybe (error "bad srcsha") $ extractSha ssha
|
|
|
|
, dstsha = fromMaybe (error "bad dstsha") $ extractSha dsha
|
|
|
|
, status = s
|
2013-12-31 20:39:11 +00:00
|
|
|
, file = asTopFilePath $ fromInternalGitPath $ Git.Filename.decode f
|
2012-12-10 18:36:57 +00:00
|
|
|
}
|
|
|
|
where
|
|
|
|
readmode = fst . Prelude.head . readOct
|
|
|
|
|
|
|
|
-- info = :<srcmode> SP <dstmode> SP <srcsha> SP <dstsha> SP <status>
|
|
|
|
-- All fields are fixed, so we can pull them out of
|
|
|
|
-- specific positions in the line.
|
|
|
|
(srcm, past_srcm) = splitAt 7 $ drop 1 info
|
|
|
|
(dstm, past_dstm) = splitAt 7 past_srcm
|
|
|
|
(ssha, past_ssha) = splitAt shaSize past_dstm
|
|
|
|
(dsha, past_dsha) = splitAt shaSize $ drop 1 past_ssha
|
|
|
|
s = drop 1 past_dsha
|