Skip to content

Commit 20010b8

Browse files
peffgitster
authored andcommitted
fetch: avoid ls-refs only to ask for HEAD symref update
When we fetch from a configured remote, we may try to update the local refs/remotes/<origin>/HEAD, and so we ask the server to advertise its HEAD to us. But if we aren't otherwise asking about any refs at all, then we know this HEAD update can never happen! To consider a new value for HEAD, the set_head() function uses guess_remote_head(). And even if it sees an explicit symref value for HEAD, it will only report that as a match if we also saw that remote ref advertised, and it mapped to a local tracking ref via get_fetch_map(). In other words, a fetch like this: git fetch origin $exact_oid:refs/heads/foo can never update HEAD, because we will never have fetched (nor even see the advertisement for) the ref that HEAD points to. Currently the command above will still call ls-refs to ask about the HEAD, even though it is pointless. This patch teaches it to skip the ls-refs call entirely in this case, which avoids a round-trip to the server. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 095bc13 commit 20010b8

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

builtin/fetch.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1782,11 +1782,10 @@ static int do_fetch(struct transport *transport,
17821782
"refs/tags/");
17831783
}
17841784

1785-
if (uses_remote_tracking(transport, rs)) {
1786-
must_list_refs = 1;
1785+
if (must_list_refs &&
1786+
uses_remote_tracking(transport, rs))
17871787
strvec_push(&transport_ls_refs_options.ref_prefixes,
17881788
"HEAD");
1789-
}
17901789

17911790
if (must_list_refs) {
17921791
trace2_region_enter("fetch", "remote_refs", the_repository);

t/t5702-protocol-v2.sh

+13
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,19 @@ test_expect_success 'exact oid fetch with tag following' '
708708
git -C exact-oid-tags rev-parse --verify my-tag
709709
'
710710

711+
test_expect_success 'exact oid fetch avoids pointless HEAD request' '
712+
git init exact-oid-head &&
713+
git -C exact-oid-head remote add origin ../prefix-parent &&
714+
715+
commit=$(git -C prefix-parent rev-parse --verify HEAD) &&
716+
717+
test_when_finished "rm -f log" &&
718+
GIT_TRACE_PACKET="$(pwd)/log" \
719+
git -C exact-oid-head fetch --no-tags origin \
720+
$commit:refs/heads/exact &&
721+
test_grep ! command=ls-refs log
722+
'
723+
711724
test_expect_success 'fetch supports various ways of have lines' '
712725
rm -rf server client trace &&
713726
git init server &&

0 commit comments

Comments
 (0)