diff options
author | Daniel Martí <mvdan@mvdan.cc> | 2020-10-26 09:42:54 +0000 |
---|---|---|
committer | Daniel Martí <mvdan@mvdan.cc> | 2021-04-03 10:58:19 +0000 |
commit | fe587ce856d1dee97829fe0ed090ba7e068335cb (patch) | |
tree | bdc6a08528b68f017d4f2cd6450eca9ba89d1153 /src/cmd/dist | |
parent | 01821137c24750ddc7a58d3469bfd8c5e9d6bd47 (diff) | |
download | go-fe587ce856d1dee97829fe0ed090ba7e068335cb.tar.gz go-fe587ce856d1dee97829fe0ed090ba7e068335cb.zip |
cmd/dist: include "go1.x-" in devel go version strings
This way, "go version" will report the "base version" or major version
that the tool corresponds to. This is the same version number that is
matched against build tags such as "go1.14" or "!go1.16".
Obtaining this version being built is non-trivial, since we can't just
import a package or query git. The added comments document the chosen
mechanism, based on a regular expression. It was chosen over AST parsing
as it would add a significant amount of code without much gain, given
how simple the goversion.go file is.
For #41116.
Change-Id: I653ae935e27c13267f23898f89c84020dcd6e194
Reviewed-on: https://go-review.googlesource.com/c/go/+/264938
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
Trust: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/cmd/dist')
-rw-r--r-- | src/cmd/dist/build.go | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index acf38e3785..8accb6db8f 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "sort" "strings" "sync" @@ -405,8 +406,22 @@ func findgoversion() string { } if !precise { - // Tag does not point at HEAD; add hash and date to version. - tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD")) + // Tag does not point at HEAD; add 1.x base version, hash, and date to version. + // + // Note that we lightly parse internal/goversion/goversion.go to + // obtain the base version. We can't just import the package, + // because cmd/dist is built with a bootstrap GOROOT which could + // be an entirely different version of Go, like 1.4. We assume + // that the file contains "const Version = <Integer>". + + goversionSource := readfile(pathf("%s/src/internal/goversion/goversion.go", goroot)) + m := regexp.MustCompile(`(?m)^const Version = (\d+)`).FindStringSubmatch(goversionSource) + if m == nil { + fatalf("internal/goversion/goversion.go does not contain 'const Version = ...'") + } + tag += fmt.Sprintf(" go1.%s-", m[1]) + + tag += chomp(run(goroot, CheckExit, "git", "log", "-n", "1", "--format=format:%h %cd", "HEAD")) } // Cache version. |