Skip to content

Commit c331cfb

Browse files
aykevldevgianlu
authored andcommitted
feat: extract commit hash from build info
The Go toolchain stores the git commit hash in the binary, which can then be extracted using `debug.ReadBuildInfo`. The advantage is that the commit hash is present even on development builds (`go build` etc).
1 parent ad15f95 commit c331cfb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

version.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@ package go_librespot
33
import (
44
"fmt"
55
"runtime"
6+
"runtime/debug"
67
"strings"
78
)
89

910
var commit, version string
1011

12+
// Extract and return the commit hash stored in the binary, if available.
13+
func commitHash() string {
14+
if info, ok := debug.ReadBuildInfo(); ok {
15+
for _, setting := range info.Settings {
16+
if setting.Key == "vcs.revision" {
17+
return setting.Value
18+
}
19+
}
20+
}
21+
return ""
22+
}
23+
1124
func VersionNumberString() string {
1225
if len(version) > 0 {
1326
return strings.TrimPrefix(version, "v")
1427
} else if len(commit) >= 8 {
1528
return commit[:8]
29+
} else if commit := commitHash(); len(commit) >= 8 {
30+
return commit[:8]
1631
} else {
1732
return "dev"
1833
}
@@ -22,6 +37,8 @@ func SpotifyLikeClientVersion() string {
2237
if len(version) > 0 {
2338
if len(commit) >= 8 {
2439
return fmt.Sprintf("%s.g%s", version, commit[:8])
40+
} else if commit := commitHash(); len(commit) >= 8 {
41+
return fmt.Sprintf("%s.g%s", version, commit[:8])
2542
} else {
2643
return version
2744
}

0 commit comments

Comments
 (0)