From 66621ab38ebd4a9a4a494d40605845c4909e0b4c Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 23 Feb 2023 19:03:14 -0800 Subject: [PATCH] tool/gocross: embed the version explicitly with linker flags We need to build gocross from multiple repos, but Go's innate git hash embedding only works when you build gocross from this repo, not when you build it from elsewhere via 'go build tailscale.com/tool/gocross'. Instead, explicitly embed the version found with 'git rev-parse HEAD', which will work from any git repo. Signed-off-by: David Anderson --- tool/gocross/gocross.go | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/tool/gocross/gocross.go b/tool/gocross/gocross.go index 4a073875b..a7bbe1ab3 100644 --- a/tool/gocross/gocross.go +++ b/tool/gocross/gocross.go @@ -16,9 +16,9 @@ "fmt" "os" "path/filepath" - runtimeDebug "runtime/debug" "tailscale.com/atomicfile" + "tailscale.com/version" ) func main() { @@ -29,12 +29,7 @@ func main() { // any time. switch os.Args[1] { case "gocross-version": - hash, err := embeddedCommit() - if err != nil { - fmt.Fprintf(os.Stderr, "getting commit hash: %v", err) - os.Exit(1) - } - fmt.Println(hash) + fmt.Println(version.GetMeta().GitCommit) os.Exit(0) case "is-gocross": // This subcommand exits with an error code when called on a @@ -132,16 +127,3 @@ func debug(format string, args ...interface{}) { fmt.Fprintf(out, format, args...) } - -func embeddedCommit() (string, error) { - bi, ok := runtimeDebug.ReadBuildInfo() - if !ok { - return "", fmt.Errorf("no build info") - } - for _, s := range bi.Settings { - if s.Key == "vcs.revision" { - return s.Value, nil - } - } - return "", fmt.Errorf("no git commit found") -}