summaryrefslogtreecommitdiff
path: root/Annex/Hook.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Annex/Hook.hs')
-rw-r--r--Annex/Hook.hs36
1 files changed, 32 insertions, 4 deletions
diff --git a/Annex/Hook.hs b/Annex/Hook.hs
index 7301a0958..a6fcf7131 100644
--- a/Annex/Hook.hs
+++ b/Annex/Hook.hs
@@ -1,9 +1,10 @@
{- git-annex git hooks
-
- - Note that it's important that the scripts not change, otherwise
- - removing old hooks using an old version of the script would fail.
+ - Note that it's important that the scripts installed by git-annex
+ - not change, otherwise removing old hooks using an old version of
+ - the script would fail.
-
- - Copyright 2013 Joey Hess <joey@kitenet.net>
+ - Copyright 2013-2014 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
@@ -12,12 +13,19 @@ module Annex.Hook where
import Common.Annex
import qualified Git.Hook as Git
-import Utility.Shell
import Config
+import qualified Annex
+import Utility.Shell
+import Utility.FileMode
+
+import qualified Data.Map as M
preCommitHook :: Git.Hook
preCommitHook = Git.Hook "pre-commit" (mkHookScript "git annex pre-commit .")
+preCommitAnnexHook :: Git.Hook
+preCommitAnnexHook = Git.Hook "pre-commit-annex" ""
+
mkHookScript :: String -> String
mkHookScript s = unlines
[ shebang_local
@@ -40,3 +48,23 @@ hookWarning :: Git.Hook -> String -> Annex ()
hookWarning h msg = do
r <- gitRepo
warning $ Git.hookName h ++ " hook (" ++ Git.hookFile h r ++ ") " ++ msg
+
+{- Runs a hook. To avoid checking if the hook exists every time,
+ - the existing hooks are cached. -}
+runAnnexHook :: Git.Hook -> Annex ()
+runAnnexHook hook = do
+ cmd <- fromRepo $ Git.hookFile hook
+ m <- Annex.getState Annex.existinghooks
+ case M.lookup hook m of
+ Just True -> run cmd
+ Just False -> noop
+ Nothing -> do
+ exists <- hookexists cmd
+ Annex.changeState $ \s -> s
+ { Annex.existinghooks = M.insert hook exists m }
+ when exists $
+ run cmd
+ where
+ hookexists f = liftIO $ isExecutable . fileMode <$> getFileStatus f
+ run cmd = unlessM (liftIO $ boolSystem cmd []) $
+ warning $ cmd ++ " failed"