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-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-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
|
|
|
|
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
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: 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
|
|
|
|
commit = inRepo $ Git.Command.run
|
|
|
|
[ Param "commit"
|
|
|
|
, Param "-q"
|
|
|
|
, Param "--allow-empty"
|
|
|
|
, Param "--no-verify"
|
|
|
|
, Param "-m", Param "content removed from git annex"
|
|
|
|
]
|
|
|
|
cleanindex = do
|
|
|
|
(diff, cleanup) <- inRepo $ DiffTree.diffIndex Git.Ref.headRef
|
|
|
|
if null diff
|
|
|
|
then void (liftIO cleanup) >> return True
|
|
|
|
else void (liftIO cleanup) >> return False
|
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
|
|
|
inRepo $ Git.Command.run [Params "rm --cached --force --quiet --", File file]
|
|
|
|
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 =
|
|
|
|
thawContent file `after` liftIO (copyFileExternal src file)
|
|
|
|
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
|
|
|
|
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
|