Skip to content

Commit ac3fda8

Browse files
jonathantanmygitster
authored andcommitted
transport-helper: skip ls-refs if unnecessary
Commit e70a303 ("fetch: do not list refs if fetching only hashes", 2018-10-07) and its ancestors taught Git, as an optimization, to skip the ls-refs step when it is not necessary during a protocol v2 fetch (for example, when lazy fetching a missing object in a partial clone, or when running "git fetch --no-tags <remote> <SHA-1>"). But that was only done for natively supported protocols; in particular, HTTP was not supported. Teach Git to skip ls-refs when using remote helpers that support connect or stateless-connect. To do this, fetch() is made an acceptable entry point. Because fetch() can now be the first function in the vtable called, "get_helper(transport);" has to be added to the beginning of that function to set the transport up (if not yet set up) before process_connect() is invoked. When fetch() is called, the transport could be taken over (this happens if "connect" or "stateless-connect" is successfully run without any "fallback" response), or not. If the transport is taken over, execution continues like execution for natively supported protocols (fetch_refs_via_pack() is executed, which will fetch refs using ls-refs if needed). If not, the remote helper interface will invoke get_refs_list() if it hasn't been invoked yet, preserving existing behavior. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 745f681 commit ac3fda8

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

t/t5702-protocol-v2.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,19 @@ test_expect_success 'fetch with http:// using protocol v2' '
631631
grep "git< version 2" log
632632
'
633633

634+
test_expect_success 'fetch with http:// by hash without tag following with protocol v2 does not list refs' '
635+
test_when_finished "rm -f log" &&
636+
637+
test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two_a &&
638+
git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" rev-parse two_a >two_a_hash &&
639+
640+
GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
641+
fetch --no-tags origin $(cat two_a_hash) &&
642+
643+
grep "fetch< version 2" log &&
644+
! grep "fetch> command=ls-refs" log
645+
'
646+
634647
test_expect_success 'fetch from namespaced repo respects namespaces' '
635648
test_when_finished "rm -f log" &&
636649

transport-helper.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ struct helper_data {
3333
check_connectivity : 1,
3434
no_disconnect_req : 1,
3535
no_private_update : 1;
36+
37+
/*
38+
* As an optimization, the transport code may invoke fetch before
39+
* get_refs_list. If this happens, and if the transport helper doesn't
40+
* support connect or stateless_connect, we need to invoke
41+
* get_refs_list ourselves if we haven't already done so. Keep track of
42+
* whether we have invoked get_refs_list.
43+
*/
44+
unsigned get_refs_list_called : 1;
45+
3646
char *export_marks;
3747
char *import_marks;
3848
/* These go from remote name (as in "list") to private name */
@@ -652,17 +662,25 @@ static int connect_helper(struct transport *transport, const char *name,
652662
return 0;
653663
}
654664

665+
static struct ref *get_refs_list_using_list(struct transport *transport,
666+
int for_push);
667+
655668
static int fetch(struct transport *transport,
656669
int nr_heads, struct ref **to_fetch)
657670
{
658671
struct helper_data *data = transport->data;
659672
int i, count;
660673

674+
get_helper(transport);
675+
661676
if (process_connect(transport, 0)) {
662677
do_take_over(transport);
663678
return transport->vtable->fetch(transport, nr_heads, to_fetch);
664679
}
665680

681+
if (!data->get_refs_list_called)
682+
get_refs_list_using_list(transport, 0);
683+
666684
count = 0;
667685
for (i = 0; i < nr_heads; i++)
668686
if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
@@ -1058,6 +1076,19 @@ static int has_attribute(const char *attrs, const char *attr)
10581076

10591077
static struct ref *get_refs_list(struct transport *transport, int for_push,
10601078
const struct argv_array *ref_prefixes)
1079+
{
1080+
get_helper(transport);
1081+
1082+
if (process_connect(transport, for_push)) {
1083+
do_take_over(transport);
1084+
return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1085+
}
1086+
1087+
return get_refs_list_using_list(transport, for_push);
1088+
}
1089+
1090+
static struct ref *get_refs_list_using_list(struct transport *transport,
1091+
int for_push)
10611092
{
10621093
struct helper_data *data = transport->data;
10631094
struct child_process *helper;
@@ -1066,13 +1097,9 @@ static struct ref *get_refs_list(struct transport *transport, int for_push,
10661097
struct ref *posn;
10671098
struct strbuf buf = STRBUF_INIT;
10681099

1100+
data->get_refs_list_called = 1;
10691101
helper = get_helper(transport);
10701102

1071-
if (process_connect(transport, for_push)) {
1072-
do_take_over(transport);
1073-
return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1074-
}
1075-
10761103
if (data->push && for_push)
10771104
write_str_in_full(helper->in, "list for-push\n");
10781105
else
@@ -1119,7 +1146,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push,
11191146
}
11201147

11211148
static struct transport_vtable vtable = {
1122-
0,
1149+
1,
11231150
set_helper_option,
11241151
get_refs_list,
11251152
fetch,

0 commit comments

Comments
 (0)