]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: include "go1.x-" in devel go version strings
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 26 Oct 2020 09:42:54 +0000 (09:42 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Sat, 3 Apr 2021 10:58:19 +0000 (10:58 +0000)
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>
src/cmd/dist/build.go

index acf38e3785742465d40ffad7a4d4113029985a6e..8accb6db8f7c67926bd7121f3cc16b7487056553 100644 (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.