Git fork

serve.[ch]: remove "serve_options", split up --advertise-refs code

The "advertise capabilities" mode of serve.c added in
ed10cb952d3 (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 42526b478e3 (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 <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Ævar Arnfjörð Bjarmason and committed by
Junio C Hamano
f234da80 760486a1

+42 -53
+12 -13
builtin/upload-pack.c
··· 16 16 { 17 17 const char *dir; 18 18 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; 21 22 struct option options[] = { 22 - OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc, 23 + OPT_BOOL(0, "stateless-rpc", &stateless_rpc, 23 24 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, 25 26 N_("exit immediately after initial ref advertisement")), 26 27 OPT_BOOL(0, "strict", &strict, 27 28 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, 29 30 N_("interrupt transfer after <n> seconds of inactivity")), 30 31 OPT_END() 31 32 }; ··· 38 39 if (argc != 1) 39 40 usage_with_options(upload_pack_usage, options); 40 41 41 - if (opts.timeout) 42 - opts.daemon_mode = 1; 43 - 44 42 setup_path(); 45 43 46 44 dir = argv[0]; ··· 50 48 51 49 switch (determine_protocol_version_server()) { 52 50 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); 56 55 break; 57 56 case protocol_v1: 58 57 /* 59 58 * v1 is just the original protocol with a version string, 60 59 * so just fall through after writing the version string. 61 60 */ 62 - if (opts.advertise_refs || !opts.stateless_rpc) 61 + if (advertise_refs || !stateless_rpc) 63 62 packet_write_fmt(1, "version 1\n"); 64 63 65 64 /* fallthrough */ 66 65 case protocol_v0: 67 - upload_pack(&opts); 66 + upload_pack(advertise_refs, stateless_rpc, timeout); 68 67 break; 69 68 case protocol_unknown_version: 70 69 BUG("unknown protocol version");
+1 -1
http-backend.c
··· 534 534 535 535 if (service_name) { 536 536 const char *argv[] = {NULL /* service name */, 537 - "--stateless-rpc", "--advertise-refs", 537 + "--advertise-refs", 538 538 ".", NULL}; 539 539 struct rpc_service *svc = select_service(hdr, service_name); 540 540
+5 -13
serve.c
··· 106 106 }, 107 107 }; 108 108 109 - static void advertise_capabilities(void) 109 + void protocol_v2_advertise_capabilities(void) 110 110 { 111 111 struct strbuf capability = STRBUF_INIT; 112 112 struct strbuf value = STRBUF_INIT; ··· 303 303 return 0; 304 304 } 305 305 306 - /* Main serve loop for protocol version 2 */ 307 - void serve(struct serve_options *options) 306 + void protocol_v2_serve_loop(int stateless_rpc) 308 307 { 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(); 318 310 319 311 /* 320 312 * If stateless-rpc was requested then exit after 321 313 * a single request/response exchange 322 314 */ 323 - if (options->stateless_rpc) { 315 + if (stateless_rpc) { 324 316 process_request(); 325 317 } else { 326 318 for (;;)
+2 -6
serve.h
··· 1 1 #ifndef SERVE_H 2 2 #define SERVE_H 3 3 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); 10 6 11 7 #endif /* SERVE_H */
+9 -5
t/helper/test-serve-v2.c
··· 10 10 11 11 int cmd__serve_v2(int argc, const char **argv) 12 12 { 13 - struct serve_options opts = SERVE_OPTIONS_INIT; 14 - 13 + int stateless_rpc = 0; 14 + int advertise_capabilities = 0; 15 15 struct option options[] = { 16 - OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc, 16 + OPT_BOOL(0, "stateless-rpc", &stateless_rpc, 17 17 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, 19 19 N_("exit immediately after advertising capabilities")), 20 20 OPT_END() 21 21 }; ··· 25 25 argc = parse_options(argc, argv, prefix, options, serve_usage, 26 26 PARSE_OPT_KEEP_DASHDASH | 27 27 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); 29 33 30 34 return 0; 31 35 }
+11 -7
upload-pack.c
··· 1214 1214 " allow-tip-sha1-in-want" : "", 1215 1215 (data->allow_uor & ALLOW_REACHABLE_SHA1) ? 1216 1216 " allow-reachable-sha1-in-want" : "", 1217 - data->stateless_rpc ? " no-done" : "", 1217 + data->no_done ? " no-done" : "", 1218 1218 symref_info.buf, 1219 1219 data->allow_filter ? " filter" : "", 1220 1220 session_id.buf, ··· 1329 1329 return parse_hide_refs_config(var, value, "uploadpack"); 1330 1330 } 1331 1331 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) 1333 1334 { 1334 1335 struct packet_reader reader; 1335 1336 struct upload_pack_data data; ··· 1338 1339 1339 1340 git_config(upload_pack_config, &data); 1340 1341 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; 1344 1346 1345 1347 head_ref_namespaced(find_symref, &data.symref); 1346 1348 1347 - if (options->advertise_refs || !data.stateless_rpc) { 1349 + if (advertise_refs || !data.stateless_rpc) { 1348 1350 reset_timeout(data.timeout); 1351 + if (advertise_refs) 1352 + data.no_done = 1; 1349 1353 head_ref_namespaced(send_ref, &data); 1350 1354 for_each_namespaced_ref(send_ref, &data); 1351 1355 advertise_shallow_grafts(1); ··· 1355 1359 for_each_namespaced_ref(check_ref, NULL); 1356 1360 } 1357 1361 1358 - if (!options->advertise_refs) { 1362 + if (!advertise_refs) { 1359 1363 packet_reader_init(&reader, 0, NULL, 0, 1360 1364 PACKET_READ_CHOMP_NEWLINE | 1361 1365 PACKET_READ_DIE_ON_ERR_PACKET);
+2 -8
upload-pack.h
··· 1 1 #ifndef UPLOAD_PACK_H 2 2 #define UPLOAD_PACK_H 3 3 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); 12 6 13 7 struct repository; 14 8 struct packet_reader;