Skip to content

Commit 9169a9c

Browse files
committed
Updated to use JGit now :)
1 parent 6fc7a8f commit 9169a9c

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def getLatestGitTag() {
5757
}
5858
}
5959

60-
6160
def getLatestGitVersion() {
6261
def result = "git describe --long --tags".execute().text.trim()
6362
if (result.empty) {
@@ -68,7 +67,6 @@ def getLatestGitVersion() {
6867
}
6968
}
7069

71-
7270
configurations {
7371
shadow
7472
}

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

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

3+
import org.eclipse.jgit.api.Git;
34
import org.eclipse.jgit.lib.Repository;
45
import org.eclipse.jgit.revwalk.RevCommit;
56
import org.eclipse.jgit.revwalk.RevWalk;
67
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
8+
import org.eclipse.jgit.transport.RefSpec;
79

810
import java.io.File;
911
import java.io.IOException;
12+
import java.util.List;
13+
import java.util.Optional;
1014

1115
public record GitVersion(String tag, String commits, String commit) {
1216
private static final GitVersion UNKNOWN = new GitVersion("0.0", "9999", "unknown");
1317

1418
public static GitVersion getGitVersion() {
1519
try {
1620
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);
40-
}
21+
try (Repository repository = builder.setGitDir(new File(".git")).readEnvironment().findGitDir().build();
22+
Git git = new Git(repository)) {
23+
24+
// Find latest tag
25+
List<RefSpec> tagRefs = git.tagList().call().stream()
26+
.map(ref -> new RefSpec(ref.getName()))
27+
.toList();
28+
29+
Optional<String> latestTag = tagRefs.stream()
30+
.map(ref -> ref.getSource().substring("refs/tags/".length()))
31+
.reduce((first, second) -> second); // Get the latest tag
32+
33+
if (latestTag.isEmpty()) return UNKNOWN; // No tags found
34+
35+
String tagName = latestTag.get();
36+
RevCommit taggedCommit = findTaggedCommit(repository, tagName);
37+
38+
// Count commits since the latest tag
39+
int commitCount = countCommitsSince(repository, taggedCommit);
40+
41+
// Get HEAD commit short hash
42+
String commitHash = repository.resolve("HEAD").getName().substring(0, 7);
43+
44+
return new GitVersion(tagName, String.valueOf(commitCount), commitHash);
4145
}
42-
} catch (IOException ignored) {}
46+
} catch (Exception ignored) {}
4347
return UNKNOWN;
4448
}
4549

50+
private static RevCommit findTaggedCommit(Repository repository, String tagName) throws IOException {
51+
try (RevWalk walk = new RevWalk(repository)) {
52+
return walk.parseCommit(repository.resolve("refs/tags/" + tagName));
53+
}
54+
}
55+
56+
private static int countCommitsSince(Repository repository, RevCommit taggedCommit) throws IOException {
57+
try (RevWalk walk = new RevWalk(repository)) {
58+
RevCommit headCommit = walk.parseCommit(repository.resolve("HEAD"));
59+
walk.markStart(headCommit);
60+
61+
int count = 0;
62+
for (RevCommit commit : walk) {
63+
if (commit.equals(taggedCommit)) break; // Stop at the tagged commit
64+
count++;
65+
}
66+
return count;
67+
}
68+
}
4669

4770
public boolean isUnknown() {
4871
return this == UNKNOWN;

0 commit comments

Comments
 (0)