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