|
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 | 1 | import org.eclipse.jgit.lib.Repository |
6 | 2 | import org.eclipse.jgit.revwalk.RevCommit |
7 | | -import org.eclipse.jgit.revwalk.RevTag |
8 | 3 | import org.eclipse.jgit.revwalk.RevWalk |
9 | 4 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
10 | 5 |
|
11 | 6 | /** |
12 | | - * Utility class for determining the version of the project based on Git tags. |
| 7 | + * Utility class for determining the version of the project based on Git commit hash. |
13 | 8 | */ |
14 | 9 | class Versioning { |
15 | 10 |
|
| 11 | + private static final String UNSPECIFIED = "unspecified" |
| 12 | + |
16 | 13 | /** |
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>". |
| 14 | + * Get the version of the project. |
| 15 | + * - Returns the abbreviated commit hash of HEAD. |
| 16 | + * - On error → returns "unspecified". |
| 17 | + * |
| 18 | + * Proper version should be assigned in CI/CD pipeline for release builds by running Gradle tasks with |
| 19 | + * -Pversion=<version> property. |
21 | 20 | * |
22 | 21 | * @param projectRootDir the root directory of the project (containing the .git directory) |
23 | 22 | * @return the version string |
24 | 23 | */ |
25 | | - static String getVersion(File projectRootDir) { |
26 | | - try ( |
27 | | - Repository repository = new FileRepositoryBuilder() |
| 24 | + static String getSnapshotVersion(File projectRootDir) { |
| 25 | + def builder = |
| 26 | + new FileRepositoryBuilder() |
28 | 27 | .setGitDir(new File(projectRootDir, ".git")) |
29 | 28 | .readEnvironment() |
30 | 29 | .findGitDir() |
31 | | - .build() |
32 | | - Git git = new Git(repository) |
33 | | - ) { |
| 30 | + |
| 31 | + try (Repository repository = builder.build()) { |
34 | 32 | def headId = repository.resolve("HEAD") |
35 | 33 | if (headId == null) { |
36 | | - System.err.println("HEAD not found in repository") |
37 | | - return "0.0.0" |
| 34 | + return UNSPECIFIED |
38 | 35 | } |
39 | 36 |
|
40 | | - RevCommit headCommit |
41 | 37 | 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 | | - } |
| 38 | + RevCommit headCommit = revWalk.parseCommit(headId) |
| 39 | + return headCommit.getId().name().substring(0, 7) |
64 | 40 | } |
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 | 41 | } catch (Exception e) { |
94 | 42 | System.err.println("Error determining version: " + e) |
95 | | - return "0.0.0" |
| 43 | + return UNSPECIFIED |
96 | 44 | } |
97 | 45 | } |
98 | 46 | } |
0 commit comments