aboutsummaryrefslogtreecommitdiff
path: root/Git/Hook.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-11-05 15:29:56 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-11-05 15:29:56 -0400
commit8bb9d23f521582e51ceaee03d1aa5084cad3de08 (patch)
tree14e8736ce9d104dc4040310219ed9bceb1fe810a /Git/Hook.hs
parent83c6fbde28cb25f377bd37d0eedde52c87874052 (diff)
refactored hook setup
Diffstat (limited to 'Git/Hook.hs')
-rw-r--r--Git/Hook.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/Git/Hook.hs b/Git/Hook.hs
new file mode 100644
index 000000000..d56a4a565
--- /dev/null
+++ b/Git/Hook.hs
@@ -0,0 +1,54 @@
+{- git hooks
+ -
+ - Copyright 2013 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Git.Hook where
+
+import Common
+import Git
+import Utility.Tmp
+
+data Hook = Hook
+ { hookName :: FilePath
+ , hookScript :: String
+ }
+
+hookFile :: Hook -> Repo -> FilePath
+hookFile h r = localGitDir r </> "hooks" </> hookName h
+
+{- Writes a hook. Returns False if the hook already exists with a different
+ - content. -}
+hookWrite :: Hook -> Repo -> IO Bool
+hookWrite h r = do
+ let f = hookFile h r
+ ifM (doesFileExist f)
+ ( expectedContent h r
+ , do
+ viaTmp writeFile f (hookScript h)
+ p <- getPermissions f
+ setPermissions f $ p {executable = True}
+ return True
+ )
+
+{- Removes a hook. Returns False if the hook contained something else, and
+ - could not be removed. -}
+hookUnWrite :: Hook -> Repo -> IO Bool
+hookUnWrite h r = do
+ let f = hookFile h r
+ ifM (doesFileExist f)
+ ( ifM (expectedContent h r)
+ ( do
+ removeFile f
+ return True
+ , return False
+ )
+ , return True
+ )
+
+expectedContent :: Hook -> Repo -> IO Bool
+expectedContent h r = do
+ content <- readFile $ hookFile h r
+ return $ content == hookScript h