Skip to content

Commit f234da8

Browse files
avargitster
authored andcommitted
serve.[ch]: remove "serve_options", split up --advertise-refs code
The "advertise capabilities" mode of serve.c added in ed10cb9 (serve: introduce git-serve, 2018-03-15) is only used by the http-backend.c to call {upload,receive}-pack with the --advertise-refs parameter. See 42526b4 (Add stateless RPC options to upload-pack, receive-pack, 2009-10-30). Let's just make cmd_upload_pack() take the two (v2) or three (v2) parameters the the v2/v1 servicing functions need directly, and pass those in via the function signature. The logic of whether daemon mode is implied by the timeout belongs in the v1 function (only used there). Once we split up the "advertise v2 refs" from "serve v2 request" it becomes clear that v2 never cared about those in combination. The only time it mattered was for v1 to emit its ref advertisement, in that case we wanted to emit the smart-http-only "no-done" capability. Since we only do that in the --advertise-refs codepath let's just have it set "do_done" itself in v1's upload_pack() just before send_ref(), at that point --advertise-refs and --stateless-rpc in combination are redundant (the only user is get_info_refs() in http-backend.c), so we can just pass in --advertise-refs only. Since we need to touch all the serve() and advertise_capabilities() codepaths let's rename them to less clever and obvious names, it's been suggested numerous times, the latest of which is [1]'s suggestion for protocol_v2_serve_loop(). Let's go with that. 1. https://lore.kernel.org/git/CAFQ2z_NyGb8rju5CKzmo6KhZXD0Dp21u-BbyCb2aNxLEoSPRJw@mail.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 760486a commit f234da8

File tree

7 files changed

+42
-53
lines changed

7 files changed

+42
-53
lines changed

builtin/upload-pack.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix)
1616
{
1717
const char *dir;
1818
int strict = 0;
19-
struct upload_pack_options opts = { 0 };
20-
struct serve_options serve_opts = SERVE_OPTIONS_INIT;
19+
int advertise_refs = 0;
20+
int stateless_rpc = 0;
21+
int timeout = 0;
2122
struct option options[] = {
22-
OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
23+
OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
2324
N_("quit after a single request/response exchange")),
24-
OPT_BOOL(0, "advertise-refs", &opts.advertise_refs,
25+
OPT_BOOL(0, "advertise-refs", &advertise_refs,
2526
N_("exit immediately after initial ref advertisement")),
2627
OPT_BOOL(0, "strict", &strict,
2728
N_("do not try <directory>/.git/ if <directory> is no Git directory")),
28-
OPT_INTEGER(0, "timeout", &opts.timeout,
29+
OPT_INTEGER(0, "timeout", &timeout,
2930
N_("interrupt transfer after <n> seconds of inactivity")),
3031
OPT_END()
3132
};
@@ -38,9 +39,6 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix)
3839
if (argc != 1)
3940
usage_with_options(upload_pack_usage, options);
4041

41-
if (opts.timeout)
42-
opts.daemon_mode = 1;
43-
4442
setup_path();
4543

4644
dir = argv[0];
@@ -50,21 +48,22 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix)
5048

