2013-11-05 19:29:56 +00:00
|
|
|
{- git-annex git hooks
|
|
|
|
-
|
2019-10-08 18:34:00 +00:00
|
|
|
- Note that it's important that the content of scripts installed by
|
|
|
|
- git-annex not change, otherwise removing old hooks using an old
|
|
|
|
- version of the script would fail.
|
2013-11-05 19:29:56 +00:00
|
|
|
-
|
2019-10-08 18:34:00 +00:00
|
|
|
- Copyright 2013-2019 Joey Hess <id@joeyh.name>
|
2013-11-05 19:29:56 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-11-05 19:29:56 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Hook where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-11-05 19:29:56 +00:00
|
|
|
import qualified Git.Hook as Git
|
2014-03-02 22:01:07 +00:00
|
|
|
import qualified Annex
|
|
|
|
import Utility.Shell
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
preCommitHook :: Git.Hook
|
2018-09-25 19:02:12 +00:00
|
|
|
preCommitHook = Git.Hook "pre-commit" (mkHookScript "git annex pre-commit .") []
|
2013-11-05 19:29:56 +00:00
|
|
|
|
2017-02-17 18:04:43 +00:00
|
|
|
postReceiveHook :: Git.Hook
|
2018-09-25 19:02:12 +00:00
|
|
|
postReceiveHook = Git.Hook "post-receive"
|
|
|
|
-- Only run git-annex post-receive when git-annex supports it,
|
|
|
|
-- to avoid failing if the repository with this hook is used
|
|
|
|
-- with an older version of git-annex.
|
|
|
|
(mkHookScript "if git annex post-receive --help >/dev/null 2>&1; then git annex post-receive; fi")
|
|
|
|
-- This is an old version of the hook script.
|
|
|
|
[ mkHookScript "git annex post-receive"
|
|
|
|
]
|
2017-02-17 18:04:43 +00:00
|
|
|
|
2018-10-25 19:40:12 +00:00
|
|
|
postCheckoutHook :: Git.Hook
|
|
|
|
postCheckoutHook = Git.Hook "post-checkout" smudgeHook []
|
|
|
|
|
|
|
|
postMergeHook :: Git.Hook
|
|
|
|
postMergeHook = Git.Hook "post-merge" smudgeHook []
|
|
|
|
|
2018-10-26 18:37:43 +00:00
|
|
|
-- Older versions of git-annex didn't support this command, but neither did
|
|
|
|
-- they support v7 repositories.
|
2018-10-25 19:40:12 +00:00
|
|
|
smudgeHook :: String
|
2018-10-26 18:37:43 +00:00
|
|
|
smudgeHook = mkHookScript "git annex smudge --update"
|
2018-10-25 19:40:12 +00:00
|
|
|
|
2014-03-02 22:01:07 +00:00
|
|
|
preCommitAnnexHook :: Git.Hook
|
2018-09-25 19:02:12 +00:00
|
|
|
preCommitAnnexHook = Git.Hook "pre-commit-annex" "" []
|
2014-03-02 22:01:07 +00:00
|
|
|
|
2015-03-20 18:52:58 +00:00
|
|
|
postUpdateAnnexHook :: Git.Hook
|
2018-09-25 19:02:12 +00:00
|
|
|
postUpdateAnnexHook = Git.Hook "post-update-annex" "" []
|
2015-03-20 18:52:58 +00:00
|
|
|
|
2013-11-05 19:29:56 +00:00
|
|
|
mkHookScript :: String -> String
|
|
|
|
mkHookScript s = unlines
|
2018-10-13 05:36:06 +00:00
|
|
|
[ shebang
|
2013-11-05 19:29:56 +00:00
|
|
|
, "# automatically configured by git-annex"
|
|
|
|
, s
|
|
|
|
]
|
|
|
|
|
|
|
|
hookWrite :: Git.Hook -> Annex ()
|
2019-08-13 19:14:17 +00:00
|
|
|
hookWrite h = unlessM (inRepo $ Git.hookWrite h) $
|
|
|
|
hookWarning h "already exists, not configuring"
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
hookUnWrite :: Git.Hook -> Annex ()
|
|
|
|
hookUnWrite h = unlessM (inRepo $ Git.hookUnWrite h) $
|
|
|
|
hookWarning h "contents modified; not deleting. Edit it to remove call to git annex."
|
|
|
|
|
|
|
|
hookWarning :: Git.Hook -> String -> Annex ()
|
|
|
|
hookWarning h msg = do
|
|
|
|
r <- gitRepo
|
|
|
|
warning $ Git.hookName h ++ " hook (" ++ Git.hookFile h r ++ ") " ++ msg
|
2014-03-02 22:01:07 +00:00
|
|
|
|
|
|
|
{- Runs a hook. To avoid checking if the hook exists every time,
|
|
|
|
- the existing hooks are cached. -}
|
|
|
|
runAnnexHook :: Git.Hook -> Annex ()
|
|
|
|
runAnnexHook hook = do
|
|
|
|
m <- Annex.getState Annex.existinghooks
|
|
|
|
case M.lookup hook m of
|
2015-01-20 17:39:07 +00:00
|
|
|
Just True -> run
|
2014-03-02 22:01:07 +00:00
|
|
|
Just False -> noop
|
|
|
|
Nothing -> do
|
2015-01-20 17:39:07 +00:00
|
|
|
exists <- inRepo $ Git.hookExists hook
|
2014-03-02 22:01:07 +00:00
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.existinghooks = M.insert hook exists m }
|
2015-01-20 17:39:07 +00:00
|
|
|
when exists run
|
2014-03-02 22:01:07 +00:00
|
|
|
where
|
2015-01-20 17:39:07 +00:00
|
|
|
run = unlessM (inRepo $ Git.runHook hook) $ do
|
|
|
|
h <- fromRepo $ Git.hookFile hook
|
|
|
|
warning $ h ++ " failed"
|