skip to Main Content

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


  1. You can replace your git command with:

    git log -1 --format="format:%h %cs"
    

    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 from rev-parse right now
    • %cs gets the commit date, in short format (YYYY-MM-DD)
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search