]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: ask server to advertise HEAD for config-less fetch
authorJeff King <peff@peff.net>
Sun, 9 Mar 2025 03:08:47 +0000 (22:08 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Mar 2025 20:13:45 +0000 (13:13 -0700)
If we're not given any refspecs (either on the command line or via
config) and we have no branch merge config, then we fetch the remote
HEAD into our local FETCH_HEAD. In that case we do not send any
ref-prefix option to the server at all, and we see the full
advertisement.

But this is sub-optimal. We only care about HEAD, so we can just ask
for that, and ignore all of the other refs.

The new test demonstrates a case where we see fewer refs (in this case
only one less, but in theory we could be ignoring millions of them).

This also removes the only case where we care about seeing some refs
from the other side, but don't add anything to the ref_prefixes list.
Cleaning this up means one less maintenance burden. Before this patch,
any code which wanted to add to the list had to make sure the list was
not empty, since an empty list meant "ask for everything". Now it really
means "we are not interested in any refs".

This should let us optimize a few more cases in subsequent patches.

Note that we'll add "HEAD" to the list of prefixes, and later code for
updating "refs/remotes/<remote>/HEAD" may likewise do so. In theory this
could cause duplicates in the list, but in practice these can't both
trigger. We hit our new case only if there are no refspecs, and the
"<remote>/HEAD" feature is enabled only when we are fetching from a
remote with configured refspecs. We could be defensive with a flag, but
it didn't seem worth it to me (the absolute worse case is a useless
redundant ref-prefix line sent to the server).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fetch.c
t/t5702-protocol-v2.sh

index 95fd0018b981fb83ebb8a15de9bff03c2d25a97c..f14275644166d8033b36733622c388c9352b2319 100644 (file)
@@ -1766,6 +1766,14 @@ static int do_fetch(struct transport *transport,
                                            branch->merge[i]->src);
                        }
                }
+
+               /*
+                * If there are no refs specified to fetch, then we just
+                * fetch HEAD; mention that to narrow the advertisement.
+                */
+               if (!transport_ls_refs_options.ref_prefixes.nr)
+                       strvec_push(&transport_ls_refs_options.ref_prefixes,
+                                   "HEAD");
        }
 
        if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
index cea8f92a3da06580b45cbc2dca785f23cbc5fce0..2f0a52a72d1636049b4734c0d1a5248dcb6a5cba 100755 (executable)
@@ -679,6 +679,21 @@ test_expect_success 'default refspec is used to filter ref when fetching' '
        grep "ref-prefix refs/tags/" log
 '
 
+test_expect_success 'set up parent for prefix tests' '
+       git init prefix-parent &&
+       git -C prefix-parent commit --allow-empty -m foo &&
+       git -C prefix-parent branch unrelated-branch
+'
+
+test_expect_success 'empty refspec filters refs when fetching' '
+       git init configless-child &&
+
+       test_when_finished "rm -f log" &&
+       GIT_TRACE_PACKET="$(pwd)/log" \
+               git -C configless-child fetch ../prefix-parent &&
+       test_grep ! unrelated-branch log
+'
+
 test_expect_success 'fetch supports various ways of have lines' '
        rm -rf server client trace &&
        git init server &&