git-annex/Command/FindKeys.hs
Joey Hess f8bc208e89
findkeys: New command, very similar to git-annex find but operating on keys
I've long been asked for `git-annex find --all` or something like that,
but pushed back on it because I feel that the command is analagous to
find(1) and so it would be surprising for it to list keys rather than
files. So instead, add a new findkeys subcommand.

Note that the use of withKeyOptions is rather strange because usually
that is used to fall back to --all rather than listing files, but here
it's made to default to --all like behavior and never list files.

A performance thing that could be improved is that withKeyOptions
always reads and caches location logs. But findkeys with no options does
not need them, so it could be made faster. That caching does speed up
options like --in though. This is really just a subset of a more general
performance thing that --all reads location logs sometimes unncessarily.
Anyway, it needs to read the location log in order to checkDead,
and it seems good that findkeys does skip dead keys.

Also, cleaned up comments on git-annex-find man page asking for --all
option.

Sponsored-by: Dartmouth College's DANDI project
2023-01-17 14:51:57 -04:00

47 lines
1.4 KiB
Haskell

{- git-annex command
-
- Copyright 2023 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
module Command.FindKeys where
import Command
import qualified Utility.Format
import qualified Command.Find
cmd :: Command
cmd = withAnnexOptions [keyMatchingOptions] $ Command.Find.mkCommand $
command "findkeys" SectionQuery "lists available keys"
paramNothing (seek <$$> optParser)
data FindKeysOptions = FindKeysOptions
{ formatOption :: Maybe Utility.Format.Format
}
optParser :: CmdParamsDesc -> Parser FindKeysOptions
optParser _ = FindKeysOptions
<$> optional (Command.Find.parseFormatOption' "${key}\0")
seek :: FindKeysOptions -> CommandSeek
seek o = do
seeker <- Command.Find.contentPresentUnlessLimited $ AnnexedFileSeeker
{ checkContentPresent = Nothing
, usesLocationLog = False
-- startAction is not actually used since this
-- is not used to seek files
, startAction = \_ _ key -> start' o key
}
withKeyOptions (Just WantAllKeys) False seeker
(commandAction . start o)
(const noop) (WorkTreeItems [])
start :: FindKeysOptions -> (SeekInput, Key, ActionItem) -> CommandStart
start o (_si, key, _ai) = start' o key
start' :: FindKeysOptions -> Key -> CommandStart
start' o key = startingCustomOutput key $ do
Command.Find.showFormatted (formatOption o) (serializeKey' key)
(Command.Find.formatVars key (AssociatedFile Nothing))
next $ return True