summaryrefslogtreecommitdiff
path: root/src/Version/Git.hs
blob: fcd54a0f353f2bd2cc9df4f15ce755d490cd0759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Version.Git (
    tGitVersion,
) where

import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Syntax.Compat

import System.Directory
import System.Exit
import System.Process

tGitVersion :: SpliceQ (Maybe String)
tGitVersion = unsafeSpliceCoerce $ do
    let git args = do
            (ExitSuccess, out, _) <- readCreateProcessWithExitCode
                (proc "git" $ [ "--git-dir=./.git", "--work-tree=." ] ++ args) ""
            return $ lines out

    mbver <- runIO $ do
        doesPathExist "./.git" >>= \case
            False -> return Nothing
            True -> do
                desc:_ <- git [ "describe", "--always", "--dirty= (dirty)" ]
                files <- git [ "ls-files" ]
                return $ Just (desc, files)

    case mbver of
        Just (_, files) -> mapM_ addDependentFile files
        Nothing -> return ()

    lift (fst <$> mbver :: Maybe String)