Skip to content

Commit

Permalink
K.A.TH: Check for presence of git executable
Browse files Browse the repository at this point in the history
As promised in [1], this makes gitHash a little more robust.

[1]: NixOS/nixpkgs#267450
  • Loading branch information
slotThe committed Dec 22, 2023
1 parent 575b7e0 commit 1c4641f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/KMonad/Args/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import KMonad.Prelude

import Language.Haskell.TH (Exp, Q)
import Language.Haskell.TH.Syntax (runIO)
import UnliftIO.Directory (findExecutable)
import UnliftIO.Process (readProcessWithExitCode)


-- | Get the git hash of the current revision at compile time
-- | Get the git hash of the current commit at compile time.
gitHash :: Q Exp
gitHash = do
str <- runIO do
(exitCode, hash, _) <- readProcessWithExitCode "git" ["rev-parse", "HEAD"] ""
pure case exitCode of
ExitSuccess -> takeWhile (/= '\n') hash
_ -> ""
findExecutable "git" >>= \case
Nothing -> pure "" -- Git not present
Just git -> do
(exitCode, hash, _) <- readProcessWithExitCode git ["rev-parse", "HEAD"] ""
pure case exitCode of
ExitSuccess -> takeWhile (/= '\n') hash
_ -> "" -- Not in a git repo
[| fromString str |]

0 comments on commit 1c4641f

Please sign in to comment.