Subject: [PATCH v8 0/7] New proc-receive hook for centralized workflow

From: Jiang Xin <[email-masked]>
Date: Sat, 4 Apr 2020 09:43:38 -0400

From: Jiang Xin <[email-masked]>

## Changes since v7

* Parallel test (make -j7 test) failed because of the failure message on
  calling `finish_command()` is not stable for test.  Remove the error
  message in this revision.

    -	if (finish_command(&proc)) {
    -		strbuf_addstr(&errmsg, "proc-receive did not exit properly\n");
    +	if (finish_command(&proc))
     		code = -1;
    -	}

## Changes since v6

* Test failed because two message streams of `copy_to_sideband()` and
  `rp_error()` are mixed together.  Store error messages in `errmsg`
  and output the errmsg after closing the sideband muxer.

* Use test_i18ncmp instead of mark the whole test case using
  C_LOCALE_OUTPUT.

* Please merge this topic after topic `jx/atomic-push`.  One test case
  depends on it.

## Range-diff v6...v7

1:  64c9ca7086 = 1:  19c66785d1 transport: not report a non-head push as a branch
2:  0d27cc5e80 ! 2:  085ded61f5 receive-pack: add new proc-receive hook
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +		    *p++ != ' ' ||
     +		    parse_oid_hex(p, &new_oid, &p) ||
     +		    *p++ != ' ') {
    -+			strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
    ++			strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
     +				    reader->line);
     +			return -1;
     +		}
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +		refname = p;
     +		status = strchr(p, ' ');
     +		if (!status) {
    -+			strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
    ++			strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
     +				    reader->line);
     +			return -1;
     +		}
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +			*msg++ = '\0';
     +		}
     +		if (strlen(status) != 2) {
    -+			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
    ++			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
     +				    status, reader->line);
     +			return -1;
     +		}
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +				hint->error_string = xstrdup(msg);
     +			else
     +				hint->error_string = "failed";
    -+			code = 1;
    ++			code = -1;
     +		} else if (strcmp("ok", status)) {
    -+			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
    ++			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
     +				    status, reader->line);
     +			return -1;
     +		}
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +
     +	for (cmd = commands; cmd; cmd = cmd->next)
     +		if (cmd->run_proc_receive &&
    -+		    !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED))
    ++		    !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
     +		    cmd->error_string = "no report from proc-receive";
    ++		    code = -1;
    ++		}
     +
     +	return code;
     +}
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +	}
     +
     +	if (version != 1) {
    -+		strbuf_addf(&errmsg, "proc-receive version '%d' is not supported\n",
    ++		strbuf_addf(&errmsg, "proc-receive version '%d' is not supported",
     +			    version);
     +		code = -1;
     +		goto cleanup;
    @@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
     +	close(proc.out);
     +	if (use_sideband)
     +		finish_async(&muxer);
    -+	if (finish_command(&proc)) {
    -+		strbuf_addstr(&errmsg, "proc-receive did not exit properly\n");
    ++	if (finish_command(&proc))
     +		code = -1;
    -+	}
     +	if (errmsg.len >0) {
     +		char *p = errmsg.buf;
     +
    @@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
     +	remote: # proc-receive hook
     +	remote: fatal: the remote end hung up unexpectedly
     +	remote: error: proc-receive version "2" is not supported
    -+	remote: proc-receive did not exit properly
     +	To <URL/of/upstream.git>
     +	 ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
     +	EOF
3:  81f086188c = 3:  230f28198f refs.c: refactor to reuse ref_is_hidden()
4:  52675b55a2 ! 4:  e6a7608a84 receive-pack: new config receive.procReceiveRefs
    @@ t/t5411-proc-receive-hook.sh: test_expect_success "cleanup" '
     +test_expect_success "add two receive.procReceiveRefs settings" '
     +	(
     +		cd "$upstream" &&
    -+		git config --add receive.procReceiveRefs refs/for/ &&
    ++		git config --add receive.procReceiveRefs refs/for &&
     +		git config --add receive.procReceiveRefs refs/review/
     +	)
     +'
5:  65876b1e75 ! 5:  e426775925 receive-pack: refactor report for proc-receive
    @@ builtin/receive-pack.c: static void write_head_info(void)
     @@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader *reader,
      			else
      				hint->error_string = "failed";
    - 			code = 1;
    + 			code = -1;
     -		} else if (strcmp("ok", status)) {
     +		} else if (!strcmp("ok", status)) {
     +			hint->extra_string = xstrdup_or_null(msg);
    @@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader
     +			/* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
     +			hint->run_proc_receive = 0;
     +		} else {
    - 			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
    + 			strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
      				    status, reader->line);
      			return -1;
     @@ builtin/receive-pack.c: static void report(struct command *commands, const char *unpack_status)
6:  04ec372c35 ! 6:  c5982067be t5412: test proc-receive hook on HTTP protocol
    @@ Metadata
     Author: Jiang Xin <[email-masked]>
     
      ## Commit message ##
    -    t5412: test proc-receive hook on HTTP protocol
    +    t5412: test the proc-receive hook on HTTP protocol
     
    -    Copy from t5411 to test "proc-receive" hook on HTTP protocol.
    +    Copy from t5411 to test the "proc-receive" hook on the HTTP protocol.
     
         Signed-off-by: Jiang Xin <[email-masked]>
     
    @@ t/t5412-proc-receive-hook-http-protocol.sh (new)
     +		-e "s/$ZERO_OID/<ZERO-OID>/g" \
     +		-e "s/[0-9a-f]\{7,\}/<OID>/g" \
     +		-e "s#To .*/upstream.git#To <URL/of/upstream.git>#" \
    -+		-e "/^error: / d" \
    -+		-e "/^remote: fatal: /d"
    ++		-e "/^error: / d"
     +}
     +
     +# Refs of upstream : master(B)  next(A)
    @@ t/t5412-proc-receive-hook-http-protocol.sh (new)
     +test_expect_success "add two receive.procReceiveRefs settings" '
     +	(
     +		cd "$upstream" &&
    -+		git config --add receive.procReceiveRefs refs/for/ &&
    ++		git config --add receive.procReceiveRefs refs/for &&
     +		git config --add receive.procReceiveRefs refs/review/
     +	)
     +'
    @@ t/t5412-proc-receive-hook-http-protocol.sh (new)
     +	test_must_fail git -C workbench push origin \
     +		HEAD:refs/for/master/topic \
     +		>out 2>&1 &&
    -+	make_user_friendly_and_stable_output <out >actual &&
    ++	# Apache calls "receive-pack" with system locale settings, which
    ++	# makes the message "remote: fatal: ..." unstable for test.
    ++	sed -e "/^remote: fatal: / d" out | \
    ++	make_user_friendly_and_stable_output >actual &&
     +	cat >expect <<-EOF &&
     +	remote: # pre-receive hook
     +	remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
     +	remote: # proc-receive hook
     +	remote: error: proc-receive version "2" is not supported
    -+	remote: proc-receive did not exit properly
     +	To <URL/of/upstream.git>
     +	 ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
     +	EOF
7:  4ab8873f12 = 7:  823b7f2ea6 doc: add documentation for the proc-receive hook

---

Jiang Xin (7):
  transport: not report a non-head push as a branch
  receive-pack: add new proc-receive hook
  refs.c: refactor to reuse ref_is_hidden()
  receive-pack: new config receive.procReceiveRefs
  receive-pack: refactor report for proc-receive
  t5412: test the proc-receive hook on HTTP protocol
  doc: add documentation for the proc-receive hook

 Documentation/config/receive.txt           |  14 +
 Documentation/githooks.txt                 |  70 ++
 Makefile                                   |   1 +
 builtin/receive-pack.c                     | 312 ++++++-
 refs.c                                     |  11 +-
 refs.h                                     |   1 +
 t/helper/test-proc-receive.c               | 172 ++++
 t/helper/test-tool.c                       |   1 +
 t/helper/test-tool.h                       |   1 +
 t/t5411-proc-receive-hook.sh               | 931 ++++++++++++++++++++
 t/t5412-proc-receive-hook-http-protocol.sh | 945 +++++++++++++++++++++
 t/t5516-fetch-push.sh                      |   2 +-
 transport-helper.c                         |  64 +-
 transport.c                                |  62 +-
 14 files changed, 2521 insertions(+), 66 deletions(-)
 create mode 100644 t/helper/test-proc-receive.c
 create mode 100755 t/t5411-proc-receive-hook.sh
 create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh

-- 
2.24.1.15.g448c31058d.agit.4.5