Git commit date as versionName

Issue

I currently use this to get my commit hash as my versionName. Is there a way to get the commit date and add it to this:

 def getCommitHash = { ->
 def stdout = new ByteArrayOutputStream()
 exec {
     commandLine 'git', 'rev-parse', '--short', 'HEAD'
     standardOutput = stdout
 }
 return stdout.toString().trim()

}

So that I get something like this: Version: 491a9d0, Date: 7-10-2022

Solution

Git provides very flexible configuration to format pretty-printing. You could just use a different git command:

git show -s --format="Version: %H, Date: %ci" HEAD

which would output something like this:

Version: e6b12a79136b513cdca7fd12915dd422f8a3141e, Date: 2022-10-06 18:27:38 +0100

Or in your case, to feed it to whatever’s running git,

commandLine 'git', 'show', '-s', "--format=Version: %H, Date: %ci", 'HEAD'

The documentation for git show contains more info on how to use placeholders in the format option.

Edit: Swapped the annonations

Answered By – mijiturka

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published