git-annex/Command/Unannex.hs

85 lines
2.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
import Control.Monad.State (liftIO)
2011-02-02 01:58:47 +00:00
import Control.Monad (unless)
import System.Directory
import System.Posix.Files
import Command
import qualified Annex
import qualified AnnexQueue
import Utility
import qualified Backend
import LocationLog
import Types
import Content
import qualified Git
import qualified Git.LsFiles as LsFiles
2010-11-08 19:15:21 +00:00
import Messages
import Locations
command :: [Command]
command = [repoCommand "unannex" paramPath seek "undo accidential add command"]
seek :: [CommandSeek]
2010-11-11 22:54:52 +00:00
seek = [withFilesInGit start]
{- The unannex subcommand undoes an add. -}
start :: CommandStartString
start file = isAnnexed file $ \(key, backend) -> do
ishere <- inAnnex key
if ishere
then do
force <- Annex.getState Annex.force
unless force $ do
g <- Annex.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-02 01:58:47 +00:00
showStart "unannex" file
2011-05-15 06:02:46 +00:00
next $ perform file key backend
else stop
perform :: FilePath -> Key -> Backend Annex -> CommandPerform
perform file key backend = do
-- force backend to always remove
ok <- Backend.removeKey backend key (Just 0)
2010-11-22 21:51:55 +00:00
if ok
2011-05-15 06:02:46 +00:00
then next $ cleanup file key
else stop
cleanup :: FilePath -> Key -> CommandCleanup
cleanup file key = do
g <- Annex.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.
AnnexQueue.add "commit" [Params "-a -m", Param "content removed from git annex"] "-a"
return True