{"thread":{"id":"66493","subject":"[PATCH] fetch: commit references fetched before backfilling tags","startedAt":"2026-10-08T22:10:37Z","lastAt":"2026-10-29T14:00:25Z","messageCount":36,"participants":["Karthik Nayak","Patrick Steinhardt","Justin Tobler","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"554545","messageId":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","threadId":"66493","inReplyTo":null,"subject":"[PATCH] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-08T22:10:37Z","receivedAt":"2026-10-08T22:10:37Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Wherein batched updates were used to fetch both\nreferences and backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. This means in situations such as shallow\nfetching the negotiation during backfilling tags, the client doesn't\nhave any references to report in the 'have' section. Since backfilling\ndoesn't use a depth limit, this can cause the server to send all the\nobjects present in the repository.\n\nFix this by committing the previous batched update and initiating a new\none for backfilling tags. Also add a test which captures this regression.\n\nWhile this does make it a little slower than master, due to creation of\ntwo transactions, It is still faster than not using batched updates:\n\nBenchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n  Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n  Range (min … max):    1.427 s …  1.558 s    10 runs\n\nBenchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n  Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n  Range (min … max):    81.4 ms …  88.6 ms    29 runs\n\nSummary\n  fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n   17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nThe issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n---\n builtin/fetch.c  | 21 +++++++++++++++++++++\n t/t5510-fetch.sh | 17 +++++++++++++++++\n 2 files changed, 38 insertions(+)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..68b04d0f8a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -2076,6 +2076,27 @@ static int do_fetch(struct transport *transport,\n \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n+\n+\t\t/*\n+\t\t * Backfilling tags has no depth limit. If we don't commit\n+\t\t * the fetched references, the backfill will report no refs\n+\t\t * in the 'have' section of the negotiation. This can cause\n+\t\t * the server to send all objects.\n+\t\t */\n+\t\tif (tags_ref_map && !atomic_fetch) {\n+\t\t\tretcode |= commit_ref_transaction(&transaction, false,\n+\t\t\t\t\t\t\t  transport->remote->name,\n+\t\t\t\t\t\t\t  &rejected_refs, &err);\n+\n+\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n+\t\t\tif (!transaction) {\n+\t\t\t\tfree_refs(tags_ref_map);\n+\t\t\t\tretcode = -1;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\t\t}\n+\n \t\tif (tags_ref_map) {\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..81865c1ecc 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,23 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'shallow fetch does not fetch objects again for tags' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --depth 5 origin &&\n+\n+\ttest $(grep -c \"fetch> command=fetch\" trace) -gt 2 &&\n+\ttest $(grep -c \"fetch> have $HEAD_OID\" trace) -eq 2\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"554573","messageId":"asjPWXAO3Cwpkerk@pks.im","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"Re: [PATCH] fetch: commit references fetched before backfilling tags","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-09T11:26:17Z","receivedAt":"2026-10-09T11:26:17Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Oct 09, 2026 at 12:10:37AM +0200, Karthik Nayak wrote:\n> In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n> fetch code was modified to use batched updates to provide a good\n> performance improvement. Wherein batched updates were used to fetch both\n> references and backfill tags.\n> \n> When using batched updates, the references aren't yet committed to disk\n> when we start backfilling tags. This means in situations such as shallow\n> fetching the negotiation during backfilling tags, the client doesn't\n> have any references to report in the 'have' section. Since backfilling\n> doesn't use a depth limit, this can cause the server to send all the\n> objects present in the repository.\n\nSo in my own words: the server sends the reference, we queue them in a\ntransaction, but don't commit it yet. We then try to backfill tags, and\nbecause we don't have the refs committed yet the backfill will think we\ndon't have any of the relevant commits that those tags point to.\nConsequently, the packfile negotiation will result in way more objects\nbeing fetched than necessary.\n\nThis makes me wonder why we even do a proper fetch. In theory, we could\nbasically just ask the server for the individual tagged objects without\nperforming any negotiation, right?\n\nOr... well, would that work with nested annotated tags? No idea.\n\n> Fix this by committing the previous batched update and initiating a new\n> one for backfilling tags. Also add a test which captures this regression.\n> \n> While this does make it a little slower than master, due to creation of\n> two transactions, It is still faster than not using batched updates:\n> \n> Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n>   Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n>   Range (min … max):    1.427 s …  1.558 s    10 runs\n> \n> Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n>   Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n>   Range (min … max):    81.4 ms …  88.6 ms    29 runs\n> \n> Summary\n>   fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n>    17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n\nI was expecting to also see HEAD~ here to back up your claim that this\nis a bit slower than master.\n\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index b2decc6cfd..68b04d0f8a 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -2076,6 +2076,27 @@ static int do_fetch(struct transport *transport,\n>  \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n>  \n>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n> +\n> +\t\t/*\n> +\t\t * Backfilling tags has no depth limit. If we don't commit\n> +\t\t * the fetched references, the backfill will report no refs\n> +\t\t * in the 'have' section of the negotiation. This can cause\n> +\t\t * the server to send all objects.\n> +\t\t */\n> +\t\tif (tags_ref_map && !atomic_fetch) {\n> +\t\t\tretcode |= commit_ref_transaction(&transaction, false,\n> +\t\t\t\t\t\t\t  transport->remote->name,\n> +\t\t\t\t\t\t\t  &rejected_refs, &err);\n> +\n> +\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n> +\t\t\tif (!transaction) {\n> +\t\t\t\tfree_refs(tags_ref_map);\n> +\t\t\t\tretcode = -1;\n> +\t\t\t\tgoto cleanup;\n> +\t\t\t}\n> +\t\t}\n\nOkay, makes sense. `commit_ref_transaction()` knows to already handle\nthe failures for us and print them. And `rejected_refs` is basically\nbeing treated additive, so if both transactions have some failures then\nthe map will contain the combined set of rejected refs.\n\n> diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\n> index 300bd5396d..81865c1ecc 100755\n> --- a/t/t5510-fetch.sh\n> +++ b/t/t5510-fetch.sh\n> @@ -1942,6 +1942,23 @@ test_expect_success \"backfill tags when providing a refspec\" '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'shallow fetch does not fetch objects again for tags' '\n> +\ttest_when_finished rm -rf source target trace &&\n> +\n> +\tgit init source &&\n> +\ttest_commit_bulk -C source 10 &&\n> +\tgit -C source tag -a tag -m tag HEAD~2 &&\n> +\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n> +\n> +\tgit init target &&\n> +\tgit -C target remote add origin ../source &&\n> +\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n> +\t\tgit -C target fetch --depth 5 origin &&\n> +\n> +\ttest $(grep -c \"fetch> command=fetch\" trace) -gt 2 &&\n> +\ttest $(grep -c \"fetch> have $HEAD_OID\" trace) -eq 2\n> +'\n\nYou don't really verify that we don't re-fetch objects, you only verify\nthat we provide \"have\" lines to the remote side. Which is ultimately the\nsame, but in a bit more of a roundabout way.\n\nThanks!\n\nPatrick\n\n"},{"id":"554622","messageId":"askY2aq8--2I2lEN@denethor","threadId":"66493","inReplyTo":"asjPWXAO3Cwpkerk@pks.im","subject":"Re: [PATCH] fetch: commit references fetched before backfilling tags","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-09T16:51:01Z","receivedAt":"2026-10-09T16:51:01Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/09 01:26PM, Patrick Steinhardt wrote:\n> On Fri, Oct 09, 2026 at 12:10:37AM +0200, Karthik Nayak wrote:\n> > In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n> > fetch code was modified to use batched updates to provide a good\n> > performance improvement. Wherein batched updates were used to fetch both\n> > references and backfill tags.\n> > \n> > When using batched updates, the references aren't yet committed to disk\n> > when we start backfilling tags. This means in situations such as shallow\n> > fetching the negotiation during backfilling tags, the client doesn't\n> > have any references to report in the 'have' section. Since backfilling\n> > doesn't use a depth limit, this can cause the server to send all the\n> > objects present in the repository.\n> \n> So in my own words: the server sends the reference, we queue them in a\n> transaction, but don't commit it yet. We then try to backfill tags, and\n> because we don't have the refs committed yet the backfill will think we\n> don't have any of the relevant commits that those tags point to.\n> Consequently, the packfile negotiation will result in way more objects\n> being fetched than necessary.\n> \n> This makes me wonder why we even do a proper fetch. In theory, we could\n> basically just ask the server for the individual tagged objects without\n> performing any negotiation, right?\n> \n> Or... well, would that work with nested annotated tags? No idea.\n\nIIUC, when we backfill tags, we only fetch tags that reference objects\nthat we have locally. The server advertises the tag reference OID and\nits recursively peeled non-tag OID so the client can figure this out:\n\n  efe1aaafb77990c4f023cec81b198e0af55bbfb5\trefs/tags/foo\n  c8dd1e3bb1152844983558802a52c9e4c17652b4\trefs/tags/foo^{}\n\nSo because there could be nested annontated tags, I think we would need\nto fetch to get the intermediate objects.\n\n-Justin\n\n"},{"id":"554666","messageId":"CAOLa=ZRML58095JPz1kzOyDTFwsdcoVx9jyxyf4gRppX1h8tVw@mail.gmail.com","threadId":"66493","inReplyTo":"askY2aq8--2I2lEN@denethor","subject":"Re: [PATCH] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-09T22:16:42Z","receivedAt":"2026-10-09T22:16:42Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> On 26/10/09 01:26PM, Patrick Steinhardt wrote:\n>> On Fri, Oct 09, 2026 at 12:10:37AM +0200, Karthik Nayak wrote:\n>> > In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n>> > fetch code was modified to use batched updates to provide a good\n>> > performance improvement. Wherein batched updates were used to fetch both\n>> > references and backfill tags.\n>> >\n>> > When using batched updates, the references aren't yet committed to disk\n>> > when we start backfilling tags. This means in situations such as shallow\n>> > fetching the negotiation during backfilling tags, the client doesn't\n>> > have any references to report in the 'have' section. Since backfilling\n>> > doesn't use a depth limit, this can cause the server to send all the\n>> > objects present in the repository.\n>>\n>> So in my own words: the server sends the reference, we queue them in a\n>> transaction, but don't commit it yet. We then try to backfill tags, and\n>> because we don't have the refs committed yet the backfill will think we\n>> don't have any of the relevant commits that those tags point to.\n>> Consequently, the packfile negotiation will result in way more objects\n>> being fetched than necessary.\n>>\n>> This makes me wonder why we even do a proper fetch. In theory, we could\n>> basically just ask the server for the individual tagged objects without\n>> performing any negotiation, right?\n>>\n>> Or... well, would that work with nested annotated tags? No idea.\n>\n> IIUC, when we backfill tags, we only fetch tags that reference objects\n> that we have locally. The server advertises the tag reference OID and\n> its recursively peeled non-tag OID so the client can figure this out:\n>\n>   efe1aaafb77990c4f023cec81b198e0af55bbfb5\trefs/tags/foo\n>   c8dd1e3bb1152844983558802a52c9e4c17652b4\trefs/tags/foo^{}\n>\n> So because there could be nested annontated tags, I think we would need\n> to fetch to get the intermediate objects.\n>\n> -Justin\n\nYup exactly this. Afaik the protocol also works by transferring the set\nof objects that form the closure of between have <> want.\n\nSo for:\n\n   foo -> [tag A] -> [tag B] -> [commit C]\n\nWe cannot simply request for A without saying we have C. We have C and\nwe know it, but the haves are obtained from the committed refs, so\nuntil refs are written to disk we never advertise C.\n"},{"id":"554667","messageId":"CAOLa=ZTBFox1K2rxzYT1x+rvJpiYiXLnjqkvummh=J-CFWY0vA@mail.gmail.com","threadId":"66493","inReplyTo":"asjPWXAO3Cwpkerk@pks.im","subject":"Re: [PATCH] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-09T22:31:29Z","receivedAt":"2026-10-09T22:31:29Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n[snip]\n\n>> Fix this by committing the previous batched update and initiating a new\n>> one for backfilling tags. Also add a test which captures this regression.\n>>\n>> While this does make it a little slower than master, due to creation of\n>> two transactions, It is still faster than not using batched updates:\n>>\n>> Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n>>   Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n>>   Range (min … max):    1.427 s …  1.558 s    10 runs\n>>\n>> Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n>>   Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n>>   Range (min … max):    81.4 ms …  88.6 ms    29 runs\n>>\n>> Summary\n>>   fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n>>    17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n>\n> I was expecting to also see HEAD~ here to back up your claim that this\n> is a bit slower than master.\n\nI had it and removed it thinking it was too much information. Will add\nit back in.\n\n[snip]\n\n>> diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\n>> index 300bd5396d..81865c1ecc 100755\n>> --- a/t/t5510-fetch.sh\n>> +++ b/t/t5510-fetch.sh\n>> @@ -1942,6 +1942,23 @@ test_expect_success \"backfill tags when providing a refspec\" '\n>>  \ttest_cmp expect actual\n>>  '\n>>\n>> +test_expect_success 'shallow fetch does not fetch objects again for tags' '\n>> +\ttest_when_finished rm -rf source target trace &&\n>> +\n>> +\tgit init source &&\n>> +\ttest_commit_bulk -C source 10 &&\n>> +\tgit -C source tag -a tag -m tag HEAD~2 &&\n>> +\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n>> +\n>> +\tgit init target &&\n>> +\tgit -C target remote add origin ../source &&\n>> +\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n>> +\t\tgit -C target fetch --depth 5 origin &&\n>> +\n>> +\ttest $(grep -c \"fetch> command=fetch\" trace) -gt 2 &&\n>> +\ttest $(grep -c \"fetch> have $HEAD_OID\" trace) -eq 2\n>> +'\n>\n> You don't really verify that we don't re-fetch objects, you only verify\n> that we provide \"have\" lines to the remote side. Which is ultimately the\n> same, but in a bit more of a roundabout way.\n>\n> Thanks!\n>\n> Patrick\n\nAh, that's what I wanted and realized the trace only says 'packfile'\nwithout listing whats in it.\n\nNow I realize I could just check the repo with `cat-file`. Let me do\nthat and clean this up.\n"},{"id":"554772","messageId":"20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"[PATCH v2] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-12T15:29:17Z","receivedAt":"2026-10-12T15:29:17Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Wherein batched updates were used to fetch both\nreferences and backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. This means in situations such as shallow\nfetching the negotiation during backfilling tags, the client doesn't\nhave any references to report in the 'have' section. Since backfilling\ndoesn't use a depth limit, this can cause the server to send all the\nobjects present in the repository.\n\nFix this by committing the previous batched update and initiating a new\none for backfilling tags. Also add a test which captures this regression.\n\nWhile this does make it a little slower than master, due to two\ntransactions:\n\nBenchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = master)\n  Time (mean ± σ):      82.1 ms ±   3.3 ms    [User: 60.2 ms, System: 24.4 ms]\n  Range (min … max):    77.4 ms …  89.4 ms    33 runs\n\nBenchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n  Time (mean ± σ):      82.6 ms ±   2.4 ms    [User: 59.3 ms, System: 25.2 ms]\n  Range (min … max):    78.1 ms …  88.7 ms    31 runs\n\nSummary\n  fetch: many refs (refformat = reftable, refcount = 10000, revision = master) ran\n   1.01 ± 0.05 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n\nIt is still faster than not using batched updates:\n\nBenchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n  Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n  Range (min … max):    1.427 s …  1.558 s    10 runs\n\nBenchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n  Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n  Range (min … max):    81.4 ms …  88.6 ms    29 runs\n\nSummary\n  fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n   17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nThe issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n---\nChanges in v2:\n- Add benchmarks between master and this branch to show perf regression.\n- Instead of checking the trace file, simply check that we didn't fetch\n  older objects in the test.\n- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com\n\nCC: Patrick Steinhardt <ps@pks.im>\nCC: Justin Tobler <jltobler@gmail.com>\n---\n builtin/fetch.c  | 21 +++++++++++++++++++++\n t/t5510-fetch.sh | 16 ++++++++++++++++\n 2 files changed, 37 insertions(+)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..68b04d0f8a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -2076,6 +2076,27 @@ static int do_fetch(struct transport *transport,\n \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n+\n+\t\t/*\n+\t\t * Backfilling tags has no depth limit. If we don't commit\n+\t\t * the fetched references, the backfill will report no refs\n+\t\t * in the 'have' section of the negotiation. This can cause\n+\t\t * the server to send all objects.\n+\t\t */\n+\t\tif (tags_ref_map && !atomic_fetch) {\n+\t\t\tretcode |= commit_ref_transaction(&transaction, false,\n+\t\t\t\t\t\t\t  transport->remote->name,\n+\t\t\t\t\t\t\t  &rejected_refs, &err);\n+\n+\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n+\t\t\tif (!transaction) {\n+\t\t\t\tfree_refs(tags_ref_map);\n+\t\t\t\tretcode = -1;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\t\t}\n+\n \t\tif (tags_ref_map) {\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..5ba372fbcb 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,22 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'shallow fetch does not fetch objects again for tags' '\n+\ttest_when_finished rm -rf source target &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\troot_oid=$(git -C source rev-parse HEAD~9) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tgit -C target fetch --depth 5 origin &&\n+\n+\t# Should not fetch objects beyond the pre-specified depth.\n+\ttest_must_fail git -C target cat-file -e \"$root_oid\"\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"554804","messageId":"xmqq4ieq66e8.fsf@gitster.g","threadId":"66493","inReplyTo":"20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com","subject":"Re: [PATCH v2] fetch: commit references fetched before backfilling tags","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-12T19:55:11Z","receivedAt":"2026-10-12T19:55:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> When using batched updates, the references aren't yet committed to disk\n> when we start backfilling tags. This means in situations such as shallow\n> fetching the negotiation during backfilling tags, the client doesn't\n> have any references to report in the 'have' section. Since backfilling\n> doesn't use a depth limit, this can cause the server to send all the\n> objects present in the repository.\n\nOuch.\n\n> Fix this by committing the previous batched update and initiating a new\n> one for backfilling tags. Also add a test which captures this regression.\n\n> @@ -2076,6 +2076,27 @@ static int do_fetch(struct transport *transport,\n>  \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n>  \n>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n> +\n> +\t\t/*\n> +\t\t * Backfilling tags has no depth limit. If we don't commit\n> +\t\t * the fetched references, the backfill will report no refs\n> +\t\t * in the 'have' section of the negotiation. This can cause\n> +\t\t * the server to send all objects.\n> +\t\t */\n\nDoes this problem only happen when the main fetch updated some ref,\nor can backfilling make such a huge negotiation even when the main\nfetch transaction is empty?\n\n> +\t\tif (tags_ref_map && !atomic_fetch) {\n> +\t\t\tretcode |= commit_ref_transaction(&transaction, false,\n> +\t\t\t\t\t\t\t  transport->remote->name,\n> +\t\t\t\t\t\t\t  &rejected_refs, &err);\n\nThis block is entered only when \"--atomic\" is not in effect, because\nwe want to update all refs or nothing, and these \"all refs\"\npresumably includes the followed tags, but does it mean those who\nwant atomic fetch should be prepared to see their fetch degenerate\ndown to a full clone?\n\n> +\n> +\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n> +\t\t\tif (!transaction) {\n> +\t\t\t\tfree_refs(tags_ref_map);\n> +\t\t\t\tretcode = -1;\n> +\t\t\t\tgoto cleanup;\n\nIs this jumping a bit too far?  If our primary fetch have finished\nsuccessfully, and opening the next transaction only for tag backfill\ntask has failed, there are tons of processing happening we skip with\nthis new \"goto\" that we would have normally performed, starting with\ncomputing summary_width used for later reporting, writing out\nFETCH_HEAD, and setting upstream for the current branch, all of\nwhich we should be able to do as long as the primary fetch\nsuccessfully finishes, no?\n\n> +\t\t\t}\n> +\t\t}\n\n> diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\n> index 300bd5396d..5ba372fbcb 100755\n> --- a/t/t5510-fetch.sh\n> +++ b/t/t5510-fetch.sh\n> @@ -1942,6 +1942,22 @@ test_expect_success \"backfill tags when providing a refspec\" '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'shallow fetch does not fetch objects again for tags' '\n> +\ttest_when_finished rm -rf source target &&\n> +\n> +\tgit init source &&\n> +\ttest_commit_bulk -C source 10 &&\n> +\tgit -C source tag -a tag -m tag HEAD~2 &&\n> +\troot_oid=$(git -C source rev-parse HEAD~9) &&\n> +\n> +\tgit init target &&\n> +\tgit -C target remote add origin ../source &&\n> +\tgit -C target fetch --depth 5 origin &&\n> +\n> +\t# Should not fetch objects beyond the pre-specified depth.\n> +\ttest_must_fail git -C target cat-file -e \"$root_oid\"\n> +'\n> +\n>  test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n>  \ttest_when_finished rm -rf base repo &&\n>  \n>\n> ---\n> base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n> change-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n>\n>\n> Thanks\n> - Karthik\n\n"},{"id":"555129","messageId":"CAOLa=ZTX16P9=Sbp3z5LpYoTzgZDb8hdk4x5xaSBbCh+dBvh4w@mail.gmail.com","threadId":"66493","inReplyTo":"xmqq4ieq66e8.fsf@gitster.g","subject":"Re: [PATCH v2] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-15T10:31:35Z","receivedAt":"2026-10-15T10:31:35Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> When using batched updates, the references aren't yet committed to disk\n>> when we start backfilling tags. This means in situations such as shallow\n>> fetching the negotiation during backfilling tags, the client doesn't\n>> have any references to report in the 'have' section. Since backfilling\n>> doesn't use a depth limit, this can cause the server to send all the\n>> objects present in the repository.\n>\n> Ouch.\n>\n\nIndeed.\n\n>> Fix this by committing the previous batched update and initiating a new\n>> one for backfilling tags. Also add a test which captures this regression.\n>\n>> @@ -2076,6 +2076,27 @@ static int do_fetch(struct transport *transport,\n>>  \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n>>\n>>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n>> +\n>> +\t\t/*\n>> +\t\t * Backfilling tags has no depth limit. If we don't commit\n>> +\t\t * the fetched references, the backfill will report no refs\n>> +\t\t * in the 'have' section of the negotiation. This can cause\n>> +\t\t * the server to send all objects.\n>> +\t\t */\n>\n> Does this problem only happen when the main fetch updated some ref,\n> or can backfilling make such a huge negotiation even when the main\n> fetch transaction is empty?\n>\n\nOnly when the main fetch updates the on-disk state. If there are no refs\nupdated in the main transaction, then there is nothing that we could've\nused in the negotiation anyway.\n\nI will modify the comment a bit, as the 'will report no refs' is a bit\ninaccurate, since it will only not report the refs we didn't yet commit.\n\n>> +\t\tif (tags_ref_map && !atomic_fetch) {\n>> +\t\t\tretcode |= commit_ref_transaction(&transaction, false,\n>> +\t\t\t\t\t\t\t  transport->remote->name,\n>> +\t\t\t\t\t\t\t  &rejected_refs, &err);\n>\n> This block is entered only when \"--atomic\" is not in effect, because\n> we want to update all refs or nothing, and these \"all refs\"\n> presumably includes the followed tags, but does it mean those who\n> want atomic fetch should be prepared to see their fetch degenerate\n> down to a full clone?\n>\n\nYes, '--atomic' always had this issue. There's not much we can do there\nto fix it. I will add it to my commit message though.\n\n>> +\n>> +\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n>> +\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n>> +\t\t\tif (!transaction) {\n>> +\t\t\t\tfree_refs(tags_ref_map);\n>> +\t\t\t\tretcode = -1;\n>> +\t\t\t\tgoto cleanup;\n>\n> Is this jumping a bit too far?  If our primary fetch have finished\n> successfully, and opening the next transaction only for tag backfill\n> task has failed, there are tons of processing happening we skip with\n> this new \"goto\" that we would have normally performed, starting with\n> computing summary_width used for later reporting, writing out\n> FETCH_HEAD, and setting upstream for the current branch, all of\n> which we should be able to do as long as the primary fetch\n> successfully finishes, no?\n>\n>> +\t\t\t}\n>> +\t\t}\n>\n\nHmm. Yeah, you have a point. I think we'll have to differentiate between\n`retcode` and a new `backfill_retcode`. Will add that in.\n\n[snip]\n\nWhile testing more with regards to your comments, I realized the new\ntest doesn't really catch the issue, since we only transfer objects we\nalready had, so checking for root_oid will fail with/without the fix.\nWill revert the test to the older version.\n"},{"id":"555198","messageId":"20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"[PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-15T21:24:46Z","receivedAt":"2026-10-15T21:24:46Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Batched updates are used both for the fetched\nreferences and to backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. This means in situations such as shallow\nfetching the negotiation during backfilling tags, the newly fetched refs\nare not available for advertisement in the 'haves' section. This can\ncause the server to send all objects the client already has.\n\nThe issue always existed when the '--atomic' flag was used, as both the\nregular ref updates and the tags need to be part of the same\ntransaction.\n\nFix this by committing the previous batched update and initiating a new\none for backfilling tags. Use a separate error variable for tracking the\nreference updates: One, it ensures that errors from the regular ref\nupdates don't cause a total failure. Two, if starting the transaction\nfor backfilling tags fails, only those updates are not made to disk. The\ninitial error is now printed straight away to clear the buffer for\nfurther errors.\n\nAdd two tests, one which validates that the 'haves' are filled when\nbackfilling tags. The other, to test that upstream is still set and tags\nare backfilled even if the main ref update is rejected.\n\nWhile this does make it a little slower than master, due to two\ntransactions:\n\nBenchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = master)\n  Time (mean ± σ):      82.1 ms ±   3.3 ms    [User: 60.2 ms, System: 24.4 ms]\n  Range (min … max):    77.4 ms …  89.4 ms    33 runs\n\nBenchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n  Time (mean ± σ):      82.6 ms ±   2.4 ms    [User: 59.3 ms, System: 25.2 ms]\n  Range (min … max):    78.1 ms …  88.7 ms    31 runs\n\nSummary\n  fetch: many refs (refformat = reftable, refcount = 10000, revision = master) ran\n   1.01 ± 0.05 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n\nIt is still faster than not using batched updates:\n\nBenchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n  Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n  Range (min … max):    1.427 s …  1.558 s    10 runs\n\nBenchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n  Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n  Range (min … max):    81.4 ms …  88.6 ms    29 runs\n\nSummary\n  fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n   17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nThe issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n---\nChanges in v3:\n- Fix the early exit, so a failed transaction_begin() only skips the\n  backfill.\n- Keep the first commit's result separately, so rejections don't skip\n  the upstream and remote-HEAD setup.\n- Test that the backfill advertises the fetched tip as a have, instead\n  of checking that the root object isn't fetched, which passed even\n  without the fix.\n- Add a test for the upstream setup when a main ref is rejected and a\n  tag is backfilled.\n- Link to v2: https://patch.msgid.link/20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com\n\nChanges in v2:\n- Add benchmarks between master and this branch to show perf regression.\n- Instead of checking the trace file, simply check that we didn't fetch\n  older objects in the test.\n- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com\n\nCC: Patrick Steinhardt <ps@pks.im>\nCC: Justin Tobler <jltobler@gmail.com>\n---\n builtin/fetch.c  | 38 +++++++++++++++++++++++++++++++++++---\n t/t5510-fetch.sh | 42 ++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 77 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..34fc37b58a 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1923,6 +1923,7 @@ static int do_fetch(struct transport *transport,\n \tstruct strmap rejected_refs = STRMAP_INIT;\n \tint summary_width = 0;\n \tint follow_remote_head;\n+\tint commit_ref_ret = 0;\n \n \tif (tags == TAGS_DEFAULT) {\n \t\tif (transport->remote->fetch_tags == 2)\n@@ -2076,6 +2077,30 @@ static int do_fetch(struct transport *transport,\n \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n+\n+\t\t/*\n+\t\t * As negotiation only reads references from disk, commit the references\n+\t\t * fetched so far, so that the backfill negotiation can advertise them as\n+\t\t * 'have's. Otherwise the server may re-send objects.\n+\t\t */\n+\t\tif (tags_ref_map && !atomic_fetch) {\n+\t\t\tcommit_ref_ret = commit_ref_transaction(&transaction, false,\n+\t\t\t\t\t\t\t\ttransport->remote->name,\n+\t\t\t\t\t\t\t\t&rejected_refs, &err);\n+\t\t\tif (err.len) {\n+\t\t\t\terror(\"%s\", err.buf);\n+\t\t\t\tstrbuf_reset(&err);\n+\t\t\t}\n+\n+\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n+\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n+\t\t\tif (!transaction) {\n+\t\t\t\tfree_refs(tags_ref_map);\n+\t\t\t\ttags_ref_map = NULL;\n+\t\t\t\tcommit_ref_ret = -1;\n+\t\t\t}\n+\t\t}\n+\n \t\tif (tags_ref_map) {\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\n@@ -2099,9 +2124,16 @@ static int do_fetch(struct transport *transport,\n \tif (verbosity >= 0)\n \t\tsummary_width = transport_summary_width(ref_map);\n \n-\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n-\t\t\t\t\t transport->remote->name,\n-\t\t\t\t\t &rejected_refs, &err);\n+\t/*\n+\t * The transaction may have failed to initialize, meaning there is nothing\n+\t * to commit. Carry forward with other operations in such cases.\n+\t */\n+\tif (transaction)\n+\t\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n+\t\t\t\t\t\t transport->remote->name,\n+\t\t\t\t\t\t &rejected_refs, &err);\n+\tretcode |= commit_ref_ret;\n+\n \t/*\n \t * With '--atomic', bail out if the transaction fails. Without '--atomic',\n \t * continue to fetch head and perform other post-fetch operations.\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..0baf18e01e 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,23 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'backfilling tags after shallow fetch advertises fetched refs as haves' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n@@ -2005,6 +2022,31 @@ test_expect_success REFFILES \"upstream tracking info is added even with conflict\n \t)\n '\n \n+test_expect_success REFFILES \"upstream tracking info is added with conflicts and backfilled tags\" '\n+\ttest_when_finished rm -rf base repo &&\n+\n+\tgit init --initial-branch=main base &&\n+\ttest_commit -C base one &&\n+\ttest_commit -C base two &&\n+\tgit -C base branch foo &&\n+\t# Backfilling only takes place for tags which are not pointing at tips.\n+\tgit -C base tag -a annotated -m annotated HEAD~1 &&\n+\n+\tgit init --bare --initial-branch=main repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit remote add origin ../base &&\n+\t\tmkdir -p refs/remotes/origin &&\n+\t\t>refs/remotes/origin/foo.lock &&\n+\t\ttest_must_fail git fetch origin --set-upstream main \\\n+\t\t\t\"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit config get branch.main.remote >actual &&\n+\t\techo \"origin\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse --verify refs/tags/annotated\n+\t)\n+'\n+\n test_expect_success REFFILES \"HEAD is updated even with conflicts\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"555635","messageId":"atftdOihoUHTdvfu@denethor","threadId":"66493","inReplyTo":"20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-20T23:05:22Z","receivedAt":"2026-10-20T23:05:22Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/15 11:24PM, Karthik Nayak wrote:\n> In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n> fetch code was modified to use batched updates to provide a good\n> performance improvement. Batched updates are used both for the fetched\n> references and to backfill tags.\n> \n> When using batched updates, the references aren't yet committed to disk\n> when we start backfilling tags. This means in situations such as shallow\n> fetching the negotiation during backfilling tags, the newly fetched refs\n> are not available for advertisement in the 'haves' section. This can\n> cause the server to send all objects the client already has.\n\nOk, without the earlier fetched references committed, the backfill fetch\nmay ultimately ask for objects it already got. Make sense.\n\n> The issue always existed when the '--atomic' flag was used, as both the\n> regular ref updates and the tags need to be part of the same\n> transaction.\n\nSo IIUC, you are saying that this problem already existed prior to\n0e358de64a, but only for atomic fetches. Furthermore, this patch doesn't\nfix anything for atomic fetches as they can only use a single\ntransaction.\n\n> Fix this by committing the previous batched update and initiating a new\n> one for backfilling tags. Use a separate error variable for tracking the\n> reference updates: One, it ensures that errors from the regular ref\n> updates don't cause a total failure. Two, if starting the transaction\n> for backfilling tags fails, only those updates are not made to disk. The\n> initial error is now printed straight away to clear the buffer for\n> further errors.\n\nNaively, I think it would be nice if it were possible supplement the\nbackfill fetch \"haves\" with the non-committed references in the pending\ntransaction directly. That would avoid having to commit the references\nin the middle and probably also fix it for atomic fetches too, but I'm\nuncertain if that would be a simple or reasonable thing to do.\n\nCommitting the intermediate state here does in part fix the issue, so it\nmay be a good first step regardless.\n\n> Add two tests, one which validates that the 'haves' are filled when\n> backfilling tags. The other, to test that upstream is still set and tags\n> are backfilled even if the main ref update is rejected.\n> \n> While this does make it a little slower than master, due to two\n> transactions:\n> \n> Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = master)\n>   Time (mean ± σ):      82.1 ms ±   3.3 ms    [User: 60.2 ms, System: 24.4 ms]\n>   Range (min … max):    77.4 ms …  89.4 ms    33 runs\n> \n> Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n>   Time (mean ± σ):      82.6 ms ±   2.4 ms    [User: 59.3 ms, System: 25.2 ms]\n>   Range (min … max):    78.1 ms …  88.7 ms    31 runs\n> \n> Summary\n>   fetch: many refs (refformat = reftable, refcount = 10000, revision = master) ran\n>    1.01 ± 0.05 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n> \n> It is still faster than not using batched updates:\n> \n> Benchmark 1: fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n>   Time (mean ± σ):      1.468 s ±  0.041 s    [User: 0.839 s, System: 0.587 s]\n>   Range (min … max):    1.427 s …  1.558 s    10 runs\n> \n> Benchmark 2: fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD)\n>   Time (mean ± σ):      84.4 ms ±   1.7 ms    [User: 60.9 ms, System: 25.8 ms]\n>   Range (min … max):    81.4 ms …  88.6 ms    29 runs\n> \n> Summary\n>   fetch: many refs (refformat = reftable, refcount = 10000, revision = HEAD) ran\n>    17.38 ± 0.61 times faster than fetch: many refs (refformat = reftable, refcount = 10000, revision = 0e358de64a9e014575d11ef884bfc9beb931e37f~1)\n> \n> Reported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n> The issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n> \n> [1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n> ---\n>  builtin/fetch.c  | 38 +++++++++++++++++++++++++++++++++++---\n>  t/t5510-fetch.sh | 42 ++++++++++++++++++++++++++++++++++++++++++\n>  2 files changed, 77 insertions(+), 3 deletions(-)\n> \n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index b2decc6cfd..34fc37b58a 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -1923,6 +1923,7 @@ static int do_fetch(struct transport *transport,\n>  \tstruct strmap rejected_refs = STRMAP_INIT;\n>  \tint summary_width = 0;\n>  \tint follow_remote_head;\n> +\tint commit_ref_ret = 0;\n>  \n>  \tif (tags == TAGS_DEFAULT) {\n>  \t\tif (transport->remote->fetch_tags == 2)\n> @@ -2076,6 +2077,30 @@ static int do_fetch(struct transport *transport,\n>  \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n>  \n>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n> +\n> +\t\t/*\n> +\t\t * As negotiation only reads references from disk, commit the references\n> +\t\t * fetched so far, so that the backfill negotiation can advertise them as\n> +\t\t * 'have's. Otherwise the server may re-send objects.\n> +\t\t */\n> +\t\tif (tags_ref_map && !atomic_fetch) {\n> +\t\t\tcommit_ref_ret = commit_ref_transaction(&transaction, false,\n> +\t\t\t\t\t\t\t\ttransport->remote->name,\n> +\t\t\t\t\t\t\t\t&rejected_refs, &err);\n> +\t\t\tif (err.len) {\n> +\t\t\t\terror(\"%s\", err.buf);\n> +\t\t\t\tstrbuf_reset(&err);\n> +\t\t\t}\n\nIt looks like we log the error and continue even if committing the\ntransaction failed. Is there a reason we would want to still try to\nstart the next transaction?\n\n> +\n> +\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n> +\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n> +\t\t\tif (!transaction) {\n> +\t\t\t\tfree_refs(tags_ref_map);\n> +\t\t\t\ttags_ref_map = NULL;\n> +\t\t\t\tcommit_ref_ret = -1;\n> +\t\t\t}\n> +\t\t}\n> +\n>  \t\tif (tags_ref_map) {\n>  \t\t\t/*\n>  \t\t\t * If backfilling of tags fails then we want to tell\n> @@ -2099,9 +2124,16 @@ static int do_fetch(struct transport *transport,\n>  \tif (verbosity >= 0)\n>  \t\tsummary_width = transport_summary_width(ref_map);\n>  \n> -\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n> -\t\t\t\t\t transport->remote->name,\n> -\t\t\t\t\t &rejected_refs, &err);\n> +\t/*\n> +\t * The transaction may have failed to initialize, meaning there is nothing\n> +\t * to commit. Carry forward with other operations in such cases.\n> +\t */\n> +\tif (transaction)\n> +\t\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n> +\t\t\t\t\t\t transport->remote->name,\n> +\t\t\t\t\t\t &rejected_refs, &err);\n> +\tretcode |= commit_ref_ret;\n\nhmmm, the error paths here are confusing me a bit. If the transaction\nfailed to be created in the first place, wouldn't we want to stop\nearlier on?\n\nThanks,\n-Justin\n\n"},{"id":"555675","messageId":"athd_dTKZYOv01UZ@pks.im","threadId":"66493","inReplyTo":"atftdOihoUHTdvfu@denethor","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-21T06:39:02Z","receivedAt":"2026-10-21T06:39:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Oct 20, 2026 at 06:05:22PM -0500, Justin Tobler wrote:\n> On 26/10/15 11:24PM, Karthik Nayak wrote:\n[snip]\n> > Fix this by committing the previous batched update and initiating a new\n> > one for backfilling tags. Use a separate error variable for tracking the\n> > reference updates: One, it ensures that errors from the regular ref\n> > updates don't cause a total failure. Two, if starting the transaction\n> > for backfilling tags fails, only those updates are not made to disk. The\n> > initial error is now printed straight away to clear the buffer for\n> > further errors.\n> \n> Naively, I think it would be nice if it were possible supplement the\n> backfill fetch \"haves\" with the non-committed references in the pending\n> transaction directly. That would avoid having to commit the references\n> in the middle and probably also fix it for atomic fetches too, but I'm\n> uncertain if that would be a simple or reasonable thing to do.\n\nHmm, I think that's a sensible idea. We should know about the ref\nupdates we have already scheduled into the transaction, so in theory we\ncould use their new object IDs in the negotiation. If that's possible it\nwould definitely be a much better fix.\n\n> Committing the intermediate state here does in part fix the issue, so it\n> may be a good first step regardless.\n\nWell, if the above suggestions works we'd want to revert this step here,\nso we'd first have to go back a step again.\n\nIf the above is infeasible then that's fine. But if it is feasible\nwithout too much effort I'd go for it directly.\n\nPatrick\n\n"},{"id":"555705","messageId":"CAOLa=ZQ_sgz9e4JFWj61HNnKXx+pTvTYhENbbtRu+ATUbXxS9g@mail.gmail.com","threadId":"66493","inReplyTo":"athd_dTKZYOv01UZ@pks.im","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-21T10:01:15Z","receivedAt":"2026-10-21T10:01:15Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Tue, Oct 20, 2026 at 06:05:22PM -0500, Justin Tobler wrote:\n>> On 26/10/15 11:24PM, Karthik Nayak wrote:\n> [snip]\n>> > Fix this by committing the previous batched update and initiating a new\n>> > one for backfilling tags. Use a separate error variable for tracking the\n>> > reference updates: One, it ensures that errors from the regular ref\n>> > updates don't cause a total failure. Two, if starting the transaction\n>> > for backfilling tags fails, only those updates are not made to disk. The\n>> > initial error is now printed straight away to clear the buffer for\n>> > further errors.\n>>\n>> Naively, I think it would be nice if it were possible supplement the\n>> backfill fetch \"haves\" with the non-committed references in the pending\n>> transaction directly. That would avoid having to commit the references\n>> in the middle and probably also fix it for atomic fetches too, but I'm\n>> uncertain if that would be a simple or reasonable thing to do.\n>\n> Hmm, I think that's a sensible idea. We should know about the ref\n> updates we have already scheduled into the transaction, so in theory we\n> could use their new object IDs in the negotiation. If that's possible it\n> would definitely be a much better fix.\n>\n>> Committing the intermediate state here does in part fix the issue, so it\n>> may be a good first step regardless.\n>\n> Well, if the above suggestions works we'd want to revert this step here,\n> so we'd first have to go back a step again.\n>\n> If the above is infeasible then that's fine. But if it is feasible\n> without too much effort I'd go for it directly.\n>\n> Patrick\n\nI'm a bit wary of going down ad we'd be exposing un-committed references\nwhich could fail as 'haves' to the server. This doesn't seem to be\nconceptually correct as we now expect GC to not clear the objects needed\nby relying on the .keep lock instead of durable references.\n\nBut seems like we have negotiation_include_tips in the transport\nstructure which seems like the right fit?\n"},{"id":"555706","messageId":"CAOLa=ZR-mg6ah4VwStR3SJvT1uWdW5uv7q9x486FN7UawkJYeg@mail.gmail.com","threadId":"66493","inReplyTo":"atftdOihoUHTdvfu@denethor","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-21T10:05:50Z","receivedAt":"2026-10-21T10:05:50Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> On 26/10/15 11:24PM, Karthik Nayak wrote:\n>> In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n>> fetch code was modified to use batched updates to provide a good\n>> performance improvement. Batched updates are used both for the fetched\n>> references and to backfill tags.\n>>\n>> When using batched updates, the references aren't yet committed to disk\n>> when we start backfilling tags. This means in situations such as shallow\n>> fetching the negotiation during backfilling tags, the newly fetched refs\n>> are not available for advertisement in the 'haves' section. This can\n>> cause the server to send all objects the client already has.\n>\n> Ok, without the earlier fetched references committed, the backfill fetch\n> may ultimately ask for objects it already got. Make sense.\n>\n>> The issue always existed when the '--atomic' flag was used, as both the\n>> regular ref updates and the tags need to be part of the same\n>> transaction.\n>\n> So IIUC, you are saying that this problem already existed prior to\n> 0e358de64a, but only for atomic fetches. Furthermore, this patch doesn't\n> fix anything for atomic fetches as they can only use a single\n> transaction.\n>\n\nYup, there's nothing we can do for atomic fetches with the current\napproach.\n\n>> Fix this by committing the previous batched update and initiating a new\n>> one for backfilling tags. Use a separate error variable for tracking the\n>> reference updates: One, it ensures that errors from the regular ref\n>> updates don't cause a total failure. Two, if starting the transaction\n>> for backfilling tags fails, only those updates are not made to disk. The\n>> initial error is now printed straight away to clear the buffer for\n>> further errors.\n>\n> Naively, I think it would be nice if it were possible supplement the\n> backfill fetch \"haves\" with the non-committed references in the pending\n> transaction directly. That would avoid having to commit the references\n> in the middle and probably also fix it for atomic fetches too, but I'm\n> uncertain if that would be a simple or reasonable thing to do.\n>\n> Committing the intermediate state here does in part fix the issue, so it\n> may be a good first step regardless.\n>\n\nI've replied to this with Patrick's email, let's continue there.\n\n[snip]\n\n>> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>> index b2decc6cfd..34fc37b58a 100644\n>> --- a/builtin/fetch.c\n>> +++ b/builtin/fetch.c\n>> @@ -1923,6 +1923,7 @@ static int do_fetch(struct transport *transport,\n>>  \tstruct strmap rejected_refs = STRMAP_INIT;\n>>  \tint summary_width = 0;\n>>  \tint follow_remote_head;\n>> +\tint commit_ref_ret = 0;\n>>\n>>  \tif (tags == TAGS_DEFAULT) {\n>>  \t\tif (transport->remote->fetch_tags == 2)\n>> @@ -2076,6 +2077,30 @@ static int do_fetch(struct transport *transport,\n>>  \t\tstruct ref *tags_ref_map = NULL, **tail = &tags_ref_map;\n>>\n>>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n>> +\n>> +\t\t/*\n>> +\t\t * As negotiation only reads references from disk, commit the references\n>> +\t\t * fetched so far, so that the backfill negotiation can advertise them as\n>> +\t\t * 'have's. Otherwise the server may re-send objects.\n>> +\t\t */\n>> +\t\tif (tags_ref_map && !atomic_fetch) {\n>> +\t\t\tcommit_ref_ret = commit_ref_transaction(&transaction, false,\n>> +\t\t\t\t\t\t\t\ttransport->remote->name,\n>> +\t\t\t\t\t\t\t\t&rejected_refs, &err);\n>> +\t\t\tif (err.len) {\n>> +\t\t\t\terror(\"%s\", err.buf);\n>> +\t\t\t\tstrbuf_reset(&err);\n>> +\t\t\t}\n>\n> It looks like we log the error and continue even if committing the\n> transaction failed. Is there a reason we would want to still try to\n> start the next transaction?\n>\n\nSince we use batched updates, some refs could fail, but we continue as\nwe may have still applied some updates and we can continue doing the\nrest of the work.\n\n>> +\n>> +\t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n>> +\t\t\t\t\t\t\t\t  REF_TRANSACTION_ALLOW_FAILURE, &err);\n>> +\t\t\tif (!transaction) {\n>> +\t\t\t\tfree_refs(tags_ref_map);\n>> +\t\t\t\ttags_ref_map = NULL;\n>> +\t\t\t\tcommit_ref_ret = -1;\n>> +\t\t\t}\n>> +\t\t}\n>> +\n>>  \t\tif (tags_ref_map) {\n>>  \t\t\t/*\n>>  \t\t\t * If backfilling of tags fails then we want to tell\n>> @@ -2099,9 +2124,16 @@ static int do_fetch(struct transport *transport,\n>>  \tif (verbosity >= 0)\n>>  \t\tsummary_width = transport_summary_width(ref_map);\n>>\n>> -\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n>> -\t\t\t\t\t transport->remote->name,\n>> -\t\t\t\t\t &rejected_refs, &err);\n>> +\t/*\n>> +\t * The transaction may have failed to initialize, meaning there is nothing\n>> +\t * to commit. Carry forward with other operations in such cases.\n>> +\t */\n>> +\tif (transaction)\n>> +\t\tretcode = commit_ref_transaction(&transaction, atomic_fetch,\n>> +\t\t\t\t\t\t transport->remote->name,\n>> +\t\t\t\t\t\t &rejected_refs, &err);\n>> +\tretcode |= commit_ref_ret;\n>\n> hmmm, the error paths here are confusing me a bit. If the transaction\n> failed to be created in the first place, wouldn't we want to stop\n> earlier on?\n>\n> Thanks,\n> -Justin\n\nNot really, since we use batched updates, the backfilling tags\ntransaction could fail to initialize but we still want to set upstreams\nand set HEAD.\n"},{"id":"555738","messageId":"atjAS1qQ7HyDYbxh@denethor","threadId":"66493","inReplyTo":"CAOLa=ZQ_sgz9e4JFWj61HNnKXx+pTvTYhENbbtRu+ATUbXxS9g@mail.gmail.com","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Justin Tobler","fromEmail":"jltobler@gmail.com","sentAt":"2026-10-21T13:59:13Z","receivedAt":"2026-10-21T13:59:13Z","isPatch":true,"sender":{"key":"jltobler@gmail.com","avatar":"https://avatars.githubusercontent.com/u/53454972?v=4"},"body":"On 26/10/21 06:01AM, Karthik Nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> > On Tue, Oct 20, 2026 at 06:05:22PM -0500, Justin Tobler wrote:\n> >> Naively, I think it would be nice if it were possible supplement the\n> >> backfill fetch \"haves\" with the non-committed references in the pending\n> >> transaction directly. That would avoid having to commit the references\n> >> in the middle and probably also fix it for atomic fetches too, but I'm\n> >> uncertain if that would be a simple or reasonable thing to do.\n> >\n> > Hmm, I think that's a sensible idea. We should know about the ref\n> > updates we have already scheduled into the transaction, so in theory we\n> > could use their new object IDs in the negotiation. If that's possible it\n> > would definitely be a much better fix.\n>\n> I'm a bit wary of going down ad we'd be exposing un-committed references\n> which could fail as 'haves' to the server. This doesn't seem to be\n> conceptually correct as we now expect GC to not clear the objects needed\n> by relying on the .keep lock instead of durable references.\n\nI'm not quite sure I follow why this would be conceptually problematic.\nShould it really matter in this particular scenario whether the tips the\nclient advertises are committed or not? The required objects should\nalready be present in the repository from the initial fetch.\n\nRegarding the continued use of \".keep\" lockfiles to prevent newly\nfetched packfiles from being GC'd, that still sounds reasonable to me.\nIt is expected that the \".keep\" lockfiles remain in place until the\nreference transaction is committed any way. To me, conceptually all we\nare doing is allowing the current inflight state of the transaction to\nbe used to affect another operation being performed within the very same\ntransaction.\n\n> But seems like we have negotiation_include_tips in the transport\n> structure which seems like the right fit?\n\nAt a quick glance, this seems like it could be a good way for the client\nto advertise to the sever the tips of the uncommitted references it has.\n\n-Justin\n\n"},{"id":"555835","messageId":"CAOLa=ZShCfEMEgvYz+s-F6X9zjO7fxuEAnzrhwEtj_RerkMirA@mail.gmail.com","threadId":"66493","inReplyTo":"atjAS1qQ7HyDYbxh@denethor","subject":"Re: [PATCH v3] fetch: commit references fetched before backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-22T09:13:57Z","receivedAt":"2026-10-22T09:13:57Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Justin Tobler <jltobler@gmail.com> writes:\n\n> On 26/10/21 06:01AM, Karthik Nayak wrote:\n>> Patrick Steinhardt <ps@pks.im> writes:\n>> > On Tue, Oct 20, 2026 at 06:05:22PM -0500, Justin Tobler wrote:\n>> >> Naively, I think it would be nice if it were possible supplement the\n>> >> backfill fetch \"haves\" with the non-committed references in the pending\n>> >> transaction directly. That would avoid having to commit the references\n>> >> in the middle and probably also fix it for atomic fetches too, but I'm\n>> >> uncertain if that would be a simple or reasonable thing to do.\n>> >\n>> > Hmm, I think that's a sensible idea. We should know about the ref\n>> > updates we have already scheduled into the transaction, so in theory we\n>> > could use their new object IDs in the negotiation. If that's possible it\n>> > would definitely be a much better fix.\n>>\n>> I'm a bit wary of going down ad we'd be exposing un-committed references\n>> which could fail as 'haves' to the server. This doesn't seem to be\n>> conceptually correct as we now expect GC to not clear the objects needed\n>> by relying on the .keep lock instead of durable references.\n>\n> I'm not quite sure I follow why this would be conceptually problematic.\n> Should it really matter in this particular scenario whether the tips the\n> client advertises are committed or not? The required objects should\n> already be present in the repository from the initial fetch.\n>\n> Regarding the continued use of \".keep\" lockfiles to prevent newly\n> fetched packfiles from being GC'd, that still sounds reasonable to me.\n> It is expected that the \".keep\" lockfiles remain in place until the\n> reference transaction is committed any way. To me, conceptually all we\n> are doing is allowing the current inflight state of the transaction to\n> be used to affect another operation being performed within the very same\n> transaction.\n>\n\nI was getting at it from the point that '.keep' lockfiles are created\nfor the reference transaction to commit. So while they keep the objects\nin place, we should only rely on a committed reference to keep objects\nin place.\n\nBut, you're right, the transaction here keeps the lockfiles in place\nuntil we commit anyways. So I guess that's okay. Let me send in a new\nversion and we can see what everyone thinks. Thanks!\n\n>> But seems like we have negotiation_include_tips in the transport\n>> structure which seems like the right fit?\n>\n> At a quick glance, this seems like it could be a good way for the client\n> to advertise to the sever the tips of the uncommitted references it has.\n>\n> -Justin\n"},{"id":"555911","messageId":"20261022-799-shallow-fetch-with-tags-v4-1-8a1c9ef399ed@gmail.com","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"[PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-22T21:00:16Z","receivedAt":"2026-10-22T21:00:16Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Batched updates are used both for the fetched\nreferences and to backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. For shallow fetches, the negotiation\nphase while backfilling tags does not advertise the newly fetched refs.\nThis can cause the server to send objects the client already has.\n\nThe issue always existed when the '--atomic' flag was used, as both the\nregular ref updates and the tags need to be part of the same\ntransaction.\n\nFix this by advertising the already fetched refs as 'haves' during the\nnegotiation. Do this by simply adding the tips to the transport's\n`negotiation_include_tips` field. This also fixes the issue for\n`--atomic` fetches since it follows the same route.\n\nAdd three tests, two which validates that the 'haves' are filled when\nbackfilling tags for atomic and non-atomic flows. The third, to test\nthat upstream is still set and tags are backfilled even if a sibling ref\nis rejected.\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nThe issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n---\nChanges in v4:\n- Instead of committing the first transaction and starting a new one,\n  advertise the refs already fetched when backfilling tags. Thanks to\n  Justin for the suggestion. This also fixes the `--atomic` flow.\n- Link to v3: https://patch.msgid.link/20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com\n\nChanges in v3:\n- Fix the early exit, so a failed transaction_begin() only skips the\n  backfill.\n- Keep the first commit's result separately, so rejections don't skip\n  the upstream and remote-HEAD setup.\n- Test that the backfill advertises the fetched tip as a have, instead\n  of checking that the root object isn't fetched, which passed even\n  without the fix.\n- Add a test for the upstream setup when a main ref is rejected and a\n  tag is backfilled.\n- Link to v2: https://patch.msgid.link/20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com\n\nChanges in v2:\n- Add benchmarks between master and this branch to show perf regression.\n- Instead of checking the trace file, simply check that we didn't fetch\n  older objects in the test.\n- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com\n\nCC: Patrick Steinhardt <ps@pks.im>\nCC: Justin Tobler <jltobler@gmail.com>\n---\n builtin/fetch.c  | 41 +++++++++++++++++++++++++++++++++++++--\n t/t5510-fetch.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 98 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..9867e8c2ef 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1676,8 +1676,10 @@ static int backfill_tags(struct display_state *display_state,\n \t\t\t struct fetch_head *fetch_head,\n \t\t\t const struct fetch_config *config,\n \t\t\t struct ref_update_display_info_array *display_array,\n-\t\t\t struct list_objects_filter_options *filter_options)\n+\t\t\t struct list_objects_filter_options *filter_options,\n+\t\t\t const struct oid_array *fetched_tips)\n {\n+\tstruct oid_array *to_free = NULL;\n \tint retcode, cannot_reuse;\n \n \t/*\n@@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n+\n+\t/*\n+\t * References fetched before the tag backfill aren't yet committed\n+\t * to disk, so negotiation cannot discover them. Advertise them to\n+\t * the server, so that it does not re-send objects we already have.\n+\t */\n+\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n+\t\tstruct oid_array **include_tips =\n+\t\t\t&transport->smart_options->negotiation_include_tips;\n+\t\tif (!*include_tips)\n+\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n+\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n+\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n+\t}\n+\n \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n \t\t\t\t\t fetch_head, config, display_array);\n \n+\tif (to_free) {\n+\t\toid_array_clear(to_free);\n+\t\tfree(to_free);\n+\t\ttransport->smart_options->negotiation_include_tips = NULL;\n+\t}\n+\n \tif (gsecondary) {\n \t\ttransport_disconnect(gsecondary);\n \t\tgsecondary = NULL;\n@@ -2077,6 +2100,17 @@ static int do_fetch(struct transport *transport,\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n \t\tif (tags_ref_map) {\n+\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n+\t\t\tstruct ref *rm;\n+\n+\t\t\t/*\n+\t\t\t * Collect the tips we just fetched so the tag backfill\n+\t\t\t * can advertise them to the server as 'haves'.\n+\t\t\t */\n+\t\t\tfor (rm = ref_map; rm; rm = rm->next)\n+\t\t\t\tif (!is_null_oid(&rm->old_oid))\n+\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n+\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\n \t\t\t * the user so, but we have to continue regardless to\n@@ -2086,8 +2120,11 @@ static int do_fetch(struct transport *transport,\n \t\t\t * the transaction and don't commit anything.\n \t\t\t */\n \t\t\tif (backfill_tags(&display_state, transport, transaction, tags_ref_map,\n-\t\t\t\t\t  &fetch_head, config, &display_array, filter_options))\n+\t\t\t\t\t  &fetch_head, config, &display_array, filter_options,\n+\t\t\t\t\t  &fetched_tips))\n \t\t\t\tretcode = 1;\n+\n+\t\t\toid_array_clear(&fetched_tips);\n \t\t}\n \n \t\tfree_refs(tags_ref_map);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..ca5f9642fc 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,40 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'backfilling tags advertises fetched refs as haves' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n+test_expect_success 'backfilling tags advertises fetched refs as haves even with atomic' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --atomic --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n@@ -2005,6 +2039,31 @@ test_expect_success REFFILES \"upstream tracking info is added even with conflict\n \t)\n '\n \n+test_expect_success REFFILES \"upstream tracking info is added with conflicts and backfilled tags\" '\n+\ttest_when_finished rm -rf base repo &&\n+\n+\tgit init --initial-branch=main base &&\n+\ttest_commit -C base one &&\n+\ttest_commit -C base two &&\n+\tgit -C base branch foo &&\n+\t# Backfilling only takes place for tags which are not pointing at tips.\n+\tgit -C base tag -a annotated -m annotated HEAD~1 &&\n+\n+\tgit init --bare --initial-branch=main repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit remote add origin ../base &&\n+\t\tmkdir -p refs/remotes/origin &&\n+\t\t>refs/remotes/origin/foo.lock &&\n+\t\ttest_must_fail git fetch origin --set-upstream main \\\n+\t\t\t\"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit config get branch.main.remote >actual &&\n+\t\techo \"origin\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse --verify refs/tags/annotated\n+\t)\n+'\n+\n test_expect_success REFFILES \"HEAD is updated even with conflicts\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"555952","messageId":"atsBbPi_EuhRdNmo@pks.im","threadId":"66493","inReplyTo":"20261022-799-shallow-fetch-with-tags-v4-1-8a1c9ef399ed@gmail.com","subject":"Re: [PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-23T06:40:44Z","receivedAt":"2026-10-23T06:40:44Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"on thu, oct 22, 2026 at 11:00:16pm +0200, karthik nayak wrote:\n> In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n> fetch code was modified to use batched updates to provide a good\n> performance improvement. Batched updates are used both for the fetched\n> references and to backfill tags.\n> \n> When using batched updates, the references aren't yet committed to disk\n> when we start backfilling tags. For shallow fetches, the negotiation\n> phase while backfilling tags does not advertise the newly fetched refs.\n> This can cause the server to send objects the client already has.\n> \n> The issue always existed when the '--atomic' flag was used, as both the\n> regular ref updates and the tags need to be part of the same\n> transaction.\n> \n> Fix this by advertising the already fetched refs as 'haves' during the\n> negotiation. Do this by simply adding the tips to the transport's\n> `negotiation_include_tips` field. This also fixes the issue for\n> `--atomic` fetches since it follows the same route.\n\nOkay. Would the client have advertised _all_ its local references to the\nserver before the batch updates? And with the new logic, would we now\nstart to advertise all of them? Mostly trying to figure out whether\nthere is a potential change in behaviour here where we may now advertise\nmillions of haves whereas we previously didn't.\n\nAnd even if that was the case it _may_ be the right thing to do. I\nmostly just want to see this discussed in the commit message :)\n\n> Add three tests, two which validates that the 'haves' are filled when\n> backfilling tags for atomic and non-atomic flows. The third, to test\n> that upstream is still set and tags are backfilled even if a sibling ref\n> is rejected.\n\nThe third test is curious. Does it have anything to do with the fix\nitself? If not, why add it now?\n\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index b2decc6cfd..9867e8c2ef 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n>  \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n>  \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n>  \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n> +\n> +\t/*\n> +\t * References fetched before the tag backfill aren't yet committed\n> +\t * to disk, so negotiation cannot discover them. Advertise them to\n> +\t * the server, so that it does not re-send objects we already have.\n> +\t */\n> +\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n> +\t\tstruct oid_array **include_tips =\n> +\t\t\t&transport->smart_options->negotiation_include_tips;\n> +\t\tif (!*include_tips)\n> +\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n> +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n> +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n> +\t}\n\nCurious. Why don't we have `struct oid_array` as a local variable and\nthen simply `transport->smart_options->negotiation_include_tips =\n&extra_tips`?\n\n>  \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n>  \t\t\t\t\t fetch_head, config, display_array);\n>  \n> +\tif (to_free) {\n> +\t\toid_array_clear(to_free);\n> +\t\tfree(to_free);\n> +\t\ttransport->smart_options->negotiation_include_tips = NULL;\n> +\t}\n\nThis could then become unconditional.\n\n> @@ -2077,6 +2100,17 @@ static int do_fetch(struct transport *transport,\n>  \n>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n>  \t\tif (tags_ref_map) {\n> +\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n> +\t\t\tstruct ref *rm;\n\nThis is just the loop variable, so it could move into the loop.\n\n> +\t\t\t/*\n> +\t\t\t * Collect the tips we just fetched so the tag backfill\n> +\t\t\t * can advertise them to the server as 'haves'.\n> +\t\t\t */\n> +\t\t\tfor (rm = ref_map; rm; rm = rm->next)\n> +\t\t\t\tif (!is_null_oid(&rm->old_oid))\n> +\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n\nMakes sense.\n\nOverall this version looks much saner to me, thanks!\n\nPatrick\n\n"},{"id":"555981","messageId":"CAOLa=ZTe6zeapUBwF2cS=w_gDQzm08RJkezD-jm9aV=7j1u6KA@mail.gmail.com","threadId":"66493","inReplyTo":"atsBbPi_EuhRdNmo@pks.im","subject":"Re: [PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-23T10:58:11Z","receivedAt":"2026-10-23T10:58:11Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> on thu, oct 22, 2026 at 11:00:16pm +0200, karthik nayak wrote:\n>> In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\n>> fetch code was modified to use batched updates to provide a good\n>> performance improvement. Batched updates are used both for the fetched\n>> references and to backfill tags.\n>>\n>> When using batched updates, the references aren't yet committed to disk\n>> when we start backfilling tags. For shallow fetches, the negotiation\n>> phase while backfilling tags does not advertise the newly fetched refs.\n>> This can cause the server to send objects the client already has.\n>>\n>> The issue always existed when the '--atomic' flag was used, as both the\n>> regular ref updates and the tags need to be part of the same\n>> transaction.\n>>\n>> Fix this by advertising the already fetched refs as 'haves' during the\n>> negotiation. Do this by simply adding the tips to the transport's\n>> `negotiation_include_tips` field. This also fixes the issue for\n>> `--atomic` fetches since it follows the same route.\n>\n> Okay. Would the client have advertised _all_ its local references to the\n> server before the batch updates? And with the new logic, would we now\n> start to advertise all of them? Mostly trying to figure out whether\n> there is a potential change in behaviour here where we may now advertise\n> millions of haves whereas we previously didn't.\n>\n\nYeah, we would, In the end, we only negotiation the OIDs, so duplicates\nwould be removed, but it is O(n) for the number of refs fetched.\n\n> And even if that was the case it _may_ be the right thing to do. I\n> mostly just want to see this discussed in the commit message :)\n>\n\nThat's fair, I'll add that in.\n\n>> Add three tests, two which validates that the 'haves' are filled when\n>> backfilling tags for atomic and non-atomic flows. The third, to test\n>> that upstream is still set and tags are backfilled even if a sibling ref\n>> is rejected.\n>\n> The third test is curious. Does it have anything to do with the fix\n> itself? If not, why add it now?\n>\n\nIt was a missing test which ensures that even if committing references\nfail, we still update the upstream information. I can move it out into a\nseparate patch.\n\n>> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>> index b2decc6cfd..9867e8c2ef 100644\n>> --- a/builtin/fetch.c\n>> +++ b/builtin/fetch.c\n>> @@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n>>  \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n>>  \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n>>  \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n>> +\n>> +\t/*\n>> +\t * References fetched before the tag backfill aren't yet committed\n>> +\t * to disk, so negotiation cannot discover them. Advertise them to\n>> +\t * the server, so that it does not re-send objects we already have.\n>> +\t */\n>> +\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n>> +\t\tstruct oid_array **include_tips =\n>> +\t\t\t&transport->smart_options->negotiation_include_tips;\n>> +\t\tif (!*include_tips)\n>> +\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n>> +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n>> +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n>> +\t}\n>\n> Curious. Why don't we have `struct oid_array` as a local variable and\n> then simply `transport->smart_options->negotiation_include_tips =\n> &extra_tips`?\n>\n\nBecause `transport->smart_options->negotiation_include_tips` may already\nbe set when this function is called, so we only free it when we are the\nexclusive setters of the field.\n\n>>  \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n>>  \t\t\t\t\t fetch_head, config, display_array);\n>>\n>> +\tif (to_free) {\n>> +\t\toid_array_clear(to_free);\n>> +\t\tfree(to_free);\n>> +\t\ttransport->smart_options->negotiation_include_tips = NULL;\n>> +\t}\n>\n> This could then become unconditional.\n>\n>> @@ -2077,6 +2100,17 @@ static int do_fetch(struct transport *transport,\n>>\n>>  \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n>>  \t\tif (tags_ref_map) {\n>> +\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n>> +\t\t\tstruct ref *rm;\n>\n> This is just the loop variable, so it could move into the loop.\n>\n\nYeah, I can inline it.\n\n>> +\t\t\t/*\n>> +\t\t\t * Collect the tips we just fetched so the tag backfill\n>> +\t\t\t * can advertise them to the server as 'haves'.\n>> +\t\t\t */\n>> +\t\t\tfor (rm = ref_map; rm; rm = rm->next)\n>> +\t\t\t\tif (!is_null_oid(&rm->old_oid))\n>> +\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n>\n> Makes sense.\n>\n> Overall this version looks much saner to me, thanks!\n>\n> Patrick\n\nThanks, I'll wait a day for any responses and send in a new version with\nthe fixes.\n"},{"id":"555993","messageId":"attMR_XBdfkO3SHx@pks.im","threadId":"66493","inReplyTo":"CAOLa=ZTe6zeapUBwF2cS=w_gDQzm08RJkezD-jm9aV=7j1u6KA@mail.gmail.com","subject":"Re: [PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-23T12:00:07Z","receivedAt":"2026-10-23T12:00:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Oct 23, 2026 at 03:58:11AM -0700, Karthik Nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> > on thu, oct 22, 2026 at 11:00:16pm +0200, karthik nayak wrote:\n> >> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> >> index b2decc6cfd..9867e8c2ef 100644\n> >> --- a/builtin/fetch.c\n> >> +++ b/builtin/fetch.c\n> >> @@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n> >>  \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n> >>  \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n> >>  \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n> >> +\n> >> +\t/*\n> >> +\t * References fetched before the tag backfill aren't yet committed\n> >> +\t * to disk, so negotiation cannot discover them. Advertise them to\n> >> +\t * the server, so that it does not re-send objects we already have.\n> >> +\t */\n> >> +\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n> >> +\t\tstruct oid_array **include_tips =\n> >> +\t\t\t&transport->smart_options->negotiation_include_tips;\n> >> +\t\tif (!*include_tips)\n> >> +\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n> >> +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n> >> +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n> >> +\t}\n> >\n> > Curious. Why don't we have `struct oid_array` as a local variable and\n> > then simply `transport->smart_options->negotiation_include_tips =\n> > &extra_tips`?\n> >\n> \n> Because `transport->smart_options->negotiation_include_tips` may already\n> be set when this function is called, so we only free it when we are the\n> exclusive setters of the field.\n\nAh, okay. But if it was set already, what does it contain? And is it\nokay that we essentially leak the fetched tips to our callers now if it\nwas set?\n\nPatrick\n\n"},{"id":"556118","messageId":"CAOLa=ZSnhGwBevejqv1Nzw92q6tt6zMDM528qFb7xqY1-6mxrg@mail.gmail.com","threadId":"66493","inReplyTo":"attMR_XBdfkO3SHx@pks.im","subject":"Re: [PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-23T22:01:01Z","receivedAt":"2026-10-23T22:01:01Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Fri, Oct 23, 2026 at 03:58:11AM -0700, Karthik Nayak wrote:\n>> Patrick Steinhardt <ps@pks.im> writes:\n>> > on thu, oct 22, 2026 at 11:00:16pm +0200, karthik nayak wrote:\n>> >> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>> >> index b2decc6cfd..9867e8c2ef 100644\n>> >> --- a/builtin/fetch.c\n>> >> +++ b/builtin/fetch.c\n>> >> @@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n>> >>  \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n>> >>  \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n>> >>  \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n>> >> +\n>> >> +\t/*\n>> >> +\t * References fetched before the tag backfill aren't yet committed\n>> >> +\t * to disk, so negotiation cannot discover them. Advertise them to\n>> >> +\t * the server, so that it does not re-send objects we already have.\n>> >> +\t */\n>> >> +\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n>> >> +\t\tstruct oid_array **include_tips =\n>> >> +\t\t\t&transport->smart_options->negotiation_include_tips;\n>> >> +\t\tif (!*include_tips)\n>> >> +\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n>> >> +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n>> >> +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n>> >> +\t}\n>> >\n>> > Curious. Why don't we have `struct oid_array` as a local variable and\n>> > then simply `transport->smart_options->negotiation_include_tips =\n>> > &extra_tips`?\n>> >\n>>\n>> Because `transport->smart_options->negotiation_include_tips` may already\n>> be set when this function is called, so we only free it when we are the\n>> exclusive setters of the field.\n>\n> Ah, okay. But if it was set already, what does it contain? And is it\n> okay that we essentially leak the fetched tips to our callers now if it\n> was set?\n>\n> Patrick\n\nSo it is set when the user uses the '--negotiation-include' option and\nprovides their own tips.\n\nWhen I wrote this code, I didn't include the free'ing that you see here.\nBut it triggered the leak sanitizer for 't5509-fetch-push-namespaces'. I\nassumed it was due to my code and added this logic, without digging too\nmuch into why my path specifically triggered it.\n\nSo, the transport does free the `negotiation_include_tips`, but only for\ntransports which supports smart_options. Meaning, we should only add the\ntips for transports with smart options. As the other route doesn't use\nit and as such doesn't free the array.\n\nThanks for the pushback, I'll fix it up in the next version.\n"},{"id":"556121","messageId":"CAOLa=ZQQwYyufEWm_cS3o9t7SDfCwDSMKU7rsnctCojc4=FoGg@mail.gmail.com","threadId":"66493","inReplyTo":"CAOLa=ZSnhGwBevejqv1Nzw92q6tt6zMDM528qFb7xqY1-6mxrg@mail.gmail.com","subject":"Re: [PATCH v4] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-23T22:12:25Z","receivedAt":"2026-10-23T22:12:25Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> Patrick Steinhardt <ps@pks.im> writes:\n>\n>> On Fri, Oct 23, 2026 at 03:58:11AM -0700, Karthik Nayak wrote:\n>>> Patrick Steinhardt <ps@pks.im> writes:\n>>> > on thu, oct 22, 2026 at 11:00:16pm +0200, karthik nayak wrote:\n>>> >> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>>> >> index b2decc6cfd..9867e8c2ef 100644\n>>> >> --- a/builtin/fetch.c\n>>> >> +++ b/builtin/fetch.c\n>>> >> @@ -1697,9 +1699,30 @@ static int backfill_tags(struct display_state *display_state,\n>>> >>  \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n>>> >>  \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n>>> >>  \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n>>> >> +\n>>> >> +\t/*\n>>> >> +\t * References fetched before the tag backfill aren't yet committed\n>>> >> +\t * to disk, so negotiation cannot discover them. Advertise them to\n>>> >> +\t * the server, so that it does not re-send objects we already have.\n>>> >> +\t */\n>>> >> +\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n>>> >> +\t\tstruct oid_array **include_tips =\n>>> >> +\t\t\t&transport->smart_options->negotiation_include_tips;\n>>> >> +\t\tif (!*include_tips)\n>>> >> +\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n>>> >> +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n>>> >> +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n>>> >> +\t}\n>>> >\n>>> > Curious. Why don't we have `struct oid_array` as a local variable and\n>>> > then simply `transport->smart_options->negotiation_include_tips =\n>>> > &extra_tips`?\n>>> >\n>>>\n>>> Because `transport->smart_options->negotiation_include_tips` may already\n>>> be set when this function is called, so we only free it when we are the\n>>> exclusive setters of the field.\n>>\n>> Ah, okay. But if it was set already, what does it contain? And is it\n>> okay that we essentially leak the fetched tips to our callers now if it\n>> was set?\n>>\n>> Patrick\n>\n> So it is set when the user uses the '--negotiation-include' option and\n> provides their own tips.\n>\n> When I wrote this code, I didn't include the free'ing that you see here.\n> But it triggered the leak sanitizer for 't5509-fetch-push-namespaces'. I\n> assumed it was due to my code and added this logic, without digging too\n> much into why my path specifically triggered it.\n>\n> So, the transport does free the `negotiation_include_tips`, but only for\n> transports which supports smart_options. Meaning, we should only add the\n> tips for transports with smart options. As the other route doesn't use\n> it and as such doesn't free the array.\n>\n> Thanks for the pushback, I'll fix it up in the next version.\n>\n\nI need to not send emails in the middle of the night. Seems like I'm\nstill wrong here.\n\nThe issue seems to be that for 't5509-fetch-push-namespaces' the tests\nwhich trigger the leaks are those which use a remote helper and seems\nlike the transport with remote-helpers don't clean up correctly. So\nwhile they support smart options, they don't do the necessary cleanup.\n\nSeems like an existing bug, will add a precursor commit to fix it.\n\nKarthik\n"},{"id":"556626","messageId":"20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"[PATCH v5 0/3] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T09:47:58Z","receivedAt":"2026-10-28T09:47:58Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\nWhen fetching, git-fetch(1) performs the reference updates in two\nsteps:\n1. Fetches the regular references\n2. Backfills any tags\n\nWith batched updates, both of these operations came under the same\nreference transaction. Unfortunately, this meant that the initial\nreferences fetched were not committed and couldn't be used as 'haves'\nduring the negotiation when backfilling tags. In shallow repositories,\nthis could mean that the server was sending objects the client already\npossessed. This series fixes this regression.\n\nEarlier versions (1-3) tried to fix this by splitting the transaction\ninto two. Which works, but makes the code more complex and is a bit\nmessy overall as we have to deal with errors and also jumps in the\ncodeflow.\n\nJustin suggested that we could use the references already available for\nnegotiation instead, that is what v4 onwards does.\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nChanges in v5:\n- Add a patch fixing a pre-existing leak in the remote-helper\n  transport's cleanup. This allows the patch to drop its manual cleanup.\n- Move the upstream-info test into its own commit.\n- Some cleanpus around the commit message and grammar\n- Link to v4: https://patch.msgid.link/20261022-799-shallow-fetch-with-tags-v4-1-8a1c9ef399ed@gmail.com\n\nChanges in v4:\n- Instead of committing the first transaction and starting a new one,\n  advertise the refs already fetched when backfilling tags. Thanks to\n  Justin for the suggestion. This also fixes the `--atomic` flow.\n- Link to v3: https://patch.msgid.link/20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com\n\nChanges in v3:\n- Fix the early exit, so a failed transaction_begin() only skips the\n  backfill.\n- Keep the first commit's result separately, so rejections don't skip\n  the upstream and remote-HEAD setup.\n- Test that the backfill advertises the fetched tip as a have, instead\n  of checking that the root object isn't fetched, which passed even\n  without the fix.\n- Add a test for the upstream setup when a main ref is rejected and a\n  tag is backfilled.\n- Link to v2: https://patch.msgid.link/20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com\n\nChanges in v2:\n- Add benchmarks between master and this branch to show perf regression.\n- Instead of checking the trace file, simply check that we didn't fetch\n  older objects in the test.\n- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com\n\nCC: Patrick Steinhardt <ps@pks.im>\nCC: Justin Tobler <jltobler@gmail.com>\n\n---\nKarthik Nayak (3):\n      transport: add `transport_options_release()`\n      fetch: advertise fetched refs when backfilling tags\n      t5510: cover upstream info update on fetch failure\n\n builtin/fetch.c    | 33 ++++++++++++++++++++++++++++--\n t/t5510-fetch.sh   | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\n transport-helper.c |  1 +\n transport.c        | 24 +++++++++++++---------\n transport.h        |  2 ++\n 5 files changed, 107 insertions(+), 12 deletions(-)\n\nRange-diff versus v4:\n\n-:  ---------- > 1:  583369ce44 transport: add `transport_options_release()`\n1:  8e21cf3377 ! 2:  3a53029bb1 fetch: advertise fetched refs when backfilling tags\n    @@ Commit message\n         `negotiation_include_tips` field. This also fixes the issue for\n         `--atomic` fetches since it follows the same route.\n     \n    -    Add three tests, two which validates that the 'haves' are filled when\n    -    backfilling tags for atomic and non-atomic flows. The third, to test\n    -    that upstream is still set and tags are backfilled even if a sibling ref\n    -    is rejected.\n    +    This does mean that all references fetched before the backfill will be\n    +    used for the negotiation. While duplicate OIDs are trimmed before\n    +    negotiation, we could still end up with a lot of distinct OIDs used in\n    +    the 'haves' section. However, this is still better than the server\n    +    transferring already existing objects to the client, which can take a\n    +    lot more time.\n    +\n    +    Add two tests, which validate that the 'haves' are filled when\n    +    backfilling tags for atomic and non-atomic flows.\n     \n         Reported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n    @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state,\n     +\t\t\t struct list_objects_filter_options *filter_options,\n     +\t\t\t const struct oid_array *fetched_tips)\n      {\n    -+\tstruct oid_array *to_free = NULL;\n      \tint retcode, cannot_reuse;\n      \n    - \t/*\n     @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state,\n      \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n      \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n    @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state,\n     +\t\tstruct oid_array **include_tips =\n     +\t\t\t&transport->smart_options->negotiation_include_tips;\n     +\t\tif (!*include_tips)\n    -+\t\t\t*include_tips = to_free = xcalloc(1, sizeof(**include_tips));\n    ++\t\t\t*include_tips = xcalloc(1, sizeof(**include_tips));\n     +\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n     +\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n     +\t}\n    @@ builtin/fetch.c: static int backfill_tags(struct display_state *display_state,\n      \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n      \t\t\t\t\t fetch_head, config, display_array);\n      \n    -+\tif (to_free) {\n    -+\t\toid_array_clear(to_free);\n    -+\t\tfree(to_free);\n    -+\t\ttransport->smart_options->negotiation_include_tips = NULL;\n    -+\t}\n    -+\n    - \tif (gsecondary) {\n    - \t\ttransport_disconnect(gsecondary);\n    - \t\tgsecondary = NULL;\n     @@ builtin/fetch.c: static int do_fetch(struct transport *transport,\n      \n      \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n      \t\tif (tags_ref_map) {\n     +\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n    -+\t\t\tstruct ref *rm;\n     +\n     +\t\t\t/*\n     +\t\t\t * Collect the tips we just fetched so the tag backfill\n     +\t\t\t * can advertise them to the server as 'haves'.\n     +\t\t\t */\n    -+\t\t\tfor (rm = ref_map; rm; rm = rm->next)\n    ++\t\t\tfor (struct ref *rm = ref_map; rm; rm = rm->next)\n     +\t\t\t\tif (!is_null_oid(&rm->old_oid))\n     +\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n     +\n    @@ t/t5510-fetch.sh: test_expect_success \"backfill tags when providing a refspec\" '\n      test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n      \ttest_when_finished rm -rf base repo &&\n      \n    -@@ t/t5510-fetch.sh: test_expect_success REFFILES \"upstream tracking info is added even with conflict\n    - \t)\n    - '\n    - \n    -+test_expect_success REFFILES \"upstream tracking info is added with conflicts and backfilled tags\" '\n    -+\ttest_when_finished rm -rf base repo &&\n    -+\n    -+\tgit init --initial-branch=main base &&\n    -+\ttest_commit -C base one &&\n    -+\ttest_commit -C base two &&\n    -+\tgit -C base branch foo &&\n    -+\t# Backfilling only takes place for tags which are not pointing at tips.\n    -+\tgit -C base tag -a annotated -m annotated HEAD~1 &&\n    -+\n    -+\tgit init --bare --initial-branch=main repo &&\n    -+\t(\n    -+\t\tcd repo &&\n    -+\t\tgit remote add origin ../base &&\n    -+\t\tmkdir -p refs/remotes/origin &&\n    -+\t\t>refs/remotes/origin/foo.lock &&\n    -+\t\ttest_must_fail git fetch origin --set-upstream main \\\n    -+\t\t\t\"+refs/heads/*:refs/remotes/origin/*\" &&\n    -+\t\tgit config get branch.main.remote >actual &&\n    -+\t\techo \"origin\" >expect &&\n    -+\t\ttest_cmp expect actual &&\n    -+\t\tgit rev-parse --verify refs/tags/annotated\n    -+\t)\n    -+'\n    -+\n    - test_expect_success REFFILES \"HEAD is updated even with conflicts\" '\n    - \ttest_when_finished rm -rf base repo &&\n    - \n-:  ---------- > 3:  e8a5ce9e5e t5510: cover upstream info update on fetch failure\n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"556627","messageId":"20261028-799-shallow-fetch-with-tags-v5-1-7c43cc9ae810@gmail.com","threadId":"66493","inReplyTo":"20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com","subject":"[PATCH v5 1/3] transport: add `transport_options_release()`","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T09:47:59Z","receivedAt":"2026-10-28T09:47:59Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The `git_transport_options` structure is used both in 'transport.c' and\n'transport-helper.c'. But while the former specifically clears any\nassociated data in the structure during the cleanup of the transport,\nthe latter misses this.\n\nThis means when using remote-helpers, data allocated for the structure\nleaks. Introduce a new `transport_options_release()`, which acts as a\ncommon path to release memory allocated with the structure and use it in\nboth files.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n transport-helper.c |  1 +\n transport.c        | 24 ++++++++++++++----------\n transport.h        |  2 ++\n 3 files changed, 17 insertions(+), 10 deletions(-)\n\ndiff --git a/transport-helper.c b/transport-helper.c\nindex d5a064d386..14d179e0f6 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -401,6 +401,7 @@ static int release_helper(struct transport *transport)\n \trefspec_clear(&data->rs);\n \tfree(data->import_marks);\n \tfree(data->export_marks);\n+\ttransport_options_release(&data->transport_options);\n \tres = disconnect_helper(transport);\n \tfree(transport->data);\n \treturn res;\ndiff --git a/transport.c b/transport.c\nindex 25e2c14a7b..4491443b28 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -1017,6 +1017,19 @@ static int connect_git(struct transport *transport,\n \treturn 0;\n }\n \n+void transport_options_release(struct git_transport_options *options)\n+{\n+\tif (options->negotiation_restrict_tips) {\n+\t\toid_array_clear(options->negotiation_restrict_tips);\n+\t\tfree(options->negotiation_restrict_tips);\n+\t}\n+\tif (options->negotiation_include_tips) {\n+\t\toid_array_clear(options->negotiation_include_tips);\n+\t\tfree(options->negotiation_include_tips);\n+\t}\n+\tlist_objects_filter_release(&options->filter_options);\n+}\n+\n static int disconnect_git(struct transport *transport)\n {\n \tstruct git_transport_data *data = transport->data;\n@@ -1028,16 +1041,7 @@ static int disconnect_git(struct transport *transport)\n \t\t\tclose(data->fd[1]);\n \t\tfinish_connect(data->conn);\n \t}\n-\n-\tif (data->options.negotiation_restrict_tips) {\n-\t\toid_array_clear(data->options.negotiation_restrict_tips);\n-\t\tfree(data->options.negotiation_restrict_tips);\n-\t}\n-\tif (data->options.negotiation_include_tips) {\n-\t\toid_array_clear(data->options.negotiation_include_tips);\n-\t\tfree(data->options.negotiation_include_tips);\n-\t}\n-\tlist_objects_filter_release(&data->options.filter_options);\n+\ttransport_options_release(&data->options);\n \toid_array_clear(&data->extra_have);\n \toid_array_clear(&data->shallow);\n \tfree(data);\ndiff --git a/transport.h b/transport.h\nindex 39193d0077..cb262ee3da 100644\n--- a/transport.h\n+++ b/transport.h\n@@ -59,6 +59,8 @@ struct git_transport_options {\n \tstruct oidset *acked_commits;\n };\n \n+void transport_options_release(struct git_transport_options *options);\n+\n enum transport_family {\n \tTRANSPORT_FAMILY_ALL = 0,\n \tTRANSPORT_FAMILY_IPV4,\n\n-- \n2.56.GIT\n\n\n"},{"id":"556628","messageId":"20261028-799-shallow-fetch-with-tags-v5-2-7c43cc9ae810@gmail.com","threadId":"66493","inReplyTo":"20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com","subject":"[PATCH v5 2/3] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T09:48:00Z","receivedAt":"2026-10-28T09:48:00Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Batched updates are used both for the fetched\nreferences and to backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. For shallow fetches, the negotiation\nphase while backfilling tags does not advertise the newly fetched refs.\nThis can cause the server to send objects the client already has.\n\nThe issue always existed when the '--atomic' flag was used, as both the\nregular ref updates and the tags need to be part of the same\ntransaction.\n\nFix this by advertising the already fetched refs as 'haves' during the\nnegotiation. Do this by simply adding the tips to the transport's\n`negotiation_include_tips` field. This also fixes the issue for\n`--atomic` fetches since it follows the same route.\n\nThis does mean that all references fetched before the backfill will be\nused for the negotiation. While duplicate OIDs are trimmed before\nnegotiation, we could still end up with a lot of distinct OIDs used in\nthe 'haves' section. However, this is still better than the server\ntransferring already existing objects to the client, which can take a\nlot more time.\n\nAdd two tests, which validate that the 'haves' are filled when\nbackfilling tags for atomic and non-atomic flows.\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n builtin/fetch.c  | 33 +++++++++++++++++++++++++++++++--\n t/t5510-fetch.sh | 34 ++++++++++++++++++++++++++++++++++\n 2 files changed, 65 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..d3974faf6b 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1676,7 +1676,8 @@ static int backfill_tags(struct display_state *display_state,\n \t\t\t struct fetch_head *fetch_head,\n \t\t\t const struct fetch_config *config,\n \t\t\t struct ref_update_display_info_array *display_array,\n-\t\t\t struct list_objects_filter_options *filter_options)\n+\t\t\t struct list_objects_filter_options *filter_options,\n+\t\t\t const struct oid_array *fetched_tips)\n {\n \tint retcode, cannot_reuse;\n \n@@ -1697,6 +1698,21 @@ static int backfill_tags(struct display_state *display_state,\n \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n+\n+\t/*\n+\t * References fetched before the tag backfill aren't yet committed\n+\t * to disk, so negotiation cannot discover them. Advertise them to\n+\t * the server, so that it does not re-send objects we already have.\n+\t */\n+\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n+\t\tstruct oid_array **include_tips =\n+\t\t\t&transport->smart_options->negotiation_include_tips;\n+\t\tif (!*include_tips)\n+\t\t\t*include_tips = xcalloc(1, sizeof(**include_tips));\n+\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n+\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n+\t}\n+\n \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n \t\t\t\t\t fetch_head, config, display_array);\n \n@@ -2077,6 +2093,16 @@ static int do_fetch(struct transport *transport,\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n \t\tif (tags_ref_map) {\n+\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n+\n+\t\t\t/*\n+\t\t\t * Collect the tips we just fetched so the tag backfill\n+\t\t\t * can advertise them to the server as 'haves'.\n+\t\t\t */\n+\t\t\tfor (struct ref *rm = ref_map; rm; rm = rm->next)\n+\t\t\t\tif (!is_null_oid(&rm->old_oid))\n+\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n+\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\n \t\t\t * the user so, but we have to continue regardless to\n@@ -2086,8 +2112,11 @@ static int do_fetch(struct transport *transport,\n \t\t\t * the transaction and don't commit anything.\n \t\t\t */\n \t\t\tif (backfill_tags(&display_state, transport, transaction, tags_ref_map,\n-\t\t\t\t\t  &fetch_head, config, &display_array, filter_options))\n+\t\t\t\t\t  &fetch_head, config, &display_array, filter_options,\n+\t\t\t\t\t  &fetched_tips))\n \t\t\t\tretcode = 1;\n+\n+\t\t\toid_array_clear(&fetched_tips);\n \t\t}\n \n \t\tfree_refs(tags_ref_map);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..7c1f8405df 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,40 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'backfilling tags advertises fetched refs as haves' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n+test_expect_success 'backfilling tags advertises fetched refs as haves even with atomic' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --atomic --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n-- \n2.56.GIT\n\n\n"},{"id":"556629","messageId":"20261028-799-shallow-fetch-with-tags-v5-3-7c43cc9ae810@gmail.com","threadId":"66493","inReplyTo":"20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com","subject":"[PATCH v5 3/3] t5510: cover upstream info update on fetch failure","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T09:48:01Z","receivedAt":"2026-10-28T09:48:01Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"git-fetch(1) performs reference updates in a single batched transaction,\nso individual updates can be rejected while the rest succeed. Even in\nthat partial-failure case the command still performs the remainder of\nits work, such as writing the upstream tracking configuration and\nbackfilling tags.\n\nWe have no test asserting that the upstream tracking information is\nwritten when one of the reference updates is rejected. Add one.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n t/t5510-fetch.sh | 25 +++++++++++++++++++++++++\n 1 file changed, 25 insertions(+)\n\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 7c1f8405df..ca5f9642fc 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -2039,6 +2039,31 @@ test_expect_success REFFILES \"upstream tracking info is added even with conflict\n \t)\n '\n \n+test_expect_success REFFILES \"upstream tracking info is added with conflicts and backfilled tags\" '\n+\ttest_when_finished rm -rf base repo &&\n+\n+\tgit init --initial-branch=main base &&\n+\ttest_commit -C base one &&\n+\ttest_commit -C base two &&\n+\tgit -C base branch foo &&\n+\t# Backfilling only takes place for tags which are not pointing at tips.\n+\tgit -C base tag -a annotated -m annotated HEAD~1 &&\n+\n+\tgit init --bare --initial-branch=main repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit remote add origin ../base &&\n+\t\tmkdir -p refs/remotes/origin &&\n+\t\t>refs/remotes/origin/foo.lock &&\n+\t\ttest_must_fail git fetch origin --set-upstream main \\\n+\t\t\t\"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit config get branch.main.remote >actual &&\n+\t\techo \"origin\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse --verify refs/tags/annotated\n+\t)\n+'\n+\n test_expect_success REFFILES \"HEAD is updated even with conflicts\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n-- \n2.56.GIT\n\n\n"},{"id":"556638","messageId":"auHfmzP3aXyonp3C@pks.im","threadId":"66493","inReplyTo":"20261028-799-shallow-fetch-with-tags-v5-1-7c43cc9ae810@gmail.com","subject":"Re: [PATCH v5 1/3] transport: add `transport_options_release()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-28T11:42:19Z","receivedAt":"2026-10-28T11:42:19Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Oct 28, 2026 at 10:47:59AM +0100, Karthik Nayak wrote:\n> The `git_transport_options` structure is used both in 'transport.c' and\n> 'transport-helper.c'. But while the former specifically clears any\n> associated data in the structure during the cleanup of the transport,\n> the latter misses this.\n> \n> This means when using remote-helpers, data allocated for the structure\n> leaks. Introduce a new `transport_options_release()`, which acts as a\n\nNit: \"a new transport_options_release\" reads a bit weird. I'd either add\n\"function\", or drop \"a new\".\n\n> common path to release memory allocated with the structure and use it in\n> both files.\n\nWhy didn't our CI detect this leak? Don't we have tests exercising this?\n\nThe patch itself looks obviously good to me, thanks.\n\nPatrick\n\n"},{"id":"556639","messageId":"auHfoVfHQJ-dRwkM@pks.im","threadId":"66493","inReplyTo":"20261028-799-shallow-fetch-with-tags-v5-3-7c43cc9ae810@gmail.com","subject":"Re: [PATCH v5 3/3] t5510: cover upstream info update on fetch failure","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-28T11:42:25Z","receivedAt":"2026-10-28T11:42:25Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Oct 28, 2026 at 10:48:01AM +0100, Karthik Nayak wrote:\n> git-fetch(1) performs reference updates in a single batched transaction,\n> so individual updates can be rejected while the rest succeed. Even in\n> that partial-failure case the command still performs the remainder of\n> its work, such as writing the upstream tracking configuration and\n> backfilling tags.\n> \n> We have no test asserting that the upstream tracking information is\n> written when one of the reference updates is rejected. Add one.\n\nVery pedantic nit: I would have ordered this commit before the preceding\none to demonstrate that this already worked beforehand.\n\nPatrick\n\n"},{"id":"556702","messageId":"CAOLa=ZTCwvUJNj6c1G8dU778ejwO5WvVL9F5C-Db=P4Au5=mQA@mail.gmail.com","threadId":"66493","inReplyTo":"auHfmzP3aXyonp3C@pks.im","subject":"Re: [PATCH v5 1/3] transport: add `transport_options_release()`","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T20:18:11Z","receivedAt":"2026-10-28T20:18:11Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Wed, Oct 28, 2026 at 10:47:59AM +0100, Karthik Nayak wrote:\n>> The `git_transport_options` structure is used both in 'transport.c' and\n>> 'transport-helper.c'. But while the former specifically clears any\n>> associated data in the structure during the cleanup of the transport,\n>> the latter misses this.\n>>\n>> This means when using remote-helpers, data allocated for the structure\n>> leaks. Introduce a new `transport_options_release()`, which acts as a\n>\n> Nit: \"a new transport_options_release\" reads a bit weird. I'd either add\n> \"function\", or drop \"a new\".\n>\n\nFair enough, I'll drop the 'a new'.\n\n>> common path to release memory allocated with the structure and use it in\n>> both files.\n>\n> Why didn't our CI detect this leak? Don't we have tests exercising this?\n>\n> The patch itself looks obviously good to me, thanks.\n>\n> Patrick\n\nWe don't have any tests which exercise this it seems. The tests in the\nnext patch do, which is why it hit the bug!\n"},{"id":"556703","messageId":"CAOLa=ZRksy4QCRDGsx7L6m-zxcj73TrQHaHAJ6rKhHXkrksWXg@mail.gmail.com","threadId":"66493","inReplyTo":"auHfoVfHQJ-dRwkM@pks.im","subject":"Re: [PATCH v5 3/3] t5510: cover upstream info update on fetch failure","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-28T20:19:07Z","receivedAt":"2026-10-28T20:19:07Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Wed, Oct 28, 2026 at 10:48:01AM +0100, Karthik Nayak wrote:\n>> git-fetch(1) performs reference updates in a single batched transaction,\n>> so individual updates can be rejected while the rest succeed. Even in\n>> that partial-failure case the command still performs the remainder of\n>> its work, such as writing the upstream tracking configuration and\n>> backfilling tags.\n>>\n>> We have no test asserting that the upstream tracking information is\n>> written when one of the reference updates is rejected. Add one.\n>\n> Very pedantic nit: I would have ordered this commit before the preceding\n> one to demonstrate that this already worked beforehand.\n>\n> Patrick\n\nFair. Seeing that both comments are nits and minor, I'll make the\nchanges locally and not re-roll unless there are other comments.\n"},{"id":"556736","messageId":"auL-r__SD8m3it5h@pks.im","threadId":"66493","inReplyTo":"CAOLa=ZTCwvUJNj6c1G8dU778ejwO5WvVL9F5C-Db=P4Au5=mQA@mail.gmail.com","subject":"Re: [PATCH v5 1/3] transport: add `transport_options_release()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-29T08:07:11Z","receivedAt":"2026-10-29T08:07:11Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Oct 28, 2026 at 04:18:11PM -0400, Karthik Nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > On Wed, Oct 28, 2026 at 10:47:59AM +0100, Karthik Nayak wrote:\n> >> The `git_transport_options` structure is used both in 'transport.c' and\n> >> 'transport-helper.c'. But while the former specifically clears any\n> >> associated data in the structure during the cleanup of the transport,\n> >> the latter misses this.\n> >>\n> >> This means when using remote-helpers, data allocated for the structure\n> >> leaks. Introduce a new `transport_options_release()`, which acts as a\n> >\n> > Nit: \"a new transport_options_release\" reads a bit weird. I'd either add\n> > \"function\", or drop \"a new\".\n> >\n> \n> Fair enough, I'll drop the 'a new'.\n> \n> >> common path to release memory allocated with the structure and use it in\n> >> both files.\n> >\n> > Why didn't our CI detect this leak? Don't we have tests exercising this?\n> >\n> > The patch itself looks obviously good to me, thanks.\n> >\n> > Patrick\n> \n> We don't have any tests which exercise this it seems. The tests in the\n> next patch do, which is why it hit the bug!\n\nThat would have been great to point out in the commit message :) Thanks\nfor clarifying!\n\nPatrick\n\n"},{"id":"556759","messageId":"CAOLa=ZRr2X58KqC75uqqrb73ALuP=KRPkNMuEM-oAqPkfe4X1g@mail.gmail.com","threadId":"66493","inReplyTo":"auL-r__SD8m3it5h@pks.im","subject":"Re: [PATCH v5 1/3] transport: add `transport_options_release()`","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-29T10:02:51Z","receivedAt":"2026-10-29T10:02:51Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Wed, Oct 28, 2026 at 04:18:11PM -0400, Karthik Nayak wrote:\n>> Patrick Steinhardt <ps@pks.im> writes:\n>>\n>> > On Wed, Oct 28, 2026 at 10:47:59AM +0100, Karthik Nayak wrote:\n>> >> The `git_transport_options` structure is used both in 'transport.c' and\n>> >> 'transport-helper.c'. But while the former specifically clears any\n>> >> associated data in the structure during the cleanup of the transport,\n>> >> the latter misses this.\n>> >>\n>> >> This means when using remote-helpers, data allocated for the structure\n>> >> leaks. Introduce a new `transport_options_release()`, which acts as a\n>> >\n>> > Nit: \"a new transport_options_release\" reads a bit weird. I'd either add\n>> > \"function\", or drop \"a new\".\n>> >\n>>\n>> Fair enough, I'll drop the 'a new'.\n>>\n>> >> common path to release memory allocated with the structure and use it in\n>> >> both files.\n>> >\n>> > Why didn't our CI detect this leak? Don't we have tests exercising this?\n>> >\n>> > The patch itself looks obviously good to me, thanks.\n>> >\n>> > Patrick\n>>\n>> We don't have any tests which exercise this it seems. The tests in the\n>> next patch do, which is why it hit the bug!\n>\n> That would have been great to point out in the commit message :) Thanks\n> for clarifying!\n>\n> Patrick\n\nLet me push a new version with all these fixes. Thanks for the review\n"},{"id":"556774","messageId":"20261029-799-shallow-fetch-with-tags-v6-0-e6ddd5201def@gmail.com","threadId":"66493","inReplyTo":"20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com","subject":"[PATCH v6 0/3] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-29T12:45:36Z","receivedAt":"2026-10-29T12:45:36Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The issue was reported by Mitja Bezenšek on GitLab's Git repo [1].\n\nWhen fetching, git-fetch(1) performs the reference updates in two\nsteps:\n1. Fetches the regular references\n2. Backfills any tags\n\nWith batched updates, both of these operations came under the same\nreference transaction. Unfortunately, this meant that the initial\nreferences fetched were not committed and couldn't be used as 'haves'\nduring the negotiation when backfilling tags. In shallow repositories,\nthis could mean that the server was sending objects the client already\npossessed. This series fixes this regression.\n\nEarlier versions (1-3) tried to fix this by splitting the transaction\ninto two. Which works, but makes the code more complex and is a bit\nmessy overall as we have to deal with errors and also jumps in the\ncodeflow.\n\nJustin suggested that we could use the references already available for\nnegotiation instead, that is what v4 onwards does.\n\n[1]: https://gitlab.com/gitlab-org/git/-/work_items/799\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\nChanges in v6:\n- Move the last commit to the front, since it adds a test to exercise\n  existing behavior.\n- Fix grammar in the commit message and mention how the next commit\n  would catch the leak fixed in 2/3.\n- Link to v5: https://patch.msgid.link/20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com\n\nChanges in v5:\n- Add a patch fixing a pre-existing leak in the remote-helper\n  transport's cleanup. This allows the patch to drop its manual cleanup.\n- Move the upstream-info test into its own commit.\n- Some cleanpus around the commit message and grammar\n- Link to v4: https://patch.msgid.link/20261022-799-shallow-fetch-with-tags-v4-1-8a1c9ef399ed@gmail.com\n\nChanges in v4:\n- Instead of committing the first transaction and starting a new one,\n  advertise the refs already fetched when backfilling tags. Thanks to\n  Justin for the suggestion. This also fixes the `--atomic` flow.\n- Link to v3: https://patch.msgid.link/20261015-799-shallow-fetch-with-tags-v3-1-90b6e38b98d2@gmail.com\n\nChanges in v3:\n- Fix the early exit, so a failed transaction_begin() only skips the\n  backfill.\n- Keep the first commit's result separately, so rejections don't skip\n  the upstream and remote-HEAD setup.\n- Test that the backfill advertises the fetched tip as a have, instead\n  of checking that the root object isn't fetched, which passed even\n  without the fix.\n- Add a test for the upstream setup when a main ref is rejected and a\n  tag is backfilled.\n- Link to v2: https://patch.msgid.link/20261012-799-shallow-fetch-with-tags-v2-1-4211e9a13d3c@gmail.com\n\nChanges in v2:\n- Add benchmarks between master and this branch to show perf regression.\n- Instead of checking the trace file, simply check that we didn't fetch\n  older objects in the test.\n- Link to v1: https://patch.msgid.link/20261009-799-shallow-fetch-with-tags-v1-1-379d61504af5@gmail.com\n\nCC: Patrick Steinhardt <ps@pks.im>\nCC: Justin Tobler <jltobler@gmail.com>\n\n---\nKarthik Nayak (3):\n      t5510: cover upstream info update on fetch failure\n      transport: add `transport_options_release()`\n      fetch: advertise fetched refs when backfilling tags\n\n builtin/fetch.c    | 33 ++++++++++++++++++++++++++++--\n t/t5510-fetch.sh   | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\n transport-helper.c |  1 +\n transport.c        | 24 +++++++++++++---------\n transport.h        |  2 ++\n 5 files changed, 107 insertions(+), 12 deletions(-)\n\nRange-diff versus v5:\n\n3:  ed24854913 = 1:  6aafee69c1 t5510: cover upstream info update on fetch failure\n1:  f15e69e2e6 ! 2:  a22b348247 transport: add `transport_options_release()`\n    @@ Commit message\n         the latter misses this.\n     \n         This means when using remote-helpers, data allocated for the structure\n    -    leaks. Introduce a new `transport_options_release()`, which acts as a\n    -    common path to release memory allocated with the structure and use it in\n    -    both files.\n    +    leaks. None of the existing tests capture this leak, but the tests\n    +    introduced in the next patch do. Fix the leak by introducing\n    +    `transport_options_release()`, which acts as a common path to release\n    +    memory allocated with the structure and use it in both files.\n     \n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n     \n2:  dc7024fafb = 3:  ebe210d2df fetch: advertise fetched refs when backfilling tags\n\n---\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nchange-id: 20261007-799-shallow-fetch-with-tags-d62b3efb29f0\n\n\nThanks\n- Karthik\n\n\n"},{"id":"556775","messageId":"20261029-799-shallow-fetch-with-tags-v6-1-e6ddd5201def@gmail.com","threadId":"66493","inReplyTo":"20261029-799-shallow-fetch-with-tags-v6-0-e6ddd5201def@gmail.com","subject":"[PATCH v6 1/3] t5510: cover upstream info update on fetch failure","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-29T12:45:37Z","receivedAt":"2026-10-29T12:45:37Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"git-fetch(1) performs reference updates in a single batched transaction,\nso individual updates can be rejected while the rest succeed. Even in\nthat partial-failure case the command still performs the remainder of\nits work, such as writing the upstream tracking configuration and\nbackfilling tags.\n\nWe have no test asserting that the upstream tracking information is\nwritten when one of the reference updates is rejected. Add one.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n t/t5510-fetch.sh | 25 +++++++++++++++++++++++++\n 1 file changed, 25 insertions(+)\n\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 300bd5396d..30871617f9 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -2005,6 +2005,31 @@ test_expect_success REFFILES \"upstream tracking info is added even with conflict\n \t)\n '\n \n+test_expect_success REFFILES \"upstream tracking info is added with conflicts and backfilled tags\" '\n+\ttest_when_finished rm -rf base repo &&\n+\n+\tgit init --initial-branch=main base &&\n+\ttest_commit -C base one &&\n+\ttest_commit -C base two &&\n+\tgit -C base branch foo &&\n+\t# Backfilling only takes place for tags which are not pointing at tips.\n+\tgit -C base tag -a annotated -m annotated HEAD~1 &&\n+\n+\tgit init --bare --initial-branch=main repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit remote add origin ../base &&\n+\t\tmkdir -p refs/remotes/origin &&\n+\t\t>refs/remotes/origin/foo.lock &&\n+\t\ttest_must_fail git fetch origin --set-upstream main \\\n+\t\t\t\"+refs/heads/*:refs/remotes/origin/*\" &&\n+\t\tgit config get branch.main.remote >actual &&\n+\t\techo \"origin\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit rev-parse --verify refs/tags/annotated\n+\t)\n+'\n+\n test_expect_success REFFILES \"HEAD is updated even with conflicts\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n-- \n2.56.GIT\n\n\n"},{"id":"556776","messageId":"20261029-799-shallow-fetch-with-tags-v6-2-e6ddd5201def@gmail.com","threadId":"66493","inReplyTo":"20261029-799-shallow-fetch-with-tags-v6-0-e6ddd5201def@gmail.com","subject":"[PATCH v6 2/3] transport: add `transport_options_release()`","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-29T12:45:38Z","receivedAt":"2026-10-29T12:45:38Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The `git_transport_options` structure is used both in 'transport.c' and\n'transport-helper.c'. But while the former specifically clears any\nassociated data in the structure during the cleanup of the transport,\nthe latter misses this.\n\nThis means when using remote-helpers, data allocated for the structure\nleaks. None of the existing tests capture this leak, but the tests\nintroduced in the next patch do. Fix the leak by introducing\n`transport_options_release()`, which acts as a common path to release\nmemory allocated with the structure and use it in both files.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n transport-helper.c |  1 +\n transport.c        | 24 ++++++++++++++----------\n transport.h        |  2 ++\n 3 files changed, 17 insertions(+), 10 deletions(-)\n\ndiff --git a/transport-helper.c b/transport-helper.c\nindex d5a064d386..14d179e0f6 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -401,6 +401,7 @@ static int release_helper(struct transport *transport)\n \trefspec_clear(&data->rs);\n \tfree(data->import_marks);\n \tfree(data->export_marks);\n+\ttransport_options_release(&data->transport_options);\n \tres = disconnect_helper(transport);\n \tfree(transport->data);\n \treturn res;\ndiff --git a/transport.c b/transport.c\nindex 25e2c14a7b..4491443b28 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -1017,6 +1017,19 @@ static int connect_git(struct transport *transport,\n \treturn 0;\n }\n \n+void transport_options_release(struct git_transport_options *options)\n+{\n+\tif (options->negotiation_restrict_tips) {\n+\t\toid_array_clear(options->negotiation_restrict_tips);\n+\t\tfree(options->negotiation_restrict_tips);\n+\t}\n+\tif (options->negotiation_include_tips) {\n+\t\toid_array_clear(options->negotiation_include_tips);\n+\t\tfree(options->negotiation_include_tips);\n+\t}\n+\tlist_objects_filter_release(&options->filter_options);\n+}\n+\n static int disconnect_git(struct transport *transport)\n {\n \tstruct git_transport_data *data = transport->data;\n@@ -1028,16 +1041,7 @@ static int disconnect_git(struct transport *transport)\n \t\t\tclose(data->fd[1]);\n \t\tfinish_connect(data->conn);\n \t}\n-\n-\tif (data->options.negotiation_restrict_tips) {\n-\t\toid_array_clear(data->options.negotiation_restrict_tips);\n-\t\tfree(data->options.negotiation_restrict_tips);\n-\t}\n-\tif (data->options.negotiation_include_tips) {\n-\t\toid_array_clear(data->options.negotiation_include_tips);\n-\t\tfree(data->options.negotiation_include_tips);\n-\t}\n-\tlist_objects_filter_release(&data->options.filter_options);\n+\ttransport_options_release(&data->options);\n \toid_array_clear(&data->extra_have);\n \toid_array_clear(&data->shallow);\n \tfree(data);\ndiff --git a/transport.h b/transport.h\nindex 39193d0077..cb262ee3da 100644\n--- a/transport.h\n+++ b/transport.h\n@@ -59,6 +59,8 @@ struct git_transport_options {\n \tstruct oidset *acked_commits;\n };\n \n+void transport_options_release(struct git_transport_options *options);\n+\n enum transport_family {\n \tTRANSPORT_FAMILY_ALL = 0,\n \tTRANSPORT_FAMILY_IPV4,\n\n-- \n2.56.GIT\n\n\n"},{"id":"556777","messageId":"20261029-799-shallow-fetch-with-tags-v6-3-e6ddd5201def@gmail.com","threadId":"66493","inReplyTo":"20261029-799-shallow-fetch-with-tags-v6-0-e6ddd5201def@gmail.com","subject":"[PATCH v6 3/3] fetch: advertise fetched refs when backfilling tags","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-29T12:45:39Z","receivedAt":"2026-10-29T12:45:39Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 0e358de64a (fetch: use batched reference updates, 2025-05-19), the\nfetch code was modified to use batched updates to provide a good\nperformance improvement. Batched updates are used both for the fetched\nreferences and to backfill tags.\n\nWhen using batched updates, the references aren't yet committed to disk\nwhen we start backfilling tags. For shallow fetches, the negotiation\nphase while backfilling tags does not advertise the newly fetched refs.\nThis can cause the server to send objects the client already has.\n\nThe issue always existed when the '--atomic' flag was used, as both the\nregular ref updates and the tags need to be part of the same\ntransaction.\n\nFix this by advertising the already fetched refs as 'haves' during the\nnegotiation. Do this by simply adding the tips to the transport's\n`negotiation_include_tips` field. This also fixes the issue for\n`--atomic` fetches since it follows the same route.\n\nThis does mean that all references fetched before the backfill will be\nused for the negotiation. While duplicate OIDs are trimmed before\nnegotiation, we could still end up with a lot of distinct OIDs used in\nthe 'haves' section. However, this is still better than the server\ntransferring already existing objects to the client, which can take a\nlot more time.\n\nAdd two tests, which validate that the 'haves' are filled when\nbackfilling tags for atomic and non-atomic flows.\n\nReported-by: Mitja Bezenšek <mitja.bezensek@login5.org>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n builtin/fetch.c  | 33 +++++++++++++++++++++++++++++++--\n t/t5510-fetch.sh | 34 ++++++++++++++++++++++++++++++++++\n 2 files changed, 65 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfd..d3974faf6b 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -1676,7 +1676,8 @@ static int backfill_tags(struct display_state *display_state,\n \t\t\t struct fetch_head *fetch_head,\n \t\t\t const struct fetch_config *config,\n \t\t\t struct ref_update_display_info_array *display_array,\n-\t\t\t struct list_objects_filter_options *filter_options)\n+\t\t\t struct list_objects_filter_options *filter_options,\n+\t\t\t const struct oid_array *fetched_tips)\n {\n \tint retcode, cannot_reuse;\n \n@@ -1697,6 +1698,21 @@ static int backfill_tags(struct display_state *display_state,\n \ttransport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);\n \ttransport_set_option(transport, TRANS_OPT_DEPTH, \"0\");\n \ttransport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);\n+\n+\t/*\n+\t * References fetched before the tag backfill aren't yet committed\n+\t * to disk, so negotiation cannot discover them. Advertise them to\n+\t * the server, so that it does not re-send objects we already have.\n+\t */\n+\tif (fetched_tips && fetched_tips->nr && transport->smart_options) {\n+\t\tstruct oid_array **include_tips =\n+\t\t\t&transport->smart_options->negotiation_include_tips;\n+\t\tif (!*include_tips)\n+\t\t\t*include_tips = xcalloc(1, sizeof(**include_tips));\n+\t\tfor (size_t i = 0; i < fetched_tips->nr; i++)\n+\t\t\toid_array_append(*include_tips, &fetched_tips->oid[i]);\n+\t}\n+\n \tretcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,\n \t\t\t\t\t fetch_head, config, display_array);\n \n@@ -2077,6 +2093,16 @@ static int do_fetch(struct transport *transport,\n \n \t\tfind_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);\n \t\tif (tags_ref_map) {\n+\t\t\tstruct oid_array fetched_tips = OID_ARRAY_INIT;\n+\n+\t\t\t/*\n+\t\t\t * Collect the tips we just fetched so the tag backfill\n+\t\t\t * can advertise them to the server as 'haves'.\n+\t\t\t */\n+\t\t\tfor (struct ref *rm = ref_map; rm; rm = rm->next)\n+\t\t\t\tif (!is_null_oid(&rm->old_oid))\n+\t\t\t\t\toid_array_append(&fetched_tips, &rm->old_oid);\n+\n \t\t\t/*\n \t\t\t * If backfilling of tags fails then we want to tell\n \t\t\t * the user so, but we have to continue regardless to\n@@ -2086,8 +2112,11 @@ static int do_fetch(struct transport *transport,\n \t\t\t * the transaction and don't commit anything.\n \t\t\t */\n \t\t\tif (backfill_tags(&display_state, transport, transaction, tags_ref_map,\n-\t\t\t\t\t  &fetch_head, config, &display_array, filter_options))\n+\t\t\t\t\t  &fetch_head, config, &display_array, filter_options,\n+\t\t\t\t\t  &fetched_tips))\n \t\t\t\tretcode = 1;\n+\n+\t\t\toid_array_clear(&fetched_tips);\n \t\t}\n \n \t\tfree_refs(tags_ref_map);\ndiff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh\nindex 30871617f9..ca5f9642fc 100755\n--- a/t/t5510-fetch.sh\n+++ b/t/t5510-fetch.sh\n@@ -1942,6 +1942,40 @@ test_expect_success \"backfill tags when providing a refspec\" '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'backfilling tags advertises fetched refs as haves' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n+test_expect_success 'backfilling tags advertises fetched refs as haves even with atomic' '\n+\ttest_when_finished rm -rf source target trace &&\n+\n+\tgit init source &&\n+\ttest_commit_bulk -C source 10 &&\n+\tgit -C source tag -a tag -m tag HEAD~2 &&\n+\tHEAD_OID=$(git -C source rev-parse HEAD) &&\n+\n+\tgit init target &&\n+\tgit -C target remote add origin ../source &&\n+\tGIT_TEST_PROTOCOL_VERSION=2 GIT_TRACE_PACKET=$(pwd)/trace \\\n+\t\tgit -C target fetch --atomic --depth 5 origin &&\n+\n+\ttest_grep \"fetch> have $HEAD_OID\" trace &&\n+\tgit -C target rev-parse --verify refs/tags/tag\n+'\n+\n test_expect_success REFFILES \"FETCH_HEAD is updated even if ref updates fail\" '\n \ttest_when_finished rm -rf base repo &&\n \n\n-- \n2.56.GIT\n\n\n"},{"id":"556784","messageId":"auNReY6zQ5x7tlbV@pks.im","threadId":"66493","inReplyTo":"20261029-799-shallow-fetch-with-tags-v6-0-e6ddd5201def@gmail.com","subject":"Re: [PATCH v6 0/3] fetch: advertise fetched refs when backfilling tags","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-29T14:00:25Z","receivedAt":"2026-10-29T14:00:25Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 29, 2026 at 01:45:36PM +0100, Karthik Nayak wrote:\n> Changes in v6:\n> - Move the last commit to the front, since it adds a test to exercise\n>   existing behavior.\n> - Fix grammar in the commit message and mention how the next commit\n>   would catch the leak fixed in 2/3.\n> - Link to v5: https://patch.msgid.link/20261028-799-shallow-fetch-with-tags-v5-0-7c43cc9ae810@gmail.com\n\nThanks, I'm happy with these changes.\n\nPatrick\n\n"}]}