From d63fb1e89a91df60a07718970fd277ce5c9d7bc3 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 1/2] cmd/version: Return commit from go.readbuildinfo --- lib/consts/consts.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/consts/consts.go b/lib/consts/consts.go index c9a30c71054..e0391afdcc6 100644 --- a/lib/consts/consts.go +++ b/lib/consts/consts.go @@ -17,15 +17,41 @@ 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": + 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 From 454aa3dbe785a7238d8f32972e1113e894e8c452 Mon Sep 17 00:00:00 2001 From: codebien <2103732+codebien@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:15:43 +0200 Subject: [PATCH 2/2] cmd/version: Drop the exported VersionDetails --- build-release.sh | 5 ----- lib/consts/consts.go | 3 --- 2 files changed, 8 deletions(-) diff --git a/build-release.sh b/build-release.sh index 3b14f732559..506e05a9cca 100755 --- a/build-release.sh +++ b/build-release.sh @@ -9,8 +9,6 @@ export OUT_DIR="${1-dist}" # To override the latest git tag as the version, pass something else as the second arg. export VERSION=${2:-$(git describe --tags --always --dirty)} -# To overwrite the version details, pass something as the third arg. Empty string disables it. -export VERSION_DETAILS=${3-"$(date -u +"%FT%T%z")/$(git describe --tags --always --long --dirty)"} set +x build() { @@ -18,9 +16,6 @@ build() { local NAME="k6-${VERSION}-${ALIAS}" local BUILD_ARGS=(-o "${OUT_DIR}/${NAME}/k6${SUFFIX}" -trimpath) - if [ -n "$VERSION_DETAILS" ]; then - BUILD_ARGS+=(-ldflags "-X go.k6.io/k6/lib/consts.VersionDetails=$VERSION_DETAILS") - fi local PACKAGE_FORMATS IFS="," read -ra PACKAGE_FORMATS <<< "${3}" diff --git a/lib/consts/consts.go b/lib/consts/consts.go index e0391afdcc6..709013f00b5 100644 --- a/lib/consts/consts.go +++ b/lib/consts/consts.go @@ -10,9 +10,6 @@ import ( // Version contains the current semantic version of k6. const Version = "0.46.0" -// VersionDetails can be set externally as part of the build process -var VersionDetails = "" //nolint:gochecknoglobals - // FullVersion returns the maximally full version and build information for // the currently running k6 executable. func FullVersion() string {