transport-helper: skip ls-refs if unnecessary

Commit e70a3030e7 ("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 <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 21, 2019 at 15:20 UTC ac3fda82bfe1c9e99c5838d052c678c78139ee34
2 files changed +46 -6
t/t5702-protocol-v2.sh
+13
@@ -631,6 +631,19 @@ test_expect_success 'fetch with http:// using protocol v2' '
631 grep "git< version 2" log
632 '
633
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 +
647 test_expect_success 'fetch from namespaced repo respects namespaces' '
648 test_when_finished "rm -f log" &&
649
transport-helper.c
+33 -6
@@ -33,6 +33,16 @@ struct helper_data {
33 check_connectivity : 1,
34 no_disconnect_req : 1,
35 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 +
46 char *export_marks;
47 char *import_marks;
48 /* 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,
662 return 0;
663 }
664
665 +static struct ref *get_refs_list_using_list(struct transport *transport,
666 + int for_push);
667 +
668 static int fetch(struct transport *transport,
669 int nr_heads, struct ref **to_fetch)
670 {
671 struct helper_data *data = transport->data;
672 int i, count;
673
674 + get_helper(transport);
675 +
676 if (process_connect(transport, 0)) {
677 do_take_over(transport);
678 return transport->vtable->fetch(transport, nr_heads, to_fetch);
679 }
680
681 + if (!data->get_refs_list_called)
682 + get_refs_list_using_list(transport, 0);
683 +
684 count = 0;
685 for (i = 0; i < nr_heads; i++)
686 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
@@ -1058,6 +1076,19 @@ static int has_attribute(const char *attrs, const char *attr)
1076
1077 static struct ref *get_refs_list(struct transport *transport, int for_push,
1078 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)
1092 {
1093 struct helper_data *data = transport->data;
1094 struct child_process *helper;
@@ -1066,13 +1097,9 @@ static struct ref *get_refs_list(struct transport *transport, int for_push,
1097 struct ref *posn;
1098 struct strbuf buf = STRBUF_INIT;
1099
1100 + data->get_refs_list_called = 1;
1101 helper = get_helper(transport);
1102
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 -
1103 if (data->push && for_push)
1104 write_str_in_full(helper->in, "list for-push\n");
1105 else
@@ -1119,7 +1146,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push,
1146 }
1147
1148 static struct transport_vtable vtable = {
1122 - 0,
1149 + 1,
1150 set_helper_option,
1151 get_refs_list,
1152 fetch,