43 lines
1 KiB
Go
43 lines
1 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package version holds the one version source of the binary. Release builds set Version with
|
|
// -ldflags "-X git.trevon.dev/trevon/hub/internal/version.Version=1.2.3" from the git tag (no leading v).
|
|
package version
|
|
|
|
import "runtime/debug"
|
|
|
|
// Version is the release version, or empty for a development build.
|
|
var Version = ""
|
|
|
|
// String returns the version to print: the stamped release version, else 0.0.0-dev with the short VCS
|
|
// revision when the Go toolchain recorded one (+dirty is appended for a modified tree).
|
|
func String() string {
|
|
if Version != "" {
|
|
return Version
|
|
}
|
|
return devVersion(debug.ReadBuildInfo)
|
|
}
|
|
|
|
func devVersion(read func() (*debug.BuildInfo, bool)) string {
|
|
out := "0.0.0-dev"
|
|
bi, ok := read()
|
|
if !ok {
|
|
return out
|
|
}
|
|
rev, dirty := "", false
|
|
for _, s := range bi.Settings {
|
|
switch s.Key {
|
|
case "vcs.revision":
|
|
rev = s.Value
|
|
case "vcs.modified":
|
|
dirty = s.Value == "true"
|
|
}
|
|
}
|
|
if len(rev) >= 7 {
|
|
out += "+" + rev[:7]
|
|
if dirty {
|
|
out += ".dirty"
|
|
}
|
|
}
|
|
return out
|
|
}
|