From 1f22507c06c8b700fbed191e494f886785571bd4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 16 Mar 2022 13:55:23 -0700 Subject: [PATCH] version: use Go 1.18's git stamping as default implementation No more manual version bumps! Fixes #81 Change-Id: I3a9e544a7248f0b83bcbacbaabbc4dabc435e62d Signed-off-by: Brad Fitzpatrick --- cmd/tailscale/depaware.txt | 2 +- version/print.go | 6 +++- version/version.go | 59 ++++++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 9f95f1fd3f62c..652fe8cbc931b 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -197,7 +197,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep reflect from crypto/x509+ regexp from github.com/tailscale/goupnp/httpu+ regexp/syntax from regexp - runtime/debug from golang.org/x/sync/singleflight + runtime/debug from golang.org/x/sync/singleflight+ sort from compress/flate+ strconv from compress/flate+ strings from bufio+ diff --git a/version/print.go b/version/print.go index 800f8f128faf6..9b7940e150622 100644 --- a/version/print.go +++ b/version/print.go @@ -15,7 +15,11 @@ func String() string { ret.WriteString(Short) ret.WriteByte('\n') if GitCommit != "" { - fmt.Fprintf(&ret, " tailscale commit: %s\n", GitCommit) + var dirty string + if GitDirty { + dirty = "-dirty" + } + fmt.Fprintf(&ret, " tailscale commit: %s%s\n", GitCommit, dirty) } if ExtraGitCommit != "" { fmt.Fprintf(&ret, " other commit: %s\n", ExtraGitCommit) diff --git a/version/version.go b/version/version.go index b8fa4d31f32e8..bec10ae9f081a 100644 --- a/version/version.go +++ b/version/version.go @@ -6,28 +6,64 @@ package version import ( + "runtime/debug" "strings" tailscaleroot "tailscale.com" ) // Long is a full version number for this build, of the form -// "x.y.z-commithash", or "date.yyyymmdd" if no actual version was -// provided. -var Long = "date.20220107" +// "x.y.z-commithash" for builds stamped in the usual way (see +// build_dist.sh in the root) or, for binaries built by hand with the +// go tool, it's of the form "1.23.0-dev20220316-t29837428937{,-dirty}" +// where "1.23.0" comes from ../VERSION.txt and the part after dev +// is YYYYMMDD of the commit time, and the part after -t is the commit +// hash. The dirty suffix is whether there are uncommitted changes. +var Long = "" // Short is a short version number for this build, of the form -// "x.y.z", or "date.yyyymmdd" if no actual version was provided. +// "x.y.z" for builds stamped in the usual way (see +// build_dist.sh in the root) or, for binaries built by hand with the // go tool, it's like Long's dev form, but ending at the date part, +// of the form "1.23.0-dev20220316". var Short = "" func init() { - // If it hasn't been link-stamped with -X (via build_dist.sh or similar), - // then use the VERSION.txt file in the root and the date in the Long - // variable above which we occasionally bump by hand. - if Short == "" { - Long = strings.TrimSpace(tailscaleroot.Version) + "-" + Long + if Long != "" && Short != "" { + // Built in the recommended way, using build_dist.sh. + return + } + + bi, ok := debug.ReadBuildInfo() + if !ok { + Long = strings.TrimSpace(tailscaleroot.Version) + "-ERR-BuildInfo" Short = Long + return + } + var dirty string // "-dirty" suffix if dirty + var commitHashAbbrev, commitDate string + for _, s := range bi.Settings { + switch s.Key { + case "vcs.revision": + GitCommit = s.Value + if len(s.Value) >= 9 { + commitHashAbbrev = s.Value[:9] + } + case "vcs.time": + if len(s.Value) >= len("yyyy-mm-dd") { + commitDate = s.Value[:len("yyyy-mm-dd")] + commitDate = strings.ReplaceAll(commitDate, "-", "") + } + case "vcs.modified": + if s.Value == "true" { + dirty = "-dirty" + GitDirty = true + } + } } + + // Backup path, using Go 1.18's built-in git stamping. + Short = strings.TrimSpace(tailscaleroot.Version) + "-dev" + commitDate + Long = Short + "-t" + commitHashAbbrev + dirty } // GitCommit, if non-empty, is the git commit of the @@ -36,6 +72,11 @@ func init() { // --exclude "*" --dirty --abbrev=200`. var GitCommit = "" +// GitDirty is whether Go stamped the binary has having dirty version +// control changes in the working directory (debug.ReadBuildInfo +// setting "vcs.modified" was true). +var GitDirty bool + // ExtraGitCommit, if non-empty, is the git commit of a "supplemental" // repository at which Tailscale was built. Its format is the same as // gitCommit.