Skip to content

Commit

Permalink
Fix gitRev TH splice
Browse files Browse the repository at this point in the history
Previously we had a TH quotation calling a TH splce, but that meant that
the TH splice was, under certain curcumstances, being cached in the object
file in `~/.cabal/store`.

Solution is to pull the TH splice into the TH quotation. This should force
the running of the `git rev-parse` command whenever the TH quotation is
evaluated.
  • Loading branch information
erikd committed Mar 13, 2024
1 parent 113ff70 commit 4d5e9f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
1 change: 0 additions & 1 deletion cardano-git-rev/cardano-git-rev.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ library
c-sources: cbits/rev.c

exposed-modules: Cardano.Git.Rev
Cardano.Git.RevFromGit

build-depends: process
, template-haskell
Expand Down
39 changes: 31 additions & 8 deletions cardano-git-rev/src/Cardano/Git/Rev.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,39 @@ module Cardano.Git.Rev
) where

import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text as Text

#if !defined(arm_HOST_ARCH)
import Cardano.Git.RevFromGit (gitRevFromGit)
#endif
import Foreign.C.String (CString)
import GHC.Foreign (peekCStringLen)
import Language.Haskell.TH (Exp, Q)
import System.IO (utf8)
import System.IO.Unsafe (unsafeDupablePerformIO)

#if !defined(arm_HOST_ARCH)
import Control.Exception (catch)
import System.Exit (ExitCode (..))
import qualified System.IO as IO
import System.IO.Error (isDoesNotExistError)
import System.Process (readProcessWithExitCode)
#endif

foreign import ccall "&_cardano_git_rev" c_gitrev :: CString

-- This must be a TH splice to ensure the git commit is captured at build time.
-- ie called as `$(gitRev)`.
gitRev :: Q Exp
gitRev =
[| if
| gitRevEmbed /= zeroRev -> gitRevEmbed
| T.null fromGit -> zeroRev
| gitRevEmbed /= zeroRev -> fromGit
| Text.null fromGit -> zeroRev
| otherwise -> fromGit
|]

-- Git revision embedded after compilation using
-- Data.FileEmbed.injectWith. If nothing has been injected,
-- this will be filled with 0 characters.
gitRevEmbed :: Text
gitRevEmbed = T.pack $ drop 28 $ unsafeDupablePerformIO (peekCStringLen utf8 (c_gitrev, 68))
gitRevEmbed = Text.pack $ drop 28 $ unsafeDupablePerformIO (peekCStringLen utf8 (c_gitrev, 68))

-- Git revision found during compilation by running git. If
-- git could not be run, then this will be empty.
Expand All @@ -45,8 +50,26 @@ fromGit :: Text
-- cross compiling to arm fails; due to a linker bug
fromGit = ""
#else
fromGit = T.strip (T.pack $(gitRevFromGit))
fromGit = Text.strip $ Text.pack (unsafeDupablePerformIO runGitRevParse)
#endif

zeroRev :: Text
zeroRev = "0000000000000000000000000000000000000000"

#if !defined(arm_HOST_ARCH)
runGitRevParse :: IO String
runGitRevParse = do
(exitCode, output, errorMessage) <- readProcessWithExitCode_ "git" ["rev-parse", "--verify", "HEAD"] ""
case exitCode of
ExitSuccess -> pure output
ExitFailure _ -> do
IO.hPutStrLn IO.stderr $ "WARNING: " ++ errorMessage
pure ""
where
readProcessWithExitCode_ :: FilePath -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode_ cmd args input =
catch (readProcessWithExitCode cmd args input) $ \e ->
if isDoesNotExistError e
then pure (ExitFailure 127, "", show e)
else pure (ExitFailure 999, "", show e)
#endif
33 changes: 0 additions & 33 deletions cardano-git-rev/src/Cardano/Git/RevFromGit.hs

This file was deleted.

0 comments on commit 4d5e9f1

Please sign in to comment.