Skip to content

Instantly share code, notes, and snippets.

@ChrisAcrobat
Last active April 13, 2020 21:49
Show Gist options
  • Save ChrisAcrobat/f2a50096d9154b00859667807d1eba8a to your computer and use it in GitHub Desktop.
Save ChrisAcrobat/f2a50096d9154b00859667807d1eba8a to your computer and use it in GitHub Desktop.
Git and date based version name.
def checkIfTracked = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'status', '--porcelain'
standardOutput = stdout
}
return stdout.toString().trim().equals("") ? "" : "-MODIFIED"
}
def getGitCommitDate = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'show', '-s', '--format=%cd'
standardOutput = stdout
}
return new Date(stdout.toString().trim())
}
def getGitMonthCommitIndex = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', '--count', 'HEAD', '--since="' + getGitCommitDate().format("yyyy-MM") + '-01"', '--before="' + getGitCommitDate().format("yyyy-MM-dd") + '"'
standardOutput = stdout
}
return stdout.toString().trim()
}
def getGitBranch = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim().equals("master") ? "" : (stdout.toString().trim().equals("develop") ? "-beta" : "-" + stdout.toString().trim().replace("feature/", "").replace("/", "-"))
}
def getVersion = { ->
// Source: https://gist.github.com/ChrisAcrobat/f2a50096d9154b00859667807d1eba8a
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'show', '-s', '--format=%cd'
standardOutput = stdout
}
def monthCount = getGitMonthCommitIndex()
def branch = getGitBranch()
return getGitCommitDate().format("yy.MM") + "." + monthCount + branch + checkIfTracked()
}
version getVersion()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment