Skip to content

Commit 6fc7a8f

Browse files
committed
Updated to use JGit now :)
1 parent 4b6d575 commit 6fc7a8f

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ dependencies {
9292
compileOnly 'org.apache.groovy:groovy:4.0.14'
9393
compileOnly gradleApi()
9494

95+
compileOnly 'org.eclipse.jgit:org.eclipse.jgit:6.9.0.202403050737-r'
9596
compileOnly 'com.mattmalec:Pterodactyl4J:2.BETA_140'
9697
compileOnly 'com.google.code.gson:gson:2.10.1'
9798

9899
shadow 'com.mattmalec:Pterodactyl4J:2.BETA_140'
99100
shadow 'com.google.code.gson:gson:2.10.1'
101+
shadow 'org.eclipse.jgit:org.eclipse.jgit:6.9.0.202403050737-r'
100102
}
101103

102104
test {

src/main/java/org/mangorage/mangobotgradle/util/GitVersion.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
package org.mangorage.mangobotgradle.util;
22

3-
import java.io.BufferedReader;
3+
import org.eclipse.jgit.lib.Repository;
4+
import org.eclipse.jgit.revwalk.RevCommit;
5+
import org.eclipse.jgit.revwalk.RevWalk;
6+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
7+
8+
import java.io.File;
49
import java.io.IOException;
5-
import java.io.InputStreamReader;
610

711
public record GitVersion(String tag, String commits, String commit) {
812
private static final GitVersion UNKNOWN = new GitVersion("0.0", "9999", "unknown");
913

1014
public static GitVersion getGitVersion() {
1115
try {
12-
var process = Runtime.getRuntime().exec("git describe --long --tags");
13-
try (var isr = new InputStreamReader(process.getInputStream())) {
14-
try (var br = new BufferedReader(isr)) {
15-
var result = br.readLine().split("-");
16-
return new GitVersion(
17-
result[0],
18-
result[1],
19-
result[2]
20-
);
16+
FileRepositoryBuilder builder = new FileRepositoryBuilder();
17+
try (Repository repository = builder.setGitDir(new File(".git")).readEnvironment().findGitDir().build()) {
18+
try (RevWalk walk = new RevWalk(repository)) {
19+
RevCommit headCommit = walk.parseCommit(repository.resolve("HEAD"));
20+
String commitHash = headCommit.getName().substring(0, 7);
21+
22+
// Find the latest tag
23+
String tag = repository.getRefDatabase().getRefsByPrefix("refs/tags/")
24+
.stream()
25+
.map(ref -> ref.getName().substring("refs/tags/".length()))
26+
.reduce((first, second) -> second) // Get the latest tag
27+
.orElse("");
28+
29+
if (tag.isBlank())
30+
return UNKNOWN;
31+
32+
// Count commits since the tag
33+
int commitCount = 0;
34+
walk.markStart(headCommit);
35+
for (RevCommit commit : walk) {
36+
commitCount++;
37+
}
38+
39+
return new GitVersion(tag, String.valueOf(commitCount), commitHash);
2140
}
2241
}
2342
} catch (IOException ignored) {}
2443
return UNKNOWN;
2544
}
2645

46+
2747
public boolean isUnknown() {
2848
return this == UNKNOWN;
2949
}

0 commit comments

Comments
 (0)