forked from git-bug/git-bug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
94 lines (79 loc) · 2.22 KB
/
Copy pathversion.go
File metadata and controls
94 lines (79 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"runtime/debug"
"strings"
"github.com/git-bug/git-bug/commands"
"golang.org/x/mod/semver"
)
// version is injected at build time with `-X main.version=<version>`. See
// //:.goreleaser.yaml for releases, and //:Makefile for local builds.
var version = "undefined"
// getVersion returns the version information embedded in the binary, in the
// format documented by `git-bug version`:
//
// <version> [commit[/dirty]] <compiler version> <platform> <arch>
//
// Only <version> has to be injected at build time. Everything else comes from
// the build metadata that go stamps into the binary on its own, which also
// covers binaries built with `go install`.
//
// TODO: support notifying the user if their version is out of date
func getVersion() string {
// this supports overriding the default version if the deprecated var used
// for setting the exact version for releases is supplied. we are doing this
// in order to give downstream package maintainers a longer window to
// migrate.
//
// TODO: 0.12.0: remove support for old build tags
if version == "undefined" && commands.GitExactTag != "" {
version = commands.GitExactTag
}
// add the v prefix if it's missing, then fall back to "undefined" if what
// we are left with isn't a real version. this guards against whatever a
// downstream packager might inject.
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if !semver.IsValid(version) {
version = "undefined"
}
var v strings.Builder
v.WriteString(version)
info, ok := debug.ReadBuildInfo()
if !ok {
v.WriteString(" (no build info)\n")
return v.String()
}
var arch, commit, platform string
var modified bool
for _, kv := range info.Settings {
switch kv.Key {
case "GOOS":
platform = kv.Value
case "GOARCH":
arch = kv.Value
case "vcs.modified":
modified = kv.Value == "true"
case "vcs.revision":
commit = kv.Value
}
}
if commit != "" {
if len(commit) > 12 {
commit = commit[:12]
}
v.WriteString(" " + commit)
if modified {
v.WriteString("/dirty")
}
}
v.WriteString(" " + info.GoVersion)
if platform != "" {
v.WriteString(" " + platform)
}
if arch != "" {
v.WriteString(" " + arch)
}
v.WriteString("\n")
return v.String()
}