optimise initialized check

Avoid running external command if annex.version is set.
This commit is contained in:
Joey Hess 2011-08-17 18:38:26 -04:00
parent 228a724d1d
commit b7a4ff1c31
4 changed files with 21 additions and 23 deletions

View file

@ -22,7 +22,6 @@ import qualified Git
import Content import Content
import Types import Types
import Command import Command
import Version
import Options import Options
import Messages import Messages
import Init import Init
@ -59,7 +58,7 @@ parseCmd argv header cmds options = do
{- Checks that the command can be run in the current environment. -} {- Checks that the command can be run in the current environment. -}
checkCmdEnviron :: Command -> Annex () checkCmdEnviron :: Command -> Annex ()
checkCmdEnviron command = when (cmdusesrepo command) $ checkVersion $ initializeSafe checkCmdEnviron command = when (cmdusesrepo command) ensureInitialized
{- Usage message with lists of commands and options. -} {- Usage message with lists of commands and options. -}
usage :: String -> [Command] -> [Option] -> String usage :: String -> [Command] -> [Option] -> String

20
Init.hs
View file

@ -6,8 +6,8 @@
-} -}
module Init ( module Init (
ensureInitialized,
initialize, initialize,
initializeSafe,
uninitialize uninitialize
) where ) where
@ -38,16 +38,20 @@ uninitialize = do
g <- Annex.gitRepo g <- Annex.gitRepo
gitPreCommitHookUnWrite g gitPreCommitHookUnWrite g
{- Call to automatically initialize if there is already a git-annex {- Will automatically initialize if there is already a git-annex
branch from somewhere. Otherwise, require a manual init branch from somewhere. Otherwise, require a manual init
to avoid git-annex accidentially being run in git to avoid git-annex accidentially being run in git
repos that did not intend to use it. -} repos that did not intend to use it. -}
initializeSafe :: Annex () ensureInitialized :: Annex ()
initializeSafe = do ensureInitialized = do
annexed <- Branch.hasSomeBranch v <- getVersion
if annexed case v of
then initialize Just version -> checkVersion version
else error "First run: git-annex init" Nothing -> do
annexed <- Branch.hasSomeBranch
if annexed
then initialize
else error "First run: git-annex init"
{- set up a git pre-commit hook, if one is not already present -} {- set up a git pre-commit hook, if one is not already present -}
gitPreCommitHookWrite :: Git.Repo -> Annex () gitPreCommitHookWrite :: Git.Repo -> Annex ()

View file

@ -81,7 +81,7 @@ tryGitConfigRead r
| Git.repoIsHttp r = store $ safely $ geturlconfig | Git.repoIsHttp r = store $ safely $ geturlconfig
| Git.repoIsUrl r = return r | Git.repoIsUrl r = return r
| otherwise = store $ safely $ do | otherwise = store $ safely $ do
onLocal r initializeSafe onLocal r ensureInitialized
Git.configRead r Git.configRead r
where where
-- Reading config can fail due to IO error or -- Reading config can fail due to IO error or

View file

@ -7,8 +7,6 @@
module Version where module Version where
import Control.Monad (unless)
import Types import Types
import qualified Annex import qualified Annex
import qualified Git import qualified Git
@ -39,14 +37,11 @@ getVersion = do
setVersion :: Annex () setVersion :: Annex ()
setVersion = setConfig versionField defaultVersion setVersion = setConfig versionField defaultVersion
checkVersion :: Annex () -> Annex () checkVersion :: Version -> Annex ()
checkVersion initaction = getVersion >>= handle checkVersion v
| v `elem` supportedVersions = return ()
| v `elem` upgradableVersions = err "Upgrade this repository: git-annex upgrade"
| otherwise = err "Upgrade git-annex."
where where
handle Nothing = initaction err msg = error $ "Repository version " ++ v ++
handle (Just v) = unless (v `elem` supportedVersions) $ " is not supported. " ++ msg
error $ "Repository version " ++ v ++
" is not supported. " ++
msg v
msg v
| v `elem` upgradableVersions = "Upgrade this repository: git-annex upgrade"
| otherwise = "Upgrade git-annex."