2010-11-14 16:35:05 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2018-12-09 18:10:37 +00:00
|
|
|
- Copyright 2010-2018 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
|
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
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-10-16 14:44:09 +00:00
|
|
|
cmd = notBareRepo $ withGlobalOptions [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
|
2018-02-19 18:28:17 +00:00
|
|
|
mkCommand = noCommit . noMessages . withGlobalOptions [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
|
2016-01-20 17:04:07 +00:00
|
|
|
<*> parseBatchOption
|
2015-07-10 16:47:35 +00: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 18:23:28 +00: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 16:05:28 +00:00
|
|
|
NoBatch -> withKeyOptions (keyOptions o) False seeker
|
2020-07-13 21:04:02 +00:00
|
|
|
(commandAction . startKeys o)
|
|
|
|
(withFilesInGitAnnex ww seeker)
|
|
|
|
=<< workTreeItems ww (findThese o)
|
2020-07-22 18:23:28 +00:00
|
|
|
Batch fmt -> batchAnnexedFilesMatching fmt seeker
|
2016-01-20 17:04:07 +00:00
|
|
|
where
|
2020-05-28 19:55:17 +00:00
|
|
|
ww = WarnUnmatchLsFiles
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
start :: FindOptions -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start o _ file key = startingCustomOutput key $ do
|
2020-11-16 13:56:32 +00:00
|
|
|
showFormatted (formatOption o) file
|
|
|
|
(formatVars key (AssociatedFile (Just file)))
|
2020-07-13 21:04:02 +00:00
|
|
|
next $ return True
|
2013-12-15 18:46:29 +00:00
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
startKeys :: FindOptions -> (SeekInput, Key, ActionItem) -> CommandStart
|
|
|
|
startKeys o (si, key, ActionItemBranchFilePath (BranchFilePath _ topf) _) =
|
|
|
|
start o si (getTopFilePath topf) key
|
2018-12-09 18:10:37 +00:00
|
|
|
startKeys _ _ = stop
|
|
|
|
|
2019-12-05 15:40:10 +00:00
|
|
|
showFormatted :: Maybe Utility.Format.Format -> S.ByteString -> [(String, String)] -> Annex ()
|
2013-12-15 18:46:29 +00:00
|
|
|
showFormatted 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
|
2019-11-26 19:27:22 +00:00
|
|
|
Nothing -> liftIO $ S8.putStrLn 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)
|
2019-11-22 20:24:04 +00:00
|
|
|
, ("keyname", decodeBS $ 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"
|