Compare commits

...

2 Commits

Author SHA1 Message Date
Marcin Rataj
2f130fd254
Merge bbefaea82e into 2896aed9f4 2026-02-19 15:00:50 +00:00
Marcin Rataj
bbefaea82e fix(version): produce shorter user agent for tagged release builds
when building from a version-tagged commit with a clean tree, omit the
redundant git commit hash from the libp2p user agent string, saving
bytes in HTTP requests and libp2p identify. `ipfs version --commit`
still reports the full commit hash.

before (tagged release): kubo/0.37.0/6898472
after  (tagged release): kubo/0.37.0

non-tagged and dirty builds are unaffected.
2026-02-19 01:31:03 +01:00
3 changed files with 27 additions and 8 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)
}