make "garble version" friendlier for devel builds

The proposal at https://go.dev/issue/50603 has been approved,
so Go will at some point start producing module pseudo-versions
even if the main module was built from a VCS clone.

To not wait until a future release like Go 1.20,
implement that ourselves with the help of module.PseudoVersion.

The result is a friendlier version output; what used to be

	$ go install && garble version
	mvdan.cc/garble (devel)

	Build settings:
	[...]

will now look like

	$ go install && garble version
	mvdan.cc/garble v0.0.0-20220505210747-22e3d30216be

	Build settings:
	[...]

Note that we don't use VCS tags in any way, so the prefix is hard-coded
as v0.0.0. That seems fine for development builds, and Go doesn't embed
VCS tag information in binaries anyway.

Finally, note that we start printing the module sum, as it's redundant.
The VCS commit hash, at least in git, should be unique enough.
pull/534/head
Daniel Martí 2 years ago
parent 61bd95bb89
commit 1a3d7868d9

@ -36,6 +36,7 @@ import (
"golang.org/x/exp/slices"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/tools/go/ast/astutil"
@ -337,12 +338,6 @@ func mainErr(args []string) error {
}
// For the tests.
if v := os.Getenv("GARBLE_TEST_VERSION"); v != "" {
mod.Version = v
}
if v := os.Getenv("GARBLE_TEST_SUM"); v != "" {
mod.Sum = v
}
if v := os.Getenv("GARBLE_TEST_SETTINGS"); v != "" {
var extra []debug.BuildSetting
if err := json.Unmarshal([]byte(v), &extra); err != nil {
@ -351,7 +346,30 @@ func mainErr(args []string) error {
info.Settings = append(info.Settings, extra...)
}
fmt.Printf("%s %s %s\n\n", mod.Path, mod.Version, mod.Sum)
// Until https://github.com/golang/go/issues/50603 is implemented,
// manually construct something like a pseudo-version.
// TODO: remove when this code is dead, hopefully in Go 1.20.
if mod.Version == "(devel)" {
var vcsTime time.Time
var vcsRevision string
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.time":
// If the format is invalid, we'll print a zero timestamp.
vcsTime, _ = time.Parse(time.RFC3339Nano, setting.Value)
case "vcs.revision":
vcsRevision = setting.Value
if len(vcsRevision) > 12 {
vcsRevision = vcsRevision[:12]
}
}
}
if vcsRevision != "" {
mod.Version = module.PseudoVersion("", "", vcsTime, vcsRevision)
}
}
fmt.Printf("%s %s\n\n", mod.Path, mod.Version)
fmt.Printf("Build settings:\n")
for _, setting := range info.Settings {
if setting.Value == "" {

@ -91,18 +91,16 @@ stdout -count=1 'Build settings'
stdout -count=3 '-compiler|GOOS|GOARCH'
[go1.19] ! stdout 'vcs'
env GARBLE_TEST_VERSION='v0.88.99'
env GARBLE_TEST_SUM='h1:someBase64epYSXwA'
# Obtained from a real build while developing.
env GARBLE_TEST_SETTINGS='[{"Key":"vcs","Value":"git"},{"Key":"vcs.revision","Value":"91ea246349544769f5100c29f79cb0f173abfeea"},{"Key":"vcs.time","Value":"2022-03-18T13:45:11Z"},{"Key":"vcs.modified","Value":"true"}]'
garble version
stdout -count=1 'mvdan.cc/garble v0\.88\.99 h1:someBase64epYSXwA'
stdout -count=1 'mvdan\.cc/garble v0\.0\.0-20220318134511-91ea24634954'
stdout -count=1 'Build settings'
stdout -count=3 '-compiler|GOOS|GOARCH'
[go1.19] stdout -count=1 'vcs git'
[go1.19] stdout -count=1 'vcs.revision 91ea246349544769f5100c29f79cb0f173abfeea'
[go1.19] stdout -count=1 'vcs.time 2022-03-18T13:45:11Z'
[go1.19] stdout -count=1 'vcs.modified true'
[go1.19] stdout -count=1 'vcs\.revision 91ea246349544769f5100c29f79cb0f173abfeea'
[go1.19] stdout -count=1 'vcs\.time 2022-03-18T13:45:11Z'
[go1.19] stdout -count=1 'vcs\.modified true'
! garble version -flag
stderr 'usage: garble version'

Loading…
Cancel
Save