internal/version: use gitCommit injection in version handling code (#25851)

This changes the CI build to store the git commit and date into package
internal/version instead of package main. Doing this essentially merges our
two ways of tracking the go-ethereum version into a single place, achieving
two objectives:

- Bad block reports, which use version.Info(), will now have the git commit
  information even when geth is built in an environment such as
  launchpad.net where git access is unavailable.

- For geth builds created by `go build ./cmd/geth` (i.e. not using `go run
  build/ci.go install`), git information stored by the go tool is now used
  in the p2p node name as well as in `geth version` and `geth
  version-check`.
This commit is contained in:
Felix Lange
2022-09-23 14:08:25 +02:00
committed by GitHub
parent e6d4aedb8c
commit 65f3c1b46f
16 changed files with 100 additions and 105 deletions
+4 -2
View File
@@ -20,15 +20,17 @@ import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/internal/version"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli/v2"
)
// NewApp creates an app with sane defaults.
func NewApp(gitCommit, gitDate, usage string) *cli.App {
func NewApp(usage string) *cli.App {
git, _ := version.VCS()
app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = params.VersionWithCommit(gitCommit, gitDate)
app.Version = params.VersionWithCommit(git.Commit, git.Date)
app.Usage = usage
app.Copyright = "Copyright 2013-2022 The go-ethereum Authors"
app.Before = func(ctx *cli.Context) error {
+2 -2
View File
@@ -23,6 +23,6 @@ import "runtime/debug"
// In Go versions before 1.18, VCS information is not available.
func vcsInfo(info *debug.BuildInfo) (gitStatus, bool) {
return gitStatus{}, false
func buildInfoVCS(info *debug.BuildInfo) (VCSInfo, bool) {
return VCSInfo{}, false
}
+18 -11
View File
@@ -19,29 +19,36 @@
package version
import "runtime/debug"
import (
"runtime/debug"
"time"
)
// In go 1.18 and beyond, the go tool embeds VCS information into the build.
// vcsInfo returns VCS information of the build.
func vcsInfo(info *debug.BuildInfo) (s gitStatus, ok bool) {
const (
govcsTimeLayout = "2006-01-02T15:04:05Z"
ourTimeLayout = "20060102"
)
// buildInfoVCS returns VCS information of the build.
func buildInfoVCS(info *debug.BuildInfo) (s VCSInfo, ok bool) {
for _, v := range info.Settings {
switch v.Key {
case "vcs.revision":
if len(v.Value) < 8 {
s.revision = v.Value
} else {
s.revision = v.Value[:8]
}
s.Commit = v.Value
case "vcs.modified":
if v.Value == "true" {
s.modified = true
s.Dirty = true
}
case "vcs.time":
s.time = v.Value
t, err := time.Parse(govcsTimeLayout, v.Value)
if err == nil {
s.Date = t.Format(ourTimeLayout)
}
}
}
if s.revision != "" && s.time != "" {
if s.Commit != "" && s.Date != "" {
ok = true
}
return
+45 -9
View File
@@ -19,6 +19,7 @@ package version
import (
"fmt"
"runtime"
"runtime/debug"
"strings"
@@ -27,6 +28,43 @@ import (
const ourPath = "github.com/ethereum/go-ethereum" // Path to our module
// These variables are set at build-time by the linker when the build is
// done by build/ci.go.
var gitCommit, gitDate string
// VCSInfo represents the git repository state.
type VCSInfo struct {
Commit string // head commit hash
Date string // commit time in YYYYMMDD format
Dirty bool
}
// VCS returns version control information of the current executable.
func VCS() (VCSInfo, bool) {
if gitCommit != "" {
// Use information set by the build script if present.
return VCSInfo{Commit: gitCommit, Date: gitDate}, true
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
if buildInfo.Main.Path == ourPath {
return buildInfoVCS(buildInfo)
}
}
return VCSInfo{}, false
}
// ClientName creates a software name/version identifier according to common
// conventions in the Ethereum p2p network.
func ClientName(clientIdentifier string) string {
git, _ := VCS()
return fmt.Sprintf("%s/v%v/%v-%v/%v",
strings.Title(clientIdentifier),
params.VersionWithCommit(git.Commit, git.Date),
runtime.GOOS, runtime.GOARCH,
runtime.Version(),
)
}
// runtimeInfo returns build and platform information about the current binary.
//
// If the package that is currently executing is a prefixed by our go-ethereum
@@ -40,22 +78,20 @@ func Info() (version, vcs string) {
return version, ""
}
version = versionInfo(buildInfo)
if status, ok := vcsInfo(buildInfo); ok {
if status, ok := VCS(); ok {
modified := ""
if status.modified {
if status.Dirty {
modified = " (dirty)"
}
vcs = status.revision + "-" + status.time + modified
commit := status.Commit
if len(commit) > 8 {
commit = commit[:8]
}
vcs = commit + "-" + status.Date + modified
}
return version, vcs
}
type gitStatus struct {
revision string
time string
modified bool
}
// versionInfo returns version information for the currently executing
// implementation.
//