2013-11-05 19:29:56 +00:00
|
|
|
{- git hooks
|
|
|
|
-
|
2018-09-25 19:02:12 +00:00
|
|
|
- Copyright 2013-2018 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
|
|
|
-}
|
|
|
|
|
2015-01-20 17:39:07 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2013-11-05 19:29:56 +00:00
|
|
|
module Git.Hook where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
import Utility.Tmp
|
2016-09-05 16:09:23 +00:00
|
|
|
import Utility.Shell
|
2015-01-20 17:39:07 +00:00
|
|
|
import Utility.FileMode
|
2023-03-21 22:22:41 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2023-03-01 19:55:58 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
|
|
|
import System.PosixCompat.Files (fileMode)
|
2023-03-21 22:22:41 +00:00
|
|
|
#endif
|
|
|
|
|
2023-11-02 17:35:54 +00:00
|
|
|
import qualified Data.ByteString as B
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
data Hook = Hook
|
|
|
|
{ hookName :: FilePath
|
|
|
|
, hookScript :: String
|
2018-09-25 19:02:12 +00:00
|
|
|
, hookOldScripts :: [String]
|
2013-11-05 19:29:56 +00:00
|
|
|
}
|
2014-03-02 22:01:07 +00:00
|
|
|
deriving (Ord)
|
|
|
|
|
|
|
|
instance Eq Hook where
|
|
|
|
a == b = hookName a == hookName b
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
hookFile :: Hook -> Repo -> FilePath
|
2019-12-09 17:49:05 +00:00
|
|
|
hookFile h r = fromRawFilePath (localGitDir r) </> "hooks" </> hookName h
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
{- Writes a hook. Returns False if the hook already exists with a different
|
2019-08-13 19:14:17 +00:00
|
|
|
- content. Upgrades old scripts.
|
|
|
|
-
|
|
|
|
- This can install hooks on both filesystem like FAT that do not support
|
|
|
|
- execute bits, and on Windows.
|
|
|
|
-
|
|
|
|
- If the filesystem does not support execute bits, it's typically mounted
|
|
|
|
- such that all files have the execute bit set. So just write the hook
|
|
|
|
- and ignore failure to make it executable.
|
|
|
|
-
|
|
|
|
- On Windows, git will run hooks that are not executable. The hook
|
|
|
|
- is run with a bundled bash, so should start with #!/bin/sh
|
|
|
|
-}
|
2013-11-05 19:29:56 +00:00
|
|
|
hookWrite :: Hook -> Repo -> IO Bool
|
2018-09-25 19:02:12 +00:00
|
|
|
hookWrite h r = ifM (doesFileExist f)
|
|
|
|
( expectedContent h r >>= \case
|
|
|
|
UnexpectedContent -> return False
|
|
|
|
ExpectedContent -> return True
|
|
|
|
OldExpectedContent -> go
|
|
|
|
, go
|
|
|
|
)
|
|
|
|
where
|
|
|
|
f = hookFile h r
|
|
|
|
go = do
|
2023-11-02 17:35:54 +00:00
|
|
|
-- On Windows, using B.writeFile here avoids
|
|
|
|
-- the newline translation done by writeFile.
|
|
|
|
-- Hook scripts on Windows could use CRLF endings, but
|
|
|
|
-- they typically use unix newlines, which does work there
|
|
|
|
-- and makes the repository more portable.
|
|
|
|
viaTmp B.writeFile f (encodeBS (hookScript h))
|
2020-11-17 17:31:12 +00:00
|
|
|
void $ tryIO $ modifyFileMode
|
|
|
|
(toRawFilePath f)
|
|
|
|
(addModes executeModes)
|
2018-09-25 19:02:12 +00:00
|
|
|
return True
|
2013-11-05 19:29:56 +00:00
|
|
|
|
|
|
|
{- Removes a hook. Returns False if the hook contained something else, and
|
|
|
|
- could not be removed. -}
|
|
|
|
hookUnWrite :: Hook -> Repo -> IO Bool
|
2018-09-25 19:02:12 +00:00
|
|
|
hookUnWrite h r = ifM (doesFileExist f)
|
|
|
|
( expectedContent h r >>= \case
|
|
|
|
UnexpectedContent -> return False
|
|
|
|
_ -> do
|
|
|
|
removeFile f
|
|
|
|
return True
|
|
|
|
, return True
|
|
|
|
)
|
|
|
|
where
|
|
|
|
f = hookFile h r
|
|
|
|
|
|
|
|
data ExpectedContent = UnexpectedContent | ExpectedContent | OldExpectedContent
|
2013-11-05 19:29:56 +00:00
|
|
|
|
2018-09-25 19:02:12 +00:00
|
|
|
expectedContent :: Hook -> Repo -> IO ExpectedContent
|
2013-11-05 19:29:56 +00:00
|
|
|
expectedContent h r = do
|
2023-11-02 17:35:54 +00:00
|
|
|
-- Note that on windows, this readFile does newline translation,
|
|
|
|
-- and so a hook file that has CRLF will be treated the same as one
|
|
|
|
-- that has LF. That is intentional, since users may have a reason
|
|
|
|
-- to prefer one or the other.
|
2013-11-05 19:29:56 +00:00
|
|
|
content <- readFile $ hookFile h r
|
2018-09-25 19:02:12 +00:00
|
|
|
return $ if content == hookScript h
|
|
|
|
then ExpectedContent
|
|
|
|
else if any (content ==) (hookOldScripts h)
|
|
|
|
then OldExpectedContent
|
|
|
|
else UnexpectedContent
|
2015-01-20 17:39:07 +00:00
|
|
|
|
|
|
|
hookExists :: Hook -> Repo -> IO Bool
|
|
|
|
hookExists h r = do
|
|
|
|
let f = hookFile h r
|
|
|
|
catchBoolIO $
|
|
|
|
#ifndef mingw32_HOST_OS
|
2023-03-01 19:55:58 +00:00
|
|
|
isExecutable . fileMode <$> R.getFileStatus (toRawFilePath f)
|
2015-01-20 17:39:07 +00:00
|
|
|
#else
|
|
|
|
doesFileExist f
|
|
|
|
#endif
|
|
|
|
|
|
|
|
runHook :: Hook -> Repo -> IO Bool
|
|
|
|
runHook h r = do
|
|
|
|
let f = hookFile h r
|
2016-09-05 16:09:23 +00:00
|
|
|
(c, ps) <- findShellCommand f
|
2015-01-20 17:39:07 +00:00
|
|
|
boolSystem c ps
|