From 1c4641f063130165516789e842629e437082c5b8 Mon Sep 17 00:00:00 2001 From: Tony Zorman Date: Fri, 22 Dec 2023 13:56:44 +0100 Subject: [PATCH] K.A.TH: Check for presence of git executable As promised in [1], this makes gitHash a little more robust. [1]: https://github.com/NixOS/nixpkgs/pull/267450 --- src/KMonad/Args/TH.hs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/KMonad/Args/TH.hs b/src/KMonad/Args/TH.hs index 0abfcc47..7b80a682 100644 --- a/src/KMonad/Args/TH.hs +++ b/src/KMonad/Args/TH.hs @@ -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 |]