Skip to content

Commit 65f3c1b

Browse files
authored
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`.
1 parent e6d4aed commit 65f3c1b

File tree

16 files changed

+100
-105
lines changed

16 files changed

+100
-105
lines changed

build/ci.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ func doInstall(cmdline []string) {
254254
func buildFlags(env build.Environment, staticLinking bool, buildTags []string) (flags []string) {
255255
var ld []string
256256
if env.Commit != "" {
257-
ld = append(ld, "-X", "main.gitCommit="+env.Commit)
258-
ld = append(ld, "-X", "main.gitDate="+env.Date)
257+
ld = append(ld, "-X", "github.com/ethereum/go-ethereum/internal/version.gitCommit="+env.Commit)
258+
ld = append(ld, "-X", "github.com/ethereum/go-ethereum/internal/version.gitDate="+env.Date)
259259
}
260260
// Strip DWARF on darwin. This used to be required for certain things,
261261
// and there is no downside to this, so we just keep doing it.

cmd/abigen/main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ import (
3333
"github.com/urfave/cli/v2"
3434
)
3535

36-
var (
37-
// Git SHA1 commit hash of the release (set via linker flags)
38-
gitCommit = ""
39-
gitDate = ""
40-
41-
app *cli.App
42-
)
43-
4436
var (
4537
// Flags needed by abigen
4638
abiFlag = &cli.StringFlag{
@@ -82,8 +74,9 @@ var (
8274
}
8375
)
8476

77+
var app = flags.NewApp("Ethereum ABI wrapper code generator")
78+
8579
func init() {
86-
app = flags.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool")
8780
app.Name = "abigen"
8881
app.Flags = []cli.Flag{
8982
abiFlag,

cmd/checkpoint-admin/main.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ import (
2828
"github.com/urfave/cli/v2"
2929
)
3030

31-
var (
32-
// Git SHA1 commit hash of the release (set via linker flags)
33-
gitCommit = ""
34-
gitDate = ""
35-
36-
app *cli.App
37-
)
31+
var app = flags.NewApp("ethereum checkpoint helper tool")
3832

3933
func init() {
40-
app = flags.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool")
4134
app.Commands = []*cli.Command{
4235
commandStatus,
4336
commandDeploy,

cmd/clef/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,7 @@ The gendoc generates example structures of the json-rpc communication types.
215215
`}
216216
)
217217

218-
var (
219-
// Git SHA1 commit hash of the release (set via linker flags)
220-
gitCommit = ""
221-
gitDate = ""
222-
223-
app = flags.NewApp(gitCommit, gitDate, "Manage Ethereum account operations")
224-
)
218+
var app = flags.NewApp("Manage Ethereum account operations")
225219

