Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
refactor: improve binary version build string
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci committed Jan 5, 2024
1 parent 333ab74 commit c6c3ae7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
VERSION ?= "$(shell git describe --tags --abbrev=0 | cut -c2-)"
COMMIT_HASH ?= "$(shell git describe --long --dirty --always --match "" || true)"

LDFLAGS ?= -s -w \
-X github.com/primevprotocol/mev-commit.version="$(VERSION)" \
-X github.com/primevprotocol/mev-commit.commitHash="$(COMMIT_HASH)"
-X github.com/primevprotocol/mev-commit.version=$(shell git describe --tags)

.PHONY: build
build: export CGO_ENABLED=0
Expand Down
76 changes: 70 additions & 6 deletions version.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
package mevcommit

import (
"runtime/debug"
"strings"
)

var (
version string
commitHash string
// version will be the version git tag if the binary is built with
// the Makefile and the tag is set. If the tag does not exist the
// version will be set to "(devel)". If the binary is built some
// other way, it will be set to "unknown".
version = "unknown"

// revision is set from the vcs.revision tag in Go 1.18+.
revision = "unknown"

// dirtyBuild is set from the vcs.modified tag in Go 1.18+.
dirtyBuild = true
)

// Version returns the version of the binary.
func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, kv := range info.Settings {
if kv.Value == "" {
continue
}
switch kv.Key {
case "vcs.revision":
revision = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}

}

// Version returns the build version string of the binary in format:
//
// <tag>-<commit-hash>[-dirty]
//
// If the tag is not set during build (the tag does not exist or the binary is
// not build with the Makefile), the tag element will remain empty. If the
// binary is built outside the vcs repository, the returned result will be
// "devel". If the binary is built with the Makefile and the repository
// is dirty, the -dirty element will be appended to the result.
//
// Examples:
//
// "devel"
// "rev-333ab74"
// "rev-333ab74-dirty"
// "v1.0.0-rev-333ab74"
// "v1.0.0-rev-333ab74-dirty"
func Version() string {
if version == "" || commitHash == "" {
return "dev-dirty"
parts := make([]string, 0, 3)
if version != "unknown" && version != "" {
parts = append(parts, version)
}
if revision != "unknown" && revision != "" {
parts = append(parts, "rev")
commit := revision
if len(commit) > 7 {
commit = commit[:7]
}
parts = append(parts, commit)
if dirtyBuild {
parts = append(parts, "dirty")
}
}
if len(parts) == 0 {
return "devel"
}
return version + "-" + commitHash
return strings.Join(parts, "-")
}

0 comments on commit c6c3ae7

Please sign in to comment.