Skip to content

Commit 46d5938

Browse files
committed
Use git CLI for version metadata
1 parent e356496 commit 46d5938

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

version.gradle

Lines changed: 27 additions & 39 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'])
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()
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'])
6958
def fullDescribe = (describe =~/(.*?)-(\d+)-g$jmeShortGitHash/)
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 =~/v?(.*)/)[0][1] : 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

@@ -137,9 +126,8 @@ task configureVersionInfo {
137126
println("Build Suffix: ${jmeVersionTag}")
138127
println("Build Version: ${jmeFullVersion}")
139128

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-
}
129+
} catch (ex) {
130+
// Failed to get repo info
131+
logger.warn("Failed to get repository info: " + ex.message + ". " + \
132+
"Only partial build info will be generated.")
145133
}

0 commit comments

Comments
 (0)