Skip to content

Commit 6d3df8e

Browse files
committed
Merge branch 'jt/clone-server-option'
"git clone" learned a new --server-option option when talking over the protocol version 2. * jt/clone-server-option: clone: send server options when using protocol v2 transport: die if server options are unsupported
2 parents ea2dab1 + 6e98305 commit 6d3df8e

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

Documentation/fetch-options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ endif::git-pull[]
216216
--server-option=<option>::
217217
Transmit the given string to the server when communicating using
218218
protocol version 2. The given string must not contain a NUL or LF
219-
character.
219+
character. The server's handling of server options, including
220+
unknown ones, is server-specific.
220221
When multiple `--server-option=<option>` are given, they are all
221222
sent to the other side in the order listed on the command line.
222223

Documentation/git-clone.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ objects from the source repository into a pack in the cloned repository.
131131
is specified. This flag forces progress status even if the
132132
standard error stream is not directed to a terminal.
133133

134+
--server-option=<option>::
135+
Transmit the given string to the server when communicating using
136+
protocol version 2. The given string must not contain a NUL or LF
137+
character. The server's handling of server options, including
138+
unknown ones, is server-specific.
139+
When multiple `--server-option=<option>` are given, they are all
140+
sent to the other side in the order listed on the command line.
141+
134142
--no-checkout::
135143
-n::
136144
No checkout of HEAD is performed after the clone is complete.

builtin/clone.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static int option_dissociate;
6666
static int max_jobs = -1;
6767
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
6868
static struct list_objects_filter_options filter_options;
69+
static struct string_list server_options = STRING_LIST_INIT_NODUP;
6970

7071
static int recurse_submodules_cb(const struct option *opt,
7172
const char *arg, int unset)
@@ -137,6 +138,8 @@ static struct option builtin_clone_options[] = {
137138
N_("separate git dir from working tree")),
138139
OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
139140
N_("set config inside the new repository")),
141+
OPT_STRING_LIST(0, "server-option", &server_options,
142+
N_("server-specific"), N_("option to transmit")),
140143
OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
141144
TRANSPORT_FAMILY_IPV4),
142145
OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
@@ -1136,6 +1139,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11361139
transport_set_option(transport, TRANS_OPT_UPLOADPACK,
11371140
option_upload_pack);
11381141

1142+
if (server_options.nr)
1143+
transport->server_options = &server_options;
1144+
11391145
if (filter_options.choice) {
11401146
struct strbuf expanded_filter_spec = STRBUF_INIT;
11411147
expand_list_objects_filter_spec(&filter_options,

t/t5702-protocol-v2.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ test_expect_success 'server-options are sent when using ls-remote' '
182182
grep "server-option=world" log
183183
'
184184

185+
test_expect_success 'warn if using server-option with ls-remote with legacy protocol' '
186+
test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
187+
ls-remote -o hello -o world "file://$(pwd)/file_parent" master 2>err &&
188+
189+
test_i18ngrep "see protocol.version in" err &&
190+
test_i18ngrep "server options require protocol version 2 or later" err
191+
'
185192

186193
test_expect_success 'clone with file:// using protocol v2' '
187194
test_when_finished "rm -f log" &&
@@ -251,6 +258,40 @@ test_expect_success 'server-options are sent when fetching' '
251258
grep "server-option=world" log
252259
'
253260

261+
test_expect_success 'warn if using server-option with fetch with legacy protocol' '
262+
test_when_finished "rm -rf temp_child" &&
263+
264+
git init temp_child &&
265+
266+
test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C temp_child -c protocol.version=0 \
267+
fetch -o hello -o world "file://$(pwd)/file_parent" master 2>err &&
268+
269+
test_i18ngrep "see protocol.version in" err &&
270+
test_i18ngrep "server options require protocol version 2 or later" err
271+
'
272+
273+
test_expect_success 'server-options are sent when cloning' '
274+
test_when_finished "rm -rf log myclone" &&
275+
276+
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
277+
clone --server-option=hello --server-option=world \
278+
"file://$(pwd)/file_parent" myclone &&
279+
280+
grep "server-option=hello" log &&
281+
grep "server-option=world" log
282+
'
283+
284+
test_expect_success 'warn if using server-option with clone with legacy protocol' '
285+
test_when_finished "rm -rf myclone" &&
286+
287+
test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
288+
clone --server-option=hello --server-option=world \
289+
"file://$(pwd)/file_parent" myclone 2>err &&
290+
291+
test_i18ngrep "see protocol.version in" err &&
292+
test_i18ngrep "server options require protocol version 2 or later" err
293+
'
294+
254295
test_expect_success 'upload-pack respects config using protocol v2' '
255296
git init server &&
256297
write_script server/.git/hook <<-\EOF &&

transport.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ static int connect_setup(struct transport *transport, int for_push)
252252
return 0;
253253
}
254254

255+
static void die_if_server_options(struct transport *transport)
256+
{
257+
if (!transport->server_options || !transport->server_options->nr)
258+
return;
259+
advise(_("see protocol.version in 'git help config' for more details"));
260+
die(_("server options require protocol version 2 or later"));
261+
}
262+
255263
/*
256264
* Obtains the protocol version from the transport and writes it to
257265
* transport->data->version, first connecting if not already connected.
@@ -286,6 +294,7 @@ static struct ref *handshake(struct transport *transport, int for_push,
286294
break;
287295
case protocol_v1:
288296
case protocol_v0:
297+
die_if_server_options(transport);
289298
get_remote_heads(&reader, &refs,
290299
for_push ? REF_NORMAL : 0,
291300
&data->extra_have,
@@ -362,6 +371,7 @@ static int fetch_refs_via_pack(struct transport *transport,
362371
break;
363372
case protocol_v1:
364373
case protocol_v0:
374+
die_if_server_options(transport);
365375
refs = fetch_pack(&args, data->fd,
366376
refs_tmp ? refs_tmp : transport->remote_refs,
367377
to_fetch, nr_heads, &data->shallow,

0 commit comments

Comments
 (0)