|
| 1 | +import org.eclipse.jgit.api.Git |
| 2 | +import org.eclipse.jgit.errors.IncorrectObjectTypeException |
| 3 | +import org.eclipse.jgit.lib.ObjectId |
| 4 | +import org.eclipse.jgit.lib.Ref |
| 5 | +import org.eclipse.jgit.lib.Repository |
| 6 | +import org.eclipse.jgit.revwalk.RevCommit |
| 7 | +import org.eclipse.jgit.revwalk.RevTag |
| 8 | +import org.eclipse.jgit.revwalk.RevWalk |
| 9 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
| 10 | + |
| 11 | +/** |
| 12 | + * Utility class for determining the version of the project based on Git tags. |
| 13 | + */ |
| 14 | +class Versioning { |
| 15 | + |
| 16 | + /** |
| 17 | + * Get the version of the project based on Git tags. |
| 18 | + * - If HEAD has a tag → return that tag. |
| 19 | + * - Otherwise → return latest tag by commit history + abbreviated commit hash. |
| 20 | + * - If no tags exist → return "0.0.0-<abbreviatedHash>". |
| 21 | + * |
| 22 | + * @param projectRootDir the root directory of the project (containing the .git directory) |
| 23 | + * @return the version string |
| 24 | + */ |
| 25 | + static String getVersion(File projectRootDir) { |
| 26 | + try ( |
| 27 | + Repository repository = new FileRepositoryBuilder() |
| 28 | + .setGitDir(new File(projectRootDir, ".git")) |
| 29 | + .readEnvironment() |
| 30 | + .findGitDir() |
| 31 | + .build() |
| 32 | + Git git = new Git(repository) |
| 33 | + ) { |
| 34 | + def headId = repository.resolve("HEAD") |
| 35 | + if (headId == null) { |
| 36 | + System.err.println("HEAD not found in repository") |
| 37 | + return "0.0.0" |
| 38 | + } |
| 39 | + |
| 40 | + RevCommit headCommit |
| 41 | + try (RevWalk revWalk = new RevWalk(repository)) { |
| 42 | + headCommit = revWalk.parseCommit(headId) |
| 43 | + } |
| 44 | + |
| 45 | + def tags = git.tagList().call() |
| 46 | + if (tags.isEmpty()) { |
| 47 | + def hash = headCommit.id.name().substring(0, 7) |
| 48 | + return "0.0.0-${hash}" |
| 49 | + } |
| 50 | + |
| 51 | + // Check if HEAD is exactly on a tag |
| 52 | + Ref tagOnHead = tags.find { |
| 53 | + try (RevWalk revWalk = new RevWalk(repository)) { |
| 54 | + ObjectId commitId |
| 55 | + try { |
| 56 | + RevTag revTag = revWalk.parseTag(it.objectId) |
| 57 | + commitId = revTag.getObject().getId() |
| 58 | + } catch (IncorrectObjectTypeException ignored) { |
| 59 | + // Not a tag object, likely a lightweight tag |
| 60 | + commitId = it.getObjectId() |
| 61 | + } |
| 62 | + return commitId == headCommit.id |
| 63 | + } |
| 64 | + } |
| 65 | + if (tagOnHead != null) { |
| 66 | + return tagOnHead.getName().replaceAll("refs/tags/", "") |
| 67 | + } |
| 68 | + |
| 69 | + // Map each tag to the commit it points to |
| 70 | + Map<Ref, RevCommit> tagToCommit = [:] |
| 71 | + try (RevWalk revWalk = new RevWalk(repository)) { |
| 72 | + tags.each { |
| 73 | + def commitId = it.getPeeledObjectId() ?: it.getObjectId() |
| 74 | + RevCommit commit = revWalk.parseCommit(commitId) |
| 75 | + tagToCommit[it] = commit |
| 76 | + } |
| 77 | + |
| 78 | + // Walk history from HEAD backwards |
| 79 | + revWalk.markStart(headCommit) |
| 80 | + for (RevCommit c : revWalk) { |
| 81 | + def tagRef = tagToCommit.find { it.getValue() == c }?.getKey() |
| 82 | + if (tagRef != null) { |
| 83 | + def tagName = tagRef.getName().replaceAll("refs/tags/", "") |
| 84 | + def abbrevHash = headCommit.getId().name().substring(0, 7) |
| 85 | + return "${tagName}-${abbrevHash}" |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Fallback if no tag found in history |
| 91 | + def fallbackHash = headCommit.getId().name().substring(0, 7) |
| 92 | + return "0.0.0-${fallbackHash}" |
| 93 | + } catch (Exception e) { |
| 94 | + System.err.println("Error determining version: " + e) |
| 95 | + return "0.0.0" |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments