Compare commits

...

3 Commits

Author SHA1 Message Date
Marcin Rataj
301afb4150
Merge 895aec35a2 into 2896aed9f4 2026-02-20 17:33:06 +00:00
Marcin Rataj
895aec35a2 chore: set version to 0.40.0-rc2 2026-02-20 18:21:38 +01:00
Marcin Rataj
1e5e6e35cf 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-20 18:21:31 +01:00
5 changed files with 112 additions and 14 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) += merkledag/pb/merkledag.pb.go namesys/pb/namesys.pb.go
# DEPS_OO_$(d) += pin/internal/pb/header.pb.go unixfs/pb/unixfs.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) $(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 # 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. # 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) 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

@ -16,12 +16,12 @@ test_id_compute_agent() {
else else
AGENT_COMMIT="${AGENT_COMMIT##$AGENT_VERSION-}" AGENT_COMMIT="${AGENT_COMMIT##$AGENT_VERSION-}"
fi fi
AGENT_VERSION="kubo/$AGENT_VERSION/$AGENT_COMMIT" AGENT_VERSION="kubo/$AGENT_VERSION"
if test -n "$AGENT_SUFFIX"; then
if test -n "$AGENT_COMMIT"; then if test -n "$AGENT_COMMIT"; then
AGENT_VERSION="$AGENT_VERSION/" AGENT_VERSION="$AGENT_VERSION/$AGENT_COMMIT"
fi fi
AGENT_VERSION="$AGENT_VERSION$AGENT_SUFFIX" if test -n "$AGENT_SUFFIX"; then
AGENT_VERSION="$AGENT_VERSION/$AGENT_SUFFIX"
fi fi
echo "$AGENT_VERSION" echo "$AGENT_VERSION"
} }

View File

@ -10,8 +10,14 @@ import (
// CurrentCommit is the current git commit, this is set as a ldflag in the Makefile. // CurrentCommit is the current git commit, this is set as a ldflag in the Makefile.
var CurrentCommit string 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. // CurrentVersionNumber is the current application's version literal.
const CurrentVersionNumber = "0.40.0-rc1" const CurrentVersionNumber = "0.40.0-rc2"
const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint
@ -19,15 +25,20 @@ const ApiVersion = "/kubo/" + CurrentVersionNumber + "/" //nolint
const RepoVersion = 18 const RepoVersion = 18
// GetUserAgentVersion is the libp2p user agent used by go-ipfs. // 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 { func GetUserAgentVersion() string {
userAgent := "kubo/" + CurrentVersionNumber + "/" + CurrentCommit // For tagged release builds with a clean tree, the commit hash is
if userAgentSuffix != "" { // redundant since the version number identifies the exact source.
if CurrentCommit != "" { commit := CurrentCommit
userAgent += "/" if taggedRelease != "" {
commit = ""
} }
userAgent += userAgentSuffix
userAgent := "kubo/" + CurrentVersionNumber
if commit != "" {
userAgent += "/" + commit
}
if userAgentSuffix != "" {
userAgent += "/" + userAgentSuffix
} }
return cmdutils.CleanAndTrim(userAgent) return cmdutils.CleanAndTrim(userAgent)
} }

79
version_test.go Normal file
View File

@ -0,0 +1,79 @@
package ipfs
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestGetUserAgentVersion verifies the user agent string used in libp2p
// identify and HTTP requests. Tagged release builds (where the commit matches
// the tag) skip the commit hash from the agent version, since the version
// number already identifies the exact source.
func TestGetUserAgentVersion(t *testing.T) {
origCommit := CurrentCommit
origTagged := taggedRelease
origSuffix := userAgentSuffix
t.Cleanup(func() {
CurrentCommit = origCommit
taggedRelease = origTagged
userAgentSuffix = origSuffix
})
tests := []struct {
name string
commit string
tagged string
suffix string
expected string
}{
// dev builds without ldflags
{
name: "no commit, no suffix",
expected: "kubo/" + CurrentVersionNumber,
},
// dev builds with commit set via ldflags
{
name: "with commit",
commit: "abc1234",
expected: "kubo/" + CurrentVersionNumber + "/abc1234",
},
{
name: "with suffix, no commit",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
{
name: "with commit and suffix",
commit: "abc1234",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/abc1234/test-suffix",
},
// tagged release builds: commit is redundant because the version
// number already maps to an exact git tag, so it is omitted to
// save bytes in identify and HTTP user-agent headers.
{
name: "tagged release ignores commit",
commit: "abc1234",
tagged: "1",
expected: "kubo/" + CurrentVersionNumber,
},
{
name: "tagged release with suffix ignores commit",
commit: "abc1234",
tagged: "1",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CurrentCommit = tt.commit
taggedRelease = tt.tagged
SetUserAgentSuffix(tt.suffix)
assert.Equal(t, tt.expected, GetUserAgentVersion())
})
}
}