1
0
mirror of https://github.com/golang/go synced 2024-09-30 02:24:43 -06:00

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>
This commit is contained in:
Daniel Martí 2020-10-26 09:42:54 +00:00
parent 01821137c2
commit fe587ce856

19
src/cmd/dist/build.go vendored
View File

@ -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.