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.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Unannex where
|
|
|
|
|
|
|
|
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
|
2016-03-09 17:43:22 +00:00
|
|
|
import Annex.Perms
|
2013-07-22 21:06:00 +00:00
|
|
|
import Annex.Content.Direct
|
2015-12-15 20:18:39 +00:00
|
|
|
import Annex.Version
|
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)
|
2015-12-21 22:00:13 +00:00
|
|
|
import qualified Database.Keys
|
2016-01-05 21:22:19 +00:00
|
|
|
import Git.FilePath
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = withGlobalOptions [annexedMatchingOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "unannex" SectionUtility
|
2017-02-11 09:38:49 +00:00
|
|
|
"undo accidental 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
|
2017-10-16 18:10:03 +00:00
|
|
|
seek ps = wrapUnannex $
|
2018-10-01 18:12:06 +00:00
|
|
|
(withFilesInGit $ commandAction . whenAnnexed start) =<< workTreeItems ps
|
2014-03-21 18:39:50 +00:00
|
|
|
|
|
|
|
wrapUnannex :: Annex a -> Annex a
|
2015-12-15 20:18:39 +00:00
|
|
|
wrapUnannex a = ifM (versionSupportsUnlockedPointers <||> isDirect)
|
2014-03-21 18:39:50 +00:00
|
|
|
( 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
|
2016-11-16 01:29:54 +00:00
|
|
|
, giveup "Cannot proceed with uncommitted changes staged in the index. Recommend you: git commit"
|
2014-03-21 18:39:50 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
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
|
2016-01-05 21:22:19 +00:00
|
|
|
Database.Keys.removeAssociatedFile key =<< inRepo (toTopFilePath file)
|
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 =
|
|
|
|
-- 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-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
|