git.iko.soy/typeable/gitrev
on master help sign in
add count of commits in HEAD

Prompted by request at https://github.com/commercialhaskell/stack/issues/752#issuecomment-129919676

commit8184c2724eb01385de5fd55f98b2094daf1ef558 authorAdam C. Foltzer <acfoltzer@galois.com> dateTue 11 Aug 2015, 10:38 (11 years ago) parent2702e69
3 files changed+16−6
Example.hs+3−1 gitrev.cabal+3−2 src/Development/GitRev.hs+10−3
Example.hs+3−1
@@ -5,7 +5,9 @@ import Development.GitRev
55panic :: String -> a
66panic msg = error panicMsg
77 where panicMsg =
8− concat [ "[panic ", $(gitBranch), "@", $(gitHash), dirty, "] ", msg ]
8+ concat [ "[panic ", $(gitBranch), "@", $(gitHash)
9+ , " (", $(gitCommitCount), " commits in HEAD)"
10+ , dirty, "] ", msg ]
911 dirty | $(gitDirty) = " (uncommitted files present)"
1012 | otherwise = ""
1113
gitrev.cabal+3−2
@@ -1,5 +1,5 @@
11name: gitrev
2−version: 1.0.0
2+version: 1.1.0
33synopsis: Compile git revision info into Haskell projects
44homepage: https://github.com/acfoltzer/gitrev
55license: BSD3
@@ -9,13 +9,14 @@ maintainer: acfoltzer@galois.com
99category: Development
1010build-type: Simple
1111cabal-version: >=1.10
12+description: Some handy Template Haskell splices for including the current git hash and branch in the code of your project. Useful for including in panic messages, @--version@ output, or diagnostic info for more informative bug reports.
1213
1314source-repository head
1415 type: git
1516 location: https://github.com/acfoltzer/gitrev.git
1617
1718library
18− build-depends: base >= 4.7,
19+ build-depends: base >= 4.7 && < 5,
1920 directory,
2021 filepath,
2122 template-haskell,
src/Development/GitRev.hs+10−3
@@ -17,16 +17,18 @@
1717-- > panic :: String -> a
1818-- > panic msg = error panicMsg
1919-- > where panicMsg =
20−-- > concat [ "[panic ", $(gitBranch), "@", $(gitHash), dirty, "] ", msg ]
20+-- > concat [ "[panic ", $(gitBranch), "@", $(gitHash)
21+-- > , " (", $(gitCommitCount), " commits in HEAD)"
22+-- > , dirty, "] ", msg ]
2123-- > dirty | $(gitDirty) = " (uncommitted files present)"
2224-- > | otherwise = ""
2325-- >
2426-- > main = panic "oh no!"
2527--
2628-- > % cabal exec runhaskell Example.hs
27−-- > Example.hs: [panic master@4a0a592c37ad908889bd2a7a411923a903ed05a3 (uncommitted files present)] oh no!
29+-- > Example.hs: [panic master@2702e69355c978805064543489c351b61ac6760b (6 commits in HEAD) (uncommitted files present)] oh no!
2830
29−module Development.GitRev (gitHash, gitBranch, gitDirty) where
31+module Development.GitRev (gitHash, gitBranch, gitDirty, gitCommitCount) where
3032
3133import Control.Applicative
3234import Control.Exception
@@ -103,3 +105,8 @@ gitDirty = do
103105 case output of
104106 "" -> conE $ mkName "Prelude.False"
105107 _ -> conE $ mkName "Prelude.True"
108+
109+-- | Return the number of commits in the current head
110+gitCommitCount :: ExpQ
111+gitCommitCount =
112+ stringE =<< runGit ["rev-list", "HEAD", "--count"] "UNKNOWN"