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
2
Answers
You can replace your git command with:
The possible options for the format string are given in the git docs.
%h
gets the short version of the commit hash, which is equivalent to what you’re getting fromrev-parse
right now%cs
gets the commit date, in short format (YYYY-MM-DD)Git provides very flexible configuration to format pretty-printing. You could just use a different git command:
which would output something like this:
Or in your case, to feed it to whatever’s running git,
The documentation for git show contains more info on how to use placeholders in the format option.
Edit: Swapped the annonations