Skip to content

Commit 095bc13

Browse files
peffgitster
authored andcommitted
fetch: stop protecting additions to ref-prefix list
When using the ref-prefix feature of protocol v2, a client which sends no prefixes at all will get the full advertisement. And so the code in git-fetch was historically loose about setting up that list based on our refspecs. There were cases where we needed to know about some refs, so we just didn't add anything to the ref-prefix list. And hence further code, like that for tag-following and updating origin/HEAD, had to be careful about adding to an empty list. E.g., see the bug fixed by bd52d9a (fetch: fix following tags when fetching specific OID, 2025-03-07). But the previous commit removed the last such case, and now we know an empty ref-prefix list (at least inside git-fetch's do_fetch() function) means that we really don't need to see any refs. So we can drop those extra conditionals. This simplifies the code a little. But it also means that some cases can now use ref prefixes when they would not otherwise. As the test shows, fetching an exact oid into a local ref can now avoid enumerating all of the refs. The refspec itself doesn't need to know about any remote refs, and the tag auto-following can just ask about refs/tags/. The same is true for asking about HEAD to update the local origin/HEAD. I didn't add a test for that yet, though, as we can optimize it even further. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 625ed92 commit 095bc13

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

builtin/fetch.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,16 +1778,14 @@ static int do_fetch(struct transport *transport,
17781778

17791779
if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
17801780
must_list_refs = 1;
1781-
if (transport_ls_refs_options.ref_prefixes.nr)
1782-
strvec_push(&transport_ls_refs_options.ref_prefixes,
1783-
"refs/tags/");
1781+
strvec_push(&transport_ls_refs_options.ref_prefixes,
1782+
"refs/tags/");
17841783
}
17851784

17861785
if (uses_remote_tracking(transport, rs)) {
17871786
must_list_refs = 1;
1788-
if (transport_ls_refs_options.ref_prefixes.nr)
1789-
strvec_push(&transport_ls_refs_options.ref_prefixes,
1790-
"HEAD");
1787+
strvec_push(&transport_ls_refs_options.ref_prefixes,
1788+
"HEAD");
17911789
}
17921790

17931791
if (must_list_refs) {

t/t5702-protocol-v2.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ test_expect_success 'default refspec is used to filter ref when fetching' '
682682
test_expect_success 'set up parent for prefix tests' '
683683
git init prefix-parent &&
684684
git -C prefix-parent commit --allow-empty -m foo &&
685+
git -C prefix-parent tag my-tag &&
685686
git -C prefix-parent branch unrelated-branch
686687
'
687688

@@ -694,6 +695,19 @@ test_expect_success 'empty refspec filters refs when fetching' '
694695
test_grep ! unrelated-branch log
695696
'
696697

698+
test_expect_success 'exact oid fetch with tag following' '
699+
git init exact-oid-tags &&
700+
701+
commit=$(git -C prefix-parent rev-parse --verify HEAD) &&
702+
703+
test_when_finished "rm -f log" &&
704+
GIT_TRACE_PACKET="$(pwd)/log" \
705+
git -C exact-oid-tags fetch ../prefix-parent \
706+
$commit:refs/heads/exact &&
707+
test_grep ! unrelated-branch log &&
708+
git -C exact-oid-tags rev-parse --verify my-tag
709+
'
710+
697711
test_expect_success 'fetch supports various ways of have lines' '
698712
rm -rf server client trace &&
699713
git init server &&

0 commit comments

Comments
 (0)