diff --git a/pkg/version/version.go b/pkg/version/version.go index 1c28fae4..5135fdcb 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -29,7 +29,19 @@ type VersionInfo struct { // When exported using `git archive`, the placeholders are replaced in the file and this version information is // preferred. Otherwise the hardcoded version is used and augmented with commit information from the build metadata. func Version(version, gitDescribe, gitHash string) *VersionInfo { + const hashLen = 7 // Same truncation length for the commit hash as used by git describe. + if !strings.HasPrefix(gitDescribe, "$") && !strings.HasPrefix(gitHash, "$") { + if strings.HasPrefix(gitDescribe, "%") { + // Only Git 2.32+ supports %(describe), older versions don't expand it but keep it as-is. + // Fall back to the hardcoded version augmented with the commit hash. + gitDescribe = version + + if len(gitHash) >= hashLen { + gitDescribe += "-g" + gitHash[:hashLen] + } + } + return &VersionInfo{ Version: gitDescribe, Commit: gitHash, @@ -49,8 +61,6 @@ func Version(version, gitDescribe, gitHash string) *VersionInfo { } } - const hashLen = 7 // Same truncation length for the commit hash as used by git describe. - if len(commit) >= hashLen { version += "-g" + commit[:hashLen]