proposed "develop" build versions (#6699)

* proposed "develop" build versions

Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/6725/head
garyschulte 8 months ago committed by GitHub
parent 4cc6b744cf
commit 39a356f824
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 58
      build.gradle
  2. 2
      gradle.properties

@ -119,7 +119,7 @@ allprojects {
apply plugin: 'net.ltgt.errorprone' apply plugin: 'net.ltgt.errorprone'
apply from: "${rootDir}/gradle/versions.gradle" apply from: "${rootDir}/gradle/versions.gradle"
version = rootProject.version version = calculateVersion()
jacoco { jacoco {
toolVersion = '0.8.8' toolVersion = '0.8.8'
@ -1038,42 +1038,50 @@ def buildTime() {
return df.format(new Date()) return df.format(new Date())
} }
// Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT
// with the git commit version.
@Memoized @Memoized
def calculateVersion() { def calculateVersion() {
String version = rootProject.version // Regex pattern for basic calendar versioning, with provision to omit patch rev
if (version.endsWith("-SNAPSHOT")) { def calVerPattern = ~/\d+\.\d+(\.\d+)?(-.*)?/
version = version.replace("-SNAPSHOT", "-dev-" + getCheckedOutGitCommitHash())
if (project.hasProperty('version') && (project.version =~ calVerPattern)) {
return "${project.version}"
} else {
// If no version is supplied or it doesn't match the semantic versioning, calculate from git
println("Generating project version as supplied is version not semver: ${project.version}")
def gitDetails = getGitCommitDetails(10) // Adjust length as needed
return "${gitDetails.date}-develop-${gitDetails.hash}"
} }
return version
} }
def getCheckedOutGitCommitHash(length = 8) { def getGitCommitDetails(length = 8) {
try { try {
def gitFolder = "$projectDir/.git/" def gitFolder = "$projectDir/.git/"
if (!file(gitFolder).isDirectory()) { if (!file(gitFolder).isDirectory()) {
// We are in a submodule. The file's contents are `gitdir: <gitFolder>\n`.
// Read the file, cut off the front, and trim the whitespace.
gitFolder = file(gitFolder).text.substring(length).trim() + "/" gitFolder = file(gitFolder).text.substring(length).trim() + "/"
} }
def takeFromHash = length def takeFromHash = length
/* def head = new File(gitFolder + "HEAD").text.split(":")
* '.git/HEAD' contains either def isCommit = head.length == 1
* in case of detached head: the currently checked out commit hash
* otherwise: a reference to a file containing the current commit hash def commitHash, refHeadFile
*/ if (isCommit) {
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD commitHash = head[0].trim().take(takeFromHash)
def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd refHeadFile = new File(gitFolder + "HEAD")
} else {
if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb refHeadFile = new File(gitFolder + head[1].trim())
commitHash = refHeadFile.text.trim().take(takeFromHash)
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master }
refHead.text.trim().take takeFromHash
// Use head file modification time as a proxy for the build date
def lastModified = new Date(refHeadFile.lastModified())
// Format the date as "yy.M" (e.g. 24.3 for March 2024)
def formattedDate = new SimpleDateFormat("yy.M").format(lastModified)
return [hash: commitHash, date: formattedDate]
} catch (Exception e) { } catch (Exception e) {
logger.warn('Could not calculate git commit, using "xxxxxxxx" (run with --info for stacktrace)') logger.warn('Could not calculate git commit details, using defaults (run with --info for stacktrace)')
logger.info('Error retrieving git commit', e) logger.info('Error retrieving git commit details', e)
return "xxxxxxxx" return [hash: "xxxxxxxx", date: "00.0"]
} }
} }

@ -1,5 +1,3 @@
version=24.2.0-SNAPSHOT
org.gradle.welcome=never org.gradle.welcome=never
# Set exports/opens flags required by Google Java Format and ErrorProne plugins. (JEP-396) # Set exports/opens flags required by Google Java Format and ErrorProne plugins. (JEP-396)
org.gradle.jvmargs=-Xmx4g \ org.gradle.jvmargs=-Xmx4g \

Loading…
Cancel
Save