Skip to content

Commit

Permalink
transport: do not list refs if possible
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jonathantanmy authored and gitster committed Oct 7, 2018
1 parent 99bcb88 commit 0177565
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
if (nr_sought)
nr_sought = remove_duplicates_in_refs(sought, nr_sought);

if (!ref) {
if (version != protocol_v2 && !ref) {
packet_flush(fd[1]);
die(_("no matching remote head"));
}
Expand Down
4 changes: 4 additions & 0 deletions t/t5702-protocol-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ test_expect_success 'dynamically fetch missing object' '
grep "version 2" trace
'

test_expect_success 'when dynamically fetching missing object, do not list refs' '
! grep "git> command=ls-refs" trace
'

test_expect_success 'partial fetch' '
rm -rf client "$(pwd)/trace" &&
git init client &&
Expand Down
13 changes: 11 additions & 2 deletions transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,17 @@ static int fetch_refs_via_pack(struct transport *transport,
args.server_options = transport->server_options;
args.negotiation_tips = data->options.negotiation_tips;

if (!data->got_remote_heads)
refs_tmp = get_refs_via_connect(transport, 0, NULL);
if (!data->got_remote_heads) {
int i;
int must_list_refs = 0;
for (i = 0; i < nr_heads; i++) {
if (!to_fetch[i]->exact_oid) {
must_list_refs = 1;
break;
}
}
refs_tmp = handshake(transport, 0, NULL, must_list_refs);
}

switch (data->version) {
case protocol_v2:
Expand Down

0 comments on commit 0177565

Please sign in to comment.