reinject: Support --json and --json-error-messages

Also fix support for operating on multiple pairs of files and keys.

Moved notAnnexed to inside starting, so error message will get into the json.

Cannot include the key in the starting as it's not known yet, so instead
add it to the json later.

Sponsored-By: the NIH-funded NICEMAN (ReproNim TR&D3) project
This commit is contained in:
Joey Hess 2023-05-08 15:43:37 -04:00
parent 91b9915b09
commit 3d8f93dc0a
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 47 additions and 35 deletions

View file

@ -39,12 +39,13 @@ git-annex (10.20230408) UNRELEASED; urgency=medium
broken. broken.
* Support --json and --json-error-messages in more commands * Support --json and --json-error-messages in more commands
(addunused, dead, describe, dropunused, expire, fix, init, log, migrate, (addunused, dead, describe, dropunused, expire, fix, init, log, migrate,
reinit, rekey, rmurl, semitrust, setpresentkey, trust, unannex, undo, reinit, reinject, rekey, rmurl, semitrust, setpresentkey, trust, unannex,
untrust, unused) undo, untrust, unused)
* log: When --raw-date is used, display only seconds from the epoch, as * log: When --raw-date is used, display only seconds from the epoch, as
documented, omitting a trailing "s" that was included in the output documented, omitting a trailing "s" that was included in the output
before. before.
* addunused: Displays the names of the files that it adds. * addunused: Displays the names of the files that it adds.
* reinject: Fix support for operating on multiple pairs of files and keys.
-- Joey Hess <id@joeyh.name> Sat, 08 Apr 2023 13:57:18 -0400 -- Joey Hess <id@joeyh.name> Sat, 08 Apr 2023 13:57:18 -0400

View file

@ -1,6 +1,6 @@
{- git-annex command {- git-annex command
- -
- Copyright 2011-2016 Joey Hess <id@joeyh.name> - Copyright 2011-2023 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU AGPL version 3 or higher. - Licensed under the GNU AGPL version 3 or higher.
-} -}
@ -18,9 +18,11 @@ import Utility.Metered
import Annex.WorkTree import Annex.WorkTree
import qualified Git import qualified Git
import qualified Annex import qualified Annex
import Utility.Aeson
import Messages.JSON (AddJSONActionItemField(..))
cmd :: Command cmd :: Command
cmd = withAnnexOptions [backendOption] $ cmd = withAnnexOptions [backendOption, jsonOptions] $
command "reinject" SectionUtility command "reinject" SectionUtility
"inject content of file back into annex" "inject content of file back into annex"
(paramRepeating (paramPair "SRC" "DEST")) (paramRepeating (paramPair "SRC" "DEST"))
@ -43,22 +45,14 @@ optParser desc = ReinjectOptions
seek :: ReinjectOptions -> CommandSeek seek :: ReinjectOptions -> CommandSeek
seek os seek os
| knownOpt os = withStrings (commandAction . startKnown) (params os) | knownOpt os = withStrings (commandAction . startKnown) (params os)
| otherwise = withWords (commandAction . startSrcDest) (params os) | otherwise = withPairs (commandAction . startSrcDest) (params os)
startSrcDest :: [FilePath] -> CommandStart startSrcDest :: (SeekInput, (String, String)) -> CommandStart
startSrcDest ps@(src:dest:[]) startSrcDest (si, (src, dest))
| src == dest = stop | src == dest = stop
| otherwise = notAnnexed src' $ | otherwise = starting "reinject" ai si $ notAnnexed src' $
lookupKey (toRawFilePath dest) >>= \case lookupKey (toRawFilePath dest) >>= \case
Just k -> go k Just key -> ifM (verifyKeyContent key src')
Nothing -> do
qp <- coreQuotePath <$> Annex.getGitConfig
giveup $ decodeBS $ quote qp $ QuotedPath src'
<> " is not an annexed file"
where
src' = toRawFilePath src
go key = starting "reinject" ai si $
ifM (verifyKeyContent key src')
( perform src' key ( perform src' key
, do , do
qp <- coreQuotePath <$> Annex.getGitConfig qp <- coreQuotePath <$> Annex.getGitConfig
@ -66,13 +60,16 @@ startSrcDest ps@(src:dest:[])
<> " does not have expected content of " <> " does not have expected content of "
<> QuotedPath (toRawFilePath dest) <> QuotedPath (toRawFilePath dest)
) )
Nothing -> do
qp <- coreQuotePath <$> Annex.getGitConfig
giveup $ decodeBS $ quote qp $ QuotedPath src'
<> " is not an annexed file"
where
src' = toRawFilePath src
ai = ActionItemOther (Just (QuotedPath src')) ai = ActionItemOther (Just (QuotedPath src'))
si = SeekInput ps
startSrcDest _ = giveup "specify a src file and a dest file"
startKnown :: FilePath -> CommandStart startKnown :: FilePath -> CommandStart
startKnown src = notAnnexed src' $ startKnown src = starting "reinject" ai si $ notAnnexed src' $ do
starting "reinject" ai si $ do
(key, _) <- genKey ks nullMeterUpdate =<< defaultBackend (key, _) <- genKey ks nullMeterUpdate =<< defaultBackend
ifM (isKnownKey key) ifM (isKnownKey key)
( perform src' key ( perform src' key
@ -86,7 +83,7 @@ startKnown src = notAnnexed src' $
ai = ActionItemOther (Just (QuotedPath src')) ai = ActionItemOther (Just (QuotedPath src'))
si = SeekInput [src] si = SeekInput [src]
notAnnexed :: RawFilePath -> CommandStart -> CommandStart notAnnexed :: RawFilePath -> CommandPerform -> CommandPerform
notAnnexed src a = notAnnexed src a =
ifM (fromRepo Git.repoIsLocalBare) ifM (fromRepo Git.repoIsLocalBare)
( a ( a
@ -100,7 +97,11 @@ notAnnexed src a =
) )
perform :: RawFilePath -> Key -> CommandPerform perform :: RawFilePath -> Key -> CommandPerform
perform src key = ifM move perform src key = do
case toJSON' (AddJSONActionItemField "key" (serializeKey key)) of
Object o -> maybeShowJSON $ AesonObject o
_ -> noop
ifM move
( next $ cleanup key ( next $ cleanup key
, giveup "failed" , giveup "failed"
) )

View file

@ -54,6 +54,16 @@ needing to specify the dest file.
Specify the key-value backend to use when checking if a file is known Specify the key-value backend to use when checking if a file is known
with the `--known` option. with the `--known` option.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
git-annex. Each line of output is a JSON object.
* `--json-error-messages`
Messages that would normally be output to standard error are included in
the JSON instead.
* Also the [[git-annex-common-options]](1) can be used. * Also the [[git-annex-common-options]](1) can be used.
# SEE ALSO # SEE ALSO

View file

@ -34,6 +34,7 @@ These commands have been updated to support --json:
* git-annex-unused * git-annex-unused
* git-annex-init * git-annex-init
* git-annex-reinit * git-annex-reinit
* git-annex-reinject
Provisional list of commands that don't support --json and maybe should: Provisional list of commands that don't support --json and maybe should:
@ -43,7 +44,6 @@ Provisional list of commands that don't support --json and maybe should:
* git-annex-enableremote * git-annex-enableremote
* git-annex-initremote * git-annex-initremote
* git-annex-merge * git-annex-merge
* git-annex-reinject
* git-annex-renameremote * git-annex-renameremote
* git-annex-sync * git-annex-sync
* git-annex-upgrade * git-annex-upgrade