module Development.GitRev
( gitBranch
, gitCommitCount
, gitCommitDate
, gitDescribe
, gitDirty
, gitDirtyTracked
, gitHash
) where
import Control.Exception
import Control.Monad
import Data.Maybe
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import System.Directory
import System.Exit
import System.FilePath
import System.Process
import Prelude ()
import Prelude.Compat
runGit :: [String] -> String -> IndexUsed -> Q String
runGit args def useIdx = do
let oops :: SomeException -> IO (ExitCode, String, String)
oops _e = return (ExitFailure 1, def, "")
gitFound <- runIO $ isJust <$> findExecutable "git"
if gitFound
then do
pwd <- runIO getDotGit
let hd = pwd </> ".git" </> "HEAD"
index = pwd </> ".git" </> "index"
packedRefs = pwd </> ".git" </> "packed-refs"
hdExists <- runIO $ doesFileExist hd
when hdExists $ do
splitAt 5 `fmap` runIO (readFile hd) >>= \case
("ref: ", relRef) -> do
let ref = pwd </> ".git" </> relRef
refExists <- runIO $ doesFileExist ref
when refExists $ addDependentFile ref
_hash -> addDependentFile hd
indexExists <- runIO $ doesFileExist index
when (indexExists && useIdx == IdxUsed) $ addDependentFile index
packedExists <- runIO $ doesFileExist packedRefs
when packedExists $ addDependentFile packedRefs
runIO $ do
(code, out, _err) <- readProcessWithExitCode "git" args "" `catch` oops
case code of
ExitSuccess -> return (takeWhile (/= '\n') out)
ExitFailure _ -> return def
else return def
getDotGit :: IO FilePath
getDotGit = do
pwd <- getGitRoot
let dotGit = pwd </> ".git"
oops = return dotGit
isDir <- doesDirectoryExist dotGit
isFile <- doesFileExist dotGit
if | isDir -> return dotGit
| not isFile -> oops
| isFile ->
splitAt 8 `fmap` readFile dotGit >>= \case
("gitdir: ", relDir) -> do
isRelDir <- doesDirectoryExist relDir
if isRelDir
then return relDir
else oops
_ -> oops
getGitRoot :: IO FilePath
getGitRoot = do
pwd <- getCurrentDirectory
(code, out, _) <-
readProcessWithExitCode "git" ["rev-parse", "--show-toplevel"] ""
case code of
ExitSuccess -> return $ takeWhile (/= '\n') out
ExitFailure _ -> return pwd
data IndexUsed = IdxUsed
| IdxNotUsed
deriving (Eq)
gitHash :: ExpQ
gitHash =
stringE =<< runGit ["rev-parse", "HEAD"] "UNKNOWN" IdxNotUsed
gitBranch :: ExpQ
gitBranch =
stringE =<< runGit ["rev-parse", "--abbrev-ref", "HEAD"] "UNKNOWN" IdxNotUsed
gitDescribe :: ExpQ
gitDescribe =
stringE =<< runGit ["describe", "--long", "--always"] "UNKNOWN" IdxNotUsed
gitDirty :: ExpQ
gitDirty = do
output <- runGit ["status", "--porcelain"] "" IdxUsed
case output of
"" -> conE falseName
_ -> conE trueName
gitDirtyTracked :: ExpQ
gitDirtyTracked = do
output <- runGit ["status", "--porcelain","--untracked-files=no"] "" IdxUsed
case output of
"" -> conE falseName
_ -> conE trueName
gitCommitCount :: ExpQ
gitCommitCount =
stringE =<< runGit ["rev-list", "HEAD", "--count"] "UNKNOWN" IdxNotUsed
gitCommitDate :: ExpQ
gitCommitDate =
stringE =<< runGit ["log", "HEAD", "-1", "--format=%cd"] "UNKNOWN" IdxNotUsed