push options: {pre,post}-receive hook learns about push options

The environment variable GIT_PUSH_OPTION_COUNT is set to the number of push options sent, and GIT_PUSH_OPTION_{0,1,..} is set to the transmitted option. The code is not executed as the push options are set to NULL, nor is the new capability advertised. There was some discussion back and forth how to present these push options to the user as there are some ways to do it: Keep all options in one environment variable ============================================ + easiest way to implement in Git - This would make things hard to parse correctly in the hook. Put the options in files instead, filenames are in GIT_PUSH_OPTION_FILES ====================================== + After a discussion about environment variables and shells, we may not want to put user data into an environment variable (see [1] for example). + We could transmit binaries, i.e. we're not bound to C strings as we are when using environment variables to the user. + Maybe easier to parse than constructing environment variable names GIT_PUSH_OPTION_{0,1,..} yourself - cleanup of the temporary files is hard to do reliably - we have race conditions with multiple clients pushing, hence we'd need to use mkstemp. That's not too bad, but still. Use environment variables, but restrict to key/value pairs ========================================================== (When the user pushes a push option `foo=bar`, we'd GIT_PUSH_OPTION_foo=bar) + very easy to parse for a simple model of push options - it's not sufficient for more elaborate models, e.g. it doesn't allow doubles (e.g. cc=reviewer@email) Present the options in different environment variables ====================================================== (This is implemented) * harder to parse as a user, but we have a sample hook for that. - doesn't allow binary files + allows the same option twice, i.e. is not restrictive about options, except for binary files. + doesn't clutter a remote directory with (possibly stale) temporary files As we first want to focus on getting simple strings to work reliably, we go with the last option for now. If we want to do transmission of binaries later, we can just attach a 'side-channel', e.g. "any push option that contains a '\0' is put into a file instead of the environment variable and we'd have new GIT_PUSH_OPTION_FILES, GIT_PUSH_OPTION_FILENAME_{0,1,..} environment variables". [1] 'Shellshock' https://lwn.net/Articles/614218/ 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 77a9745d19b2b308c1ce97d18053322d1471bc4c
3 files changed +76 -13
Documentation/githooks.txt
+18
@@ -247,6 +247,15 @@ Both standard output and standard error output are forwarded to
247 'git send-pack' on the other end, so you can simply `echo` messages
248 for the user.
249
250 +The number of push options given on the command line of
251 +`git push --push-option=...` can be read from the environment
252 +variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
253 +found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
254 +If it is negotiated to not use the push options phase, the
255 +environment variables will not be set. If the client selects
256 +to use push options, but doesn't transmit any, the count variable
257 +will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
258 +
259 [[update]]
260 update
261 ~~~~~~
@@ -322,6 +331,15 @@ a sample script `post-receive-email` provided in the `contrib/hooks`
331 directory in Git distribution, which implements sending commit
332 emails.
333
334 +The number of push options given on the command line of
335 +`git push --push-option=...` can be read from the environment
336 +variable `GIT_PUSH_OPTION_COUNT`, and the options themselves are
337 +found in `GIT_PUSH_OPTION_0`, `GIT_PUSH_OPTION_1`,...
338 +If it is negotiated to not use the push options phase, the
339 +environment variables will not be set. If the client selects
340 +to use push options, but doesn't transmit any, the count variable
341 +will be set to zero, `GIT_PUSH_OPTION_COUNT=0`.
342 +
343 [[post-update]]
344 post-update
345 ~~~~~~~~~~~
builtin/receive-pack.c
+34 -13
@@ -550,8 +550,16 @@ static void prepare_push_cert_sha1(struct child_process *proc)
550 }
551 }
552
553 +struct receive_hook_feed_state {
554 + struct command *cmd;
555 + int skip_broken;
556 + struct strbuf buf;
557 + const struct string_list *push_options;
558 +};
559 +
560 typedef int (*feed_fn)(void *, const char **, size_t *);
554 -static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
561 +static int run_and_feed_hook(const char *hook_name, feed_fn feed,
562 + struct receive_hook_feed_state *feed_state)
563 {
564 struct child_process proc = CHILD_PROCESS_INIT;
565 struct async muxer;
@@ -567,6 +575,16 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_sta
575 proc.argv = argv;
576 proc.in = -1;
577 proc.stdout_to_stderr = 1;
578 + if (feed_state->push_options) {
579 + int i;
580 + for (i = 0; i < feed_state->push_options->nr; i++)
581 + argv_array_pushf(&proc.env_array,
582 + "GIT_PUSH_OPTION_%d=%s", i,
583 + feed_state->push_options->items[i].string);
584 + argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d",
585 + feed_state->push_options->nr);
586 + } else
587 + argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT");
588
589 if (use_sideband) {
590 memset(&muxer, 0, sizeof(muxer));
@@ -606,12 +624,6 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_sta
624 return finish_command(&proc);
625 }
626
609 -struct receive_hook_feed_state {
610 - struct command *cmd;
611 - int skip_broken;
612 - struct strbuf buf;
613 -};
614 -
627 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
628 {
629 struct receive_hook_feed_state *state = state_;
@@ -634,8 +646,10 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
646 return 0;
647 }
648
637 -static int run_receive_hook(struct command *commands, const char *hook_name,
638 - int skip_broken)
649 +static int run_receive_hook(struct command *commands,
650 + const char *hook_name,
651 + int skip_broken,
652 + const struct string_list *push_options)
653 {
654 struct receive_hook_feed_state state;
655 int status;
@@ -646,6 +660,7 @@ static int run_receive_hook(struct command *commands, const char *hook_name,
660 if (feed_receive_hook(&state, NULL, NULL))
661 return 0;
662 state.cmd = commands;
663 + state.push_options = push_options;
664 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
665 strbuf_release(&state.buf);
666 return status;
@@ -1316,7 +1331,8 @@ cleanup:
1331
1332 static void execute_commands(struct command *commands,
1333 const char *unpacker_error,
1319 - struct shallow_info *si)
1334 + struct shallow_info *si,
1335 + const struct string_list *push_options)
1336 {
1337 struct command *cmd;
1338 unsigned char sha1[20];
@@ -1335,7 +1351,7 @@ static void execute_commands(struct command *commands,
1351
1352 reject_updates_to_hidden(commands);
1353
1338 - if (run_receive_hook(commands, "pre-receive", 0)) {
1354 + if (run_receive_hook(commands, "pre-receive", 0, push_options)) {
1355 for (cmd = commands; cmd; cmd = cmd->next) {
1356 if (!cmd->error_string)
1357 cmd->error_string = "pre-receive hook declined";
@@ -1756,6 +1772,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1772
1773 if ((commands = read_head_info(&shallow)) != NULL) {
1774 const char *unpack_status = NULL;
1775 + struct string_list push_options = STRING_LIST_INIT_DUP;
1776
1777 prepare_shallow_info(&si, &shallow);
1778 if (!si.nr_ours && !si.nr_theirs)
@@ -1764,13 +1781,17 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1781 unpack_status = unpack_with_sideband(&si);
1782 update_shallow_info(commands, &si, &ref);
1783 }
1767 - execute_commands(commands, unpack_status, &si);
1784 + execute_commands(commands, unpack_status, &si,
1785 + &push_options);
1786 if (pack_lockfile)
1787 unlink_or_warn(pack_lockfile);
1788 if (report_status)
1789 report(commands, unpack_status);
1772 - run_receive_hook(commands, "post-receive", 1);
1790 + run_receive_hook(commands, "post-receive", 1,
1791 + &push_options);
1792 run_update_post_hook(commands);
1793 + if (push_options.nr)
1794 + string_list_clear(&push_options, 0);
1795 if (auto_gc) {
1796 const char *argv_gc_auto[] = {
1797 "gc", "--auto", "--quiet", NULL,
templates/hooks--pre-receive.sample new
+24
@@ -0,0 +1,24 @@
1 +#!/bin/sh
2 +#
3 +# An example hook script to make use of push options.
4 +# The example simply echoes all push options that start with 'echoback='
5 +# and rejects all pushes when the "reject" push option is used.
6 +#
7 +# To enable this hook, rename this file to "pre-receive".
8 +
9 +if test -n "$GIT_PUSH_OPTION_COUNT"
10 +then
11 + i=0
12 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"
13 + do
14 + eval "value=\$GIT_PUSH_OPTION_$i"
15 + case "$value" in
16 + echoback=*)
17 + echo "echo from the pre-receive-hook: ${value#*=}" >&2
18 + ;;
19 + reject)
20 + exit 1
21 + esac
22 + i=$((i + 1))
23 + done
24 +fi