Skip to content

Commit 4c70baa

Browse files
committed
UPSTREAM: <carry>: consider "" unset so build-info can fill commit/date
1 parent 05a5f82 commit 4c70baa

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

internal/shared/version/version.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,26 @@ var (
1717
}
1818
)
1919

20+
// isUnset returns true when the provided string should be treated as an
21+
// "unset" value. Builds that inject ldflags such as "-X var=" will set the
22+
// variable to the empty string, which previously prevented the runtime build
23+
// information gathered via debug.ReadBuildInfo from populating the field. For
24+
// the purposes of version reporting we treat both the empty string and the
25+
// literal "unknown" as unset.
26+
func isUnset(s string) bool {
27+
return s == "" || s == "unknown"
28+
}
29+
2030
func String() string {
21-
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState)
31+
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q",
32+
valueOrUnknown(version), valueOrUnknown(gitCommit), valueOrUnknown(commitDate), valueOrUnknown(repoState))
33+
}
34+
35+
func valueOrUnknown(v string) string {
36+
if v == "" {
37+
return "unknown"
38+
}
39+
return v
2240
}
2341

2442
func init() {
@@ -29,18 +47,20 @@ func init() {
2947
for _, setting := range info.Settings {
3048
switch setting.Key {
3149
case "vcs.revision":
32-
if gitCommit == "unknown" {
50+
if isUnset(gitCommit) {
3351
gitCommit = setting.Value
3452
}
3553
case "vcs.time":
36-
commitDate = setting.Value
54+
if isUnset(commitDate) {
55+
commitDate = setting.Value
56+
}
3757
case "vcs.modified":
3858
if v, ok := stateMap[setting.Value]; ok {
3959
repoState = v
4060
}
4161
}
4262
}
43-
if version == "unknown" {
63+
if isUnset(version) {
4464
version = info.Main.Version
4565
}
4666
}

0 commit comments

Comments
 (0)