Fix icingadb --version output when using older Git versions

When building Icinga DB from a tarball generated using `git archive` using a
Git version older than 2.32, the version output could incorrectly state "Icinga
DB version: %(describe)". This commit detects this situation and implements a
fallback.
This commit is contained in:
Julian Brost 2022-05-24 17:49:52 +02:00
parent 64aca11bcb
commit cd78d96f04

View file

@ -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]