2010-11-02 23:04:24 +00:00
|
|
|
{- 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)
|
2010-11-02 23:04:24 +00:00
|
|
|
import System.Directory
|
2011-07-04 20:06:28 +00:00
|
|
|
import System.Posix.Files
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
2011-04-07 17:59:31 +00:00
|
|
|
import qualified AnnexQueue
|
2010-11-02 23:04:24 +00:00
|
|
|
import Utility
|
|
|
|
import qualified Backend
|
|
|
|
import LocationLog
|
|
|
|
import Types
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.LsFiles as LsFiles
|
2010-11-08 19:15:21 +00:00
|
|
|
import Messages
|
2011-07-04 20:06:28 +00:00
|
|
|
import Locations
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "unannex" paramPath seek "undo accidential add command"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2010-11-11 22:54:52 +00:00
|
|
|
seek = [withFilesInGit start]
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
{- The unannex subcommand undoes an add. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
start :: CommandStartString
|
2010-11-02 23:04:24 +00:00
|
|
|
start file = isAnnexed file $ \(key, backend) -> do
|
2011-01-08 19:14:41 +00:00
|
|
|
ishere <- inAnnex key
|
|
|
|
if ishere
|
|
|
|
then do
|
2011-02-09 15:02:21 +00:00
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
unless force $ do
|
|
|
|
g <- Annex.gitRepo
|
2011-06-29 15:55:16 +00:00
|
|
|
staged <- liftIO $ LsFiles.staged g [Git.workTree g]
|
2011-02-09 15:02:21 +00:00
|
|
|
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
|
|
|
|
2011-01-08 19:14:41 +00:00
|
|
|
showStart "unannex" file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ perform file key backend
|
|
|
|
else stop
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-01-26 01:02:34 +00:00
|
|
|
perform :: FilePath -> Key -> Backend Annex -> CommandPerform
|
2010-11-02 23:04:24 +00:00
|
|
|
perform file key backend = do
|
|
|
|
-- force backend to always remove
|
2010-11-28 19:28:20 +00:00
|
|
|
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
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
cleanup :: FilePath -> Key -> CommandCleanup
|
2010-11-02 23:04:24 +00:00
|
|
|
cleanup file key = do
|
|
|
|
g <- Annex.gitRepo
|
2010-11-08 23:26:37 +00:00
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
liftIO $ removeFile file
|
2011-02-28 20:10:16 +00:00
|
|
|
liftIO $ Git.run g "rm" [Params "--quiet --", File file]
|
2010-11-02 23:04:24 +00:00
|
|
|
-- git rm deletes empty directories; put them back
|
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir file)
|
2010-11-08 23:26:37 +00:00
|
|
|
|
2011-07-04 20:06:28 +00:00
|
|
|
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
|
2011-02-02 01:26:19 +00:00
|
|
|
|
|
|
|
-- 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-04-07 17:59:31 +00:00
|
|
|
AnnexQueue.add "commit" [Params "-a -m", Param "content removed from git annex"] "-a"
|
2011-02-02 01:26:19 +00:00
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
return True
|