summaryrefslogtreecommitdiff
path: root/main/Version/Git.hs
blob: 2aae6e3964af5dd18237573bbab1fa484c37fe25 (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
module Version.Git (
    tGitVersion,
) where

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

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

tGitVersion :: Code Q (Maybe String)
tGitVersion = unsafeCodeCoerce $ 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)