226220
func init() {
227221
app.Name = "Clef"

cmd/devp2p/main.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,17 @@ package main
1919
import (
2020
"fmt"
2121
"os"
22-
"path/filepath"
2322

2423
"github.com/ethereum/go-ethereum/internal/debug"
2524
"github.com/ethereum/go-ethereum/internal/flags"
2625
"github.com/ethereum/go-ethereum/p2p/enode"
27-
"github.com/ethereum/go-ethereum/params"
2826
"github.com/urfave/cli/v2"
2927
)
3028

31-
var (
32-
// Git information set by linker when building with ci.go.
33-
gitCommit string
34-
gitDate string
35-
app = &cli.App{
36-
Name: filepath.Base(os.Args[0]),
37-
Usage: "go-ethereum devp2p tool",
38-
Version: params.VersionWithCommit(gitCommit, gitDate),
39-
Writer: os.Stdout,
40-
HideVersion: true,
41-
}
42-
)
29+
var app = flags.NewApp("go-ethereum devp2p tool")
4330

4431
func init() {
45-
// Set up the CLI app.
32+
app.HideVersion = true
4633
app.Flags = append(app.Flags, debug.Flags...)
4734
app.Before = func(ctx *cli.Context) error {
4835
flags.MigrateGlobalFlags(ctx)
@@ -56,6 +43,7 @@ func init() {
5643
fmt.Fprintf(os.Stderr, "No such command: %s\n", cmd)
5744
os.Exit(1)
5845
}
46+
5947
// Add subcommands.
6048
app.Commands = []*cli.Command{
6149
enrdumpCommand,

cmd/ethkey/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ const (
2828
defaultKeyfileName = "keyfile.json"
2929
)
3030

31-
// Git SHA1 commit hash of the release (set via linker flags)
32-
var gitCommit = ""
33-
var gitDate = ""
34-
3531
var app *cli.App
3632

3733
func init() {
38-
app = flags.NewApp(gitCommit, gitDate, "an Ethereum key manager")
34+
app = flags.NewApp("Ethereum key manager")
3935
app.Commands = []*cli.Command{
4036
commandGenerate,
4137
commandInspect,

cmd/evm/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ import (
2727
"github.com/urfave/cli/v2"
2828
)
2929

30-
var (
31-
gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
32-
gitDate = ""
33-
34-
app = flags.NewApp(gitCommit, gitDate, "the evm command line interface")
35-
)
36-
3730
var (
3831
DebugFlag = &cli.BoolFlag{
3932
Name: "debug",
@@ -192,6 +185,8 @@ var blockBuilderCommand = &cli.Command{
192185
},
193186
}
194187

188+
var app = flags.NewApp("the evm command line interface")
189+
195190
func init() {
196191
app.Flags = []cli.Flag{
197192
BenchFlag,

cmd/faucet/faucet.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/ethereum/go-ethereum/eth/ethconfig"
5050
"github.com/ethereum/go-ethereum/ethclient"
5151
"github.com/ethereum/go-ethereum/ethstats"
52+
"github.com/ethereum/go-ethereum/internal/version"
5253
"github.com/ethereum/go-ethereum/les"
5354
"github.com/ethereum/go-ethereum/log"
5455
"github.com/ethereum/go-ethereum/node"
@@ -93,11 +94,6 @@ var (
9394
ether = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
9495
)
9596

96-
var (
97-
gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
98-
gitDate = "" // Git commit date YYYYMMDD of the release (set via linker flags)
99-
)
100-
10197
//go:embed faucet.html
10298
var websiteTmpl string
10399

@@ -226,9 +222,10 @@ type wsConn struct {
226222

227223
func newFaucet(genesis *core.Genesis, port int, enodes []*enode.Node, network uint64, stats string, ks *keystore.KeyStore, index []byte) (*faucet, error) {
228224
// Assemble the raw devp2p protocol stack
225+
git, _ := version.VCS()
229226
stack, err := node.New(&node.Config{
230227
Name: "geth",
231-
Version: params.VersionWithCommit(gitCommit, gitDate),
228+
Version: params.VersionWithCommit(git.Commit, git.Date),
232229
DataDir: filepath.Join(os.Getenv("HOME"), ".faucet"),
233230
P2P: p2p.Config{
234231
NAT: nat.Any(),

cmd/geth/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/ethereum/go-ethereum/eth/ethconfig"
3636
"github.com/ethereum/go-ethereum/internal/ethapi"
3737
"github.com/ethereum/go-ethereum/internal/flags"
38+
"github.com/ethereum/go-ethereum/internal/version"
3839
"github.com/ethereum/go-ethereum/log"
3940
"github.com/ethereum/go-ethereum/metrics"
4041
"github.com/ethereum/go-ethereum/node"
@@ -108,9 +109,10 @@ func loadConfig(file string, cfg *gethConfig) error {
108109
}
109110

110111
func defaultNodeConfig() node.Config {
112+
git, _ := version.VCS()
111113
cfg := node.DefaultConfig
112114
cfg.Name = clientIdentifier
113-
cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
115+
cfg.Version = params.VersionWithCommit(git.Commit, git.Date)
114116
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
115117
cfg.WSModules = append(cfg.WSModules, "eth")
116118
cfg.IPCPath = "geth.ipc"

cmd/geth/main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ const (
5252
)
5353

5454
var (
55-
// Git SHA1 commit hash of the release (set via linker flags)
56-
gitCommit = ""
57-
gitDate = ""
58-
// The app that holds all commands and flags.
59-
app = flags.NewApp(gitCommit, gitDate, "the go-ethereum command line interface")
6055
// flags that configure the node
6156
nodeFlags = flags.Merge([]cli.Flag{
6257
utils.IdentityFlag,
@@ -205,6 +200,8 @@ var (
205200
}
206201
)
207202

203+
var app = flags.NewApp("the go-ethereum command line interface")
204+
208205
func init() {
209206
// Initialize the CLI app and start Geth
210207
app.Action = geth

0 commit comments

Comments
 (0)