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 * initremote: Avoid creating a remote that is not encrypted when gpg is
broken. broken.
* Support --json and --json-error-messages in more commands * 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 * 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: Display the names of the files that it adds.
-- 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 2012 Joey Hess <id@joeyh.name> - Copyright 2012-2023 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU AGPL version 3 or higher. - Licensed under the GNU AGPL version 3 or higher.
-} -}
@ -15,28 +15,29 @@ import Annex.Ingest
import Command.Unused (withUnusedMaps, UnusedMaps(..), startUnused) import Command.Unused (withUnusedMaps, UnusedMaps(..), startUnused)
cmd :: Command cmd :: Command
cmd = command "addunused" SectionMaintenance cmd = withAnnexOptions [jsonOptions] $
"add back unused files" command "addunused" SectionMaintenance
(paramRepeating paramNumRange) (withParams seek) "add back unused files"
(paramRepeating paramNumRange) (withParams seek)
seek :: CmdParams -> CommandSeek seek :: CmdParams -> CommandSeek
seek = withUnusedMaps start seek = withUnusedMaps start
start :: UnusedMaps -> Int -> CommandStart start :: UnusedMaps -> Int -> CommandStart
start = startUnused "addunused" perform start = startUnused go (other "bad") (other "tmp")
(performOther "bad")
(performOther "tmp")
perform :: Key -> CommandPerform
perform key = next $ do
logStatus key InfoPresent
addSymlink file key Nothing
return True
where 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 import qualified Utility.RawFilePath as R
cmd :: Command cmd :: Command
cmd = command "dropunused" SectionMaintenance cmd = withAnnexOptions [jsonOptions] $
"drop unused file content" command "dropunused" SectionMaintenance
(paramRepeating paramNumRange) (seek <$$> optParser) "drop unused file content"
(paramRepeating paramNumRange) (seek <$$> optParser)
data DropUnusedOptions = DropUnusedOptions data DropUnusedOptions = DropUnusedOptions
{ rangesToDrop :: CmdParams { rangesToDrop :: CmdParams
@ -42,10 +43,15 @@ seek o = do
withUnusedMaps (start from numcopies mincopies) (rangesToDrop o) withUnusedMaps (start from numcopies mincopies) (rangesToDrop o)
start :: Maybe Remote -> NumCopies -> MinCopies -> UnusedMaps -> Int -> CommandStart start :: Maybe Remote -> NumCopies -> MinCopies -> UnusedMaps -> Int -> CommandStart
start from numcopies mincopies = startUnused "dropunused" start from numcopies mincopies = startUnused
(perform from numcopies mincopies) (go (perform from numcopies mincopies))
(performOther gitAnnexBadLocation) (go (performOther gitAnnexBadLocation))
(performOther gitAnnexTmpObjectLocation) (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 :: Maybe Remote -> NumCopies -> MinCopies -> Key -> CommandPerform
perform from numcopies mincopies key = case from of 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 {- Seek action for unused content. Finds the number in the maps, and
- calls one of 3 actions, depending on the type of unused file. -} - calls one of 3 actions, depending on the type of unused file. -}
startUnused :: String startUnused
-> (Key -> CommandPerform) :: (Int -> Key -> CommandStart)
-> (Key -> CommandPerform) -> (Int -> Key -> CommandStart)
-> (Key -> CommandPerform) -> (Int -> Key -> CommandStart)
-> UnusedMaps -> Int -> CommandStart -> UnusedMaps -> Int -> CommandStart
startUnused message unused badunused tmpunused maps n = search startUnused unused badunused tmpunused maps n = search
[ (unusedMap maps, unused) [ (unusedMap maps, unused)
, (unusedBadMap maps, badunused) , (unusedBadMap maps, badunused)
, (unusedTmpMap maps, tmpunused) , (unusedTmpMap maps, tmpunused)
@ -337,7 +337,4 @@ startUnused message unused badunused tmpunused maps n = search
search ((m, a):rest) = search ((m, a):rest) =
case M.lookup n m of case M.lookup n m of
Nothing -> search rest Nothing -> search rest
Just key -> starting message Just key -> a n key
(ActionItemOther $ Just $ UnquotedString $ show n)
(SeekInput [])
(a key)

View file

@ -15,7 +15,17 @@ The files will have names starting with "unused."
# OPTIONS # 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 # 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 the last repository that is storing their content. Data loss can
result from using this option. 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. * Also the [[git-annex-common-options]](1) can be used.
# SEE ALSO # SEE ALSO
@ -36,6 +46,10 @@ Or, specify "all" to drop all unused data.
[[git-annex-unused]](1) [[git-annex-unused]](1)
[[git-annex-drop]](1)
[[git-annex-copy]](1)
# AUTHOR # AUTHOR
Joey Hess <id@joeyh.name> Joey Hess <id@joeyh.name>

View file

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