unused: Support --json and --json-error-messages

Generalized AddJSONActionItemField to allow it to add several fields. Not entirely
happy with that, since the names of the fields have to be carefully chosen to
not conflict with other json fields. And fields added that way can't be parsed
back in FromJSON, except for the "fields" field that is special cased for metadata.
Still, I couldn't see another way to do it.

Also, omit file:null from the json output. Which does affect other commands,
eg git-annex whereis --all --json. Hopefully that won't break something that expects
a null file. If it did, that could be reverted, but it would be ugly to have
file:null in the unused --json

Sponsored-By: the NIH-funded NICEMAN (ReproNim TR&D3) project
This commit is contained in:
Joey Hess 2023-05-08 14:39:12 -04:00
parent dd90c7abda
commit c208442292
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
6 changed files with 40 additions and 16 deletions

View file

@ -34,15 +34,19 @@ import Logs.View (is_branchView)
import Annex.BloomFilter
import qualified Database.Keys
import Annex.InodeSentinal
import Utility.Aeson
import Messages.JSON (AddJSONActionItemField(..))
import qualified Data.Map as M
import qualified Data.Vector as V
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import Data.Char
cmd :: Command
cmd = command "unused" SectionMaintenance "look for unused file content"
paramNothing (seek <$$> optParser)
cmd = withAnnexOptions [jsonOptions] $
command "unused" SectionMaintenance "look for unused file content"
paramNothing (seek <$$> optParser)
data UnusedOptions = UnusedOptions
{ fromRemote :: Maybe RemoteName
@ -105,13 +109,18 @@ checkRemoteUnused remotename refspec = go =<< Remote.nameToUUID remotename
Just ks -> excludeReferenced refspec ks
Nothing -> giveup "This repository is read-only."
check :: FilePath -> ([(Int, Key)] -> String) -> Annex [Key] -> Int -> Annex Int
check file msg a c = do
check :: String -> ([(Int, Key)] -> String) -> Annex [Key] -> Int -> Annex Int
check fileprefix msg a c = do
l <- a
let unusedlist = number c l
unless (null l) $
showLongNote $ UnquotedString $ msg unusedlist
updateUnusedLog (toRawFilePath file) (M.fromList unusedlist)
let v = V.fromList $ map (\(n, k) -> (show n, serializeKey k)) unusedlist
let f = (if null fileprefix then "unused" else fileprefix) ++ "-list"
case toJSON' (AddJSONActionItemField f v) of
Object o -> maybeShowJSON $ AesonObject o
_ -> noop
updateUnusedLog (toRawFilePath fileprefix) (M.fromList unusedlist)
return $ c + length l
number :: Int -> [a] -> [(Int, a)]