git-annex/Annex.hs
Joey Hess 78a325b093 add a small cache of the most recently accessed item from the git-annex branch
This will speed up typical cases like git-annex get, which currently
has to read the location log once, then read it a second time in order to
add a line to it. Since these reads now involve more than just reading
in a file, it seemed good to add a cache layer.

Only the most recent thing needs to be cached, because git-annex has
good locality; it operates on one file at a time, and only cares
about one item from the branch per file.
2011-06-22 14:18:49 -04:00

102 lines
2.3 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 Types.Branch
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
, branchupdated :: Bool
, branchcache :: BranchCache
, 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
, branchupdated = False
, branchcache = emptyBranchCache
, 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