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

@ -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