undo command
This commit was sponsored by Andrew Cant.
This commit is contained in:
parent
d22d650f59
commit
13260ccc3a
7 changed files with 139 additions and 6 deletions
|
@ -7,10 +7,12 @@
|
|||
|
||||
module Git.DiffTree (
|
||||
DiffTreeItem(..),
|
||||
isDiffOf,
|
||||
diffTree,
|
||||
diffTreeRecursive,
|
||||
diffIndex,
|
||||
diffWorkTree,
|
||||
diffLog,
|
||||
) where
|
||||
|
||||
import Numeric
|
||||
|
@ -33,6 +35,13 @@ data DiffTreeItem = DiffTreeItem
|
|||
, file :: TopFilePath
|
||||
} deriving Show
|
||||
|
||||
{- 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)
|
||||
|
||||
{- Diffs two tree Refs. -}
|
||||
diffTree :: Ref -> Ref -> Repo -> IO ([DiffTreeItem], IO Bool)
|
||||
diffTree src dst = getdiff (Param "diff-tree")
|
||||
|
@ -66,16 +75,23 @@ diffIndex' ref params repo =
|
|||
, return ([], return True)
|
||||
)
|
||||
|
||||
{- 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)
|
||||
|
||||
getdiff :: CommandParam -> [CommandParam] -> Repo -> IO ([DiffTreeItem], IO Bool)
|
||||
getdiff command params repo = do
|
||||
(diff, cleanup) <- pipeNullSplit ps repo
|
||||
return (parseDiffTree diff, cleanup)
|
||||
return (parseDiffRaw diff, cleanup)
|
||||
where
|
||||
ps = command : Params "-z --raw --no-renames -l0" : params
|
||||
|
||||
{- Parses diff-tree output. -}
|
||||
parseDiffTree :: [String] -> [DiffTreeItem]
|
||||
parseDiffTree l = go l []
|
||||
{- Parses --raw output used by diff-tree and git-log. -}
|
||||
parseDiffRaw :: [String] -> [DiffTreeItem]
|
||||
parseDiffRaw l = go l []
|
||||
where
|
||||
go [] c = c
|
||||
go (info:f:rest) c = go rest (mk info f : c)
|
||||
|
|
|
@ -19,7 +19,8 @@ module Git.UpdateIndex (
|
|||
updateIndexLine,
|
||||
stageFile,
|
||||
unstageFile,
|
||||
stageSymlink
|
||||
stageSymlink,
|
||||
stageDiffTreeItem,
|
||||
) where
|
||||
|
||||
import Common
|
||||
|
@ -28,6 +29,7 @@ import Git.Types
|
|||
import Git.Command
|
||||
import Git.FilePath
|
||||
import Git.Sha
|
||||
import qualified Git.DiffTree as Diff
|
||||
|
||||
{- Streamers are passed a callback and should feed it lines in the form
|
||||
- read by update-index, and generated by ls-tree. -}
|
||||
|
@ -95,7 +97,10 @@ stageFile sha filetype file repo = do
|
|||
unstageFile :: FilePath -> Repo -> IO Streamer
|
||||
unstageFile file repo = do
|
||||
p <- toTopFilePath file repo
|
||||
return $ pureStreamer $ "0 " ++ fromRef nullSha ++ "\t" ++ indexPath p
|
||||
return $ unstageFile' p
|
||||
|
||||
unstageFile' :: TopFilePath -> Streamer
|
||||
unstageFile' p = pureStreamer $ "0 " ++ fromRef nullSha ++ "\t" ++ indexPath p
|
||||
|
||||
{- A streamer that adds a symlink to the index. -}
|
||||
stageSymlink :: FilePath -> Sha -> Repo -> IO Streamer
|
||||
|
@ -106,5 +111,11 @@ stageSymlink file sha repo = do
|
|||
<*> toTopFilePath file repo
|
||||
return $ pureStreamer line
|
||||
|
||||
{- A streamer that applies a DiffTreeItem to the index. -}
|
||||
stageDiffTreeItem :: Diff.DiffTreeItem -> Streamer
|
||||
stageDiffTreeItem d = case toBlobType (Diff.dstmode d) of
|
||||
Nothing -> unstageFile' (Diff.file d)
|
||||
Just t -> pureStreamer $ updateIndexLine (Diff.dstsha d) t (Diff.file d)
|
||||
|
||||
indexPath :: TopFilePath -> InternalGitPath
|
||||
indexPath = toInternalGitPath . getTopFilePath
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue