From e872c3f64825bd8b8e8cc14105e3fc4431b09a89 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sat, 29 Dec 2012 14:45:19 -0400 Subject: [PATCH] convert notBareRepo to a CommandCheck This avoids some small overhead by only running the check once per command; it also ensures that, even if the command doesn't find anything to run on, it still fails to run when in a bare repo. --- Checks.hs | 5 +++++ Command.hs | 7 ------- Command/Add.hs | 5 +++-- Command/AddUrl.hs | 4 ++-- Command/Direct.hs | 7 +++---- Command/FromKey.hs | 7 ++++--- Command/Import.hs | 4 ++-- Command/Indirect.hs | 7 +++---- Command/Watch.hs | 4 ++-- Command/WebApp.hs | 4 ++-- 10 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Checks.hs b/Checks.hs index 3757abb27d..92e9f7e38e 100644 --- a/Checks.hs +++ b/Checks.hs @@ -14,6 +14,7 @@ import Common.Annex import Types.Command import Init import Config +import qualified Git commonChecks :: [CommandCheck] commonChecks = [repoExists] @@ -25,6 +26,10 @@ notDirect :: Command -> Command notDirect = addCheck $ whenM isDirect $ error "You cannot run this subcommand in a direct mode repository." +notBareRepo :: Command -> Command +notBareRepo = addCheck $ whenM (fromRepo Git.repoIsLocalBare) $ + error "You cannot run this subcommand in a bare repository." + dontCheck :: CommandCheck -> Command -> Command dontCheck check cmd = mutateCheck cmd $ \c -> filter (/= check) c diff --git a/Command.hs b/Command.hs index 478dfdc39e..8225f7b1bb 100644 --- a/Command.hs +++ b/Command.hs @@ -17,7 +17,6 @@ module Command ( doCommand, whenAnnexed, ifAnnexed, - notBareRepo, isBareRepo, numCopies, numCopiesCheck, @@ -97,12 +96,6 @@ whenAnnexed a file = ifAnnexed file (a file) (return Nothing) ifAnnexed :: FilePath -> ((Key, Backend) -> Annex a) -> Annex a -> Annex a ifAnnexed file yes no = maybe no yes =<< Backend.lookupFile file -notBareRepo :: Annex a -> Annex a -notBareRepo a = do - whenM isBareRepo $ - error "You cannot run this subcommand in a bare repository." - a - isBareRepo :: Annex Bool isBareRepo = fromRepo Git.repoIsLocalBare diff --git a/Command/Add.hs b/Command/Add.hs index 9f203346f1..b3181cfd7f 100644 --- a/Command/Add.hs +++ b/Command/Add.hs @@ -23,7 +23,8 @@ import Utility.FileMode import Config def :: [Command] -def = [notDirect $ command "add" paramPaths seek "add files to annex"] +def = [notDirect $ notBareRepo $ + command "add" paramPaths seek "add files to annex"] {- Add acts on both files not checked into git yet, and unlocked files. -} seek :: [CommandSeek] @@ -33,7 +34,7 @@ seek = [withFilesNotInGit start, withFilesUnlocked start] - backend, and then moving it into the annex directory and setting up - the symlink pointing to its content. -} start :: FilePath -> CommandStart -start file = notBareRepo $ ifAnnexed file fixup add +start file = ifAnnexed file fixup add where add = do s <- liftIO $ getSymbolicLinkStatus file diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs index dca515b70a..9c6e8aa86c 100644 --- a/Command/AddUrl.hs +++ b/Command/AddUrl.hs @@ -24,7 +24,7 @@ import Types.KeySource import Config def :: [Command] -def = [notDirect $ withOptions [fileOption, pathdepthOption] $ +def = [notDirect $ notBareRepo $ withOptions [fileOption, pathdepthOption] $ command "addurl" (paramRepeating paramUrl) seek "add urls to annex"] fileOption :: Option @@ -39,7 +39,7 @@ seek = [withField fileOption return $ \f -> withStrings $ start f d] start :: Maybe FilePath -> Maybe Int -> String -> CommandStart -start optfile pathdepth s = notBareRepo $ go $ fromMaybe bad $ parseURI s +start optfile pathdepth s = go $ fromMaybe bad $ parseURI s where bad = fromMaybe (error $ "bad url " ++ s) $ parseURI $ escapeURIString isUnescapedInURI s diff --git a/Command/Direct.hs b/Command/Direct.hs index 8e7f401452..0c007bb105 100644 --- a/Command/Direct.hs +++ b/Command/Direct.hs @@ -16,15 +16,14 @@ import Config import Annex.Direct def :: [Command] -def = [command "direct" paramNothing seek "switch repository to direct mode"] +def = [notBareRepo $ + command "direct" paramNothing seek "switch repository to direct mode"] seek :: [CommandSeek] seek = [withNothing start] start :: CommandStart -start = notBareRepo $ - ifM isDirect - ( stop , next perform ) +start = ifM isDirect ( stop , next perform ) perform :: CommandPerform perform = do diff --git a/Command/FromKey.hs b/Command/FromKey.hs index a2ab55c9c7..d023be686c 100644 --- a/Command/FromKey.hs +++ b/Command/FromKey.hs @@ -14,14 +14,15 @@ import Annex.Content import Types.Key def :: [Command] -def = [notDirect $ command "fromkey" (paramPair paramKey paramPath) seek - "adds a file using a specific key"] +def = [notDirect $ notBareRepo $ + command "fromkey" (paramPair paramKey paramPath) seek + "adds a file using a specific key"] seek :: [CommandSeek] seek = [withWords start] start :: [String] -> CommandStart -start (keyname:file:[]) = notBareRepo $ do +start (keyname:file:[]) = do let key = fromMaybe (error "bad key") $ file2key keyname inbackend <- inAnnex key unless inbackend $ error $ diff --git a/Command/Import.hs b/Command/Import.hs index fc1bf5b4b2..e8e839e4fa 100644 --- a/Command/Import.hs +++ b/Command/Import.hs @@ -13,14 +13,14 @@ import qualified Annex import qualified Command.Add def :: [Command] -def = [notDirect $ command "import" paramPaths seek +def = [notDirect $ notBareRepo $ command "import" paramPaths seek "move and add files from outside git working copy"] seek :: [CommandSeek] seek = [withPathContents start] start :: (FilePath, FilePath) -> CommandStart -start (srcfile, destfile) = notBareRepo $ +start (srcfile, destfile) = ifM (liftIO $ isRegularFile <$> getSymbolicLinkStatus srcfile) ( do showStart "import" destfile diff --git a/Command/Indirect.hs b/Command/Indirect.hs index 058a7b7504..168d837ff9 100644 --- a/Command/Indirect.hs +++ b/Command/Indirect.hs @@ -18,15 +18,14 @@ import Annex.Content import Annex.CatFile def :: [Command] -def = [command "indirect" paramNothing seek "switch repository to indirect mode"] +def = [notBareRepo $ command "indirect" paramNothing seek + "switch repository to indirect mode"] seek :: [CommandSeek] seek = [withNothing start] start :: CommandStart -start = notBareRepo $ - ifM isDirect - ( next perform, stop ) +start = ifM isDirect ( next perform, stop ) perform :: CommandPerform perform = do diff --git a/Command/Watch.hs b/Command/Watch.hs index eb70ef6b10..25b5c6bba6 100644 --- a/Command/Watch.hs +++ b/Command/Watch.hs @@ -13,7 +13,7 @@ import Command import Option def :: [Command] -def = [withOptions [foregroundOption, stopOption] $ +def = [notBareRepo $ withOptions [foregroundOption, stopOption] $ command "watch" paramNothing seek "watch for changes"] seek :: [CommandSeek] @@ -28,7 +28,7 @@ stopOption :: Option stopOption = Option.flag [] "stop" "stop daemon" start :: Bool -> Bool -> Bool -> CommandStart -start assistant foreground stopdaemon = notBareRepo $ do +start assistant foreground stopdaemon = do if stopdaemon then stopDaemon else startDaemon assistant foreground Nothing -- does not return diff --git a/Command/WebApp.hs b/Command/WebApp.hs index 11ba23d83f..581d6d4dd8 100644 --- a/Command/WebApp.hs +++ b/Command/WebApp.hs @@ -29,7 +29,7 @@ import Control.Concurrent import Control.Concurrent.STM def :: [Command] -def = [noCommit $ noRepo startNoRepo $ dontCheck repoExists $ +def = [noCommit $ noRepo startNoRepo $ dontCheck repoExists $ notBareRepo $ command "webapp" paramNothing seek "launch webapp"] seek :: [CommandSeek] @@ -39,7 +39,7 @@ start :: CommandStart start = start' True start' :: Bool -> CommandStart -start' allowauto = notBareRepo $ do +start' allowauto = do liftIO $ ensureInstalled ifM isInitialized ( go , auto ) stop