I'm working to add a version number to a build process. For versions built on developer machines, I want to know whether the workspace contained changes that may have been included in the build, and suffix the SHA1 with -dirty
.
For example:
c2bc0d223739c841c5e810c6c439562aa9d67f5f
c2bc0d223739c841c5e810c6c439562aa9d67f5f-dirty
The first form can be obtained via:
git rev-parse HEAD
However I cannot work out how to append the -dirty
suffix when the workspace is dirty. git describe
provides this, but only on tag names. We don't really use tags (the last was 400 or so revisions ago) and they're not as conclusive as the SHA1.
Can this be done in a single command or will I need to do some bash scripting?
To find a git commit id (or hash), you can simply use the git log command. This would show you the commit history, listing the commits in chronological order, with the latest commit first.
You can use --match
with something that's never going to match to force describe to not match any tags. It's a bit hackish but it works:
git describe --match=NeVeRmAtCh --always --abbrev=40 --dirty
This worked for me. I took the accepted answer and found the bare minimum version that worked.
git describe --always --dirty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With