diff --git a/cmd/ipfs/Rules.mk b/cmd/ipfs/Rules.mk index a1590def8..7dca2c3e9 100644 --- a/cmd/ipfs/Rules.mk +++ b/cmd/ipfs/Rules.mk @@ -12,7 +12,7 @@ PATH := $(realpath $(d)):$(PATH) # DEPS_OO_$(d) += merkledag/pb/merkledag.pb.go namesys/pb/namesys.pb.go # DEPS_OO_$(d) += pin/internal/pb/header.pb.go unixfs/pb/unixfs.pb.go -$(d)_flags =-ldflags="-X "github.com/ipfs/kubo".CurrentCommit=$(git-hash)" +$(d)_flags =-ldflags="-X "github.com/ipfs/kubo".CurrentCommit=$(git-hash) -X "github.com/ipfs/kubo".taggedRelease=$(git-tag)" $(IPFS_BIN_$(d)): GOFLAGS += $(cmd/ipfs_flags) diff --git a/mk/git.mk b/mk/git.mk index a4b618e0c..9474c2d33 100644 --- a/mk/git.mk +++ b/mk/git.mk @@ -2,3 +2,11 @@ # If that fails (e.g., we're building a docker image and have an empty objects # directory), assume the source isn't dirty and build anyways. git-hash:=$(shell git describe --always --match=NeVeRmAtCh --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null) + +# Detect if HEAD is a clean, tagged release. Used to omit redundant commit +# hash from the libp2p user agent (the version number suffices). +ifeq ($(findstring dirty,$(git-hash)),) + git-tag:=$(shell git tag --points-at HEAD 2>/dev/null | grep '^v' | head -1) +else + git-tag:= +endif diff --git a/version.go b/version.go index f2e3a81b8..78b0a2e28 100644 --- a/version.go +++ b/version.go @@ -10,6 +10,12 @@ import ( // CurrentCommit is the current git commit, this is set as a ldflag in the Makefile. var CurrentCommit string +// taggedRelease is set via ldflag when building from a version-tagged commit +// with a clean tree. When set, the commit hash is omitted from the libp2p +// identify agent version and the HTTP user agent, since the version number +// already identifies the exact source. +var taggedRelease string + // CurrentVersionNumber is the current application's version literal. const CurrentVersionNumber = "0.40.0-rc1" @@ -19,15 +25,20 @@ const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint const RepoVersion = 18 // GetUserAgentVersion is the libp2p user agent used by go-ipfs. -// -// Note: This will end in `/` when no commit is available. This is expected. func GetUserAgentVersion() string { - userAgent := "kubo/" + CurrentVersionNumber + "/" + CurrentCommit + // For tagged release builds with a clean tree, the commit hash is + // redundant since the version number identifies the exact source. + commit := CurrentCommit + if taggedRelease != "" { + commit = "" + } + + userAgent := "kubo/" + CurrentVersionNumber + if commit != "" { + userAgent += "/" + commit + } if userAgentSuffix != "" { - if CurrentCommit != "" { - userAgent += "/" - } - userAgent += userAgentSuffix + userAgent += "/" + userAgentSuffix } return cmdutils.CleanAndTrim(userAgent) }