Skip to content

Commit 79d787b

Browse files
Use jgit for resolving version (#777)
1 parent dfb52d0 commit 79d787b

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

build.gradle

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ buildscript {
99
classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0'
1010
classpath 'org.ow2.asm:asm:6.2.1'
1111
classpath 'org.ow2.asm:asm-tree:6.2.1'
12+
classpath 'org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r'
1213
}
1314
}
1415

@@ -20,8 +21,7 @@ apply plugin: "com.gradle.plugin-publish"
2021
apply plugin: 'license'
2122

2223
group = 'net.minecraftforge.gradle'
23-
version = '2.3-SNAPSHOT'
24-
//version = '2.2.1'
24+
version = gitVersion()
2525
archivesBaseName = 'ForgeGradle'
2626
targetCompatibility = '1.8'
2727
sourceCompatibility = '1.8'
@@ -426,7 +426,51 @@ file('build').mkdirs()
426426
file('build/version.txt').text = version;
427427

428428
def getGitHash() {
429-
def process = 'git rev-parse --short HEAD'.execute()
430-
process.waitFor()
431-
return '-' + (process.exitValue() ? 'unknown' : process.text.trim())
429+
return '-' + gitInfo(rootProject.file('.')).hash
430+
}
431+
432+
def gitInfo(dir) {
433+
String.metaClass.rsplit = { String del, int limit = -1 ->
434+
def lst = new ArrayList()
435+
def x = 0, idx
436+
def tmp = delegate
437+
while ((idx = tmp.lastIndexOf(del)) != -1 && (limit == -1 || x++ < limit)) {
438+
lst.add(0, tmp.substring(idx + del.length(), tmp.length()))
439+
tmp = tmp.substring(0, idx)
440+
}
441+
lst.add(0, tmp)
442+
return lst
443+
}
444+
445+
def git = null
446+
try {
447+
git = org.eclipse.jgit.api.Git.open(dir)
448+
} catch (org.eclipse.jgit.errors.RepositoryNotFoundException e) {
449+
return [
450+
tag: '0.0',
451+
offset: '0',
452+
hash: '00000000',
453+
branch: 'master',
454+
commit: '0000000000000000000000',
455+
abbreviatedId: '00000000'
456+
]
457+
}
458+
def desc = git.describe().setLong(true).setTags(true).call().rsplit('-', 2)
459+
def head = git.repository.exactRef('HEAD')
460+
def longBranch = head.symbolic ? head?.target?.name : null // matches Repository.getFullBranch() but returning null when on a detached HEAD
461+
462+
def ret = [:]
463+
ret.tag = desc[0]
464+
ret.offset = desc[1]
465+
ret.hash = desc[2]
466+
ret.branch = longBranch != null ? org.eclipse.jgit.lib.Repository.shortenRefName(longBranch) : null
467+
ret.commit = org.eclipse.jgit.lib.ObjectId.toString(head.objectId)
468+
ret.abbreviatedId = head.objectId.abbreviate(8).name()
469+
470+
return ret
471+
}
472+
473+
def gitVersion() {
474+
def info = gitInfo(rootProject.file('.'))
475+
return "${info.tag}.${info.offset}".toString()
432476
}

0 commit comments

Comments
 (0)