Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/version: Commit hash #3327

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ 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() {
local ALIAS="$1" SUFFIX="${2}" # Any other arguments are passed to the go build command as env vars
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}"
Expand Down
39 changes: 31 additions & 8 deletions lib/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,45 @@ 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 {
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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We even have vcs.time so ... I guess we can even add that if somebody wants 🤷

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
Expand Down