Skip to content

Commit fe47c03

Browse files
authored
*: v1.6.0-rc5 cherry-picking (#3931)
Cherry-picking for v1.6.0-rc5. category: misc ticket: none
1 parent 32ce96b commit fe47c03

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

app/version/version.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"regexp"
9+
"runtime/debug"
910
"strconv"
1011

1112
"github.com/obolnetwork/charon/app/errors"
@@ -22,9 +23,9 @@ var version = "v1.6-rc"
2223
var Version, _ = Parse(version) // Error is caught in tests.
2324

2425
var (
25-
// These variables are populated with build information via -ldflags.
26-
vcsRevision = "unknown"
27-
vcsTime = "unknown"
26+
// These variables are populated with build information via -ldflags when binaries are built, but not in Dockerfile.
27+
vcsRevision = ""
28+
vcsTime = ""
2829
)
2930

3031
// Supported returns the supported minor versions in order of precedence.
@@ -42,7 +43,33 @@ func Supported() []SemVer {
4243

4344
// GitCommit returns the git commit hash and timestamp from build info.
4445
func GitCommit() (hash string, timestamp string) {
45-
return vcsRevision, vcsTime
46+
if vcsRevision != "" {
47+
return vcsRevision, vcsTime
48+
}
49+
50+
hash, timestamp = "unknown", "unknown"
51+
hashLen := 7
52+
53+
info, ok := debug.ReadBuildInfo()
54+
if !ok {
55+
return hash, timestamp
56+
}
57+
58+
for _, s := range info.Settings {
59+
switch s.Key {
60+
case "vcs.revision":
61+
if len(s.Value) < hashLen {
62+
hashLen = len(s.Value)
63+
}
64+
65+
hash = s.Value[:hashLen]
66+
case "vcs.time":
67+
timestamp = s.Value
68+
default:
69+
}
70+
}
71+
72+
return hash, timestamp
4673
}
4774

4875
// LogInfo logs charon version information along-with the provided message.

0 commit comments

Comments
 (0)