mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-27 00:31:55 -06:00
317b1018a3
If I didn't mess up the workflow, this should remove some ugliness from the version string (e.g. 0.13.2+57ddbe0.57ddbe0, dupe commit hash, as a result of https://github.com/matrix-org/dendrite/pull/3147)
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime/debug"
|
|
"strings"
|
|
)
|
|
|
|
// the final version string
|
|
var version string
|
|
|
|
// -ldflags "-X github.com/matrix-org/dendrite/internal.branch=master"
|
|
var branch string
|
|
|
|
// -ldflags "-X github.com/matrix-org/dendrite/internal.build=alpha"
|
|
var build string
|
|
|
|
const (
|
|
VersionMajor = 0
|
|
VersionMinor = 13
|
|
VersionPatch = 4
|
|
VersionTag = "" // example: "rc1"
|
|
|
|
gitRevLen = 7 // 7 matches the displayed characters on github.com
|
|
)
|
|
|
|
func VersionString() string {
|
|
return version
|
|
}
|
|
|
|
func init() {
|
|
version = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
|
|
if VersionTag != "" {
|
|
version += "-" + VersionTag
|
|
}
|
|
parts := []string{}
|
|
if build != "" {
|
|
parts = append(parts, build)
|
|
}
|
|
if branch != "" {
|
|
parts = append(parts, branch)
|
|
}
|
|
|
|
defer func() {
|
|
if len(parts) > 0 {
|
|
version += "+" + strings.Join(parts, ".")
|
|
}
|
|
}()
|
|
|
|
// Try to get the revision Dendrite was build from.
|
|
// If we can't, e.g. Dendrite wasn't built (go run) or no VCS version is present,
|
|
// we just use the provided version above.
|
|
info, ok := debug.ReadBuildInfo()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
for _, setting := range info.Settings {
|
|
if setting.Key == "vcs.revision" {
|
|
revLen := len(setting.Value)
|
|
if revLen >= gitRevLen {
|
|
parts = append(parts, setting.Value[:gitRevLen])
|
|
} else {
|
|
parts = append(parts, setting.Value[:revLen])
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|