Skip to content
This repository was archived by the owner on Apr 4, 2026. It is now read-only.

Commit 20bb7f1

Browse files
authored
fix: multiple edge cases led to weird build issues when having no tags
1 parent 408ea97 commit 20bb7f1

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

build.gradle.kts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99
fun getLatestTag(): String {
1010
try {
11-
// fetch all tags (remote + local)
11+
// Fetch all tags
1212
ProcessBuilder("git", "fetch", "--tags")
1313
.redirectErrorStream(true)
1414
.start()
@@ -17,37 +17,38 @@ fun getLatestTag(): String {
1717
waitFor()
1818
}
1919

20-
// get current branch
2120
val branch = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD")
2221
.redirectErrorStream(true)
2322
.start()
2423
.inputStream
2524
.bufferedReader()
2625
.use { it.readText().trim() }
2726

28-
// get latest tag
27+
// Try to get latest tag
2928
val tagProcess = ProcessBuilder("git", "describe", "--tags", "--abbrev=0")
3029
.redirectErrorStream(true)
3130
.start()
32-
3331
val rawTag = tagProcess.inputStream.bufferedReader().use { it.readText().trim() }
3432
tagProcess.waitFor()
3533

36-
if (rawTag.isEmpty()) return "unknown"
34+
val hasTag = rawTag.isNotEmpty() && !rawTag.startsWith("fatal:")
35+
36+
// Always get commit hash (works even if no tag)
37+
val commitProcess = ProcessBuilder("git", "rev-parse", "--short", "HEAD")
38+
.redirectErrorStream(true)
39+
.start()
40+
val commit = commitProcess.inputStream.bufferedReader().use { it.readText().trim() }
41+
commitProcess.waitFor()
3742

38-
val tag = rawTag.removePrefix("v")
43+
// If no commit found (super rare, empty repo)
44+
if (commit.isEmpty()) return "unknown"
3945

40-
return if (branch == "release") {
41-
tag
46+
return if (hasTag) {
47+
val tag = rawTag.removePrefix("v")
48+
if (branch == "release") tag else "$tag+$commit"
4249
} else {
43-
// get short commit hash
44-
val commitProcess = ProcessBuilder("git", "rev-parse", "--short", "HEAD")
45-
.redirectErrorStream(true)
46-
.start()
47-
val commit = commitProcess.inputStream.bufferedReader().use { it.readText().trim() }
48-
commitProcess.waitFor()
49-
50-
"$tag+$commit"
50+
// no tag → default to 1.0.0 + commit
51+
"1.0.0+$commit"
5152
}
5253
} catch (e: Exception) {
5354
return "unknown"

0 commit comments

Comments
 (0)