Skip to content

Commit 079c172

Browse files
committed
refactor git version computation to handle detached checkouts better
1 parent 9697a14 commit 079c172

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

settings.gradle.kts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ val gitDescribe: Provider<String> =
6565
"describe",
6666
"--always",
6767
"--tags",
68-
"--dirty=-SNAPSHOT",
69-
"--broken=-SNAPSHOT",
68+
"--dirty=-DIRTY",
69+
"--broken=-BROKEN",
7070
"--match=v[0-9]*\\.[0-9]*\\.[0-9]*",
7171
)
7272
isIgnoreExitValue = true
@@ -96,21 +96,21 @@ val currentCommitHash: Provider<String> =
9696
isIgnoreExitValue = true
9797
}.standardOutput.asText.map { it.trim() }
9898

99+
val semverRegex = Regex("""v[0-9]+\.[0-9]+\.[0-9]+""")
100+
99101
val gitVersion: Provider<String> =
100102
gitDescribe.zip(currentBranchName) { described, branch ->
101-
val snapshot = if ("SNAPSHOT" in described) "-SNAPSHOT" else ""
102-
103-
val descriptions = described.split("-")
103+
val detached = branch.isNullOrBlank()
104104

105-
if (branch.isNullOrBlank() && descriptions.size in 1..2) {
106-
// detached if there's no current branch, or if 'git described' indicates additional commits
107-
val tag = descriptions.getOrNull(0)
108-
"$tag$snapshot"
105+
if (!detached) {
106+
"$branch-SNAPSHOT"
109107
} else {
110-
// current commit is attached
111-
"$branch$snapshot"
108+
val descriptions = described.split("-")
109+
val head = descriptions.singleOrNull() ?: ""
110+
val headIsVersioned = head.matches(semverRegex)
111+
if (headIsVersioned) head else "$branch-SNAPSHOT"
112112
}
113-
}.orElse(currentCommitHash)
113+
}.orElse(currentCommitHash) // fall back to using the git commit hash
114114

115115
gradle.allprojects {
116116
extensions.add<Provider<String>>("gitVersion", gitVersion)

0 commit comments

Comments
 (0)