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. 54
      build.gradle
  2. 2
      gradle.properties

@ -119,7 +119,7 @@ allprojects {
apply plugin: 'net.ltgt.errorprone'
apply from: "${rootDir}/gradle/versions.gradle"
version = rootProject.version
version = calculateVersion()
jacoco {
toolVersion = '0.8.8'
@ -1038,42 +1038,50 @@ def buildTime() {
return df.format(new Date())
}
// Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT
// with the git commit version.
@Memoized
def calculateVersion() {
String version = rootProject.version
if (version.endsWith("-SNAPSHOT")) {
version = version.replace("-SNAPSHOT", "-dev-" + getCheckedOutGitCommitHash())
// Regex pattern for basic calendar versioning, with provision to omit patch rev
def calVerPattern = ~/\d+\.\d+(\.\d+)?(-.*)?/
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 {
def gitFolder = "$projectDir/.git/"
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() + "/"
}
def takeFromHash = length
/*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash
* otherwise: a reference to a file containing the current commit hash
*/
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd
def head = new File(gitFolder + "HEAD").text.split(":")
def isCommit = head.length == 1
def commitHash, refHeadFile
if (isCommit) {
commitHash = head[0].trim().take(takeFromHash)
refHeadFile = new File(gitFolder + "HEAD")
} else {
refHeadFile = new File(gitFolder + head[1].trim())
commitHash = refHeadFile.text.trim().take(takeFromHash)
}
if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb
// 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)
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
refHead.text.trim().take takeFromHash
return [hash: commitHash, date: formattedDate]
} catch (Exception e) {
logger.warn('Could not calculate git commit, using "xxxxxxxx" (run with --info for stacktrace)')
logger.info('Error retrieving git commit', e)
return "xxxxxxxx"
logger.warn('Could not calculate git commit details, using defaults (run with --info for stacktrace)')
logger.info('Error retrieving git commit details', e)
return [hash: "xxxxxxxx", date: "00.0"]
}
}

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

Loading…
Cancel
Save