From: Karthik Nayak Date: Wed, 28 Oct 2026 09:47:58 GMT Subject: [PATCH v5 0/3] fetch: advertise fetched refs when backfilling tags Message-ID: <20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com> In-Reply-To: <20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com> The issue was reported by Mitja Bezenšek on GitLab's Git repo [1]. When fetching, git-fetch(1) performs the reference updates in two steps: 1. Fetches the regular references 2. Backfills any tags With batched updates, both of these operations came under the same reference transaction. Unfortunately, this meant that the initial references fetched were not committed and couldn't be used as 'haves' during the negotiation when backfilling tags. In shallow repositories, this could mean that the server was sending objects the client already possessed. This series fixes this regression. Earlier versions (1-3) tried to fix this by splitting the transaction into two. Which works, but makes the code more complex and is a bit messy overall as we have to deal with errors and also jumps in the codeflow. Justin suggested that we could use the references already available for negotiation instead, that is what v4 onwards does. [1]: https://gitlab.com/gitlab-org/git/-/work_items/799 Signed-off-by: Karthik Nayak --- Changes in v5: - Add a patch fixing a pre-existing leak in the remote-helper transport's cleanup. This allows the patch to drop its manual cleanup. - Move the upstream-info test into its own commit. - Some cleanpus around the commit message and grammar - Link to v4: https://patch.msgid.link/20261022-799-shallow-fetch-with-tags-v4-1-8a1c9ef399ed@gmail.com Changes in v4: - Instead of committing the first transaction and starting a new one, advertise the refs already fetched when backfilling tags. Thanks to Justin for the suggestion. This also fixes the `--atomic` flow. - Link to v3: https://patch.msgid.link/20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com 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 CC: Justin Tobler --- Karthik Nayak (3): transport: add `transport_options_release()` fetch: advertise fetched refs when backfilling tags t5510: cover upstream info update on fetch failure builtin/fetch.c | 33 ++++++++++++++++++++++++++++-- t/t5510-fetch.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ transport-helper.c | 1 + transport.c | 24 +++++++++++++--------- transport.h | 2 ++ 5 files changed, 107 insertions(+), 12 deletions(-) Range-diff versus v4: -: ---------- > 1: 583369ce44 transport: add `transport_options_release()` 1: 8e21cf3377 ! 2: 3a53029bb1 fetch: advertise fetched refs when backfilling tags @@ Commit message `negotiation_include_tips` field. This also fixes the issue for `--atomic` fetches since it follows the same route. - Add three tests, two which validates that the 'haves' are filled when - backfilling tags for atomic and non-atomic flows. The third, to test - that upstream is still set and tags are backfilled even if a sibling ref - is rejected. + This does mean that all references fetched before the backfill will be + used for the negotiation. While duplicate OIDs are trimmed before + negotiation, we could still end up with a lot of distinct OIDs used in + the 'haves' section. However, this is still better than the server + transferring already existing objects to the client, which can take a + lot more time. + + Add two tests, which validate that the 'haves' are filled when + backfilling tags for atomic and non-atomic flows. Reported-by: Mitja Bezenšek Signed-off-by: Karthik Nayak @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state, + struct list_objects_filter_options *filter_options, + const struct oid_array *fetched_tips) { -+ struct oid_array *to_free = NULL; int retcode, cannot_reuse; - /* @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state, transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); transport_set_option(transport, TRANS_OPT_DEPTH, "0"); @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state, + struct oid_array **include_tips = + &transport->smart_options->negotiation_include_tips; + if (!*include_tips) -+ *include_tips = to_free = xcalloc(1, sizeof(**include_tips)); ++ *include_tips = xcalloc(1, sizeof(**include_tips)); + for (size_t i = 0; i < fetched_tips->nr; i++) + oid_array_append(*include_tips, &fetched_tips->oid[i]); + } @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state, retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map, fetch_head, config, display_array); -+ if (to_free) { -+ oid_array_clear(to_free); -+ free(to_free); -+ transport->smart_options->negotiation_include_tips = NULL; -+ } -+ - if (gsecondary) { - transport_disconnect(gsecondary); - gsecondary = NULL; @@ builtin/fetch.c: static int do_fetch(struct transport *transport, find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail); if (tags_ref_map) { + struct oid_array fetched_tips = OID_ARRAY_INIT; -+ struct ref *rm; + + /* + * Collect the tips we just fetched so the tag backfill + * can advertise them to the server as 'haves'. + */ -+ for (rm = ref_map; rm; rm = rm->next) ++ for (struct ref *rm = ref_map; rm; rm = rm->next) + if (!is_null_oid(&rm->old_oid)) + oid_array_append(&fetched_tips, &rm->old_oid); + @@ t/t5510-fetch.sh: test_expect_success "backfill tags when providing a refspec" ' test_expect_success REFFILES "FETCH_HEAD is updated even if ref updates fail" ' test_when_finished rm -rf base repo && -@@ t/t5510-fetch.sh: 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 && - -: ---------- > 3: e8a5ce9e5e t5510: cover upstream info update on fetch failure --- base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd change-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0 Thanks - Karthik