Skip to content

Commit 0177565

Browse files
jonathantanmygitster
authored andcommitted
transport: do not list refs if possible
When all refs to be fetched are exact OIDs, it is possible to perform a fetch without requiring the remote to list refs if protocol v2 is used. Teach Git to do this. This currently has an effect only for lazy fetches done from partial clones. The change necessary to likewise optimize "git fetch <remote> <sha-1>" will be done in a subsequent patch. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 99bcb88 commit 0177565

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
15981598
if (nr_sought)
15991599
nr_sought = remove_duplicates_in_refs(sought, nr_sought);
16001600

1601-
if (!ref) {
1601+
if (version != protocol_v2 && !ref) {
16021602
packet_flush(fd[1]);
16031603
die(_("no matching remote head"));
16041604
}

t/t5702-protocol-v2.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ test_expect_success 'dynamically fetch missing object' '
286286
grep "version 2" trace
287287
'
288288

289+
test_expect_success 'when dynamically fetching missing object, do not list refs' '
290+
! grep "git> command=ls-refs" trace
291+
'
292+
289293
test_expect_success 'partial fetch' '
290294
rm -rf client "$(pwd)/trace" &&
291295
git init client &&

transport.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,17 @@ static int fetch_refs_via_pack(struct transport *transport,
341341
args.server_options = transport->server_options;
342342
args.negotiation_tips = data->options.negotiation_tips;
343343

344-
if (!data->got_remote_heads)
345-
refs_tmp = get_refs_via_connect(transport, 0, NULL);
344+
if (!data->got_remote_heads) {
345+
int i;
346+
int must_list_refs = 0;
347+
for (i = 0; i < nr_heads; i++) {
348+
if (!to_fetch[i]->exact_oid) {
349+
must_list_refs = 1;
350+
break;
351+
}
352+
}
353+
refs_tmp = handshake(transport, 0, NULL, must_list_refs);
354+
}
346355

347356
switch (data->version) {
348357
case protocol_v2:

0 commit comments

Comments
 (0)