Skip to content

Commit be4d74f

Browse files
C. Brownkaralabe
C. Brown
authored andcommitted
cmd, internal/build, docker: advertise commit date in unstable build versions (ethereum#19522)
* add-date-to unstable * fields-insteadof-split * internal/build: support building with missing git * docker: add git history back to support commit date in version * internal/build: use PR commits hashes for PR builds
1 parent c113723 commit be4d74f

File tree

17 files changed

+75
-38
lines changed

17 files changed

+75
-38
lines changed

.dockerignore

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
**/.git
2-
.git
3-
!.git/HEAD
4-
!.git/refs/heads
51
**/*_test.go
62

73
build/_workspace

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Build Geth in a stock Go builder container
22
FROM golang:1.12-alpine as builder
33

4-
RUN apk add --no-cache make gcc musl-dev linux-headers
4+
RUN apk add --no-cache make gcc musl-dev linux-headers git
55

66
ADD . /go-ethereum
77
RUN cd /go-ethereum && make geth

Dockerfile.alltools

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Build Geth in a stock Go builder container
22
FROM golang:1.12-alpine as builder
33

4-
RUN apk add --no-cache make gcc musl-dev linux-headers
4+
RUN apk add --no-cache make gcc musl-dev linux-headers git
55

66
ADD . /go-ethereum
77
RUN cd /go-ethereum && make all

build/ci.go

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ func buildFlags(env build.Environment) (flags []string) {
284284
var ld []string
285285
if env.Commit != "" {
286286
ld = append(ld, "-X", "main.gitCommit="+env.Commit)
287+
ld = append(ld, "-X", "main.gitDate="+env.Date)
287288
}
288289
if runtime.GOOS == "darwin" {
289290
ld = append(ld, "-s")

cmd/ethkey/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ const (
3030

3131
// Git SHA1 commit hash of the release (set via linker flags)
3232
var gitCommit = ""
33+
var gitDate = ""
3334

3435
var app *cli.App
3536

3637
func init() {
37-
app = utils.NewApp(gitCommit, "an Ethereum key manager")
38+
app = utils.NewApp(gitCommit, gitDate, "an Ethereum key manager")
3839
app.Commands = []cli.Command{
3940
commandGenerate,
4041
commandInspect,

cmd/evm/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import (
2727
)
2828

2929
var gitCommit = "" // Git SHA1 commit hash of the release (set via linker flags)
30+
var gitDate = ""
3031

3132
var (
32-
app = utils.NewApp(gitCommit, "the evm command line interface")
33+
app = utils.NewApp(gitCommit, gitDate, "the evm command line interface")
3334

3435
DebugFlag = cli.BoolFlag{
3536
Name: "debug",

cmd/geth/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func loadConfig(file string, cfg *gethConfig) error {
101101
func defaultNodeConfig() node.Config {
102102
cfg := node.DefaultConfig
103103
cfg.Name = clientIdentifier
104-
cfg.Version = params.VersionWithCommit(gitCommit)
104+
cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
105105
cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
106106
cfg.WSModules = append(cfg.WSModules, "eth", "shh")
107107
cfg.IPCPath = "geth.ipc"

cmd/geth/consolecmd_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestConsoleWelcome(t *testing.T) {
5050
geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
5151
geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
5252
geth.SetTemplateFunc("gover", runtime.Version)
53-
geth.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
53+
geth.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
5454
geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
5555
geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
5656

@@ -133,7 +133,7 @@ func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
133133
attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
134134
attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
135135
attach.SetTemplateFunc("gover", runtime.Version)
136-
attach.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
136+
attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
137137
attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
138138
attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
139139
attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })

cmd/geth/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ const (
5050
var (
5151
// Git SHA1 commit hash of the release (set via linker flags)
5252
gitCommit = ""
53+
gitDate = ""
5354
// The app that holds all commands and flags.
54-
app = utils.NewApp(gitCommit, "the go-ethereum command line interface")
55+
app = utils.NewApp(gitCommit, gitDate, "the go-ethereum command line interface")
5556
// flags that configure the node
5657
nodeFlags = []cli.Flag{
5758
utils.IdentityFlag,

cmd/geth/misccmd.go

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func version(ctx *cli.Context) error {
112112
if gitCommit != "" {
113113
fmt.Println("Git Commit:", gitCommit)
114114
}
115+
if gitDate != "" {
116+
fmt.Println("Git Commit Date:", gitDate)
117+
}
115118
fmt.Println("Architecture:", runtime.GOARCH)
116119
fmt.Println("Protocol Versions:", eth.ProtocolVersions)
117120
fmt.Println("Network Id:", eth.DefaultConfig.NetworkId)

cmd/swarm/global-store/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
var (
2727
version = "0.1"
2828
gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
29+
gitDate string
2930
)
3031

3132
func main() {
@@ -45,6 +46,9 @@ func newApp() (app *cli.App) {
4546
if len(gitCommit) >= 8 {
4647
app.Version += "-" + gitCommit[:8]
4748
}
49+
if gitDate != "" {
50+
app.Version += "-" + gitDate
51+
}
4852
app.Usage = "Swarm Global Store"
4953

5054
// app flags (for all commands)

cmd/swarm/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func init() {
102102
utils.ListenPortFlag.Value = 30399
103103
}
104104

105-
var app = utils.NewApp("", "Ethereum Swarm")
105+
var app = utils.NewApp("", "", "Ethereum Swarm")
106106

107107
// This init function creates the cli.App.
108108
func init() {

cmd/swarm/swarm-snapshot/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
)
2626

2727
var gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
28+
var gitDate string
2829

2930
// default value for "create" command --nodes flag
3031
const defaultNodes = 8
@@ -40,7 +41,7 @@ func main() {
4041
// newApp construct a new instance of Swarm Snapshot Utility.
4142
// Method Run is called on it in the main function and in tests.
4243
func newApp() (app *cli.App) {
43-
app = utils.NewApp(gitCommit, "Swarm Snapshot Utility")
44+
app = utils.NewApp(gitCommit, gitDate, "Swarm Snapshot Utility")
4445

4546
app.Name = "swarm-snapshot"
4647
app.Usage = ""

cmd/utils/flags.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,13 @@ GLOBAL OPTIONS:
9292
}
9393

9494
// NewApp creates an app with sane defaults.
95-
func NewApp(gitCommit, usage string) *cli.App {
95+
func NewApp(gitCommit, gitDate, usage string) *cli.App {
9696
app := cli.NewApp()
9797
app.Name = filepath.Base(os.Args[0])
9898
app.Author = ""
9999
//app.Authors = nil
100100
app.Email = ""
101-
app.Version = params.VersionWithMeta
102-
if len(gitCommit) >= 8 {
103-
app.Version += "-" + gitCommit[:8]
104-
}
101+
app.Version = params.VersionWithCommit(gitCommit, gitDate)
105102
app.Usage = usage
106103
return app
107104
}

internal/build/env.go

+40-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"flag"
2121
"fmt"
2222
"os"
23+
"strconv"
2324
"strings"
25+
"time"
2426
)
2527

2628
var (
@@ -35,39 +37,49 @@ var (
3537

3638
// Environment contains metadata provided by the build environment.
3739
type Environment struct {
38-
Name string // name of the environment
39-
Repo string // name of GitHub repo
40-
Commit, Branch, Tag string // Git info
41-
Buildnum string
42-
IsPullRequest bool
43-
IsCronJob bool
40+
Name string // name of the environment
41+
Repo string // name of GitHub repo
42+
Commit, Date, Branch, Tag string // Git info
43+
Buildnum string
44+
IsPullRequest bool
45+
IsCronJob bool
4446
}
4547

4648
func (env Environment) String() string {
47-
return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)",
48-
env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
49+
return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
50+
env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
4951
}
5052

5153
// Env returns metadata about the current CI environment, falling back to LocalEnv
5254
// if not running on CI.
5355
func Env() Environment {
5456
switch {
5557
case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
58+
commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
59+
if commit == "" {
60+
os.Getenv("TRAVIS_COMMIT")
61+
}
5662
return Environment{
5763
Name: "travis",
5864
Repo: os.Getenv("TRAVIS_REPO_SLUG"),
59-
Commit: os.Getenv("TRAVIS_COMMIT"),
65+
Commit: commit,
66+
Date: getDate(commit),
6067
Branch: os.Getenv("TRAVIS_BRANCH"),
6168
Tag: os.Getenv("TRAVIS_TAG"),
6269
Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"),
6370
IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
6471
IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
6572
}
6673
case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
74+
commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
75+
if commit == "" {
76+
os.Getenv("APPVEYOR_REPO_COMMIT")
77+
}
6778
return Environment{
6879
Name: "appveyor",
6980
Repo: os.Getenv("APPVEYOR_REPO_NAME"),
70-
Commit: os.Getenv("APPVEYOR_REPO_COMMIT"),
81+
Commit: commit,
82+
Date: getDate(commit),
7183
Branch: os.Getenv("APPVEYOR_REPO_BRANCH"),
7284
Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"),
7385
Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"),
@@ -84,14 +96,15 @@ func LocalEnv() Environment {
8496
env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
8597

8698
head := readGitFile("HEAD")
87-
if splits := strings.Split(head, " "); len(splits) == 2 {
88-
head = splits[1]
99+
if fields := strings.Fields(head); len(fields) == 2 {
100+
head = fields[1]
89101
} else {
90102
return env
91103
}
92104
if env.Commit == "" {
93105
env.Commit = readGitFile(head)
94106
}
107+
env.Date = getDate(env.Commit)
95108
if env.Branch == "" {
96109
if head != "HEAD" {
97110
env.Branch = strings.TrimPrefix(head, "refs/heads/")
@@ -107,6 +120,21 @@ func firstLine(s string) string {
107120
return strings.Split(s, "\n")[0]
108121
}
109122

123+
func getDate(commit string) string {
124+
if commit == "" {
125+
return ""
126+
}
127+
out := RunGit("show", "-s", "--format=%ct", commit)
128+
if out == "" {
129+
return ""
130+
}
131+
date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
132+
if err != nil {
133+
panic(fmt.Sprintf("failed to parse git commit date: %v", err))
134+
}
135+
return time.Unix(date, 0).Format("20060102")
136+
}
137+
110138
func applyEnvFlags(env Environment) Environment {
111139
if !flag.Parsed() {
112140
panic("you need to call flag.Parse before Env or LocalEnv")

internal/build/util.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ func RunGit(args ...string) string {
6868
cmd := exec.Command("git", args...)
6969
var stdout, stderr bytes.Buffer
7070
cmd.Stdout, cmd.Stderr = &stdout, &stderr
71-
if err := cmd.Run(); err == exec.ErrNotFound {
72-
if !warnedAboutGit {
73-
log.Println("Warning: can't find 'git' in PATH")
74-
warnedAboutGit = true
71+
if err := cmd.Run(); err != nil {
72+
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
73+
if !warnedAboutGit {
74+
log.Println("Warning: can't find 'git' in PATH")
75+
warnedAboutGit = true
76+
}
77+
return ""
7578
}
76-
return ""
77-
} else if err != nil {
7879
log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
7980
}
8081
return strings.TrimSpace(stdout.String())

params/version.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ func ArchiveVersion(gitCommit string) string {
5555
return vsn
5656
}
5757

58-
func VersionWithCommit(gitCommit string) string {
58+
func VersionWithCommit(gitCommit, gitDate string) string {
5959
vsn := VersionWithMeta
6060
if len(gitCommit) >= 8 {
6161
vsn += "-" + gitCommit[:8]
6262
}
63+
if (VersionMeta != "stable") && (gitDate != "") {
64+
vsn += "-" + gitDate
65+
}
6366
return vsn
6467
}

0 commit comments

Comments
 (0)