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.
This commit is contained in:
Joey Hess 2012-01-06 03:06:25 -04:00
parent 2051d80462
commit 0a36f92a31
13 changed files with 107 additions and 75 deletions

View file

@ -17,6 +17,10 @@ module Annex (
eval,
getState,
changeState,
setFlag,
setField,
getFlag,
getField,
gitRepo,
inRepo,
fromRepo,
@ -38,7 +42,6 @@ import Types.BranchState
import Types.TrustLevel
import Types.UUID
import qualified Utility.Matcher
import qualified Utility.Format
import qualified Data.Map as M
-- git-annex's monad
@ -76,17 +79,16 @@ data AnnexState = AnnexState
, force :: Bool
, fast :: Bool
, auto :: Bool
, format :: Maybe Utility.Format.Format
, branchstate :: BranchState
, catfilehandle :: Maybe CatFileHandle
, forcebackend :: Maybe String
, forcenumcopies :: Maybe Int
, toremote :: Maybe String
, fromremote :: Maybe String
, limit :: Matcher (FilePath -> Annex Bool)
, forcetrust :: [(UUID, TrustLevel)]
, trustmap :: Maybe TrustMap
, ciphers :: M.Map EncryptedCipher Cipher
, flags :: M.Map String Bool
, fields :: M.Map String String
}
newState :: Git.Repo -> AnnexState
@ -99,17 +101,16 @@ newState gitrepo = AnnexState
, force = False
, fast = False
, auto = False
, format = Nothing
, branchstate = startBranchState
, catfilehandle = Nothing
, forcebackend = Nothing
, forcenumcopies = Nothing
, toremote = Nothing
, fromremote = Nothing
, limit = Left []
, forcetrust = []
, trustmap = Nothing
, ciphers = M.empty
, flags = M.empty
, fields = M.empty
}
{- Create and returns an Annex state object for the specified git repo. -}
@ -134,6 +135,24 @@ getState = gets
changeState :: (AnnexState -> AnnexState) -> Annex ()
changeState = modify
{- Sets a flag to True -}
setFlag :: String -> Annex ()
setFlag flag = changeState $ \s ->
s { flags = M.insert flag True $ flags s }
{- Sets a field to a value -}
setField :: String -> String -> Annex ()
setField field value = changeState $ \s ->
s { fields = M.insert field value $ fields s }
{- Checks if a flag was set. -}
getFlag :: String -> Annex Bool
getFlag flag = fromMaybe False . M.lookup flag <$> getState flags
{- Gets the value of a field. -}
getField :: String -> Annex (Maybe String)
getField field = M.lookup field <$> getState fields
{- Returns the annex's git repository. -}
gitRepo :: Annex Git.Repo
gitRepo = getState repo