git-annex/Command/Unannex.hs

77 lines
2 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Unannex where
2011-10-05 16:02:51 -04:00
import Common.Annex
import Command
import qualified Command.Drop
import qualified Annex
2011-10-04 00:40:47 -04:00
import qualified Annex.Queue
2011-09-23 18:13:24 -04:00
import Utility.FileMode
import LocationLog
2011-10-04 00:40:47 -04:00
import Annex.Content
import qualified Git
import qualified Git.LsFiles as LsFiles
command :: [Command]
2011-09-15 14:33:37 -04:00
command = [repoCommand "unannex" paramPaths seek "undo accidential add command"]
seek :: [CommandSeek]
2010-11-11 18:54:52 -04:00
seek = [withFilesInGit start]
{- The unannex subcommand undoes an add. -}
start :: FilePath -> CommandStart
start file = isAnnexed file $ \(key, _) -> do
ishere <- inAnnex key
if ishere
then do
force <- Annex.getState Annex.force
unless force $ do
g <- gitRepo
staged <- liftIO $ LsFiles.staged g [Git.workTree g]
unless (null staged) $
error "This command cannot be run when there are already files staged for commit."
Annex.changeState $ \s -> s { Annex.force = True }
2011-02-01 21:58:47 -04:00
showStart "unannex" file
next $ perform file key
2011-05-15 02:02:46 -04:00
else stop
perform :: FilePath -> Key -> CommandPerform
perform file key = do
ok <- Command.Drop.dropKey key (Just 0) -- always remove
2010-11-22 17:51:55 -04:00
if ok
2011-05-15 02:02:46 -04:00
then next $ cleanup file key
else stop
cleanup :: FilePath -> Key -> CommandCleanup
cleanup file key = do
g <- gitRepo
liftIO $ removeFile file
liftIO $ Git.run g "rm" [Params "--quiet --", File file]
-- git rm deletes empty directories; put them back
liftIO $ createDirectoryIfMissing True (parentDir file)
fast <- Annex.getState Annex.fast
if fast
then liftIO $ do
-- fast mode: hard link to content in annex
createLink (gitAnnexLocation g key) file
allowWrite file
else do
fromAnnex key file
logStatus key InfoMissing
-- Commit staged changes at end to avoid confusing the
-- pre-commit hook if this file is later added back to
-- git as a normal, non-annexed file.
2011-10-04 00:40:47 -04:00
Annex.Queue.add "commit" [Param "-m", Param "content removed from git annex"] []
return True