Skip to content

Commit

Permalink
proposed "develop" build versions (#6699)
Browse files Browse the repository at this point in the history
* proposed "develop" build versions

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte authored Mar 13, 2024
1 parent 4cc6b74 commit 39a356f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
58 changes: 33 additions & 25 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb

def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
refHead.text.trim().take takeFromHash
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)
}

// 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) {
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"]
}
}

Expand Down
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down

0 comments on commit 39a356f

Please sign in to comment.