2011-10-31 16:33:41 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-10-31 19:18:41 +00:00
|
|
|
module Command.Reinject where
|
2011-10-31 16:33:41 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import Logs.Location
|
|
|
|
import Annex.Content
|
|
|
|
import qualified Command.Fsck
|
2014-04-17 22:03:39 +00:00
|
|
|
import qualified Backend
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2014-10-14 18:20:10 +00:00
|
|
|
cmd :: [Command]
|
|
|
|
cmd = [command "reinject" (paramPair "SRC" "DEST") seek
|
2013-03-24 22:28:21 +00:00
|
|
|
SectionUtility "sets content of annexed file"]
|
2011-10-31 16:33:41 +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 = withWords start
|
2011-10-31 16:33:41 +00:00
|
|
|
|
|
|
|
start :: [FilePath] -> CommandStart
|
2011-10-31 20:46:51 +00:00
|
|
|
start (src:dest:[])
|
|
|
|
| src == dest = stop
|
2012-02-16 04:41:30 +00:00
|
|
|
| otherwise =
|
2011-12-12 17:43:52 +00:00
|
|
|
ifAnnexed src
|
|
|
|
(error $ "cannot used annexed file as src: " ++ src)
|
2011-12-16 20:56:31 +00:00
|
|
|
go
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go = do
|
|
|
|
showStart "reinject" dest
|
|
|
|
next $ whenAnnexed (perform src) dest
|
2011-10-31 16:33:41 +00:00
|
|
|
start _ = error "specify a src file and a dest file"
|
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
perform :: FilePath -> FilePath -> Key -> CommandPerform
|
|
|
|
perform src dest key = do
|
2012-09-16 05:17:48 +00:00
|
|
|
{- Check the content before accepting it. -}
|
2014-04-17 22:03:39 +00:00
|
|
|
v <- Backend.getBackend dest key
|
|
|
|
case v of
|
|
|
|
Nothing -> stop
|
|
|
|
Just backend ->
|
|
|
|
ifM (Command.Fsck.checkKeySizeOr reject key src
|
|
|
|
<&&> Command.Fsck.checkBackendOr reject backend key src)
|
|
|
|
( do
|
|
|
|
unlessM move $ error "mv failed!"
|
|
|
|
next $ cleanup key
|
|
|
|
, error "not reinjecting"
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
-- the file might be on a different filesystem,
|
|
|
|
-- so mv is used rather than simply calling
|
|
|
|
-- moveToObjectDir; disk space is also
|
|
|
|
-- checked this way.
|
|
|
|
move = getViaTmp key $ \tmp ->
|
|
|
|
liftIO $ boolSystem "mv" [File src, File tmp]
|
|
|
|
reject = const $ return "wrong file?"
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2012-09-16 05:17:48 +00:00
|
|
|
cleanup :: Key -> CommandCleanup
|
|
|
|
cleanup key = do
|
2011-10-31 16:33:41 +00:00
|
|
|
logStatus key InfoPresent
|
2012-09-16 05:17:48 +00:00
|
|
|
return True
|