2011-10-31 16:33:41 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-05-08 19:43:37 +00:00
|
|
|
- Copyright 2011-2023 Joey Hess <id@joeyh.name>
|
2011-10-31 16:33:41 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-10-31 16:33:41 +00:00
|
|
|
-}
|
|
|
|
|
2023-04-10 16:56:45 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2011-10-31 19:18:41 +00:00
|
|
|
module Command.Reinject where
|
2011-10-31 16:33:41 +00:00
|
|
|
|
|
|
|
import Command
|
|
|
|
import Logs.Location
|
|
|
|
import Annex.Content
|
2016-04-22 17:49:32 +00:00
|
|
|
import Backend
|
|
|
|
import Types.KeySource
|
2019-06-25 15:37:52 +00:00
|
|
|
import Utility.Metered
|
2022-10-26 17:58:20 +00:00
|
|
|
import Annex.WorkTree
|
2020-01-06 18:22:22 +00:00
|
|
|
import qualified Git
|
2023-04-10 16:56:45 +00:00
|
|
|
import qualified Annex
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd :: Command
|
2023-05-08 19:43:37 +00:00
|
|
|
cmd = withAnnexOptions [backendOption, jsonOptions] $
|
2022-06-29 17:28:08 +00:00
|
|
|
command "reinject" SectionUtility
|
|
|
|
"inject content of file back into annex"
|
|
|
|
(paramRepeating (paramPair "SRC" "DEST"))
|
|
|
|
(seek <$$> optParser)
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
data ReinjectOptions = ReinjectOptions
|
|
|
|
{ params :: CmdParams
|
|
|
|
, knownOpt :: Bool
|
2023-06-26 18:05:31 +00:00
|
|
|
, guessKeysOpt :: Bool
|
2016-04-22 17:49:32 +00:00
|
|
|
}
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser ReinjectOptions
|
|
|
|
optParser desc = ReinjectOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> switch
|
|
|
|
( long "known"
|
|
|
|
<> help "inject all known files"
|
|
|
|
<> hidden
|
|
|
|
)
|
2023-06-26 18:05:31 +00:00
|
|
|
<*> switch
|
|
|
|
( long "guesskeys"
|
|
|
|
<> help "inject files that are named like keys"
|
|
|
|
<> hidden
|
|
|
|
)
|
2016-04-22 17:49:32 +00:00
|
|
|
|
|
|
|
seek :: ReinjectOptions -> CommandSeek
|
|
|
|
seek os
|
2023-06-26 18:05:31 +00:00
|
|
|
| guessKeysOpt os && knownOpt os = giveup "Cannot combine --known with --guesskeys"
|
|
|
|
| guessKeysOpt os = withStrings (commandAction . startGuessKeys) (params os)
|
2018-10-01 18:12:06 +00:00
|
|
|
| knownOpt os = withStrings (commandAction . startKnown) (params os)
|
2023-05-08 19:43:37 +00:00
|
|
|
| otherwise = withPairs (commandAction . startSrcDest) (params os)
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-05-08 19:43:37 +00:00
|
|
|
startSrcDest :: (SeekInput, (String, String)) -> CommandStart
|
|
|
|
startSrcDest (si, (src, dest))
|
2011-10-31 20:46:51 +00:00
|
|
|
| src == dest = stop
|
2023-05-08 19:43:37 +00:00
|
|
|
| otherwise = starting "reinject" ai si $ notAnnexed src' $
|
2022-10-26 17:58:20 +00:00
|
|
|
lookupKey (toRawFilePath dest) >>= \case
|
2023-05-08 19:43:37 +00:00
|
|
|
Just key -> ifM (verifyKeyContent key src')
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " does not have expected content of "
|
|
|
|
<> QuotedPath (toRawFilePath dest)
|
|
|
|
)
|
2023-04-10 16:56:45 +00:00
|
|
|
Nothing -> do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " is not an annexed file"
|
2017-02-09 19:40:44 +00:00
|
|
|
where
|
2020-11-02 20:31:28 +00:00
|
|
|
src' = toRawFilePath src
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-06-26 18:05:31 +00:00
|
|
|
startGuessKeys :: FilePath -> CommandStart
|
|
|
|
startGuessKeys src = starting "reinject" ai si $ notAnnexed src' $
|
|
|
|
case fileKey (toRawFilePath (takeFileName src)) of
|
|
|
|
Just key -> ifM (verifyKeyContent key src')
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " does not have expected content"
|
|
|
|
)
|
|
|
|
Nothing -> do
|
|
|
|
warning "Not named like an object file; skipping"
|
|
|
|
next $ return True
|
|
|
|
where
|
|
|
|
src' = toRawFilePath src
|
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
|
|
|
si = SeekInput [src]
|
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
startKnown :: FilePath -> CommandStart
|
2023-05-08 19:43:37 +00:00
|
|
|
startKnown src = starting "reinject" ai si $ notAnnexed src' $ do
|
|
|
|
(key, _) <- genKey ks nullMeterUpdate =<< defaultBackend
|
|
|
|
ifM (isKnownKey key)
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
warning "Not known content; skipping"
|
|
|
|
next $ return True
|
|
|
|
)
|
2020-02-21 13:34:59 +00:00
|
|
|
where
|
|
|
|
src' = toRawFilePath src
|
2020-11-02 20:31:28 +00:00
|
|
|
ks = KeySource src' src' Nothing
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
2020-09-14 20:49:33 +00:00
|
|
|
si = SeekInput [src]
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-05-08 19:43:37 +00:00
|
|
|
notAnnexed :: RawFilePath -> CommandPerform -> CommandPerform
|
2020-01-06 18:22:22 +00:00
|
|
|
notAnnexed src a =
|
|
|
|
ifM (fromRepo Git.repoIsLocalBare)
|
|
|
|
( a
|
2022-10-26 17:58:20 +00:00
|
|
|
, lookupKey src >>= \case
|
2023-04-10 16:56:45 +00:00
|
|
|
Just _ -> do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $
|
|
|
|
"cannot used annexed file as src: "
|
|
|
|
<> QuotedPath src
|
2022-10-26 17:58:20 +00:00
|
|
|
Nothing -> a
|
2020-01-06 18:22:22 +00:00
|
|
|
)
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2020-11-02 20:31:28 +00:00
|
|
|
perform :: RawFilePath -> Key -> CommandPerform
|
2023-05-08 19:43:37 +00:00
|
|
|
perform src key = do
|
2023-05-08 20:03:34 +00:00
|
|
|
maybeAddJSONField "key" (serializeKey key)
|
2023-05-08 19:43:37 +00:00
|
|
|
ifM move
|
|
|
|
( next $ cleanup key
|
|
|
|
, giveup "failed"
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
disk free checking for unsized keys
Improve disk free space checking when transferring unsized keys to
local git remotes. Since the size of the object file is known, can
check that instead.
Getting unsized keys from local git remotes does not check the actual
object size. It would be harder to handle that direction because the size
check is run locally, before anything involving the remote is done. So it
doesn't know the size of the file on the remote.
Also, transferring unsized keys to other remotes, including ssh remotes and
p2p remotes don't do disk size checking for unsized keys. This would need a
change in protocol.
(It does seem like it would be possible to implement the same thing for
directory special remotes though.)
In some sense, it might be better to not ever do disk free checking for
unsized keys, than to do it only sometimes. A user might notice this
direction working and consider it a bug that the other direction does not.
On the other hand, disk reserve checking is not implemented for most
special remotes at all, and yet it is implemented for a few, which is also
inconsistent, but best effort. And so doing this best effort seems to make
some sense. Fundamentally, if the user wants the size to always be checked,
they should not use unsized keys.
Sponsored-by: Brock Spratlen on Patreon
2024-01-16 18:29:10 +00:00
|
|
|
move = checkDiskSpaceToGet key Nothing False $
|
2020-11-16 18:09:55 +00:00
|
|
|
moveAnnex key (AssociatedFile Nothing) src
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2012-09-16 05:17:48 +00:00
|
|
|
cleanup :: Key -> CommandCleanup
|
|
|
|
cleanup key = do
|
2024-08-23 20:35:12 +00:00
|
|
|
logStatus NoLiveUpdate key InfoPresent
|
2012-09-16 05:17:48 +00:00
|
|
|
return True
|