From 29f7aebc99815efba9b56285217301147ad18084 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 | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/consts/consts.go b/lib/consts/consts.go index c9a30c71054e..b613a4c861aa 100644 --- a/lib/consts/consts.go +++ b/lib/consts/consts.go @@ -8,7 +8,7 @@ import ( ) // Version contains the current semantic version of k6. -const Version = "0.46.0" +const Version = "0.46.1" // VersionDetails can be set externally as part of the build process var VersionDetails = "" //nolint:gochecknoglobals @@ -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