2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2013-07-22 21:06:00 +00:00
|
|
|
- Copyright 2010-2013 Joey Hess <joey@kitenet.net>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-11 20:03:00 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
module Command.Unannex where
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-02 23:04:24 +00:00
|
|
|
import Command
|
2013-07-22 21:06:00 +00:00
|
|
|
import Config
|
2010-11-02 23:04:24 +00:00
|
|
|
import qualified Annex
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Location
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2013-07-22 21:06:00 +00:00
|
|
|
import Annex.Content.Direct
|
2011-12-14 19:56:11 +00:00
|
|
|
import qualified Git.Command
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
2013-07-22 21:06:00 +00:00
|
|
|
def = [command "unannex" paramPaths seek SectionUtility
|
2013-03-24 22:28:21 +00:00
|
|
|
"undo accidential add command"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2011-11-11 03:35:08 +00:00
|
|
|
seek = [withFilesInGit $ whenAnnexed start]
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
start :: FilePath -> (Key, Backend) -> CommandStart
|
2011-12-09 16:23:45 +00:00
|
|
|
start file (key, _) = stopUnless (inAnnex key) $ do
|
|
|
|
showStart "unannex" file
|
2013-07-22 21:06:00 +00:00
|
|
|
next $ ifM isDirect
|
|
|
|
( performDirect file key
|
|
|
|
, performIndirect file key)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2013-07-22 21:06:00 +00:00
|
|
|
performIndirect :: FilePath -> Key -> CommandPerform
|
|
|
|
performIndirect file key = do
|
2010-11-02 23:04:24 +00:00
|
|
|
liftIO $ removeFile file
|
2013-07-22 21:06:00 +00:00
|
|
|
|
2011-12-09 17:07:31 +00:00
|
|
|
-- git rm deletes empty directory without --cached
|
2013-07-22 21:06:00 +00:00
|
|
|
inRepo $ Git.Command.run [Params "rm --cached --force --quiet --", File file]
|
2011-12-09 17:07:31 +00:00
|
|
|
|
|
|
|
-- If the file was already committed, it is now staged for removal.
|
|
|
|
-- Commit that removal now, to avoid later confusing the
|
2013-07-22 21:06:00 +00:00
|
|
|
-- pre-commit hook, if this file is later added back to
|
|
|
|
-- git as a normal non-annexed file, to thinking that the
|
|
|
|
-- file has been unlocked and needs to be re-annexed.
|
|
|
|
(s, reap) <- inRepo $ LsFiles.staged [file]
|
|
|
|
when (not $ null s) $
|
2013-03-03 17:39:07 +00:00
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Param "commit"
|
|
|
|
, Param "-q"
|
2013-07-22 21:06:00 +00:00
|
|
|
, Param "--no-verify"
|
2013-03-03 17:39:07 +00:00
|
|
|
, Param "-m", Param "content removed from git annex"
|
|
|
|
, Param "--", File file
|
|
|
|
]
|
2013-07-22 21:06:00 +00:00
|
|
|
void $ liftIO reap
|
2010-11-08 23:26:37 +00:00
|
|
|
|
2013-07-22 21:06:00 +00:00
|
|
|
next $ cleanupIndirect file key
|
|
|
|
|
|
|
|
cleanupIndirect :: FilePath -> Key -> CommandCleanup
|
|
|
|
cleanupIndirect file key = do
|
2012-03-14 21:43:34 +00:00
|
|
|
ifM (Annex.getState Annex.fast)
|
2013-02-06 18:02:18 +00:00
|
|
|
( goFast
|
|
|
|
, go
|
2012-03-14 21:43:34 +00:00
|
|
|
)
|
2010-11-02 23:04:24 +00:00
|
|
|
return True
|
2013-02-06 18:02:18 +00:00
|
|
|
where
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-05-11 20:03:00 +00:00
|
|
|
goFast = go
|
|
|
|
#else
|
2013-02-06 18:02:18 +00:00
|
|
|
goFast = do
|
|
|
|
-- fast mode: hard link to content in annex
|
2013-04-04 19:46:33 +00:00
|
|
|
src <- calcRepo $ gitAnnexLocation key
|
2013-02-06 18:02:18 +00:00
|
|
|
-- creating a hard link could fall; fall back to non fast mode
|
|
|
|
ifM (liftIO $ catchBoolIO $ createLink src file >> return True)
|
|
|
|
( thawContent file
|
|
|
|
, go
|
|
|
|
)
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2013-02-06 18:02:18 +00:00
|
|
|
go = do
|
|
|
|
fromAnnex key file
|
|
|
|
logStatus key InfoMissing
|
2013-07-22 21:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
performDirect :: FilePath -> Key -> CommandPerform
|
|
|
|
performDirect file key = do
|
|
|
|
-- --force is needed when the file is not committed
|
|
|
|
inRepo $ Git.Command.run [Params "rm --cached --force --quiet --", File file]
|
|
|
|
next $ cleanupDirect file key
|
|
|
|
|
|
|
|
{- The direct mode file is not touched during unannex, so the content
|
|
|
|
- is already where it needs to be, so this does not need to do anything
|
|
|
|
- except remove it from the associated file map (which also updates
|
|
|
|
- the location log if this was the last copy), and, if this was the last
|
|
|
|
- associated file, remove the inode cache. -}
|
|
|
|
cleanupDirect :: FilePath -> Key -> CommandCleanup
|
|
|
|
cleanupDirect file key = do
|
|
|
|
fs <- removeAssociatedFile key file
|
|
|
|
when (null fs) $
|
|
|
|
removeInodeCache key
|
|
|
|
return True
|