fetch: don't ask for remote HEAD if followRemoteHEAD is "never"

When we are going to consider updating the refs/remotes/*/HEAD symref, we have to ask the remote side where its HEAD points. But if we know that the feature is disabled by config, we don't need to bother! This saves a little bit of work and network communication for the server. And even a little bit of effort on the client, as our local set_head() function did a bit of work matching the remote HEAD before realizing that we're not going to do anything with it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 18, 2025 at 01:41 UTC aab0f899d9349bed824bf545b7398ab16c27a204
2 files changed +6 -5
builtin/fetch.c
+2 -4
@@ -1643,9 +1643,6 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
1643 string_list_append(&heads, strip_refshead(ref->name));
1644 }
1645
1646 - if (follow_remote_head == FOLLOW_REMOTE_NEVER)
1647 - goto cleanup;
1648 -
1646 if (!heads.nr)
1647 result = 1;
1648 else if (heads.nr > 1)
@@ -1729,7 +1726,8 @@ static int do_fetch(struct transport *transport,
1726 if (transport->remote->fetch.nr) {
1727 refspec_ref_prefixes(&transport->remote->fetch,
1728 &transport_ls_refs_options.ref_prefixes);
1732 - do_set_head = 1;
1729 + if (transport->remote->follow_remote_head != FOLLOW_REMOTE_NEVER)
1730 + do_set_head = 1;
1731 }
1732 if (branch_has_merge_config(branch) &&
1733 !strcmp(branch->remote_name, transport->remote->name)) {
t/t5510-fetch.sh
+4 -1
@@ -119,7 +119,10 @@ test_expect_success "fetch test followRemoteHEAD never" '
119 cd two &&
120 git update-ref --no-deref -d refs/remotes/origin/HEAD &&
121 git config set remote.origin.followRemoteHEAD "never" &&
122 - git fetch &&
122 + GIT_TRACE_PACKET=$PWD/trace.out git fetch &&
123 + # Confirm that we do not even ask for HEAD when we are
124 + # not going to act on it.
125 + test_grep ! "ref-prefix HEAD" trace.out &&
126 test_must_fail git rev-parse --verify refs/remotes/origin/HEAD
127 )
128 '