2011-10-30 03:48:46 +00:00
|
|
|
{- git-annex command checks
|
|
|
|
-
|
|
|
|
- Common sanity checks for commands, and an interface to selectively
|
|
|
|
- remove them, or add others.
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Checks where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Command
|
|
|
|
import Init
|
2012-12-29 18:28:19 +00:00
|
|
|
import Config
|
2012-12-29 18:45:19 +00:00
|
|
|
import qualified Git
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
commonChecks :: [CommandCheck]
|
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
|
|
|
commonChecks = [repoExists]
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
repoExists :: CommandCheck
|
|
|
|
repoExists = CommandCheck 0 ensureInitialized
|
|
|
|
|
2012-12-29 18:28:19 +00:00
|
|
|
notDirect :: Command -> Command
|
|
|
|
notDirect = addCheck $ whenM isDirect $
|
|
|
|
error "You cannot run this subcommand in a direct mode repository."
|
|
|
|
|
2012-12-29 18:45:19 +00:00
|
|
|
notBareRepo :: Command -> Command
|
|
|
|
notBareRepo = addCheck $ whenM (fromRepo Git.repoIsLocalBare) $
|
|
|
|
error "You cannot run this subcommand in a bare repository."
|
|
|
|
|
2011-10-30 03:48:46 +00:00
|
|
|
dontCheck :: CommandCheck -> Command -> Command
|
|
|
|
dontCheck check cmd = mutateCheck cmd $ \c -> filter (/= check) c
|
|
|
|
|
|
|
|
addCheck :: Annex () -> Command -> Command
|
2012-01-07 01:53:47 +00:00
|
|
|
addCheck check cmd = mutateCheck cmd $ \c ->
|
|
|
|
CommandCheck (length c + 100) check : c
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
mutateCheck :: Command -> ([CommandCheck] -> [CommandCheck]) -> Command
|
|
|
|
mutateCheck cmd@(Command { cmdcheck = c }) a = cmd { cmdcheck = a c }
|
2012-12-29 18:28:19 +00:00
|
|
|
|