2010-11-14 16:35:05 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-04-11 18:57:09 +00:00
|
|
|
- Copyright 2010-2023 Joey Hess <id@joeyh.name>
|
2010-11-14 16:35:05 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-11-14 16:35:05 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Find where
|
|
|
|
|
2015-01-28 19:55:17 +00:00
|
|
|
import Data.Default
|
2011-12-22 22:31:44 +00:00
|
|
|
import qualified Data.Map as M
|
2019-12-05 15:40:10 +00:00
|
|
|
import qualified Data.ByteString as S
|
2021-10-06 00:20:08 +00:00
|
|
|
import qualified Data.ByteString.Short as S (fromShort)
|
2019-11-26 19:27:22 +00:00
|
|
|
import qualified Data.ByteString.Char8 as S8
|
2011-12-22 22:31:44 +00:00
|
|
|
|
2010-11-14 16:35:05 +00:00
|
|
|
import Command
|
2011-09-19 00:41:51 +00:00
|
|
|
import Limit
|
2017-02-24 19:16:56 +00:00
|
|
|
import Types.Key
|
2018-12-09 18:10:37 +00:00
|
|
|
import Git.FilePath
|
2011-12-22 22:31:44 +00:00
|
|
|
import qualified Utility.Format
|
|
|
|
import Utility.DataUnits
|
2023-04-12 17:48:21 +00:00
|
|
|
import Utility.Terminal
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2022-07-29 16:52:12 +00:00
|
|
|
cmd = withAnnexOptions [annexedMatchingOptions] $ mkCommand $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "find" SectionQuery "lists available files"
|
2015-07-10 16:47:35 +00:00
|
|
|
paramPaths (seek <$$> optParser)
|
2014-04-17 22:41:24 +00:00
|
|
|
|
|
|
|
mkCommand :: Command -> Command
|
2022-06-29 17:28:08 +00:00
|
|
|
mkCommand = noCommit . noMessages . withAnnexOptions [jsonOptions]
|
2015-07-10 16:47:35 +00:00
|
|
|
|
|
|
|
data FindOptions = FindOptions
|
|
|
|
{ findThese :: CmdParams
|
|
|
|
, formatOption :: Maybe Utility.Format.Format
|
2018-12-09 18:10:37 +00:00
|
|
|
, keyOptions :: Maybe KeyOptions
|
2016-01-20 17:04:07 +00:00
|
|
|
, batchOption :: BatchMode
|
2015-07-10 16:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser FindOptions
|
|
|
|
optParser desc = FindOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> optional parseFormatOption
|
2018-12-09 18:10:37 +00:00
|
|
|
<*> optional parseBranchKeysOption
|
2021-08-25 18:20:33 +00:00
|
|
|
<*> parseBatchOption False
|
2015-07-10 16:47:35 +00:00
|
|
|
|
|
|
|
parseFormatOption :: Parser Utility.Format.Format
|
2023-01-17 18:42:29 +00:00
|
|
|
parseFormatOption = parseFormatOption' "${file}\0"
|
|
|
|
|
|
|
|
parseFormatOption' :: String -> Parser Utility.Format.Format
|
|
|
|
parseFormatOption' print0format =
|
2015-07-10 16:47:35 +00:00
|
|
|
option (Utility.Format.gen <$> str)
|
|
|
|
( long "format" <> metavar paramFormat
|
|
|
|
<> help "control format of output"
|
|
|
|
)
|
2023-01-17 18:42:29 +00:00
|
|
|
<|> flag' (Utility.Format.gen print0format)
|
2015-07-10 16:47:35 +00:00
|
|
|
( long "print0"
|
2023-01-17 18:42:29 +00:00
|
|
|
<> help "use nulls to separate output rather than lines"
|
2015-07-10 16:47:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
seek :: FindOptions -> CommandSeek
|
2020-07-22 18:23:28 +00:00
|
|
|
seek o = do
|
2022-07-29 16:52:12 +00:00
|
|
|
unless (isJust (keyOptions o)) $
|
|
|
|
checkNotBareRepo
|
2023-04-11 18:57:09 +00:00
|
|
|
isterminal <- liftIO $ checkIsTerminal stdout
|
2023-01-17 18:42:29 +00:00
|
|
|
seeker <- contentPresentUnlessLimited $ AnnexedFileSeeker
|
2023-04-11 18:57:09 +00:00
|
|
|
{ startAction = start o isterminal
|
2023-01-17 18:42:29 +00:00
|
|
|
, checkContentPresent = Nothing
|
2020-07-22 18:23:28 +00:00
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
|
|
|
case batchOption o of
|
2020-07-24 16:05:28 +00:00
|
|
|
NoBatch -> withKeyOptions (keyOptions o) False seeker
|
2023-04-11 18:57:09 +00:00
|
|
|
(commandAction . startKeys o isterminal)
|
2020-07-13 21:04:02 +00:00
|
|
|
(withFilesInGitAnnex ww seeker)
|
|
|
|
=<< workTreeItems ww (findThese o)
|
2022-01-26 16:59:55 +00:00
|
|
|
Batch fmt -> batchOnly (keyOptions o) (findThese o) $
|
|
|
|
batchAnnexedFiles fmt seeker
|
2016-01-20 17:04:07 +00:00
|
|
|
where
|
2023-04-25 23:26:20 +00:00
|
|
|
ww = WarnUnmatchLsFiles "find"
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2023-01-17 18:42:29 +00:00
|
|
|
-- Default to needing content to be present, but if the user specified a
|
|
|
|
-- limit, content does not need to be present.
|
|
|
|
contentPresentUnlessLimited :: AnnexedFileSeeker -> Annex AnnexedFileSeeker
|
|
|
|
contentPresentUnlessLimited s = do
|
|
|
|
islimited <- limited
|
|
|
|
return $ s
|
|
|
|
{ checkContentPresent = if islimited
|
|
|
|
then Nothing
|
|
|
|
else Just True
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:57:09 +00:00
|
|
|
start :: FindOptions -> IsTerminal -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start o isterminal _ file key = startingCustomOutput key $ do
|
|
|
|
showFormatted isterminal (formatOption o) file
|
2020-11-16 13:56:32 +00:00
|
|
|
(formatVars key (AssociatedFile (Just file)))
|
2020-07-13 21:04:02 +00:00
|
|
|
next $ return True
|
2013-12-15 18:46:29 +00:00
|
|
|
|
2023-04-11 18:57:09 +00:00
|
|
|
startKeys :: FindOptions -> IsTerminal -> (SeekInput, Key, ActionItem) -> CommandStart
|
|
|
|
startKeys o isterminal (si, key, ActionItemBranchFilePath (BranchFilePath _ topf) _) =
|
|
|
|
start o isterminal si (getTopFilePath topf) key
|
|
|
|
startKeys _ _ _ = stop
|
2018-12-09 18:10:37 +00:00
|
|
|
|
2023-04-11 18:57:09 +00:00
|
|
|
showFormatted :: IsTerminal -> Maybe Utility.Format.Format -> S.ByteString -> [(String, String)] -> Annex ()
|
|
|
|
showFormatted (IsTerminal isterminal) format unformatted vars =
|
2016-07-26 23:15:34 +00:00
|
|
|
unlessM (showFullJSON $ JSONChunk vars) $
|
2013-12-15 18:46:29 +00:00
|
|
|
case format of
|
2023-04-11 18:57:09 +00:00
|
|
|
Nothing -> do
|
|
|
|
liftIO $ S8.putStrLn $ if isterminal
|
2023-04-26 04:12:38 +00:00
|
|
|
then Utility.Format.encode_c (const False) unformatted
|
2023-04-11 18:57:09 +00:00
|
|
|
else unformatted
|
2013-12-15 18:46:29 +00:00
|
|
|
Just formatter -> liftIO $ putStr $
|
|
|
|
Utility.Format.format formatter $
|
|
|
|
M.fromList vars
|
|
|
|
|
2020-11-16 13:56:32 +00:00
|
|
|
formatVars :: Key -> AssociatedFile -> [(String, String)]
|
|
|
|
formatVars key (AssociatedFile af) =
|
|
|
|
(maybe id (\f l -> (("file", fromRawFilePath f) : l)) af)
|
2019-01-14 17:03:35 +00:00
|
|
|
[ ("key", serializeKey key)
|
2019-11-22 20:24:04 +00:00
|
|
|
, ("backend", decodeBS $ formatKeyVariety $ fromKey keyVariety key)
|
2013-12-15 18:46:29 +00:00
|
|
|
, ("bytesize", size show)
|
|
|
|
, ("humansize", size $ roughSize storageUnits True)
|
2021-10-06 00:20:08 +00:00
|
|
|
, ("keyname", decodeBS $ S.fromShort $ fromKey keyName key)
|
2019-12-11 18:12:22 +00:00
|
|
|
, ("hashdirlower", fromRawFilePath $ hashDirLower def key)
|
|
|
|
, ("hashdirmixed", fromRawFilePath $ hashDirMixed def key)
|
2019-11-22 20:24:04 +00:00
|
|
|
, ("mtime", whenavail show $ fromKey keyMtime key)
|
2013-12-15 18:46:29 +00:00
|
|
|
]
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2019-11-22 20:24:04 +00:00
|
|
|
size c = whenavail c $ fromKey keySize key
|
2013-12-15 18:46:29 +00:00
|
|
|
whenavail = maybe "unknown"
|