Skip to content

Commit 83d874c

Browse files
committed
Simplify Versioning.groovy
1 parent e1b52cb commit 83d874c

File tree

2 files changed

+20
-72
lines changed

2 files changed

+20
-72
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ group = "com.github.malczuuu"
1313
* version. Otherwise, the version is derived from the git tag and build number.
1414
*/
1515
if (version == null || version == "unspecified") {
16-
version = Versioning.getVersion(rootProject.rootDir)
16+
version = Versioning.getSnapshotVersion(rootProject.rootDir)
1717
}
1818

1919
java {
@@ -80,7 +80,7 @@ spotless {
8080

8181
tasks.register("printVersion") {
8282
doLast {
83-
println "Project version: ${Versioning.getVersion(rootProject.rootDir)}"
83+
println "Project version: ${Versioning.getVersion(version)}"
8484
}
8585
}
8686

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,46 @@
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
51
import org.eclipse.jgit.lib.Repository
62
import org.eclipse.jgit.revwalk.RevCommit
7-
import org.eclipse.jgit.revwalk.RevTag
83
import org.eclipse.jgit.revwalk.RevWalk
94
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
105

116
/**
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.
138
*/
149
class Versioning {
1510

11+
private static final String UNSPECIFIED = "unspecified"
12+
1613
/**
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.
2120
*
2221
* @param projectRootDir the root directory of the project (containing the .git directory)
2322
* @return the version string
2423
*/
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()
2827
.setGitDir(new File(projectRootDir, ".git"))
2928
.readEnvironment()
3029
.findGitDir()
31-
.build()
32-
Git git = new Git(repository)
33-
) {
30+
31+
try (Repository repository = builder.build()) {
3432
def headId = repository.resolve("HEAD")
3533
if (headId == null) {
36-
System.err.println("HEAD not found in repository")
37-
return "0.0.0"
34+
return UNSPECIFIED
3835
}
3936

40-
RevCommit headCommit
4137
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)
6440
}
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}"
9341
} catch (Exception e) {
9442
System.err.println("Error determining version: " + e)
95-
return "0.0.0"
43+
return UNSPECIFIED
9644
}
9745
}
9846
}

0 commit comments

Comments
 (0)