-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
98 lines (91 loc) · 2.51 KB
/
Copy pathversion.go
File metadata and controls
98 lines (91 loc) · 2.51 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
95
96
97
98
package main
import (
_ "embed"
"regexp"
"runtime/debug"
"strings"
"time"
)
//go:embed VERSION
var versionFile string
// releaseVersionRe matches a tagged module release (optionally with a
// pre-release suffix). Pseudo-versions (vX.Y.Z-yyyymmddhhmmss-abcdef) are
// rejected by isReleaseVersion.
var releaseVersionRe = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$`)
// isReleaseVersion reports whether s looks like a real release tag rather
// than a Go module pseudo-version or "(devel)".
//
// Pseudo-versions look like:
//
// vX.Y.Z-yyyymmddhhmmss-abcdefabcdef
// vX.Y.Z-0.yyyymmddhhmmss-abcdefabcdef
// vX.Y.Z-N.yyyymmddhhmmss-abcdefabcdef
func isReleaseVersion(s string) bool {
if !releaseVersionRe.MatchString(s) {
return false
}
// A 14-digit UTC timestamp anywhere after the first hyphen marks a
// pseudo-version (see https://go.dev/ref/mod#pseudo-versions).
for i := 0; i+14 <= len(s); i++ {
if s[i] < '0' || s[i] > '9' {
continue
}
ok := true
for j := 0; j < 14; j++ {
if s[i+j] < '0' || s[i+j] > '9' {
ok = false
break
}
}
if ok {
return false
}
}
return true
}
// applyBuildIdentity fills version/commit/date when ldflags did not
// (plain `go build` / `go install`). Prefer, in order:
// 1. -ldflags -X main.version=... (goreleaser, nix)
// 2. Go module version when it is a real release tag (go install @vX.Y.Z)
// 3. embedded VERSION file (repo source of truth, without "v" prefix)
//
// Keep VERSION in sync with the latest release tag (v0.2.1 → 0.2.1).
func applyBuildIdentity() {
if bi, ok := debug.ReadBuildInfo(); ok {
if commit == "" || date == "" {
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
if commit == "" {
commit = s.Value
}
case "vcs.time":
if date == "" {
if t, err := time.Parse(time.RFC3339, s.Value); err == nil {
date = t.UTC().Format("2006-01-02")
} else {
date = s.Value
}
}
}
}
}
// go install github.com/…@v1.2.3 → Main.Version is "v1.2.3".
// Local checkouts often report pseudo-versions; ignore those so the
// embedded VERSION file remains the identity for source builds.
if (version == "" || version == "dev") && isReleaseVersion(bi.Main.Version) {
version = bi.Main.Version
}
}
if version == "" || version == "dev" {
v := strings.TrimSpace(versionFile)
v = strings.TrimPrefix(v, "v")
if v != "" {
version = "v" + v
}
}
// Shorten full SHAs for display.
if len(commit) > 12 {
commit = commit[:12]
}
}