2010-11-14 16:35:05 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <id@joeyh.name>
|
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
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2014-10-14 18:20:10 +00:00
|
|
|
cmd :: [Command]
|
|
|
|
cmd = [mkCommand $ command "find" paramPaths seek SectionQuery "lists available files"]
|
2014-04-17 22:41:24 +00:00
|
|
|
|
|
|
|
mkCommand :: Command -> Command
|
|
|
|
mkCommand = noCommit . noMessages . withOptions [formatOption, print0Option, jsonOption]
|
2012-01-06 02:48:59 +00:00
|
|
|
|
2012-01-06 14:14:37 +00:00
|
|
|
formatOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
formatOption = fieldOption [] "format" paramFormat "control format of output"
|
2012-01-06 14:14:37 +00:00
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
getFormat :: Annex (Maybe Utility.Format.Format)
|
|
|
|
getFormat = getOptionField formatOption $ return . fmap Utility.Format.gen
|
2013-12-15 18:46:29 +00:00
|
|
|
|
2012-01-06 02:48:59 +00:00
|
|
|
print0Option :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
print0Option = Option [] ["print0"] (NoArg set)
|
2012-01-06 02:48:59 +00:00
|
|
|
"terminate output with null"
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2014-01-26 20:25:55 +00:00
|
|
|
set = Annex.setField (optionName 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
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek ps = do
|
|
|
|
format <- getFormat
|
|
|
|
withFilesInGit (whenAnnexed $ start format) ps
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
start :: Maybe Utility.Format.Format -> FilePath -> Key -> 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"
|