git-annex/Annex.hs
2011-06-20 21:37:18 -04:00

97 lines
2.2 KiB
Haskell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{- git-annex monad
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Annex (
Annex,
AnnexState(..),
new,
run,
eval,
getState,
changeState,
gitRepo
) where
import Control.Monad.State
import qualified GitRepo as Git
import GitQueue
import Types.Backend
import Types.Remote
import Types.Crypto
import TrustLevel
import Types.UUID
-- git-annex's monad
type Annex = StateT AnnexState IO
-- internal state storage
data AnnexState = AnnexState
{ repo :: Git.Repo
, backends :: [Backend Annex]
, supportedBackends :: [Backend Annex]
, remotes :: [Remote Annex]
, repoqueue :: Queue
, quiet :: Bool
, force :: Bool
, fast :: Bool
, forcebackend :: Maybe String
, forcenumcopies :: Maybe Int
, defaultkey :: Maybe String
, toremote :: Maybe String
, fromremote :: Maybe String
, exclude :: [String]
, forcetrust :: [(UUID, TrustLevel)]
, cipher :: Maybe Cipher
}
newState :: [Backend Annex] -> Git.Repo -> AnnexState
newState allbackends gitrepo = AnnexState
{ repo = gitrepo
, backends = []
, remotes = []
, supportedBackends = allbackends
, repoqueue = empty
, quiet = False
, force = False
, fast = False
, forcebackend = Nothing
, forcenumcopies = Nothing
, defaultkey = Nothing
, toremote = Nothing
, fromremote = Nothing
, exclude = []
, forcetrust = []
, cipher = Nothing
}
{- Create and returns an Annex state object for the specified git repo. -}
new :: Git.Repo -> [Backend Annex] -> IO AnnexState
new gitrepo allbackends =
newState allbackends `liftM` (liftIO . Git.configRead) gitrepo
{- performs an action in the Annex monad -}
run :: AnnexState -> Annex a -> IO (a, AnnexState)
run = flip runStateT
eval :: AnnexState -> Annex a -> IO a
eval = flip evalStateT
{- Gets a value from the internal state, selected by the passed value
- constructor. -}
getState :: (AnnexState -> a) -> Annex a
getState = gets
{- Applies a state mutation function to change the internal state.
-
- Example: changeState $ \s -> s { quiet = True }
-}
changeState :: (AnnexState -> AnnexState) -> Annex ()
changeState = modify
{- Returns the git repository being acted on -}
gitRepo :: Annex Git.Repo
gitRepo = getState repo