5149
switch (determine_protocol_version_server()) {
5250
case protocol_v2:
53-
serve_opts.advertise_capabilities = opts.advertise_refs;
54-
serve_opts.stateless_rpc = opts.stateless_rpc;
55-
serve(&serve_opts);
51+
if (advertise_refs)
52+
protocol_v2_advertise_capabilities();
53+
else
54+
protocol_v2_serve_loop(stateless_rpc);
5655
break;
5756
case protocol_v1:
5857
/*
5958
* v1 is just the original protocol with a version string,
6059
* so just fall through after writing the version string.
6160
*/
62-
if (opts.advertise_refs || !opts.stateless_rpc)
61+
if (advertise_refs || !stateless_rpc)
6362
packet_write_fmt(1, "version 1\n");
6463

6564
/* fallthrough */
6665
case protocol_v0:
67-
upload_pack(&opts);
66+
upload_pack(advertise_refs, stateless_rpc, timeout);
6867
break;
6968
case protocol_unknown_version:
7069
BUG("unknown protocol version");

http-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ static void get_info_refs(struct strbuf *hdr, char *arg)
534534

535535
if (service_name) {
536536
const char *argv[] = {NULL /* service name */,
537-
"--stateless-rpc", "--advertise-refs",
537+
"--advertise-refs",
538538
".", NULL};
539539
struct rpc_service *svc = select_service(hdr, service_name);
540540

serve.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static struct protocol_capability capabilities[] = {
106106
},
107107
};
108108

109-
static void advertise_capabilities(void)
109+
void protocol_v2_advertise_capabilities(void)
110110
{
111111
struct strbuf capability = STRBUF_INIT;
112112
struct strbuf value = STRBUF_INIT;
@@ -303,24 +303,16 @@ static int process_request(void)
303303
return 0;
304304
}
305305

306-
/* Main serve loop for protocol version 2 */
307-
void serve(struct serve_options *options)
306+
void protocol_v2_serve_loop(int stateless_rpc)
308307
{
309-
if (options->advertise_capabilities || !options->stateless_rpc) {
310-
advertise_capabilities();
311-
/*
312-
* If only the list of capabilities was requested exit
313-
* immediately after advertising capabilities
314-
*/
315-
if (options->advertise_capabilities)
316-
return;
317-
}
308+
if (!stateless_rpc)
309+
protocol_v2_advertise_capabilities();
318310

319311
/*
320312
* If stateless-rpc was requested then exit after
321313
* a single request/response exchange
322314
*/
323-
if (options->stateless_rpc) {
315+
if (stateless_rpc) {
324316
process_request();
325317
} else {
326318
for (;;)

serve.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#ifndef SERVE_H
22
#define SERVE_H
33

4-
struct serve_options {
5-
unsigned advertise_capabilities;
6-
unsigned stateless_rpc;
7-
};
8-
#define SERVE_OPTIONS_INIT { 0 }
9-
void serve(struct serve_options *options);
4+
void protocol_v2_advertise_capabilities(void);
5+
void protocol_v2_serve_loop(int stateless_rpc);
106

117
#endif /* SERVE_H */

t/helper/test-serve-v2.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ static char const * const serve_usage[] = {
1010

1111
int cmd__serve_v2(int argc, const char **argv)
1212
{
13-
struct serve_options opts = SERVE_OPTIONS_INIT;
14-
13+
int stateless_rpc = 0;
14+
int advertise_capabilities = 0;
1515
struct option options[] = {
16-
OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
16+
OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1717
N_("quit after a single request/response exchange")),
18-
OPT_BOOL(0, "advertise-capabilities", &opts.advertise_capabilities,
18+
OPT_BOOL(0, "advertise-capabilities", &advertise_capabilities,
1919
N_("exit immediately after advertising capabilities")),
2020
OPT_END()
2121
};
@@ -25,7 +25,11 @@ int cmd__serve_v2(int argc, const char **argv)
2525
argc = parse_options(argc, argv, prefix, options, serve_usage,
2626
PARSE_OPT_KEEP_DASHDASH |
2727
PARSE_OPT_KEEP_UNKNOWN);
28-
serve(&opts);
28+
29+
if (advertise_capabilities)
30+
protocol_v2_advertise_capabilities();
31+
else
32+
protocol_v2_serve_loop(stateless_rpc);
2933

3034
return 0;
3135
}

upload-pack.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
12141214
" allow-tip-sha1-in-want" : "",
12151215
(data->allow_uor & ALLOW_REACHABLE_SHA1) ?
12161216
" allow-reachable-sha1-in-want" : "",
1217-
data->stateless_rpc ? " no-done" : "",
1217+
data->no_done ? " no-done" : "",
12181218
symref_info.buf,
12191219
data->allow_filter ? " filter" : "",
12201220
session_id.buf,
@@ -1329,7 +1329,8 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
13291329
return parse_hide_refs_config(var, value, "uploadpack");
13301330
}
13311331

1332-
void upload_pack(struct upload_pack_options *options)
1332+
void upload_pack(const int advertise_refs, const int stateless_rpc,
1333+
const int timeout)
13331334
{
13341335
struct packet_reader reader;
13351336
struct upload_pack_data data;
@@ -1338,14 +1339,17 @@ void upload_pack(struct upload_pack_options *options)
13381339

13391340
git_config(upload_pack_config, &data);
13401341

1341-
data.stateless_rpc = options->stateless_rpc;
1342-
data.daemon_mode = options->daemon_mode;
1343-
data.timeout = options->timeout;
1342+
data.stateless_rpc = stateless_rpc;
1343+
data.timeout = timeout;
1344+
if (data.timeout)
1345+
data.daemon_mode = 1;
13441346

13451347
head_ref_namespaced(find_symref, &data.symref);
13461348

1347-
if (options->advertise_refs || !data.stateless_rpc) {
1349+
if (advertise_refs || !data.stateless_rpc) {
13481350
reset_timeout(data.timeout);
1351+
if (advertise_refs)
1352+
data.no_done = 1;
13491353
head_ref_namespaced(send_ref, &data);
13501354
for_each_namespaced_ref(send_ref, &data);
13511355
advertise_shallow_grafts(1);
@@ -1355,7 +1359,7 @@ void upload_pack(struct upload_pack_options *options)
13551359
for_each_namespaced_ref(check_ref, NULL);
13561360
}
13571361

1358-
if (!options->advertise_refs) {
1362+
if (!advertise_refs) {
13591363
packet_reader_init(&reader, 0, NULL, 0,
13601364
PACKET_READ_CHOMP_NEWLINE |
13611365
PACKET_READ_DIE_ON_ERR_PACKET);

upload-pack.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#ifndef UPLOAD_PACK_H
22
#define UPLOAD_PACK_H
33

4-
struct upload_pack_options {
5-
int stateless_rpc;
6-
int advertise_refs;
7-
unsigned int timeout;
8-
int daemon_mode;
9-
};
10-
11-
void upload_pack(struct upload_pack_options *options);
4+
void upload_pack(const int advertise_refs, const int stateless_rpc,
5+
const int timeout);
126

137
struct repository;
148
struct packet_reader;

0 commit comments

Comments
 (0)