{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
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 :: [String] -> String -> IndexUsed -> Q String
runGit [String]
args String
def IndexUsed
useIdx = do
let oops :: SomeException -> IO (ExitCode, String, String)
oops :: SomeException -> IO (ExitCode, String, String)
oops SomeException
_e = forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ExitCode
ExitFailure Int
1, String
def, String
"")
Bool
gitFound <- forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ forall a. Maybe a -> Bool
isJust forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO (Maybe String)
findExecutable String
"git"
if Bool
gitFound
then do
String
pwd <- forall a. IO a -> Q a
runIO IO String
getDotGit
let hd :: String
hd = String
pwd String -> String -> String
</> String
".git" String -> String -> String
</> String
"HEAD"
index :: String
index = String
pwd String -> String -> String
</> String
".git" String -> String -> String
</> String
"index"
packedRefs :: String
packedRefs = String
pwd String -> String -> String
</> String
".git" String -> String -> String
</> String
"packed-refs"
Bool
hdExists <- forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
hd
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hdExists forall a b. (a -> b) -> a -> b
$ do
forall a. Int -> [a] -> ([a], [a])
splitAt Int
5 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. IO a -> Q a
runIO (String -> IO String
readFile String
hd) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
(String
"ref: ", String
relRef) -> do
let ref :: String
ref = String
pwd String -> String -> String
</> String
".git" String -> String -> String
</> String
relRef
Bool
refExists <- forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
ref
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
refExists forall a b. (a -> b) -> a -> b
$ String -> Q ()
addDependentFile String
ref
(String, String)
_hash -> String -> Q ()
addDependentFile String
hd
Bool
indexExists <- forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
index
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
indexExists Bool -> Bool -> Bool
&& IndexUsed
useIdx forall a. Eq a => a -> a -> Bool
== IndexUsed
IdxUsed) forall a b. (a -> b) -> a -> b
$ String -> Q ()
addDependentFile String
index
Bool
packedExists <- forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ String -> IO Bool
doesFileExist String
packedRefs
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
packedExists forall a b. (a -> b) -> a -> b
$ String -> Q ()
addDependentFile String
packedRefs
forall a. IO a -> Q a
runIO forall a b. (a -> b) -> a -> b
$ do
(ExitCode
code, String
out, String
_err) <- String -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode String
"git" [String]
args String
"" forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` SomeException -> IO (ExitCode, String, String)
oops
case ExitCode
code of
ExitCode
ExitSuccess -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Char
'\n') String
out)
ExitFailure Int
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return String
def
else forall (m :: * -> *) a. Monad m => a -> m a
return String
def
getDotGit :: IO FilePath
getDotGit :: IO String
getDotGit = do
String
pwd <- IO String
getGitRoot
let dotGit :: String
dotGit = String
pwd String -> String -> String
</> String
".git"
oops :: IO String
oops = forall (m :: * -> *) a. Monad m => a -> m a
return String
dotGit
Bool
isDir <- String -> IO Bool
doesDirectoryExist String
dotGit
Bool
isFile <- String -> IO Bool
doesFileExist String
dotGit
if | Bool
isDir -> forall (m :: * -> *) a. Monad m => a -> m a
return String
dotGit
| Bool -> Bool
not Bool
isFile -> IO String
oops
| Bool
isFile ->
forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> IO String
readFile String
dotGit forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
(String
"gitdir: ", String
relDir) -> do
Bool
isRelDir <- String -> IO Bool
doesDirectoryExist String
relDir
if Bool
isRelDir
then forall (m :: * -> *) a. Monad m => a -> m a
return String
relDir
else IO String
oops
(String, String)
_ -> IO String
oops
getGitRoot :: IO FilePath
getGitRoot :: IO String
getGitRoot = do
String
pwd <- IO String
getCurrentDirectory
(ExitCode
code, String
out, String
_) <-
String -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode String
"git" [String
"rev-parse", String
"--show-toplevel"] String
""
case ExitCode
code of
ExitCode
ExitSuccess -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Char
'\n') String
out
ExitFailure Int
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return String
pwd
data IndexUsed = IdxUsed
| IdxNotUsed
deriving (IndexUsed -> IndexUsed -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IndexUsed -> IndexUsed -> Bool
$c/= :: IndexUsed -> IndexUsed -> Bool
== :: IndexUsed -> IndexUsed -> Bool
$c== :: IndexUsed -> IndexUsed -> Bool
Eq)
gitHash :: ExpQ
gitHash :: ExpQ
gitHash =
forall (m :: * -> *). Quote m => String -> m Exp
stringE forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [String] -> String -> IndexUsed -> Q String
runGit [String
"rev-parse", String
"HEAD"] String
"UNKNOWN" IndexUsed
IdxNotUsed
gitBranch :: ExpQ
gitBranch :: ExpQ
gitBranch =
forall (m :: * -> *). Quote m => String -> m Exp
stringE forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [String] -> String -> IndexUsed -> Q String
runGit [String
"rev-parse", String
"--abbrev-ref", String
"HEAD"] String
"UNKNOWN" IndexUsed
IdxNotUsed
gitDescribe :: ExpQ
gitDescribe :: ExpQ
gitDescribe =
forall (m :: * -> *). Quote m => String -> m Exp
stringE forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [String] -> String -> IndexUsed -> Q String
runGit [String
"describe", String
"--long", String
"--always"] String
"UNKNOWN" IndexUsed
IdxNotUsed
gitDirty :: ExpQ
gitDirty :: ExpQ
gitDirty = do
String
output <- [String] -> String -> IndexUsed -> Q String
runGit [String
"status", String
"--porcelain"] String
"" IndexUsed
IdxUsed
case String
output of
String
"" -> forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
falseName
String
_ -> forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
trueName
gitDirtyTracked :: ExpQ
gitDirtyTracked :: ExpQ
gitDirtyTracked = do
String
output <- [String] -> String -> IndexUsed -> Q String
runGit [String
"status", String
"--porcelain",String
"--untracked-files=no"] String
"" IndexUsed
IdxUsed
case String
output of
String
"" -> forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
falseName
String
_ -> forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
trueName
gitCommitCount :: ExpQ
gitCommitCount :: ExpQ
gitCommitCount =
forall (m :: * -> *). Quote m => String -> m Exp
stringE forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [String] -> String -> IndexUsed -> Q String
runGit [String
"rev-list", String
"HEAD", String
"--count"] String
"UNKNOWN" IndexUsed
IdxNotUsed
gitCommitDate :: ExpQ
gitCommitDate :: ExpQ
gitCommitDate =
forall (m :: * -> *). Quote m => String -> m Exp
stringE forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [String] -> String -> IndexUsed -> Q String
runGit [String
"log", String
"HEAD", String
"-1", String
"--format=%cd"] String
"UNKNOWN" IndexUsed
IdxNotUsed