In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the fetch code was modified to use batched updates to provide a good performance improvement. Batched updates are used both for the fetched references and to backfill tags.
When using batched updates, the references aren't yet committed to disk when we start backfilling tags. This means in situations such as shallow fetching the negotiation during backfilling tags, the newly fetched refs are not available for advertisement in the 'haves' section. This can cause the server to send all objects the client already has.
The issue always existed when the '--atomic' flag was used, as both the regular ref updates and the tags need to be part of the same transaction.
Fix this by committing the previous batched update and initiating a new one for backfilling tags. Use a separate error variable for tracking the reference updates: One, it ensures that errors from the regular ref updates don't cause a total failure. Two, if starting the transaction for backfilling tags fails, only those updates are not made to disk. The initial error is now printed straight away to clear the buffer for further errors.
Add two tests, one which validates that the 'haves' are filled when backfilling tags. The other, to test that upstream is still set and tags are backfilled even if the main ref update is rejected.
While this does make it a little slower than master, due to two transactions:
Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = master)
Time (mean ± σ): 82.1 ms ± 3.3 ms [User: 60.2 ms, System: 24.4 ms]
Range (min … max): 77.4 ms … 89.4 ms 33 runs
Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)
Time (mean ± σ): 82.6 ms ± 2.4 ms [User: 59.3 ms, System: 25.2 ms]
Range (min … max): 78.1 ms … 88.7 ms 31 runs
Summary
fetch: many refs (refformat = reftable, refcount = 10000, revision = master) ran
1.01 ± 0.05 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)
It is still faster than not using batched updates:
Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)
Time (mean ± σ): 1.468 s ± 0.041 s [User: 0.839 s, System: 0.587 s]
Range (min … max): 1.427 s … 1.558 s 10 runs
Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)
Time (mean ± σ): 84.4 ms ± 1.7 ms [User: 60.9 ms, System: 25.8 ms]
Range (min … max): 81.4 ms … 88.6 ms 29 runs
Summary
fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran
17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)
Reported-by: Mitja Bezenšek <mitja.bezensek@login5.org>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
The issue was reported by Mitja Bezenšek on GitLab's Git repo [1].
[1]: https://gitlab.com/gitlab-org/git/-/work_items/799
---
Changes in v3:
- Fix the early exit, so a failed transaction_begin() only skips the
backfill.
- Keep the first commit's result separately, so rejections don't skip
the upstream and remote-HEAD setup.
- Test that the backfill advertises the fetched tip as a have, instead
of checking that the root object isn't fetched, which passed even
without the fix.
- Add a test for the upstream setup when a main ref is rejected and a
tag is backfilled.
- Link to v2: https://patch.msgid.link/20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com
Changes in v2:
- Add benchmarks between master and this branch to show perf regression.
- Instead of checking the trace file, simply check that we didn't fetch
older objects in the test.
- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com
CC: Patrick Steinhardt <ps@pks.im>
CC: Justin Tobler <jltobler@gmail.com>
---
builtin/fetch.c | 38 +++++++++++++++++++++++++++++++++++---
t/t5510-fetch.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+), 3 deletions(-)
Show changes to 2 files +77 −4
builtin/fetch.c, t/t5510-fetch.sh
diff --git a/builtin/fetch.c b/builtin/fetch.c
index b2decc6cfd..34fc37b58a 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1923,6 +1923,7 @@ static int do_fetch(struct transport *transport,
struct strmap rejected_refs = STRMAP_INIT;
int summary_width = 0;
int follow_remote_head;
+ int commit_ref_ret = 0;
if (tags == TAGS_DEFAULT) {
if (transport->remote->fetch_tags == 2)
@@ -2076,6 +2077,30 @@ static int do_fetch(struct transport *transport,
struct ref *tags_ref_map = NULL, **tail = &tags_ref_map;
find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);
+
+ /*
+ * As negotiation only reads references from disk, commit the references
+ * fetched so far, so that the backfill negotiation can advertise them as
+ * 'have's. Otherwise the server may re-send objects.
+ */
+ if (tags_ref_map && !atomic_fetch) {
+ commit_ref_ret = commit_ref_transaction(&transaction, false,
+ transport->remote->name,
+ &rejected_refs, &err);
+ if (err.len) {
+ error("%s", err.buf);
+ strbuf_reset(&err);
+ }
+
+ transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
+ REF_TRANSACTION_ALLOW_FAILURE, &err);
+ if (!transaction) {
+ free_refs(tags_ref_map);
+ tags_ref_map = NULL;
+ commit_ref_ret = -1;
+ }
+ }
+
if (tags_ref_map) {
/*
* If backfilling of tags fails then we want to tell
@@ -2099,9 +2124,16 @@ static int do_fetch(struct transport *transport,
if (verbosity >= 0)
summary_width = transport_summary_width(ref_map);
- retcode = commit_ref_transaction(&transaction, atomic_fetch,
- transport->remote->name,
- &rejected_refs, &err);
+ /*
+ * The transaction may have failed to initialize, meaning there is nothing
+ * to commit. Carry forward with other operations in such cases.
+ */
+ if (transaction)
+ retcode = commit_ref_transaction(&transaction, atomic_fetch,
+ transport->remote->name,
+ &rejected_refs, &err);
+ retcode |= commit_ref_ret;
+
/*
* With '--atomic', bail out if the transaction fails. Without '--atomic',
* continue to fetch head and perform other post-fetch operations.
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index 300bd5396d..0baf18e01e 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -1942,6 +1942,23 @@ test_expect_success "backfill tags when providing a refspec" '
test_cmp expect actual
'
+test_expect_success 'backfilling tags after shallow fetch advertises fetched refs as haves' '
+ test_when_finished rm -rf source target trace &&
+
+ git init source &&
+ test_commit_bulk -C source 10 &&
+ git -C source tag -a tag -m tag HEAD~2 &&
+ HEAD_OID=$(git -C source rev-parse HEAD) &&
+
+ git init target &&
+ git -C target remote add origin ../source &&
+ GIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \
+ git -C target fetch --depth 5 origin &&
+
+ test_grep "fetch> have $HEAD_OID" trace &&
+ git -C target rev-parse --verify refs/tags/tag
+'
+
test_expect_success REFFILES "FETCH_HEAD is updated even if ref updates fail" '
test_when_finished rm -rf base repo &&
@@ -2005,6 +2022,31 @@ test_expect_success REFFILES "upstream tracking info is added even with conflict
)
'
+test_expect_success REFFILES "upstream tracking info is added with conflicts and backfilled tags" '
+ test_when_finished rm -rf base repo &&
+
+ git init --initial-branch=main base &&
+ test_commit -C base one &&
+ test_commit -C base two &&
+ git -C base branch foo &&
+ # Backfilling only takes place for tags which are not pointing at tips.
+ git -C base tag -a annotated -m annotated HEAD~1 &&
+
+ git init --bare --initial-branch=main repo &&
+ (
+ cd repo &&
+ git remote add origin ../base &&
+ mkdir -p refs/remotes/origin &&
+ >refs/remotes/origin/foo.lock &&
+ test_must_fail git fetch origin --set-upstream main \
+ "+refs/heads/*:refs/remotes/origin/*" &&
+ git config get branch.main.remote >actual &&
+ echo "origin" >expect &&
+ test_cmp expect actual &&
+ git rev-parse --verify refs/tags/annotated
+ )
+'
+
test_expect_success REFFILES "HEAD is updated even with conflicts" '
test_when_finished rm -rf base repo &&
---
base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd
change-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0
Thanks
- Karthik