From 1a9af823bc2e4684148f3a1a1725a7a0fff16d7c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 5 May 2023 14:01:40 -0400 Subject: [PATCH] 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 --- CHANGELOG | 4 +- Command/AddUnused.hs | 39 ++++++++++--------- Command/DropUnused.hs | 20 ++++++---- Command/Unused.hs | 15 +++---- doc/git-annex-addunused.mdwn | 12 +++++- doc/git-annex-dropunused.mdwn | 14 +++++++ ...annex__and_ideally_any_other_command_.mdwn | 4 +- 7 files changed, 69 insertions(+), 39 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 70d408c4b3..23b29b2a03 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 Sat, 08 Apr 2023 13:57:18 -0400 diff --git a/Command/AddUnused.hs b/Command/AddUnused.hs index 5a7e412c72..9ce2881bba 100644 --- a/Command/AddUnused.hs +++ b/Command/AddUnused.hs @@ -1,6 +1,6 @@ {- git-annex command - - - Copyright 2012 Joey Hess + - Copyright 2012-2023 Joey Hess - - 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" diff --git a/Command/DropUnused.hs b/Command/DropUnused.hs index 9fcd995be7..32fda45888 100644 --- a/Command/DropUnused.hs +++ b/Command/DropUnused.hs @@ -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 diff --git a/Command/Unused.hs b/Command/Unused.hs index 26e0f29bb3..aa4f70aee9 100644 --- a/Command/Unused.hs +++ b/Command/Unused.hs @@ -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 diff --git a/doc/git-annex-addunused.mdwn b/doc/git-annex-addunused.mdwn index 2d948bdda1..ff6155c440 100644 --- a/doc/git-annex-addunused.mdwn +++ b/doc/git-annex-addunused.mdwn @@ -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 diff --git a/doc/git-annex-dropunused.mdwn b/doc/git-annex-dropunused.mdwn index c52df98179..739c59c1c6 100644 --- a/doc/git-annex-dropunused.mdwn +++ b/doc/git-annex-dropunused.mdwn @@ -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 diff --git a/doc/todo/--json_for_unannex__and_ideally_any_other_command_.mdwn b/doc/todo/--json_for_unannex__and_ideally_any_other_command_.mdwn index 4bb810296d..d0eff8e4bd 100644 --- a/doc/todo/--json_for_unannex__and_ideally_any_other_command_.mdwn +++ b/doc/todo/--json_for_unannex__and_ideally_any_other_command_.mdwn @@ -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