Skip to content

Commit 4e18b18

Browse files
authored
Use git CLI for version metadata (#2709)
1 parent c46a9bb commit 4e18b18

2 files changed

Lines changed: 32 additions & 46 deletions

File tree

gradle/libs.versions.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ androidx-lifecycle-common = "androidx.lifecycle:lifecycle-common:2.7.0"
1616
android-build-gradle = "com.android.tools.build:gradle:9.1.0"
1717
android-support-appcompat = "com.android.support:appcompat-v7:28.0.0"
1818
androidx-test-runner = "androidx.test:runner:1.7.0"
19-
gradle-git = "org.ajoberstar:gradle-git:1.2.0"
2019
groovy-test = "org.apache.groovy:groovy-test:4.0.31"
2120
gson = "com.google.code.gson:gson:2.13.2"
2221
j-ogg-vorbis = "com.github.stephengold:j-ogg-vorbis:1.0.6"

version.gradle

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11

22
import java.text.SimpleDateFormat
3-
import org.ajoberstar.grgit.*
4-
5-
buildscript {
6-
repositories {
7-
mavenCentral()
8-
}
9-
dependencies {
10-
classpath libs.gradle.git
11-
}
12-
}
133

144
ext {
155
jmeRevision = 0
@@ -22,8 +12,22 @@ ext {
2212
jmeVersionTag="SNAPSHOT"
2313
}
2414

25-
task configureVersionInfo {
26-
try {
15+
def gitOutput = { List<String> args ->
16+
def output = providers.exec {
17+
commandLine(['git'] + args)
18+
ignoreExitValue = true
19+
}
20+
if (output.result.get().exitValue == 0) {
21+
return output.standardOutput.asText.get().trim()
22+
}
23+
return ""
24+
}
25+
26+
def normalizeBranchName = { String branchName ->
27+
branchName && branchName != "HEAD" ? branchName : "unknown"
28+
}
29+
30+
try {
2731
// Users can configure behavior by setting properties on the command
2832
// line:
2933
//
@@ -39,43 +43,28 @@ task configureVersionInfo {
3943
// Set to true if a non-master branch name should be included in the automatically
4044
// generated version.
4145

42-
def grgit = Grgit.open(project.file('.'))
43-
def head = grgit.head()
44-
jmeRevision = grgit.log(includes: [head]).size()
45-
jmeGitHash = head.id
46-
jmeShortGitHash = head.abbreviatedId
47-
jmeBranchName = grgit.branch.current.name
46+
def revisionOutput = gitOutput(['rev-list', '--count', 'HEAD'])
47+
jmeRevision = revisionOutput ? revisionOutput.toInteger() : 0
48+
jmeGitHash = gitOutput(['rev-parse', 'HEAD'])
49+
jmeShortGitHash = gitOutput(['rev-parse', '--short=7', 'HEAD'])
50+
jmeBranchName = normalizeBranchName(gitOutput(['branch', '--show-current']) ?: gitOutput(['rev-parse', '--abbrev-ref', 'HEAD']))
4851

4952
// This code will find an exact-match tag if the current
5053
// commit is the same as the tag commit.
51-
jmeGitTag = grgit.tag.list().find { it.commit == head }
54+
jmeGitTag = gitOutput(['describe', '--tags', '--exact-match', 'HEAD'])
5255
def latestTag;
5356
if( jmeGitTag ) {
54-
// Just use the name. We keep jmeGitTag separate because there
55-
// is some logic that wants to know if this specific commit has
56-
// a tag versus 'whatever tag we are a child of'... which is what
57-
// 'latestTag' will be.
58-
jmeGitTag = jmeGitTag.name
5957
latestTag = jmeGitTag;
6058
} else {
61-
// Use describe to find the most recent tag. Unfortunately,
62-
// in this version of grgit, we don't have the 'always' options
63-
// so we can't make as many assumptions about the format of the
64-
// string.
65-
// If the commit is an exact match then it will return just the
66-
// tag name... else it will be tagName-commitCount-abbreviatedId
67-
// We'll use some groovy regex magic to get the tag either way.
68-
def describe = grgit.describe()
69-
def fullDescribe = (describe =~/(.*?)-(\d+)-g$jmeShortGitHash/)
70-
latestTag = fullDescribe ? fullDescribe[0][1] : describe
59+
latestTag = gitOutput(['describe', '--tags', '--abbrev=0', 'HEAD'])
7160
println "Latest tag:" + latestTag
7261
}
7362

7463
// We could enhance this with some more regex if we wanted to sanity
7564
// check that it was formatted like our versions.
76-
def tagVersion = (latestTag =~/v?(.*)/)[0][1];
65+
def tagVersion = latestTag ? latestTag.replaceFirst(/^v/, "") : jmeVersion;
7766
// If the branch is not master then use the tag.
78-
if( jmeBranchName != "master" ) {
67+
if( jmeBranchName != "master" && latestTag ) {
7968
jmeVersion = tagVersion
8069
}
8170

@@ -97,9 +86,8 @@ task configureVersionInfo {
9786
// real CI builds should be using non-SNAPSHOT versions so we may
9887
// eventually want to change the script to always use -SNAPSHOT with
9988
// a CI option to turn it off.
100-
// We could also check the grgit.status for unstaged modified/removed files.
101-
// def unstaged = grgit.status().unstaged;
102-
// def modCount = unstaged.modified.size() + unstaged.removed.size()
89+
// We could also check for unstaged modified/removed files.
90+
// def modCount = ...
10391
// ...but that seems like a wasteful check considering only official
10492
// version builds should not have a -SNAPSHOT.
10593

@@ -113,7 +101,7 @@ task configureVersionInfo {
113101
println "Auto-detecting version"
114102
jmeVersionTag = "SNAPSHOT"
115103

116-
if( includeBranchInVersion == "true" && jmeBranchName != "master" ) {
104+
if( includeBranchInVersion == "true" && jmeBranchName != "master" && jmeBranchName != "unknown" ) {
117105
jmeFullVersion = baseVersion + "-" + jmeBranchName + "-" + jmeVersionTag;
118106
} else {
119107
jmeFullVersion = baseVersion + "-" + jmeVersionTag;
@@ -137,9 +125,8 @@ task configureVersionInfo {
137125
println("Build Suffix: ${jmeVersionTag}")
138126
println("Build Version: ${jmeFullVersion}")
139127

140-
} catch (ex) {
141-
// Failed to get repo info
142-
logger.warn("Failed to get repository info: " + ex.message + ". " + \
143-
"Only partial build info will be generated.")
144-
}
128+
} catch (ex) {
129+
// Failed to get repo info
130+
logger.warn("Failed to get repository info: " + ex.message + ". " + \
131+
"Only partial build info will be generated.")
145132
}

0 commit comments

Comments
 (0)