Skip to content

Commit 247b9f6

Browse files
geekosaurMikolaj
authored andcommitted
record Git revision in devel builds
`cabal --version` in such builds will include the Git commit and branch (if not `master`).
1 parent e301eb6 commit 247b9f6

File tree

8 files changed

+125
-2
lines changed

8 files changed

+125
-2
lines changed

Cabal/Cabal.cabal

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ source-repository head
2929
location: https://github.com/haskell/cabal/
3030
subdir: Cabal
3131

32+
flag git-rev
33+
description: include Git revision hash in version
34+
default: False
35+
manual: True
36+
3237
library
3338
default-language: Haskell2010
3439
hs-source-dirs: src
@@ -51,6 +56,10 @@ library
5156
else
5257
build-depends: unix >= 2.6.0.0 && < 2.9
5358

59+
if flag(git-rev)
60+
build-depends: githash ^>= 0.1.7.0
61+
cpp-options: -DGIT_REV
62+
5463
ghc-options:
5564
-Wall
5665
-fno-ignore-asserts

Cabal/src/Distribution/Simple/Utils.hs

+36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{-# LANGUAGE LambdaCase #-}
88
{-# LANGUAGE RankNTypes #-}
99
{-# LANGUAGE ScopedTypeVariables #-}
10+
#ifdef GIT_REV
11+
{-# LANGUAGE TemplateHaskell #-}
12+
#endif
1013

1114
-----------------------------------------------------------------------------
1215

@@ -26,6 +29,7 @@
2629
-- various directory and file functions that do extra logging.
2730
module Distribution.Simple.Utils
2831
( cabalVersion
32+
, cabalGitInfo
2933

3034
-- * logging and errors
3135
, dieNoVerbosity
@@ -285,6 +289,16 @@ import System.IO.Unsafe
285289
import qualified System.Process as Process
286290
import qualified Text.PrettyPrint as Disp
287291

292+
#ifdef GIT_REV
293+
import Data.Either (isLeft)
294+
import GitHash
295+
( giHash
296+
, giBranch
297+
, giCommitDate
298+
, tGitInfoCwdTry
299+
)
300+
#endif
301+
288302
-- We only get our own version number when we're building with ourselves
289303
cabalVersion :: Version
290304
#if defined(BOOTSTRAPPED_CABAL)
@@ -295,6 +309,28 @@ cabalVersion = mkVersion [CABAL_VERSION]
295309
cabalVersion = mkVersion [3,0] --used when bootstrapping
296310
#endif
297311

312+
-- |
313+
-- `Cabal` Git information. Only filled in if built in a Git tree in
314+
-- developmnent mode and Template Haskell is available.
315+
cabalGitInfo :: String
316+
#ifdef GIT_REV
317+
cabalGitInfo = concat [ "(commit "
318+
, giHash'
319+
, branchInfo
320+
, ", "
321+
, either (const "") giCommitDate gi'
322+
, ")"
323+
]
324+
where
325+
gi' = $$tGitInfoCwdTry
326+
giHash' = take 7 . either (const "") giHash $ gi'
327+
branchInfo | isLeft gi' = ""
328+
| either id giBranch gi' == "master" = ""
329+
| otherwise = " on " <> either id giBranch gi'
330+
#else
331+
cabalGitInfo = ""
332+
#endif
333+
298334
-- ----------------------------------------------------------------------------
299335
-- Exception and logging utils
300336

cabal-install/cabal-install.cabal

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Flag lukko
3939
default: True
4040
manual: True
4141

42+
flag git-rev
43+
description: include Git revision hash in version
44+
default: False
45+
manual: True
46+
4247
common warnings
4348
ghc-options:
4449
-Wall
@@ -269,6 +274,9 @@ library
269274
if impl(ghc >=8.2)
270275
build-depends: process >= 1.6.15.0
271276

277+
if flag(git-rev)
278+
build-depends: githash ^>= 0.1.7.0
279+
cpp-options: -DGIT_REV
272280

273281
executable cabal
274282
import: warnings, base-dep
@@ -282,6 +290,9 @@ executable cabal
282290
if os(aix)
283291
extra-libraries: bsd
284292

293+
if flag(git-rev)
294+
cpp-options: -DGIT_REV
295+
285296
build-depends:
286297
cabal-install
287298

cabal-install/src/Distribution/Client/Main.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ import Distribution.Client.Utils
173173
, relaxEncodingErrors
174174
)
175175
import Distribution.Client.Version
176-
( cabalInstallVersion
176+
( cabalInstallGitInfo
177+
, cabalInstallVersion
177178
)
178179

179180
import Distribution.Package (packageId)
@@ -227,7 +228,8 @@ import Distribution.Simple.Program
227228
import Distribution.Simple.Program.Db (reconfigurePrograms)
228229
import qualified Distribution.Simple.Setup as Cabal
229230
import Distribution.Simple.Utils
230-
( cabalVersion
231+
( cabalGitInfo
232+
, cabalVersion
231233
, createDirectoryIfMissingVerbose
232234
, dieNoVerbosity
233235
, dieWithException
@@ -414,9 +416,16 @@ mainWorker args = do
414416
putStrLn $
415417
"cabal-install version "
416418
++ display cabalInstallVersion
419+
++ " "
420+
++ cabalInstallGitInfo
417421
++ "\ncompiled using version "
418422
++ display cabalVersion
419423
++ " of the Cabal library "
424+
++ cabalGitInfo'
425+
where
426+
cabalGitInfo'
427+
| cabalGitInfo == cabalInstallGitInfo = "(in-tree)"
428+
| otherwise = cabalGitInfo
420429

421430
commands = map commandFromSpec commandSpecs
422431
commandSpecs =
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
1+
{-# LANGUAGE CPP #-}
2+
#ifdef GIT_REV
3+
{-# LANGUAGE TemplateHaskell #-}
4+
#endif
5+
16
-- | Provides the version number of @cabal-install@.
27
module Distribution.Client.Version
38
( cabalInstallVersion
9+
, cabalInstallGitInfo
410
) where
511

612
import Distribution.Version
713

814
import qualified Paths_cabal_install as PackageInfo
915

16+
#ifdef GIT_REV
17+
import Data.Either (isLeft)
18+
import GitHash
19+
( giHash
20+
, giBranch
21+
, giCommitDate
22+
, tGitInfoCwdTry
23+
)
24+
#endif
25+
1026
-- |
1127
-- This value determines the output of `cabal-install --version`.
1228
cabalInstallVersion :: Version
1329
cabalInstallVersion = mkVersion' PackageInfo.version
30+
31+
-- |
32+
-- `cabal-install` Git information. Only filled in if built in a Git tree in
33+
-- developmnent mode and Template Haskell is available.
34+
cabalInstallGitInfo :: String
35+
#ifdef GIT_REV
36+
cabalInstallGitInfo = concat [ "(commit "
37+
, giHash'
38+
, branchInfo
39+
, ", "
40+
, either (const "") giCommitDate gi'
41+
, ")"
42+
]
43+
where
44+
gi' = $$tGitInfoCwdTry
45+
giHash' = take 7 . either (const "") giHash $ gi'
46+
branchInfo | isLeft gi' = ""
47+
| either id giBranch gi' == "master" = ""
48+
| otherwise = " on " <> either id giBranch gi'
49+
#else
50+
cabalInstallGitInfo = ""
51+
#endif

cabal.project

+8
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ import: project-cabal/pkgs.config
44
import: project-cabal/constraints.config
55

66
tests: True
7+
8+
-- if you are developing on a system without TH, use a `cabal.project.local`
9+
-- to disable this
10+
package cabal-install
11+
flags: +git-rev
12+
13+
package Cabal
14+
flags: +git-rev

cabal.release.project

+7
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ import: project-cabal/pkgs/install.config
33
import: project-cabal/pkgs/tests.config
44

55
index-state: hackage.haskell.org 2024-09-06T14:16:40Z
6+
7+
-- never include this or its TH dependency in a release!
8+
package cabal-install
9+
flags: -git-rev
10+
11+
package Cabal
12+
flags: -git-rev

cabal.validate.project

+5
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ tests: True
77
write-ghc-environment-files: never
88
program-options
99
ghc-options: -Werror
10+
11+
-- if you are developing on a system without TH, use a `cabal.validate.project.local`
12+
-- to disable this
13+
package cabal-install
14+
flags: +git-rev

0 commit comments

Comments
 (0)