addunused, dropunused: Support --json and --json-error-messages

This also changes addunused to display the names of the files that it adds.
That seems like a general usability improvement, and not displaying the input
number does not seem likely to be a problem to a user, since the filename
is based on the key. Displaying the filename was necessary to get it and the key
included in the json.

dropunused does not include the key in the json. It would be possible to
add, but would need more changes. And I doubt that dropunused --json
would be used in a situation where a program cared which keys were
dropped. Note that drop --unused does have the key in its json, so such
a program could just use it. Or could just dropkey --batch with the
specific keys it wants to drop if it cares about specific keys.

Sponsored-By: the NIH-funded NICEMAN (ReproNim TR&D3) project
This commit is contained in:
Joey Hess 2023-05-05 14:01:40 -04:00
parent 972fd05688
commit 1a9af823bc
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
7 changed files with 69 additions and 39 deletions

View file

@ -38,10 +38,12 @@ git-annex (10.20230408) UNRELEASED; urgency=medium
* initremote: Avoid creating a remote that is not encrypted when gpg is
broken.
* Support --json and --json-error-messages in more commands
(fix, log, migrate, rekey, rmurl, setpresentkey, unannex, undo)
(addunused, dropunused, fix, log, migrate, rekey, rmurl,
setpresentkey, unannex, undo)
* log: When --raw-date is used, display only seconds from the epoch, as
documented, omitting a trailing "s" that was included in the output
before.
* addunused: Display the names of the files that it adds.
-- Joey Hess <id@joeyh.name> Sat, 08 Apr 2023 13:57:18 -0400

View file

@ -1,6 +1,6 @@
{- git-annex command
-
- Copyright 2012 Joey Hess <id@joeyh.name>
- Copyright 2012-2023 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
@ -15,28 +15,29 @@ import Annex.Ingest
import Command.Unused (withUnusedMaps, UnusedMaps(..), startUnused)
cmd :: Command
cmd = command "addunused" SectionMaintenance
"add back unused files"
(paramRepeating paramNumRange) (withParams seek)
cmd = withAnnexOptions [jsonOptions] $
command "addunused" SectionMaintenance
"add back unused files"
(paramRepeating paramNumRange) (withParams seek)
seek :: CmdParams -> CommandSeek
seek = withUnusedMaps start
start :: UnusedMaps -> Int -> CommandStart
start = startUnused "addunused" perform
(performOther "bad")
(performOther "tmp")
perform :: Key -> CommandPerform
perform key = next $ do
logStatus key InfoPresent
addSymlink file key Nothing
return True
start = startUnused go (other "bad") (other "tmp")
where
file = "unused." <> keyFile key
go n key = do
let file = "unused." <> keyFile key
starting "addunused"
(ActionItemTreeFile file)
(SeekInput [show n]) $
next $ do
logStatus key InfoPresent
addSymlink file key Nothing
return True
{- The content is not in the annex, but in another directory, and
- it seems better to error out, rather than moving bad/tmp content
- into the annex. -}
other n _ _ = giveup $ "cannot addunused " ++ n ++ "content"
{- The content is not in the annex, but in another directory, and
- it seems better to error out, rather than moving bad/tmp content into
- the annex. -}
performOther :: String -> Key -> CommandPerform
performOther other _ = giveup $ "cannot addunused " ++ other ++ "content"

View file

@ -20,9 +20,10 @@ import Annex.Content
import qualified Utility.RawFilePath as R
cmd :: Command
cmd = command "dropunused" SectionMaintenance
"drop unused file content"
(paramRepeating paramNumRange) (seek <$$> optParser)
cmd = withAnnexOptions [jsonOptions] $
command "dropunused" SectionMaintenance
"drop unused file content"
(paramRepeating paramNumRange) (seek <$$> optParser)
data DropUnusedOptions = DropUnusedOptions
{ rangesToDrop :: CmdParams
@ -42,10 +43,15 @@ seek o = do
withUnusedMaps (start from numcopies mincopies) (rangesToDrop o)
start :: Maybe Remote -> NumCopies -> MinCopies -> UnusedMaps -> Int -> CommandStart
start from numcopies mincopies = startUnused "dropunused"
(perform from numcopies mincopies)
(performOther gitAnnexBadLocation)
(performOther gitAnnexTmpObjectLocation)
start from numcopies mincopies = startUnused
(go (perform from numcopies mincopies))
(go (performOther gitAnnexBadLocation))
(go (performOther gitAnnexTmpObjectLocation))
where
go a n key = starting "dropunused"
(ActionItemOther $ Just $ UnquotedString $ show n)
(SeekInput [show n])
(a key)
perform :: Maybe Remote -> NumCopies -> MinCopies -> Key -> CommandPerform
perform from numcopies mincopies key = case from of

View file

@ -322,12 +322,12 @@ unusedSpec m spec
{- Seek action for unused content. Finds the number in the maps, and
- calls one of 3 actions, depending on the type of unused file. -}
startUnused :: String
-> (Key -> CommandPerform)
-> (Key -> CommandPerform)
-> (Key -> CommandPerform)
startUnused
:: (Int -> Key -> CommandStart)
-> (Int -> Key -> CommandStart)
-> (Int -> Key -> CommandStart)
-> UnusedMaps -> Int -> CommandStart
startUnused message unused badunused tmpunused maps n = search
startUnused unused badunused tmpunused maps n = search
[ (unusedMap maps, unused)
, (unusedBadMap maps, badunused)
, (unusedTmpMap maps, tmpunused)
@ -337,7 +337,4 @@ startUnused message unused badunused tmpunused maps n = search
search ((m, a):rest) =
case M.lookup n m of
Nothing -> search rest
Just key -> starting message
(ActionItemOther $ Just $ UnquotedString $ show n)
(SeekInput [])
(a key)
Just key -> a n key

View file

@ -15,7 +15,17 @@ The files will have names starting with "unused."
# OPTIONS
* The [[git-annex-common-options]](1) can be used.
* `--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.
* The [[git-annex-common-options]](1) can also be used.
# SEE ALSO

View file

@ -28,6 +28,16 @@ Or, specify "all" to drop all unused data.
the last repository that is storing their content. Data loss can
result from using this 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.
# SEE ALSO
@ -36,6 +46,10 @@ Or, specify "all" to drop all unused data.
[[git-annex-unused]](1)
[[git-annex-drop]](1)
[[git-annex-copy]](1)
# AUTHOR
Joey Hess <id@joeyh.name>

View file

@ -23,16 +23,16 @@ These commands have been updated to support --json:
* git-annex-rekey
* git-annex-undo
* git-annex-migrate
* git-annex-addunused
* git-annex-dropunused
Provisional list of commands that don't support --json and maybe should:
(Feel free to reorder things to the top)
* git-annex-addunused
* git-annex-adjust
* git-annex-configremote
* git-annex-dead
* git-annex-dropunused
* git-annex-enableremote
* git-annex-expire
* git-annex-importfeed