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
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2013-10-28 20:56:01 +00:00
|
|
|
import Utility.CopyFile
|
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
|
|
|
|
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]
|
2013-09-25 07:09:06 +00:00
|
|
|
unless (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
|
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)
|
2013-10-28 20:56:01 +00:00
|
|
|
( hardlinkfrom src
|
|
|
|
, 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
|