ls-remote: send server options when using protocol v2

Teach ls-remote to optionally accept server options by specifying them on the cmdline via '-o' or '--server-option'. These server options are sent to the remote end when querying for the remote end's refs using protocol version 2. If communicating using a protocol other than v2 the provided options are ignored and not sent to the remote end. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Apr 23, 2018 at 15:46 UTC ff473221b4d6cf0894ba47370492d853a36c024d
7 files changed +46 -3
Documentation/git-ls-remote.txt
+8
@@ -60,6 +60,14 @@ OPTIONS
60 upload-pack only shows the symref HEAD, so it will be the only
61 one shown by ls-remote.
62
63 +-o <option>::
64 +--server-option=<option>::
65 + Transmit the given string to the server when communicating using
66 + protocol version 2. The given string must not contain a NUL or LF
67 + character.
68 + When multiple `--server-option=<option>` are given, they are all
69 + sent to the other side in the order listed on the command line.
70 +
71 <repository>::
72 The "remote" repository to query. This parameter can be
73 either a URL or the name of a remote (see the GIT URLS and
builtin/ls-remote.c
+4
@@ -45,6 +45,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
45 const char *uploadpack = NULL;
46 const char **pattern = NULL;
47 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
48 + struct string_list server_options = STRING_LIST_INIT_DUP;
49
50 struct remote *remote;
51 struct transport *transport;
@@ -67,6 +68,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
68 2, PARSE_OPT_NOCOMPLETE),
69 OPT_BOOL(0, "symref", &show_symref_target,
70 N_("show underlying ref in addition to the object pointed by it")),
71 + OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
72 OPT_END()
73 };
74
@@ -107,6 +109,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
109 transport = transport_get(remote, NULL);
110 if (uploadpack != NULL)
111 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
112 + if (server_options.nr)
113 + transport->server_options = &server_options;
114
115 ref = transport_get_remote_refs(transport, &ref_prefixes);
116 if (transport_disconnect(transport))
connect.c
+8 -1
@@ -408,7 +408,8 @@ out:
408
409 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
410 struct ref **list, int for_push,
411 - const struct argv_array *ref_prefixes)
411 + const struct argv_array *ref_prefixes,
412 + const struct string_list *server_options)
413 {
414 int i;
415 *list = NULL;
@@ -419,6 +420,12 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
420 if (server_supports_v2("agent", 0))
421 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
422
423 + if (server_options && server_options->nr &&
424 + server_supports_v2("server-option", 1))
425 + for (i = 0; i < server_options->nr; i++)
426 + packet_write_fmt(fd_out, "server-option=%s",
427 + server_options->items[i].string);
428 +
429 packet_delim(fd_out);
430 /* When pushing we don't want to request the peeled tags */
431 if (!for_push)
remote.h
+3 -1
@@ -153,6 +153,7 @@ void free_refs(struct ref *ref);
153 struct oid_array;
154 struct packet_reader;
155 struct argv_array;
156 +struct string_list;
157 extern struct ref **get_remote_heads(struct packet_reader *reader,
158 struct ref **list, unsigned int flags,
159 struct oid_array *extra_have,
@@ -161,7 +162,8 @@ extern struct ref **get_remote_heads(struct packet_reader *reader,
162 /* Used for protocol v2 in order to retrieve refs from a remote */
163 extern struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
164 struct ref **list, int for_push,
164 - const struct argv_array *ref_prefixes);
165 + const struct argv_array *ref_prefixes,
166 + const struct string_list *server_options);
167
168 int resolve_remote_symref(struct ref *ref, struct ref *list);
169 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);
t/t5702-protocol-v2.sh
+16
@@ -154,6 +154,22 @@ test_expect_success 'ref advertisment is filtered with ls-remote using protocol
154 test_cmp actual expect
155 '
156
157 +test_expect_success 'server-options are sent when using ls-remote' '
158 + test_when_finished "rm -f log" &&
159 +
160 + GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
161 + ls-remote -o hello -o world "file://$(pwd)/file_parent" master >actual &&
162 +
163 + cat >expect <<-EOF &&
164 + $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
165 + EOF
166 +
167 + test_cmp actual expect &&
168 + grep "server-option=hello" log &&
169 + grep "server-option=world" log
170 +'
171 +
172 +
173 test_expect_success 'clone with file:// using protocol v2' '
174 test_when_finished "rm -f log" &&
175
transport.c
+1 -1
@@ -218,7 +218,7 @@ static struct ref *get_refs_via_connect(struct transport *transport, int for_pus
218 switch (data->version) {
219 case protocol_v2:
220 get_remote_refs(data->fd[1], &reader, &refs, for_push,
221 - ref_prefixes);
221 + ref_prefixes, transport->server_options);
222 break;
223 case protocol_v1:
224 case protocol_v0:
transport.h
+6
@@ -71,6 +71,12 @@ struct transport {
71 */
72 const struct string_list *push_options;
73
74 + /*
75 + * These strings will be passed to the remote side on each command
76 + * request, if both sides support the server-option capability.
77 + */
78 + const struct string_list *server_options;
79 +
80 char *pack_lockfile;
81 signed verbose : 3;
82 /**