pre-commit: Update direct mode mappings.

Making the pre-commit hook look at git diff-index to find changed direct
mode files and update the mappings works pretty well.

One case where it does not work is when a file is git annex added, and then
git rmed, and then this is committed. That's a no-op commit, so the hook
probably doesn't even run, and it certianly never notices that the file
was deleted, so the mapping will still have the original filename in it.

For this and other reasons, it's important that the mappings still be
treated as possibly inconsistent.

Also, the assistant now allows the pre-commit hook to run when in direct
mode, so the mappings also get updated there.
This commit is contained in:
Joey Hess 2013-02-06 12:40:59 -04:00
parent ceb732bea7
commit 547d7745fb
8 changed files with 75 additions and 33 deletions

View file

@ -9,7 +9,7 @@ module Git.DiffTree (
DiffTreeItem(..),
diffTree,
diffTreeRecursive,
parseDiffTree
diffIndex,
) where
import Numeric
@ -20,6 +20,7 @@ import Git
import Git.Sha
import Git.Command
import qualified Git.Filename
import qualified Git.Ref
data DiffTreeItem = DiffTreeItem
{ srcmode :: FileMode
@ -32,19 +33,29 @@ data DiffTreeItem = DiffTreeItem
{- Diffs two tree Refs. -}
diffTree :: Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
diffTree = diffTree' []
diffTree src dst = getdiff (Param "diff-tree")
[Param (show src), Param (show dst)]
{- Diffs two tree Refs, recursing into sub-trees -}
diffTreeRecursive :: Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
diffTreeRecursive = diffTree' [Param "-r"]
diffTreeRecursive src dst = getdiff (Param "diff-tree")
[Param "-r", Param (show src), Param (show dst)]
diffTree' :: [CommandParam] -> Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
diffTree' params src dst repo = do
{- Diffs between the repository and index. Does nothing if there is not
- yet a commit in the repository. -}
diffIndex :: Repo -> IO ([DiffTreeItem], IO Bool)
diffIndex repo = do
ifM (Git.Ref.headExists repo)
( getdiff (Param "diff-index") [Param "--cached", Param "HEAD"] repo
, return ([], return True)
)
getdiff :: CommandParam -> [CommandParam] -> Repo -> IO ([DiffTreeItem], IO Bool)
getdiff command params repo = do
(diff, cleanup) <- pipeNullSplit ps repo
return (parseDiffTree diff, cleanup)
where
ps = Params "diff-tree -z --raw --no-renames -l0" : params ++
[Param (show src), Param (show dst)]
ps = command : Params "-z --raw --no-renames -l0" : params
{- Parses diff-tree output. -}
parseDiffTree :: [String] -> [DiffTreeItem]