receive-pack: implement advertising and receiving push options

The pre/post receive hook may be interested in more information from the user. This information can be transmitted when both client and server support the "push-options" capability, which when used is a phase directly after update commands ended by a flush pkt. Similar to the atomic option, the server capability can be disabled via the `receive.advertisePushOptions` config variable. While documenting this, fix a nit in the `receive.advertiseAtomic` wording. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jul 14, 2016 at 14:49 UTC c714e45f87301562b940e3621431ed7f7fbd16e5
4 files changed +52 -6
Documentation/config.txt
+7 -2
@@ -2410,8 +2410,13 @@ rebase.instructionFormat
2410
2411 receive.advertiseAtomic::
2412 By default, git-receive-pack will advertise the atomic push
2413 - capability to its clients. If you don't want to this capability
2414 - to be advertised, set this variable to false.
2413 + capability to its clients. If you don't want to advertise this
2414 + capability, set this variable to false.
2415 +
2416 +receive.advertisePushOptions::
2417 + By default, git-receive-pack will advertise the push options
2418 + capability to its clients. If you don't want to advertise this
2419 + capability, set this variable to false.
2420
2421 receive.autogc::
2422 By default, git-receive-pack will run "git-gc --auto" after
Documentation/technical/pack-protocol.txt
+6 -4
@@ -454,7 +454,8 @@ The reference discovery phase is done nearly the same way as it is in the
454 fetching protocol. Each reference obj-id and name on the server is sent
455 in packet-line format to the client, followed by a flush-pkt. The only
456 real difference is that the capability listing is different - the only
457 -possible values are 'report-status', 'delete-refs' and 'ofs-delta'.
457 +possible values are 'report-status', 'delete-refs', 'ofs-delta' and
458 +'push-options'.
459
460 Reference Update Request and Packfile Transfer
461 ----------------------------------------------
@@ -465,9 +466,10 @@ that it wants to update, it sends a line listing the obj-id currently on
466 the server, the obj-id the client would like to update it to and the name
467 of the reference.
468
468 -This list is followed by a flush-pkt and then the packfile that should
469 -contain all the objects that the server will need to complete the new
470 -references.
469 +This list is followed by a flush-pkt. Then the push options are transmitted
470 +one per packet followed by another flush-pkt. After that the packfile that
471 +should contain all the objects that the server will need to complete the new
472 +references will be sent.
473
474 ----
475 update-request = *shallow ( command-list | push-cert ) [packfile]
Documentation/technical/protocol-capabilities.txt
+9
@@ -253,6 +253,15 @@ atomic pushes. If the pushing client requests this capability, the server
253 will update the refs in one atomic transaction. Either all refs are
254 updated or none.
255
256 +push-options
257 +------------
258 +
259 +If the server sends the 'push-options' capability it is able to accept
260 +push options after the update commands have been sent, but before the
261 +packfile is streamed. If the pushing client requests this capability,
262 +the server will pass the options to the pre- and post- receive hooks
263 +that process this push request.
264 +
265 allow-tip-sha1-in-want
266 ----------------------
267
builtin/receive-pack.c
+30
@@ -44,10 +44,12 @@ static struct strbuf fsck_msg_types = STRBUF_INIT;
44 static int receive_unpack_limit = -1;
45 static int transfer_unpack_limit = -1;
46 static int advertise_atomic_push = 1;
47 +static int advertise_push_options;
48 static int unpack_limit = 100;
49 static int report_status;
50 static int use_sideband;
51 static int use_atomic;
52 +static int use_push_options;
53 static int quiet;
54 static int prefer_ofs_delta = 1;
55 static int auto_update_server_info;
@@ -193,6 +195,11 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
195 return 0;
196 }
197
198 + if (strcmp(var, "receive.advertisepushoptions") == 0) {
199 + advertise_push_options = git_config_bool(var, value);
200 + return 0;
201 + }
202 +
203 return git_default_config(var, value, cb);
204 }
205
@@ -211,6 +218,8 @@ static void show_ref(const char *path, const unsigned char *sha1)
218 strbuf_addstr(&cap, " ofs-delta");
219 if (push_cert_nonce)
220 strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
221 + if (advertise_push_options)
222 + strbuf_addstr(&cap, " push-options");
223 strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
224 packet_write(1, "%s %s%c%s\n",
225 sha1_to_hex(sha1), path, 0, cap.buf);
@@ -1455,6 +1464,9 @@ static struct command *read_head_info(struct sha1_array *shallow)
1464 if (advertise_atomic_push
1465 && parse_feature_request(feature_list, "atomic"))
1466 use_atomic = 1;
1467 + if (advertise_push_options
1468 + && parse_feature_request(feature_list, "push-options"))
1469 + use_push_options = 1;
1470 }
1471
1472 if (!strcmp(line, "push-cert")) {
@@ -1487,6 +1499,21 @@ static struct command *read_head_info(struct sha1_array *shallow)
1499 return commands;
1500 }
1501
1502 +static void read_push_options(struct string_list *options)
1503 +{
1504 + while (1) {
1505 + char *line;
1506 + int len;
1507 +
1508 + line = packet_read_line(0, &len);
1509 +
1510 + if (!line)
1511 + break;
1512 +
1513 + string_list_append(options, line);
1514 + }
1515 +}
1516 +
1517 static const char *parse_pack_header(struct pack_header *hdr)
1518 {
1519 switch (read_pack_header(0, hdr)) {
@@ -1774,6 +1801,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1801 const char *unpack_status = NULL;
1802 struct string_list push_options = STRING_LIST_INIT_DUP;
1803
1804 + if (use_push_options)
1805 + read_push_options(&push_options);
1806 +
1807 prepare_shallow_info(&si, &shallow);
1808 if (!si.nr_ours && !si.nr_theirs)
1809 shallow_update = 0;