Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Move to typed quotes #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import Development.GitRev
panic :: String -> a
panic msg = error panicMsg
where panicMsg =
concat [ "[panic ", $(gitBranch), "@", $(gitHash)
, " (", $(gitCommitDate), ")"
, " (", $(gitCommitCount), " commits in HEAD)"
concat [ "[panic ", $$(gitBranch), "@", $$(gitHash)
, " (", $$(gitCommitDate), ")"
, " (", $$(gitCommitCount), " commits in HEAD)"
, dirty, "] ", msg ]
dirty | $(gitDirty) = " (uncommitted files present)"
| otherwise = ""
dirty | $$(gitDirty) = " (uncommitted files present)"
| otherwise = ""

main = panic "oh no!"
4 changes: 2 additions & 2 deletions gitrev.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ library
build-depends: base >= 4.6 && < 5,
directory,
filepath,
template-haskell,
template-haskell >= 2.9 && < 3,
process
hs-source-dirs: src
default-language: Haskell2010
exposed-modules: Development.GitRev
exposed-modules: Development.GitRev
39 changes: 23 additions & 16 deletions src/Development/GitRev.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
-- > panic :: String -> a
-- > panic msg = error panicMsg
-- > where panicMsg =
-- > concat [ "[panic ", $(gitBranch), "@", $(gitHash)
-- > , " (", $(gitCommitDate), ")"
-- > , " (", $(gitCommitCount), " commits in HEAD)"
-- > concat [ "[panic ", $$(gitBranch), "@", $$(gitHash)
-- > , " (", $$(gitCommitDate), ")"
-- > , " (", $$(gitCommitCount), " commits in HEAD)"
-- > , dirty, "] ", msg ]
-- > dirty | $(gitDirty) = " (uncommitted files present)"
-- > | otherwise = ""
-- > dirty | $$(gitDirty) = " (uncommitted files present)"
-- > | otherwise = ""
-- >
-- > main = panic "oh no!"
--
Expand Down Expand Up @@ -92,32 +92,39 @@ data IndexUsed = IdxUsed -- ^ The git index is used

-- | Return the hash of the current git commit, or @UNKNOWN@ if not in
-- a git repository
gitHash :: ExpQ
gitHash :: Q (TExp String)
gitHash =
stringE =<< runGit ["rev-parse", "HEAD"] "UNKNOWN" IdxNotUsed
stringT =<< runGit ["rev-parse", "HEAD"] "UNKNOWN" IdxNotUsed

-- | Return the branch (or tag) name of the current git commit, or @UNKNOWN@
-- if not in a git repository. For detached heads, this will just be
-- "HEAD"
gitBranch :: ExpQ
gitBranch :: Q (TExp String)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TExpQ String is another alias

gitBranch =
stringE =<< runGit ["rev-parse", "--abbrev-ref", "HEAD"] "UNKNOWN" IdxNotUsed
stringT =<< runGit ["rev-parse", "--abbrev-ref", "HEAD"] "UNKNOWN" IdxNotUsed

-- | Return @True@ if there are non-committed files present in the
-- repository
gitDirty :: ExpQ
gitDirty :: Q (TExp Bool)
gitDirty = do
output <- runGit ["status", "--porcelain"] "" IdxUsed
case output of
"" -> conE falseName
_ -> conE trueName
"" -> boolT False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just be [|| False ||]

_ -> boolT True

-- | Return the number of commits in the current head
gitCommitCount :: ExpQ
gitCommitCount :: Q (TExp String)
gitCommitCount =
stringE =<< runGit ["rev-list", "HEAD", "--count"] "UNKNOWN" IdxNotUsed
stringT =<< runGit ["rev-list", "HEAD", "--count"] "UNKNOWN" IdxNotUsed

-- | Return the commit date of the current head
gitCommitDate :: ExpQ
gitCommitDate :: Q (TExp String)
gitCommitDate =
stringE =<< runGit ["log", "HEAD", "-1", "--format=%cd"] "UNKNOWN" IdxNotUsed
stringT =<< runGit ["log", "HEAD", "-1", "--format=%cd"] "UNKNOWN" IdxNotUsed

stringT :: String -> Q (TExp String)
stringT s = TExp <$> stringE s

boolT :: Bool -> Q (TExp Bool)
boolT False = TExp <$> conE falseName
boolT True = TExp <$> conE trueName