diff --git a/main.go b/main.go index 48eee52..8efd34a 100644 --- a/main.go +++ b/main.go @@ -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 == "" { diff --git a/testdata/scripts/help.txt b/testdata/scripts/help.txt index bd819ab..783c05e 100644 --- a/testdata/scripts/help.txt +++ b/testdata/scripts/help.txt @@ -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'