Skip to content

Commit ff47322

Browse files
bmwillgitster
authored andcommitted
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>
1 parent ecc3e53 commit ff47322

File tree

7 files changed

+46
-3
lines changed

7 files changed

+46
-3
lines changed

Documentation/git-ls-remote.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ OPTIONS
6060
upload-pack only shows the symref HEAD, so it will be the only
6161
one shown by ls-remote.
6262

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+
6371
<repository>::
6472
The "remote" repository to query. This parameter can be
6573
either a URL or the name of a remote (see the GIT URLS and

builtin/ls-remote.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
4545
const char *uploadpack = NULL;
4646
const char **pattern = NULL;
4747
struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
48+
struct string_list server_options = STRING_LIST_INIT_DUP;
4849

4950
struct remote *remote;
5051
struct transport *transport;
@@ -67,6 +68,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
6768
2, PARSE_OPT_NOCOMPLETE),
6869
OPT_BOOL(0, "symref", &show_symref_target,
6970
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")),
7072
OPT_END()
7173
};
7274

@@ -107,6 +109,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
107109
transport = transport_get(remote, NULL);
108110
if (uploadpack != NULL)
109111
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
112+
if (server_options.nr)
113+
transport->server_options = &server_options;
110114

111115
ref = transport_get_remote_refs(transport, &ref_prefixes);
112116
if (transport_disconnect(transport))

connect.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ static int process_ref_v2(const char *line, struct ref ***list)
408408

409409
struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
410410
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)
412413
{
413414
int i;
414415
*list = NULL;
@@ -419,6 +420,12 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
419420
if (server_supports_v2("agent", 0))
420421
packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
421422

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+
422429
packet_delim(fd_out);
423430
/* When pushing we don't want to request the peeled tags */
424431
if (!for_push)

remote.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ void free_refs(struct ref *ref);
153153
struct oid_array;
154154
struct packet_reader;
155155
struct argv_array;
156+
struct string_list;
156157
extern struct ref **get_remote_heads(struct packet_reader *reader,
157158
struct ref **list, unsigned int flags,
158159
struct oid_array *extra_have,
@@ -161,7 +162,8 @@ extern struct ref **get_remote_heads(struct packet_reader *reader,
161162
/* Used for protocol v2 in order to retrieve refs from a remote */
162163
extern struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
163164
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);
165167

166168
int resolve_remote_symref(struct ref *ref, struct ref *list);
167169
int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);

t/t5702-protocol-v2.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ test_expect_success 'ref advertisment is filtered with ls-remote using protocol
154154
test_cmp actual expect
155155
'
156156

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+
157173
test_expect_success 'clone with file:// using protocol v2' '
158174
test_when_finished "rm -f log" &&
159175

transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static struct ref *get_refs_via_connect(struct transport *transport, int for_pus
218218
switch (data->version) {
219219
case protocol_v2:
220220
get_remote_refs(data->fd[1], &reader, &refs, for_push,
221-
ref_prefixes);
221+
ref_prefixes, transport->server_options);
222222
break;
223223
case protocol_v1:
224224
case protocol_v0:

transport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ struct transport {
7171
*/
7272
const struct string_list *push_options;
7373

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+
7480
char *pack_lockfile;
7581
signed verbose : 3;
7682
/**

0 commit comments

Comments
 (0)