Repository navigation
fix(version): print toolchain vcs commit when ldflags are empty #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a129955
fix(version): print toolchain vcs commit when ldflags are empty
7c8ee5a
Merge branch 'main' into fix/version-vcs-stamp
toasterbook88 97c1c1d
fix(version): mark a dirty revision and keep the build clock
4095fde
docs(version): state the commit line and test the live resolver
d9f154b
merge origin/main into fix/version-vcs-stamp
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,61 @@ | ||
| package buildinfo | ||
|
|
||
| import "runtime/debug" | ||
|
|
||
| // Commit, Date, and GoVersion are set at build time via -ldflags. | ||
| // Example: | ||
| // | ||
| // go build -ldflags "-X github.com/toasterbook88/axis/internal/buildinfo.Commit=abc1234 | ||
| // -X github.com/toasterbook88/axis/internal/buildinfo.Date=2026-04-01T12:00:00Z | ||
| // -X github.com/toasterbook88/axis/internal/buildinfo.GoVersion=go1.26.2" | ||
| // | ||
| // When the commit ldflag is empty, ResolvedCommit uses vcs.revision and | ||
| // appends -dirty when vcs.modified is true. ResolvedDate is only the ldflag | ||
| // build time. vcs.time is the revision time and is not a build time. | ||
| var ( | ||
| Commit string | ||
| Date string | ||
| GoVersion string | ||
| ) | ||
|
|
||
| // ResolvedCommit returns the ldflags commit, or the toolchain revision. | ||
| // A dirty toolchain revision is marked so it is not printed as a clean commit. | ||
| func ResolvedCommit() string { | ||
| if Commit != "" { | ||
| return Commit | ||
| } | ||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok || info == nil { | ||
| return "" | ||
| } | ||
| return commitFromSettings(info.Settings) | ||
| } | ||
|
|
||
| func commitFromSettings(settings []debug.BuildSetting) string { | ||
| rev := settingFrom(settings, "vcs.revision") | ||
| if rev == "" { | ||
| return "" | ||
| } | ||
| if settingFrom(settings, "vcs.modified") == "true" { | ||
| return rev + "-dirty" | ||
| } | ||
| return rev | ||
| } | ||
|
|
||
| // ResolvedDate returns the ldflag build time. It does not use vcs.time. | ||
| func ResolvedDate() string { | ||
| return dateFromSettings(Date, nil) | ||
| } | ||
|
|
||
| func dateFromSettings(date string, _ []debug.BuildSetting) string { | ||
| return date | ||
| } | ||
|
|
||
| func settingFrom(settings []debug.BuildSetting, key string) string { | ||
| for _, s := range settings { | ||
| if s.Key == key { | ||
| return s.Value | ||
| } | ||
| } | ||
| return "" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package buildinfo | ||
|
|
||
| import ( | ||
| "runtime/debug" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestResolvedCommitPrefersLdflags(t *testing.T) { | ||
| old := Commit | ||
| t.Cleanup(func() { Commit = old }) | ||
| Commit = "abc1234" | ||
| if got := ResolvedCommit(); got != "abc1234" { | ||
| t.Fatalf("ResolvedCommit() = %q, want ldflags value", got) | ||
| } | ||
| } | ||
|
|
||
| func TestResolvedDatePrefersLdflags(t *testing.T) { | ||
| old := Date | ||
| t.Cleanup(func() { Date = old }) | ||
| Date = "2026-04-01T12:00:00Z" | ||
| if got := ResolvedDate(); got != "2026-04-01T12:00:00Z" { | ||
| t.Fatalf("ResolvedDate() = %q, want ldflags value", got) | ||
| } | ||
| } | ||
|
|
||
| func TestResolvedCommitEmptyLdflagMatchesToolchainSettings(t *testing.T) { | ||
| old := Commit | ||
| t.Cleanup(func() { Commit = old }) | ||
| Commit = "" | ||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok || info == nil { | ||
| t.Fatal("ReadBuildInfo unavailable") | ||
| } | ||
| want := commitFromSettings(info.Settings) | ||
| if got := ResolvedCommit(); got != want { | ||
| t.Fatalf("ResolvedCommit() = %q, want toolchain settings %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestResolvedDateEmptyLdflagStaysEmpty(t *testing.T) { | ||
| old := Date | ||
| t.Cleanup(func() { Date = old }) | ||
| Date = "" | ||
| if got := ResolvedDate(); got != "" { | ||
| t.Fatalf("ResolvedDate() = %q, want empty ldflag", got) | ||
| } | ||
| } | ||
|
|
||
| func TestCommitFromSettingsMarksADirtyTree(t *testing.T) { | ||
| settings := []debug.BuildSetting{ | ||
| {Key: "vcs.revision", Value: "abc1234"}, | ||
| {Key: "vcs.modified", Value: "true"}, | ||
| } | ||
| if got := commitFromSettings(settings); got != "abc1234-dirty" { | ||
| t.Fatalf("commitFromSettings() = %q, want dirty revision", got) | ||
| } | ||
| settings[1].Value = "false" | ||
| if got := commitFromSettings(settings); got != "abc1234" { | ||
| t.Fatalf("commitFromSettings() = %q, want clean revision", got) | ||
| } | ||
| if got := commitFromSettings([]debug.BuildSetting{{Key: "vcs.modified", Value: "true"}}); got != "" { | ||
| t.Fatalf("commitFromSettings() = %q, want empty without a revision", got) | ||
| } | ||
| } | ||
|
|
||
| func TestDateFromSettingsIgnoresRevisionTime(t *testing.T) { | ||
| settings := []debug.BuildSetting{{Key: "vcs.time", Value: "2020-01-01T00:00:00Z"}} | ||
| if got := dateFromSettings("", settings); got != "" { | ||
| t.Fatalf("dateFromSettings() = %q, want empty build time", got) | ||
| } | ||
| if got := dateFromSettings("2026-04-01T12:00:00Z", settings); got != "2026-04-01T12:00:00Z" { | ||
| t.Fatalf("dateFromSettings() = %q, want ldflag build time", got) | ||
| } | ||
| } | ||
|
|
||
| func TestSettingFrom(t *testing.T) { | ||
| settings := []debug.BuildSetting{{Key: "vcs.revision", Value: "deadbeef"}} | ||
| if got := settingFrom(settings, "vcs.revision"); got != "deadbeef" { | ||
| t.Fatalf("settingFrom() = %q", got) | ||
| } | ||
| if got := settingFrom(settings, "vcs.time"); got != "" { | ||
| t.Fatalf("missing key = %q, want empty", got) | ||
| } | ||
| if got := settingFrom(nil, "vcs.revision"); got != "" { | ||
| t.Fatalf("nil settings = %q, want empty", got) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.