2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2013 Joey Hess <id@joeyh.name>
|
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-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
|
2014-07-04 15:36:59 +00:00
|
|
|
import qualified Git.Branch
|
2014-03-21 18:39:50 +00:00
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.DiffTree as DiffTree
|
2013-10-28 20:56:01 +00:00
|
|
|
import Utility.CopyFile
|
2014-03-21 18:39:50 +00:00
|
|
|
import Command.PreCommit (lockPreCommitHook)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 17:18:46 +00:00
|
|
|
cmd = withGlobalOptions annexedMatchingOptions $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "unannex" SectionUtility
|
2015-07-08 16:33:27 +00:00
|
|
|
"undo accidential add command"
|
2015-07-08 19:08:02 +00:00
|
|
|
paramPaths (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2014-03-21 18:39:50 +00:00
|
|
|
seek = wrapUnannex . (withFilesInGit $ whenAnnexed start)
|
|
|
|
|
|
|
|
wrapUnannex :: Annex a -> Annex a
|
|
|
|
wrapUnannex a = ifM isDirect
|
|
|
|
( a
|
|
|
|
{- Run with the pre-commit hook disabled, to avoid confusing
|
|
|
|
- behavior if an unannexed file is added back to git as
|
|
|
|
- a normal, non-annexed file and then committed.
|
|
|
|
- Otherwise, the pre-commit hook would think that the file
|
|
|
|
- has been unlocked and needs to be re-annexed.
|
|
|
|
-
|
|
|
|
- At the end, make a commit removing the unannexed files.
|
|
|
|
-}
|
|
|
|
, ifM cleanindex
|
|
|
|
( lockPreCommitHook $ commit `after` a
|
|
|
|
, error "Cannot proceed with uncommitted changes staged in the index. Recommend you: git commit"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
where
|
2014-07-04 15:36:59 +00:00
|
|
|
commit = inRepo $ Git.Branch.commitCommand Git.Branch.ManualCommit
|
|
|
|
[ Param "-q"
|
2014-03-21 18:39:50 +00:00
|
|
|
, Param "--allow-empty"
|
|
|
|
, Param "--no-verify"
|
|
|
|
, Param "-m", Param "content removed from git annex"
|
|
|
|
]
|
2015-02-25 17:59:23 +00:00
|
|
|
cleanindex = ifM (inRepo Git.Ref.headExists)
|
|
|
|
( do
|
|
|
|
(diff, cleanup) <- inRepo $ DiffTree.diffIndex Git.Ref.headRef
|
|
|
|
if null diff
|
|
|
|
then void (liftIO cleanup) >> return True
|
|
|
|
else void (liftIO cleanup) >> return False
|
|
|
|
, return False
|
|
|
|
)
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
start :: FilePath -> Key -> CommandStart
|
|
|
|
start file key = stopUnless (inAnnex key) $ do
|
2011-12-09 16:23:45 +00:00
|
|
|
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
|
2015-06-01 17:52:23 +00:00
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Param "rm"
|
|
|
|
, Param "--cached"
|
|
|
|
, Param "--force"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param "--"
|
|
|
|
, File file
|
|
|
|
]
|
2013-07-22 21:06:00 +00:00
|
|
|
next $ cleanupIndirect file key
|
|
|
|
|
|
|
|
cleanupIndirect :: FilePath -> Key -> CommandCleanup
|
|
|
|
cleanupIndirect file key = do
|
2013-10-28 20:56:01 +00:00
|
|
|
src <- calcRepo $ gitAnnexLocation key
|
2012-03-14 21:43:34 +00:00
|
|
|
ifM (Annex.getState Annex.fast)
|
2014-04-15 18:23:08 +00:00
|
|
|
( do
|
|
|
|
-- Only make a hard link if the annexed file does not
|
|
|
|
-- already have other hard links pointing at it.
|
|
|
|
-- This avoids unannexing (and uninit) ending up
|
|
|
|
-- hard linking files together, which would be
|
|
|
|
-- surprising.
|
|
|
|
s <- liftIO $ getFileStatus src
|
|
|
|
if linkCount s > 1
|
|
|
|
then copyfrom src
|
|
|
|
else hardlinkfrom src
|
2013-10-28 20:56:01 +00:00
|
|
|
, copyfrom src
|
2012-03-14 21:43:34 +00:00
|
|
|
)
|
2013-02-06 18:02:18 +00:00
|
|
|
where
|
2013-10-28 20:56:01 +00:00
|
|
|
copyfrom src =
|
2014-08-27 00:06:43 +00:00
|
|
|
thawContent file `after` liftIO (copyFileExternal CopyAllMetaData src file)
|
2013-10-28 20:56:01 +00:00
|
|
|
hardlinkfrom src =
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
-- creating a hard link could fall; fall back to copying
|
2013-02-06 18:02:18 +00:00
|
|
|
ifM (liftIO $ catchBoolIO $ createLink src file >> return True)
|
2013-10-28 20:56:01 +00:00
|
|
|
( return True
|
|
|
|
, copyfrom src
|
2013-02-06 18:02:18 +00:00
|
|
|
)
|
2013-10-28 20:56:01 +00:00
|
|
|
#else
|
|
|
|
copyfrom src
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
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
|
2015-06-01 17:52:23 +00:00
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Param "rm"
|
|
|
|
, Param "--cached"
|
|
|
|
, Param "--force"
|
|
|
|
, Param "--quiet"
|
|
|
|
, Param "--"
|
|
|
|
, File file
|
|
|
|
]
|
2013-07-22 21:06:00 +00:00
|
|
|
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
|