2010-11-14 16:35:05 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2012-01-06 02:48:59 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2010-11-14 16:35:05 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Find where
|
|
|
|
|
2011-12-22 22:31:44 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-14 16:35:05 +00:00
|
|
|
import Command
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-09-19 00:41:51 +00:00
|
|
|
import Limit
|
2011-11-22 18:06:31 +00:00
|
|
|
import qualified Annex
|
2011-12-22 22:31:44 +00:00
|
|
|
import qualified Utility.Format
|
|
|
|
import Utility.DataUnits
|
|
|
|
import Types.Key
|
2012-01-06 14:14:37 +00:00
|
|
|
import qualified Option
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
2013-07-31 00:24:27 +00:00
|
|
|
def = [noCommit $ noMessages $ withOptions [formatOption, print0Option] $
|
2013-03-24 22:28:21 +00:00
|
|
|
command "find" paramPaths seek SectionQuery "lists available files"]
|
2012-01-06 02:48:59 +00:00
|
|
|
|
2012-01-06 14:14:37 +00:00
|
|
|
formatOption :: Option
|
|
|
|
formatOption = Option.field [] "format" paramFormat "control format of output"
|
|
|
|
|
2013-12-15 18:46:29 +00:00
|
|
|
withFormat :: (Maybe Utility.Format.Format -> CommandSeek) -> CommandSeek
|
|
|
|
withFormat = withField formatOption $ return . fmap Utility.Format.gen
|
|
|
|
|
2012-01-06 02:48:59 +00:00
|
|
|
print0Option :: Option
|
2012-01-06 14:14:37 +00:00
|
|
|
print0Option = Option.Option [] ["print0"] (Option.NoArg set)
|
2012-01-06 02:48:59 +00:00
|
|
|
"terminate output with null"
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
set = Annex.setField (Option.name formatOption) "${file}\0"
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2013-12-15 18:46:29 +00:00
|
|
|
seek = [withFormat $ \f -> withFilesInGit $ whenAnnexed $ start f]
|
2010-11-14 16:35:05 +00:00
|
|
|
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
start :: Maybe Utility.Format.Format -> FilePath -> (Key, Backend) -> CommandStart
|
|
|
|
start format file (key, _) = do
|
2011-09-19 00:41:51 +00:00
|
|
|
-- only files inAnnex are shown, unless the user has requested
|
|
|
|
-- others via a limit
|
2012-03-17 04:22:05 +00:00
|
|
|
whenM (limited <||> inAnnex key) $
|
2013-12-15 18:46:29 +00:00
|
|
|
showFormatted format file $ ("file", file) : keyVars key
|
2011-05-15 06:02:46 +00:00
|
|
|
stop
|
2013-12-15 18:46:29 +00:00
|
|
|
|
|
|
|
showFormatted :: Maybe Utility.Format.Format -> String -> [(String, String)] -> Annex ()
|
|
|
|
showFormatted format unformatted vars =
|
|
|
|
unlessM (showFullJSON vars) $
|
|
|
|
case format of
|
|
|
|
Nothing -> liftIO $ putStrLn unformatted
|
|
|
|
Just formatter -> liftIO $ putStr $
|
|
|
|
Utility.Format.format formatter $
|
|
|
|
M.fromList vars
|
|
|
|
|
|
|
|
keyVars :: Key -> [(String, String)]
|
|
|
|
keyVars key =
|
|
|
|
[ ("key", key2file key)
|
|
|
|
, ("backend", keyBackendName key)
|
|
|
|
, ("bytesize", size show)
|
|
|
|
, ("humansize", size $ roughSize storageUnits True)
|
|
|
|
, ("keyname", keyName key)
|
|
|
|
, ("hashdirlower", hashDirLower key)
|
|
|
|
, ("hashdirmixed", hashDirMixed key)
|
|
|
|
, ("mtime", whenavail show $ keyMtime key)
|
|
|
|
]
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-12-15 18:46:29 +00:00
|
|
|
size c = whenavail c $ keySize key
|
|
|
|
whenavail = maybe "unknown"
|