2014-11-13 20:41:21 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-11-13 20:41:21 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-11-13 20:41:21 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Undo where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Config
|
|
|
|
import Annex.Direct
|
|
|
|
import Annex.CatFile
|
|
|
|
import Git.DiffTree
|
|
|
|
import Git.FilePath
|
|
|
|
import Git.UpdateIndex
|
|
|
|
import Git.Sha
|
|
|
|
import qualified Git.LsFiles as LsFiles
|
|
|
|
import qualified Git.Command as Git
|
|
|
|
import qualified Git.Branch
|
|
|
|
import qualified Command.Sync
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = notBareRepo $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "undo" SectionCommon
|
|
|
|
"undo last change to a file or directory"
|
|
|
|
paramPaths (withParams seek)
|
2014-11-13 20:41:21 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2014-11-13 20:41:21 +00:00
|
|
|
seek ps = do
|
|
|
|
-- Safety first; avoid any undo that would touch files that are not
|
|
|
|
-- in the index.
|
|
|
|
(fs, cleanup) <- inRepo $ LsFiles.notInRepo False ps
|
|
|
|
unless (null fs) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ "Cannot undo changes to files that are not checked into git: " ++ unwords fs
|
2014-11-13 20:41:21 +00:00
|
|
|
void $ liftIO $ cleanup
|
|
|
|
|
|
|
|
-- Committing staged changes before undo allows later
|
|
|
|
-- undoing the undo. It would be nicer to only commit staged
|
|
|
|
-- changes to the specified files, rather than all staged changes,
|
|
|
|
-- but that is difficult to do; a partial git-commit can't be done
|
|
|
|
-- in direct mode.
|
|
|
|
void $ Command.Sync.commitStaged Git.Branch.ManualCommit
|
|
|
|
"commit before undo"
|
|
|
|
|
2018-10-01 18:12:06 +00:00
|
|
|
withStrings (commandAction . start) ps
|
2014-11-13 20:41:21 +00:00
|
|
|
|
|
|
|
start :: FilePath -> CommandStart
|
|
|
|
start p = do
|
|
|
|
showStart "undo" p
|
|
|
|
next $ perform p
|
|
|
|
|
|
|
|
perform :: FilePath -> CommandPerform
|
|
|
|
perform p = do
|
|
|
|
g <- gitRepo
|
|
|
|
|
|
|
|
-- Get the reversed diff that needs to be applied to undo.
|
|
|
|
(diff, cleanup) <- inRepo $
|
|
|
|
diffLog [Param "-R", Param "--", Param p]
|
|
|
|
top <- inRepo $ toTopFilePath p
|
|
|
|
let diff' = filter (`isDiffOf` top) diff
|
|
|
|
liftIO $ streamUpdateIndex g (map stageDiffTreeItem diff')
|
|
|
|
|
|
|
|
-- Take two passes through the diff, first doing any removals,
|
|
|
|
-- and then any adds. This order is necessary to handle eg, removing
|
|
|
|
-- a directory and replacing it with a file.
|
|
|
|
let (removals, adds) = partition (\di -> dstsha di == nullSha) diff'
|
|
|
|
let mkrel di = liftIO $ relPathCwdToFile $ fromTopFilePath (file di) g
|
|
|
|
|
|
|
|
forM_ removals $ \di -> do
|
|
|
|
f <- mkrel di
|
|
|
|
whenM isDirect $
|
|
|
|
maybe noop (`removeDirect` f)
|
2015-12-07 19:22:01 +00:00
|
|
|
=<< catKey (srcsha di)
|
2014-11-13 20:41:21 +00:00
|
|
|
liftIO $ nukeFile f
|
|
|
|
|
|
|
|
forM_ adds $ \di -> do
|
|
|
|
f <- mkrel di
|
|
|
|
inRepo $ Git.run [Param "checkout", Param "--", File f]
|
|
|
|
whenM isDirect $
|
|
|
|
maybe noop (`toDirect` f)
|
2015-12-07 19:22:01 +00:00
|
|
|
=<< catKey (dstsha di)
|
2014-11-13 20:41:21 +00:00
|
|
|
|
|
|
|
next $ liftIO cleanup
|