|
1 | 1 | package org.mangorage.mangobotgradle.util; |
2 | 2 |
|
| 3 | +import org.eclipse.jgit.api.Git; |
3 | 4 | import org.eclipse.jgit.lib.Repository; |
4 | 5 | import org.eclipse.jgit.revwalk.RevCommit; |
5 | 6 | import org.eclipse.jgit.revwalk.RevWalk; |
6 | 7 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
| 8 | +import org.eclipse.jgit.transport.RefSpec; |
7 | 9 |
|
8 | 10 | import java.io.File; |
9 | 11 | import java.io.IOException; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
10 | 14 |
|
11 | 15 | public record GitVersion(String tag, String commits, String commit) { |
12 | 16 | private static final GitVersion UNKNOWN = new GitVersion("0.0", "9999", "unknown"); |
13 | 17 |
|
14 | 18 | public static GitVersion getGitVersion() { |
15 | 19 | try { |
16 | 20 | 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); |
41 | 45 | } |
42 | | - } catch (IOException ignored) {} |
| 46 | + } catch (Exception ignored) {} |
43 | 47 | return UNKNOWN; |
44 | 48 | } |
45 | 49 |
|
| 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 | + } |
46 | 69 |
|
47 | 70 | public boolean isUnknown() { |
48 | 71 | return this == UNKNOWN; |
|
0 commit comments