From 87e8a3a74367348197e46a211c2d01d5575fd605 Mon Sep 17 00:00:00 2001 From: codebien <2103732+codebien@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:27:29 +0200 Subject: [PATCH] cmd/version: Return commit from go.readbuildinfo --- lib/consts/consts.go | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/consts/consts.go b/lib/consts/consts.go index c9a30c71054e..7dc724318d6a 100644 --- a/lib/consts/consts.go +++ b/lib/consts/consts.go @@ -17,15 +17,46 @@ var VersionDetails = "" //nolint:gochecknoglobals // the currently running k6 executable. func FullVersion() string { goVersionArch := fmt.Sprintf("%s, %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH) - if VersionDetails != "" { - return fmt.Sprintf("%s (%s, %s)", Version, VersionDetails, goVersionArch) + + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return fmt.Sprintf("%s (%s)", Version, goVersionArch) + } + + var ( + commit string + dirty bool + ) + for _, s := range buildInfo.Settings { + switch s.Key { + case "vcs.revision": + if s.Value == "" { + // it should not happen but applying definsive coding + // for edge cases, for example in the event that the local .git repository + // has been deleted before compiling + } + commitLen := 10 + if len(s.Value) < commitLen { + commitLen = len(s.Value) + } + commit = s.Value[:commitLen] + case "vcs.modified": + if s.Value == "true" { + dirty = true + } + default: + } + } + + if commit == "" { + return fmt.Sprintf("%s (%s)", Version, goVersionArch) } - if buildInfo, ok := debug.ReadBuildInfo(); ok { - return fmt.Sprintf("%s (%s, %s)", Version, buildInfo.Main.Version, goVersionArch) + if dirty { + commit += "-dirty" } - return fmt.Sprintf("%s (dev build, %s)", Version, goVersionArch) + return fmt.Sprintf("%s (commit/%s, %s)", Version, commit, goVersionArch) } // Banner returns the ASCII-art banner with the k6 logo and stylized website URL