From b7a4ff1c3185404d36d34b372b016be052394a95 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 17 Aug 2011 18:38:26 -0400 Subject: [PATCH] optimise initialized check Avoid running external command if annex.version is set. --- CmdLine.hs | 3 +-- Init.hs | 20 ++++++++++++-------- Remote/Git.hs | 2 +- Version.hs | 19 +++++++------------ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/CmdLine.hs b/CmdLine.hs index 0590f11124..2652e5b8fc 100644 --- a/CmdLine.hs +++ b/CmdLine.hs @@ -22,7 +22,6 @@ import qualified Git import Content import Types import Command -import Version import Options import Messages import Init @@ -59,7 +58,7 @@ parseCmd argv header cmds options = do {- Checks that the command can be run in the current environment. -} 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 :: String -> [Command] -> [Option] -> String diff --git a/Init.hs b/Init.hs index 36d3ed0fac..ae92998bb9 100644 --- a/Init.hs +++ b/Init.hs @@ -6,8 +6,8 @@ -} module Init ( + ensureInitialized, initialize, - initializeSafe, uninitialize ) where @@ -38,16 +38,20 @@ uninitialize = do g <- Annex.gitRepo 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 to avoid git-annex accidentially being run in git repos that did not intend to use it. -} -initializeSafe :: Annex () -initializeSafe = do - annexed <- Branch.hasSomeBranch - if annexed - then initialize - else error "First run: git-annex init" +ensureInitialized :: Annex () +ensureInitialized = do + v <- getVersion + case v of + Just version -> checkVersion version + 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 -} gitPreCommitHookWrite :: Git.Repo -> Annex () diff --git a/Remote/Git.hs b/Remote/Git.hs index c588cc73bb..d8ecd33c4a 100644 --- a/Remote/Git.hs +++ b/Remote/Git.hs @@ -81,7 +81,7 @@ tryGitConfigRead r | Git.repoIsHttp r = store $ safely $ geturlconfig | Git.repoIsUrl r = return r | otherwise = store $ safely $ do - onLocal r initializeSafe + onLocal r ensureInitialized Git.configRead r where -- Reading config can fail due to IO error or diff --git a/Version.hs b/Version.hs index 44fd2e9de1..fcf6bc4d1d 100644 --- a/Version.hs +++ b/Version.hs @@ -7,8 +7,6 @@ module Version where -import Control.Monad (unless) - import Types import qualified Annex import qualified Git @@ -39,14 +37,11 @@ getVersion = do setVersion :: Annex () setVersion = setConfig versionField defaultVersion -checkVersion :: Annex () -> Annex () -checkVersion initaction = getVersion >>= handle +checkVersion :: Version -> Annex () +checkVersion v + | v `elem` supportedVersions = return () + | v `elem` upgradableVersions = err "Upgrade this repository: git-annex upgrade" + | otherwise = err "Upgrade git-annex." where - handle Nothing = initaction - handle (Just v) = unless (v `elem` supportedVersions) $ - error $ "Repository version " ++ v ++ - " is not supported. " ++ - msg v - msg v - | v `elem` upgradableVersions = "Upgrade this repository: git-annex upgrade" - | otherwise = "Upgrade git-annex." + err msg = error $ "Repository version " ++ v ++ + " is not supported. " ++ msg