Skip to content

Commit caf1a1d

Browse files
committed
Use git CLI for version metadata
1 parent 1856c92 commit caf1a1d

2 files changed

Lines changed: 30 additions & 44 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: 30 additions & 43 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,18 @@ 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+
try {
2727
// Users can configure behavior by setting properties on the command
2828
// line:
2929
//
@@ -39,43 +39,32 @@ task configureVersionInfo {
3939
// Set to true if a non-master branch name should be included in the automatically
4040
// generated version.
4141

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
42+
def revisionOutput = gitOutput(['rev-list', '--count', 'HEAD'])
43+
jmeRevision = revisionOutput ? revisionOutput.toInteger() : 0
44+
jmeGitHash = gitOutput(['rev-parse', 'HEAD'])
45+
jmeShortGitHash = gitOutput(['rev-parse', '--short=7', 'HEAD'])
46+
jmeBranchName = gitOutput(['branch', '--show-current']) ?: gitOutput(['rev-parse', '--abbrev-ref', 'HEAD']) ?: "unknown"
4847

4948
// This code will find an exact-match tag if the current
5049
// commit is the same as the tag commit.
51-
jmeGitTag = grgit.tag.list().find { it.commit == head }
50+
jmeGitTag = gitOutput(['describe', '--tags', '--exact-match', 'HEAD'])
5251
def latestTag;
5352
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
5953
latestTag = jmeGitTag;
6054
} 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/)
55+
// If the commit is an exact match then this returns just the tag
56+
// name, else it returns tagName-commitCount-abbreviatedId.
57+
def describe = gitOutput(['describe', '--tags', '--abbrev=7', 'HEAD'])
58+
def fullDescribe = (describe =~/(.*?)-(\d+)-g${jmeShortGitHash}[0-9a-f]*/)
7059
latestTag = fullDescribe ? fullDescribe[0][1] : describe
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

@@ -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)