{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Cardano.Config.Git.Rev (
      gitRev
    ) where

import           Cardano.Prelude

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

import           Cardano.Config.Git.RevFromGit (gitRevFromGit)

gitRev :: Text
gitRev :: Text
gitRev | Text
gitRevEmbed Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
zeroRev = Text
gitRevEmbed
       | Text -> Bool
T.null Text
fromGit         = Text
zeroRev
       | Bool
otherwise              = Text
fromGit
    where
        -- Git revision embedded after compilation using
        -- Data.FileEmbed.injectWith. If nothing has been injected,
        -- this will be filled with 0 characters.
        gitRevEmbed :: Text
        gitRevEmbed :: Text
gitRevEmbed = ByteString -> Text
decodeUtf8 $(dummySpaceWith "gitrev" 40)

        -- Git revision found during compilation by running git. If
        -- git could not be run, then this will be empty.
#if defined(arm_HOST_ARCH)
        -- cross compiling to arm fails; due to a linker bug
        fromGit = ""
#else
        fromGit :: Text
fromGit = Text -> Text
T.strip (String -> Text
T.pack $(String
gitRevFromGit))
#endif

zeroRev :: Text
zeroRev :: Text
zeroRev = Text
"0000000000000000000000000000000000000000"