diff --git a/Cabal/src/Distribution/Simple/Utils.hs b/Cabal/src/Distribution/Simple/Utils.hs index d51601e5c27..c771b9e6764 100644 --- a/Cabal/src/Distribution/Simple/Utils.hs +++ b/Cabal/src/Distribution/Simple/Utils.hs @@ -9,6 +9,9 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} +#ifdef GIT_REV +{-# LANGUAGE TemplateHaskell #-} +#endif ----------------------------------------------------------------------------- diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal index 3e847ac7dac..bf81cccf5ef 100644 --- a/cabal-install/cabal-install.cabal +++ b/cabal-install/cabal-install.cabal @@ -39,6 +39,11 @@ Flag lukko default: True manual: True +flag git-rev + description: include Git revision hash in version + default: False + manual: True + common warnings ghc-options: -Wall @@ -270,6 +275,9 @@ library if impl(ghc >=8.2) build-depends: process >= 1.6.15.0 + if flag(git-rev) + build-depends: githash ^>= 0.1.7.0 + cpp-options: -DGIT_REV executable cabal import: warnings, base-dep @@ -283,6 +291,9 @@ executable cabal if os(aix) extra-libraries: bsd + if flag(git-rev) + ghc-options: -DGIT_REV + build-depends: cabal-install diff --git a/cabal-install/src/Distribution/Client/Main.hs b/cabal-install/src/Distribution/Client/Main.hs index e6278a5ef9a..7266a4b8b4d 100644 --- a/cabal-install/src/Distribution/Client/Main.hs +++ b/cabal-install/src/Distribution/Client/Main.hs @@ -174,6 +174,7 @@ import Distribution.Client.Utils ) import Distribution.Client.Version ( cabalInstallVersion + , cabalInstallGitInfo ) import Distribution.Package (packageId) @@ -413,6 +414,8 @@ mainWorker args = do putStrLn $ "cabal-install version " ++ display cabalInstallVersion + ++ " " + ++ cabalInstallGitInfo ++ "\ncompiled using version " ++ display cabalVersion ++ " of the Cabal library " diff --git a/cabal-install/src/Distribution/Client/Version.hs b/cabal-install/src/Distribution/Client/Version.hs index f5c6bec510d..e50f4231090 100644 --- a/cabal-install/src/Distribution/Client/Version.hs +++ b/cabal-install/src/Distribution/Client/Version.hs @@ -1,13 +1,51 @@ +{-# LANGUAGE CPP #-} +#ifdef GIT_REV +{-# LANGUAGE TemplateHaskell #-} +#endif + -- | Provides the version number of @cabal-install@. module Distribution.Client.Version ( cabalInstallVersion + , cabalInstallGitInfo ) where import Distribution.Version import qualified Paths_cabal_install as PackageInfo +#ifdef GIT_REV +import Data.Either (isLeft) +import GitHash + ( giHash + , giBranch + , giCommitDate + , tGitInfoCwdTry + ) +#endif + -- | -- This value determines the output of `cabal-install --version`. cabalInstallVersion :: Version cabalInstallVersion = mkVersion' PackageInfo.version + +-- | +-- `cabal-install` Git information. Only filled in if built in a Git tree in +-- developmnent mode and Template Haskell is available. +cabalInstallGitInfo :: String +#ifdef GIT_REV +cabalInstallGitInfo = concat [ "(commit " + , giHash' + , branchInfo + , ", " + , either (const "") giCommitDate gi' + , ")" + ] + where + gi' = $$tGitInfoCwdTry + giHash' = take 7 . either (const "") giHash $ gi' + branchInfo | isLeft gi' = "" + | either id giBranch gi' == "master" = "" + | otherwise = " on " <> either id giBranch gi' +#else +cabalInstallGitInfo = "" +#endif diff --git a/cabal.project b/cabal.project index e368c280c99..8443cb96d7f 100644 --- a/cabal.project +++ b/cabal.project @@ -4,3 +4,8 @@ import: project-cabal/pkgs.config import: project-cabal/constraints.config tests: True + +-- if you are developing on a system without TH, use a `cabal.project.local` +-- to disable this +package cabal-install + flags: +git-rev diff --git a/cabal.release.project b/cabal.release.project index 74a2632797b..13f7e822a25 100644 --- a/cabal.release.project +++ b/cabal.release.project @@ -6,3 +6,10 @@ constraints: hashable -arch-native index-state: hackage.haskell.org 2024-09-06T14:16:40Z + +-- never include this or its TH dependency in a release! +package Cabal + flags: -git-rev + +package cabal-install + flags: -git-rev diff --git a/cabal.validate.project b/cabal.validate.project index 2e3084cccf0..bae8909341a 100644 --- a/cabal.validate.project +++ b/cabal.validate.project @@ -12,3 +12,8 @@ program-options -- as such we cannot enable "-march=native". constraints: hashable -arch-native + +-- if you are developing on a system without TH, use a `cabal.validate.project.local` +-- to disable this +package cabal-install + flags: +git-rev