2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-10 17:00:30 +00:00
|
|
|
|
|
|
|
// Package version provides the version that the binary was built at.
|
|
|
|
package version
|
|
|
|
|
2021-10-22 16:44:37 +00:00
|
|
|
import (
|
2023-02-11 02:07:37 +00:00
|
|
|
"fmt"
|
2022-03-16 20:55:23 +00:00
|
|
|
"runtime/debug"
|
2021-10-22 16:44:37 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
tailscaleroot "tailscale.com"
|
2023-02-11 02:07:37 +00:00
|
|
|
"tailscale.com/types/lazy"
|
2021-10-22 16:44:37 +00:00
|
|
|
)
|
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
// Stamp vars can have their value set at build time by linker flags (see
|
|
|
|
// build_dist.sh for an example). When set, these stamps serve as additional
|
|
|
|
// inputs to computing the binary's version as returned by the functions in this
|
|
|
|
// package.
|
|
|
|
//
|
|
|
|
// All stamps are optional.
|
|
|
|
var (
|
|
|
|
// longStamp is the full version identifier of the build. If set, it is
|
|
|
|
// returned verbatim by Long() and other functions that return Long()'s
|
|
|
|
// output.
|
|
|
|
longStamp string
|
|
|
|
|
|
|
|
// shortStamp is the short version identifier of the build. If set, it
|
|
|
|
// is returned verbatim by Short() and other functions that return Short()'s
|
|
|
|
// output.
|
|
|
|
shortStamp string
|
|
|
|
|
|
|
|
// gitCommitStamp is the git commit of the github.com/tailscale/tailscale
|
|
|
|
// repository at which Tailscale was built. Its format is the one returned
|
|
|
|
// by `git rev-parse <commit>`. If set, it is used instead of any git commit
|
|
|
|
// information embedded by the Go tool.
|
|
|
|
gitCommitStamp string
|
|
|
|
|
|
|
|
// gitDirtyStamp is whether the git checkout from which the code was built
|
|
|
|
// was dirty. Its value is ORed with the dirty bit embedded by the Go tool.
|
|
|
|
//
|
|
|
|
// We need this because when we build binaries from another repo that
|
|
|
|
// imports tailscale.com, the Go tool doesn't stamp any dirtiness info into
|
|
|
|
// the binary. Instead, we have to inject the dirty bit ourselves here.
|
|
|
|
gitDirtyStamp bool
|
|
|
|
|
|
|
|
// extraGitCommit, is the git commit of a "supplemental" repository at which
|
|
|
|
// Tailscale was built. Its format is the same as gitCommit.
|
|
|
|
//
|
|
|
|
// extraGitCommit is used to track the source revision when the main
|
|
|
|
// Tailscale repository is integrated into and built from another repository
|
|
|
|
// (for example, Tailscale's proprietary code, or the Android OSS
|
|
|
|
// repository). Together, gitCommit and extraGitCommit exactly describe what
|
|
|
|
// repositories and commits were used in a build.
|
|
|
|
extraGitCommitStamp string
|
|
|
|
)
|
2023-02-11 06:20:36 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
var long lazy.SyncValue[string]
|
2023-02-11 06:20:36 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
// Long returns a full version number for this build, of one of the forms:
|
|
|
|
//
|
|
|
|
// - "x.y.z-commithash-otherhash" for release builds distributed by Tailscale
|
|
|
|
// - "x.y.z-commithash" for release builds built with build_dist.sh
|
|
|
|
// - "x.y.z-changecount-commithash-otherhash" for untagged release branch
|
|
|
|
// builds by Tailscale (these are not distributed).
|
|
|
|
// - "x.y.z-changecount-commithash" for untagged release branch builds
|
|
|
|
// built with build_dist.sh
|
|
|
|
// - "x.y.z-devYYYYMMDD-commithash{,-dirty}" for builds made with plain "go
|
|
|
|
// build" or "go install"
|
|
|
|
// - "x.y.z-ERR-BuildInfo" for builds made by plain "go run"
|
2023-02-11 06:20:36 +00:00
|
|
|
func Long() string {
|
2023-02-11 02:07:37 +00:00
|
|
|
return long.Get(func() string {
|
|
|
|
if longStamp != "" {
|
|
|
|
return longStamp
|
|
|
|
}
|
|
|
|
bi := getEmbeddedInfo()
|
|
|
|
if !bi.valid {
|
|
|
|
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s-dev%s-t%s%s", strings.TrimSpace(tailscaleroot.VersionDotTxt), bi.commitDate, bi.commitAbbrev(), dirtyString())
|
|
|
|
})
|
2023-02-11 06:20:36 +00:00
|
|
|
}
|
2023-02-10 21:02:29 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
var short lazy.SyncValue[string]
|
|
|
|
|
|
|
|
// Short returns a short version number for this build, of the forms:
|
|
|
|
//
|
|
|
|
// - "x.y.z" for builds distributed by Tailscale or built with build_dist.sh
|
|
|
|
// - "x.y.z-devYYYYMMDD" for builds made with plain "go build" or "go install"
|
|
|
|
// - "x.y.z-ERR-BuildInfo" for builds made by plain "go run"
|
2023-02-11 06:20:36 +00:00
|
|
|
func Short() string {
|
2023-02-11 02:07:37 +00:00
|
|
|
return short.Get(func() string {
|
|
|
|
if shortStamp != "" {
|
|
|
|
return shortStamp
|
|
|
|
}
|
|
|
|
bi := getEmbeddedInfo()
|
|
|
|
if !bi.valid {
|
|
|
|
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-ERR-BuildInfo"
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(tailscaleroot.VersionDotTxt) + "-dev" + bi.commitDate
|
|
|
|
})
|
2023-02-11 06:20:36 +00:00
|
|
|
}
|
2020-10-27 04:23:58 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
type embeddedInfo struct {
|
|
|
|
valid bool
|
|
|
|
commit string
|
|
|
|
commitDate string
|
|
|
|
dirty bool
|
|
|
|
}
|
2023-02-07 07:23:00 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
func (i embeddedInfo) commitAbbrev() string {
|
|
|
|
if len(i.commit) >= 9 {
|
|
|
|
return i.commit[:9]
|
2022-03-16 20:55:23 +00:00
|
|
|
}
|
2023-02-11 02:07:37 +00:00
|
|
|
return i.commit
|
|
|
|
}
|
2022-03-16 20:55:23 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
var getEmbeddedInfo = lazy.SyncFunc(func() embeddedInfo {
|
2022-03-16 20:55:23 +00:00
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
2023-02-11 02:07:37 +00:00
|
|
|
return embeddedInfo{}
|
2022-03-16 20:55:23 +00:00
|
|
|
}
|
2023-02-11 02:07:37 +00:00
|
|
|
ret := embeddedInfo{valid: true}
|
2022-03-16 20:55:23 +00:00
|
|
|
for _, s := range bi.Settings {
|
|
|
|
switch s.Key {
|
|
|
|
case "vcs.revision":
|
2023-02-11 02:07:37 +00:00
|
|
|
ret.commit = s.Value
|
2022-03-16 20:55:23 +00:00
|
|
|
case "vcs.time":
|
|
|
|
if len(s.Value) >= len("yyyy-mm-dd") {
|
2023-02-11 02:07:37 +00:00
|
|
|
ret.commitDate = s.Value[:len("yyyy-mm-dd")]
|
|
|
|
ret.commitDate = strings.ReplaceAll(ret.commitDate, "-", "")
|
2022-03-16 20:55:23 +00:00
|
|
|
}
|
|
|
|
case "vcs.modified":
|
2023-05-07 07:20:29 +00:00
|
|
|
ret.dirty = s.Value == "true"
|
2022-03-16 20:55:23 +00:00
|
|
|
}
|
2021-06-03 18:19:29 +00:00
|
|
|
}
|
2023-02-11 05:52:58 +00:00
|
|
|
if ret.commit == "" || ret.commitDate == "" {
|
|
|
|
// Build info is present in the binary, but has no useful data. Act as
|
|
|
|
// if it's missing.
|
|
|
|
return embeddedInfo{}
|
|
|
|
}
|
2023-02-11 02:07:37 +00:00
|
|
|
return ret
|
|
|
|
})
|
2022-03-16 20:55:23 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
func gitCommit() string {
|
|
|
|
if gitCommitStamp != "" {
|
|
|
|
return gitCommitStamp
|
|
|
|
}
|
|
|
|
return getEmbeddedInfo().commit
|
2021-06-03 18:19:29 +00:00
|
|
|
}
|
2020-10-27 04:23:58 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
func gitDirty() bool {
|
|
|
|
if gitDirtyStamp {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return getEmbeddedInfo().dirty
|
|
|
|
}
|
2023-02-10 21:02:29 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
func dirtyString() string {
|
|
|
|
if gitDirty() {
|
|
|
|
return "-dirty"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2023-02-10 21:02:29 +00:00
|
|
|
|
2023-02-11 02:07:37 +00:00
|
|
|
func majorMinorPatch() string {
|
|
|
|
ret, _, _ := strings.Cut(Short(), "-")
|
|
|
|
return ret
|
|
|
|
}
|