From patchwork Sun Mar 8 15:38:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 11425709 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0F7FD139A for ; Sun, 8 Mar 2020 15:38:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C62702084E for ; Sun, 8 Mar 2020 15:38:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="scG8Atcl" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726292AbgCHPiV (ORCPT ); Sun, 8 Mar 2020 11:38:21 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:46197 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726260AbgCHPiV (ORCPT ); Sun, 8 Mar 2020 11:38:21 -0400 Received: by mail-pl1-f193.google.com with SMTP id w12so2955595pll.13 for ; Sun, 08 Mar 2020 08:38:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AuSuczbp7OAbcM+VS15c2HS1KScBgkjTZzdeNHBKt2I=; b=scG8AtclmBZIrH7hZF8q8GbehKbU9jYVYPK2Qwv7xqYLvHZ5/1kBisudjQkvpqMFpM MdKUX8IitMW0tFmZn8PwBNBSzgCqhmDc0EZfdB/iEmMdKh/ERZlbjKwj9Se8mqGpRzhZ vFeHJbVtboUlDF8SHchcZo4glHAztmm02uJPTnh7qs1I89vVpAQbPh4i3nwwDWhl/i+Q QotDdoBDD7Xk/qCk0ZEiI7t4OSENp+gWUimNV3M0iOk63qm9g3Gli3JIkZu+JanfozPG w1sqHg4ZrfdL9siKTdAkMKx/a7u1VqvovmQtc8nOyn5WOVSqZEpJmofrz6beERsC+m8u 2hww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AuSuczbp7OAbcM+VS15c2HS1KScBgkjTZzdeNHBKt2I=; b=oSS8yatgBoh8M+KZCoKxUXgixsnSbK5OmdZmLFJW3BsbgM9a4D7MURy0YyG+iCI7pK rM60oP/G272IA0bGCRbRk1FDIjbrZHATUl86//smBu0ckh1PwMPG/qEcqY/z6qDGXKry 5/SkgT/CayOdSEemNe5dTdgzZpK9HTLakuKHIaiC9+uCFzyqYhXo0oTZww4ZR1y7i0TX ww+o/mLfZOh6HaSurYYi0a99MeR8x0RNXWAypzWiQVcgXXS5JcR/KN1nJSrA3U2x87Iu FmUEVGK9ANIqVg06Nxt9IhwcmScgAMb91EqnXVs6SWJnGLC/djmJ2xNGAHmPpbwSA7dw 32Zg== X-Gm-Message-State: ANhLgQ2EKc1i7n92VXEUgGDHT4wY+xSOK3ghlxgJboPj0wv+bm02QcG/ aXkFU1PWAI92lCoJJecvbWQ= X-Google-Smtp-Source: ADFU+vuTny3inpH+BkBtHVluUYZAS2ilrT22+KyhZ38UFQUYJ9YagLJZk+0yZRYF7dYkaz9qoE73jA== X-Received: by 2002:a17:902:b604:: with SMTP id b4mr11803341pls.340.1583681900031; Sun, 08 Mar 2020 08:38:20 -0700 (PDT) Received: from tigtog.localdomain.localdomain ([144.34.163.219]) by smtp.gmail.com with ESMTPSA id a143sm18141508pfd.108.2020.03.08.08.38.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2020 08:38:19 -0700 (PDT) From: Jiang Xin To: Junio C Hamano , Git List Cc: Jiang Xin Subject: [PATCH v2 3/5] receive-pack: new config receive.procReceiveRefs Date: Sun, 8 Mar 2020 11:38:14 -0400 Message-Id: <20200308153816.4690-1-worldhello.net@gmail.com> X-Mailer: git-send-email 2.26.0.rc0 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jiang Xin Add a new multi-valued config variable "receive.procReceiveRefs" for `receive-pack` command, like the follows: git config --system --add receive.procReceiveRefs refs/for/ git config --system --add receive.procReceiveRefs refs/drafts/ If the specific prefix strings match the reference names of the commands which are sent by git client to `receive-pack`, these commands will be executed by an external hook (named "proc-receive"), instead of the internal `execute_commands` function. For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook. Signed-off-by: Jiang Xin --- Documentation/config/receive.txt | 14 +++ builtin/receive-pack.c | 49 ++++++-- t/t5411-proc-receive-hook.sh | 189 +++++++++++++++++++++++++++---- 3 files changed, 220 insertions(+), 32 deletions(-) diff --git a/Documentation/config/receive.txt b/Documentation/config/receive.txt index 65f78aac37..0178f2d478 100644 --- a/Documentation/config/receive.txt +++ b/Documentation/config/receive.txt @@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs. diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 23d0c224d2..5aff682758 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -76,6 +76,7 @@ static struct object_id push_cert_oid; static struct signature_check sigcheck; static const char *push_cert_nonce; static const char *cert_nonce_seed; +static struct string_list proc_receive_refs; static const char *NONCE_UNSOLICITED = "UNSOLICITED"; static const char *NONCE_BAD = "BAD"; @@ -228,6 +229,20 @@ static int receive_pack_config(const char *var, const char *value, void *cb) return 0; } + if (strcmp(var, "receive.procreceiverefs") == 0) { + char *prefix; + int len; + + if (!value) + return config_error_nonbool(var); + prefix = xstrdup(value); + len = strlen(prefix); + while (len && prefix[len - 1] == '/') + prefix[--len] = '\0'; + string_list_insert(&proc_receive_refs, prefix); + return 0; + } + return git_default_config(var, value, cb); } @@ -1544,17 +1559,30 @@ static void execute_commands(struct command *commands, /* Try to find commands that have special prefix in their reference names, * and mark them to run an external "proc-receive" hook later. */ - for (cmd = commands; cmd; cmd = cmd->next) { - if (!should_process_cmd(cmd)) - continue; + if (proc_receive_refs.nr > 0) { + struct strbuf refname_full = STRBUF_INIT; + size_t prefix_len; - /* TODO: replace the fixed prefix by looking up git config variables. */ - if (!strncmp(cmd->ref_name, "refs/for/", 9)) { - cmd->have_special_ref = 1; - have_special_ref = 1; - } else { - have_normal_ref = 1; + strbuf_addstr(&refname_full, get_git_namespace()); + prefix_len = refname_full.len; + + for (cmd = commands; cmd; cmd = cmd->next) { + if (!should_process_cmd(cmd)) + continue; + + strbuf_setlen(&refname_full, prefix_len); + strbuf_addstr(&refname_full, cmd->ref_name); + if (ref_is_matched(&proc_receive_refs, cmd->ref_name, refname_full.buf)) { + cmd->have_special_ref = 1; + have_special_ref = 1; + } else { + have_normal_ref = 1; + } } + + strbuf_release(&refname_full); + } else { + have_normal_ref = 1; } if (run_receive_hook(commands, "pre-receive", 0, push_options)) { @@ -2011,6 +2039,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) OPT_END() }; + string_list_init(&proc_receive_refs, 0); + packet_trace_identity("receive-pack"); argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0); @@ -2126,5 +2156,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) oid_array_clear(&shallow); oid_array_clear(&ref); free((void *)push_cert_nonce); + string_list_clear(&proc_receive_refs, 0); return 0; } diff --git a/t/t5411-proc-receive-hook.sh b/t/t5411-proc-receive-hook.sh index 01f67965bf..e3bb421078 100755 --- a/t/t5411-proc-receive-hook.sh +++ b/t/t5411-proc-receive-hook.sh @@ -91,17 +91,77 @@ test_expect_success "(1) standard git-push command" ' ( cd work && git update-ref HEAD $A && - git push origin HEAD HEAD:maint HEAD:a/b/c 2>&1 + git push origin HEAD HEAD:maint 2>&1 ) >out && grep "^remote:" out | sed -e "s/ *\$//g" >actual && cat >expect <<-EOF && remote: execute: pre-receive hook remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/master. remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/maint. - remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/a/b/c. remote: execute: post-receive hook remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/master. remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/maint. + EOF + test_cmp expect actual && + ( + cd $bare && + git show-ref + ) >actual && + cat >expect <<-EOF && + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + +test_expect_success "(1) push one special ref (create one)" ' + ( + cd work && + git update-ref HEAD $B && + git push origin HEAD:refs/for/master/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: execute: pre-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: execute: post-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + EOF + test_cmp expect actual && + ( + cd $bare && + git show-ref + ) >actual && + cat >expect <<-EOF && + $B refs/for/master/my/topic + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + +test_expect_success "(1) cleanup" ' + ( + cd $bare && + git update-ref -d refs/for/master/my/topic + ) +' + +test_expect_success "(2) add config to turn on proc-receive" ' + git -C $bare config --add receive.procReceiveRefs refs/for/ +' + +test_expect_success "(2) standard git-push command" ' + ( + cd work && + git update-ref HEAD $A && + git push origin HEAD:a/b/c 2>&1 + ) >out && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: execute: pre-receive hook + remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/a/b/c. + remote: execute: post-receive hook remote: >> old: $ZERO_OID, new: $A, ref: refs/heads/a/b/c. EOF test_cmp expect actual && @@ -117,7 +177,7 @@ test_expect_success "(1) standard git-push command" ' test_cmp expect actual ' -test_expect_success "(1) push one special ref" ' +test_expect_success "(2) push one special ref" ' ( cd work && git update-ref HEAD $B && @@ -145,9 +205,10 @@ test_expect_success "(1) push one special ref" ' test_cmp expect actual ' -test_expect_success "(1) push both a normal and a special refs" ' +test_expect_success "(2) push both a normal and a special refs" ' ( cd work && + git update-ref HEAD $B && git push origin \ HEAD:refs/for/maint/my/topic \ HEAD:refs/heads/master @@ -176,7 +237,7 @@ test_expect_success "(1) push both a normal and a special refs" ' test_cmp expect actual ' -test_expect_success "(1) cleanup" ' +test_expect_success "(2) cleanup" ' ( cd $bare && git update-ref refs/heads/master $A $B && @@ -190,11 +251,93 @@ test_expect_success "(1) cleanup" ' test_cmp expect actual ' -test_expect_success "(2) remove proc-receive hook" ' +test_expect_success "(2) push two special references (one is not registered)" ' + ( + cd work && + git push origin \ + HEAD:refs/for/maint/my/topic \ + HEAD:refs/drafts/maint/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: execute: pre-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/maint/my/topic. + remote: >> old: $ZERO_OID, new: $B, ref: refs/drafts/maint/my/topic. + remote: execute: proc-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/maint/my/topic. + remote: execute: post-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/maint/my/topic. + remote: >> old: $ZERO_OID, new: $B, ref: refs/drafts/maint/my/topic. + EOF + test_cmp expect actual && + ( + cd $bare && + git show-ref + ) >actual && + cat >expect <<-EOF && + $B refs/drafts/maint/my/topic + $A refs/heads/a/b/c + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + +test_expect_success "(2) cleanup" ' + ( + cd $bare && + git update-ref -d refs/drafts/maint/my/topic && + git show-ref + ) >actual && + cat >expect <<-EOF && + $A refs/heads/a/b/c + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + +test_expect_success "(2) register new ref prefix" ' + git -C $bare config --add receive.procReceiveRefs refs/drafts/ +' + +test_expect_success "(2) push to two special references (all registered)" ' + ( + cd work && + git push origin \ + HEAD:refs/for/master/my/topic \ + HEAD:refs/drafts/maint/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: execute: pre-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: >> old: $ZERO_OID, new: $B, ref: refs/drafts/maint/my/topic. + remote: execute: proc-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: >> old: $ZERO_OID, new: $B, ref: refs/drafts/maint/my/topic. + remote: execute: post-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: >> old: $ZERO_OID, new: $B, ref: refs/drafts/maint/my/topic. + EOF + test_cmp expect actual && + ( + cd $bare && + git show-ref + ) >actual && + cat >expect <<-EOF && + $A refs/heads/a/b/c + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + +test_expect_success "(3) remove proc-receive hook" ' mv $bare/hooks/proc-receive $bare/hooks/proc-receive.ok ' -test_expect_success "(2) standard git-push command" ' +test_expect_success "(3) standard git-push command" ' ( cd work && git update-ref HEAD $B && @@ -220,7 +363,7 @@ test_expect_success "(2) standard git-push command" ' test_cmp expect actual ' -test_expect_success "(2) cleanup" ' +test_expect_success "(3) cleanup" ' ( cd $bare && git update-ref refs/heads/master $A $B && @@ -234,7 +377,7 @@ test_expect_success "(2) cleanup" ' test_cmp expect actual ' -test_expect_success "(2) push one special ref (failed)" ' +test_expect_success "(3) push one special ref (failed)" ' ( cd work && git update-ref HEAD $B && @@ -249,7 +392,7 @@ test_expect_success "(2) push one special ref (failed)" ' test_cmp expect actual ' -test_expect_success "(2) push both a normal and a special refs (one failed)" ' +test_expect_success "(3) push both a normal and a special refs (one failed)" ' ( cd work && git update-ref HEAD $B && @@ -279,7 +422,7 @@ test_expect_success "(2) push both a normal and a special refs (one failed)" ' test_cmp expect actual ' -test_expect_success "(2) cleanup" ' +test_expect_success "(3) cleanup" ' ( cd $bare && git update-ref refs/heads/master $A $B && @@ -293,7 +436,7 @@ test_expect_success "(2) cleanup" ' test_cmp expect actual ' -test_expect_success "(2) atomic push both a normal and a special refs (failed)" ' +test_expect_success "(3) atomic push both a normal and a special refs (failed)" ' ( cd work && git update-ref HEAD $B && @@ -321,7 +464,7 @@ test_expect_success "(2) atomic push both a normal and a special refs (failed)" test_cmp expect actual ' -test_expect_success "(3) new proc-receive hook (return error)" ' +test_expect_success "(4) new proc-receive hook (return error)" ' cat >$bare/hooks/proc-receive <<-EOF && #!/bin/sh @@ -338,7 +481,7 @@ test_expect_success "(3) new proc-receive hook (return error)" ' chmod a+x $bare/hooks/proc-receive ' -test_expect_success "(3) standard git-push command" ' +test_expect_success "(4) standard git-push command" ' ( cd work && git update-ref HEAD $B && @@ -364,7 +507,7 @@ test_expect_success "(3) standard git-push command" ' test_cmp expect actual ' -test_expect_success "(3) cleanup" ' +test_expect_success "(4) cleanup" ' ( cd $bare && git update-ref refs/heads/master $A $B && @@ -378,7 +521,7 @@ test_expect_success "(3) cleanup" ' test_cmp expect actual ' -test_expect_success "(3) push both a normal and a special refs (one failed)" ' +test_expect_success "(4) push both a normal and a special refs (one failed)" ' ( cd work && git update-ref HEAD $B && @@ -410,7 +553,7 @@ test_expect_success "(3) push both a normal and a special refs (one failed)" ' test_cmp expect actual ' -test_expect_success "(3) cleanup" ' +test_expect_success "(4) cleanup" ' ( cd $bare && git update-ref refs/heads/master $A $B && @@ -424,7 +567,7 @@ test_expect_success "(3) cleanup" ' test_cmp expect actual ' -test_expect_success "(3) atomic push a normal and a special refs (failed)" ' +test_expect_success "(4) atomic push a normal and a special refs (failed)" ' ( cd work && git update-ref HEAD $B && @@ -454,12 +597,12 @@ test_expect_success "(3) atomic push a normal and a special refs (failed)" ' test_cmp expect actual ' -test_expect_success "(4) restore proc-receive hook" ' +test_expect_success "(5) restore proc-receive hook" ' mv $bare/hooks/proc-receive $bare/hooks/proc-receive.fail && mv $bare/hooks/proc-receive.ok $bare/hooks/proc-receive ' -test_expect_success "(4) push two special references" ' +test_expect_success "(5) push two special references" ' ( cd work && git update-ref HEAD $B && @@ -492,7 +635,7 @@ test_expect_success "(4) push two special references" ' test_cmp expect actual ' -test_expect_success "(5) new pre-receive hook hook (return error)" ' +test_expect_success "(6) new pre-receive hook hook (return error)" ' mv $bare/hooks/pre-receive $bare/hooks/pre-receive.ok && cat >$bare/hooks/pre-receive <<-EOF && #!/bin/sh @@ -510,7 +653,7 @@ test_expect_success "(5) new pre-receive hook hook (return error)" ' chmod a+x $bare/hooks/pre-receive ' -test_expect_success "(5) push two special references (declined)" ' +test_expect_success "(6) push two special references (declined)" ' ( cd work && test_must_fail git push origin \ @@ -527,7 +670,7 @@ test_expect_success "(5) push two special references (declined)" ' test_cmp expect actual ' -test_expect_success "(5) push both a normal and a special refs (declined)" ' +test_expect_success "(6) push both a normal and a special refs (declined)" ' ( cd work && test_must_fail git push origin \ From patchwork Sun Mar 8 15:38:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 11425711 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6542E14B7 for ; Sun, 8 Mar 2020 15:38:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 463E120866 for ; Sun, 8 Mar 2020 15:38:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EQsf3s5F" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726318AbgCHPiW (ORCPT ); Sun, 8 Mar 2020 11:38:22 -0400 Received: from mail-pf1-f182.google.com ([209.85.210.182]:43741 "EHLO mail-pf1-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726271AbgCHPiW (ORCPT ); Sun, 8 Mar 2020 11:38:22 -0400 Received: by mail-pf1-f182.google.com with SMTP id c144so3657366pfb.10 for ; Sun, 08 Mar 2020 08:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vc5YaSnlEaNjsL7hECj+viFsVC6sdb8TgOzX+JmXC3I=; b=EQsf3s5FOEmsHeCEOO/zKGga2XbYUqv/A9vpvqjnhy2JT2sAcVwBENjByVE1mQVCkH Fhs1kKzZPW+mg8NRAvqudpY2fXw5fdFZ4qzPub+EGzmx8PQh5+0Uz8WVUx4x7Gvo86Ru aEa++073XKmZQV+SMJSSA34K6clNjLX317fbVhwiWMhM/yqFttHFAIBdvGGkuLoLO2MH 0KyXs2JsUKwiYR4iqc4guE9hkC4dRr4gsqu/wTP82kFPQp06wadqyRAzSjfCbXuq3tXt vQ+A3TIv8A2mOlLBrCgCDIlQ8M4MZtsyHlXWz4ffXziSbybppWJnoyGPzlbVptqK7kVh jNZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vc5YaSnlEaNjsL7hECj+viFsVC6sdb8TgOzX+JmXC3I=; b=fE5X7pi9Jc0dS/2ZpkBCJrSaXQMu6F+QvDzsu3HacPD0W66Phc77wAi/YvW5Y8s1ue P974xNeMRLzi8QZQ3zCi2YedP/YFqcGREpa5d6ARjQQ+jmkvZ8wQjQhSDcCuDSw/lq9d IIwVlteO+dSs7b87OPV8QFFHfEaxyak1Wz2+Z+TjBqPSf52kY7k/HP1XElzJtWmpYYK4 M5xNMX8LdWdr7yD6F66+XI1X2KWYo0gF0zFBIZIUyJrxjeShrSnDHHMEjxsfgHk4GI1J Mfn6sNz+x6KgIJhAcModxoylB6zbZMMRH6oV4uRwF83ol/kImdP4bPwIbzPilBs2WalV BT5A== X-Gm-Message-State: ANhLgQ3Dg5gTbVFdBYiQDBLd/qIzw/RMuqECbA0rnHKOWBAJb5sUwUP1 UlpUDGiY6NEbSAQvBhnlup776ieXwZOBTw== X-Google-Smtp-Source: ADFU+vvAN/04bz/XpeV1J2dcYwbwa+o7jDlPiGRwOxiUOE8jikrlltIGYswqdeZqwTyLR4v07izGxQ== X-Received: by 2002:a62:2ad1:: with SMTP id q200mr13290217pfq.123.1583681900757; Sun, 08 Mar 2020 08:38:20 -0700 (PDT) Received: from tigtog.localdomain.localdomain ([144.34.163.219]) by smtp.gmail.com with ESMTPSA id a143sm18141508pfd.108.2020.03.08.08.38.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2020 08:38:20 -0700 (PDT) From: Jiang Xin To: Junio C Hamano , Git List Cc: Jiang Xin Subject: [PATCH v2 4/5] receive-pack: read env from proc-receive output Date: Sun, 8 Mar 2020 11:38:15 -0400 Message-Id: <20200308153816.4690-2-worldhello.net@gmail.com> X-Mailer: git-send-email 2.26.0.rc0 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jiang Xin The “post-receive” hook may need the pull request ID generated by the “proc-receive” hook. The results can be passed between hooks by environment variables. Each line of the message received from the standard output of the “proc-receive” hook in the key=value format is parsed as environment and these variables will be sent to environment of the “post-receive” hook. Signed-off-by: Jiang Xin --- builtin/receive-pack.c | 38 ++++++++++++++++++-- t/t5411-proc-receive-hook.sh | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 5aff682758..d854952410 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -87,6 +87,7 @@ static const char *nonce_status; static long nonce_stamp_slop; static timestamp_t nonce_stamp_slop_limit; static struct ref_transaction *transaction; +struct argv_array post_receive_env_array; static enum { KEEPALIVE_NEVER = 0, @@ -694,7 +695,9 @@ struct receive_hook_feed_state { }; typedef int (*feed_fn)(void *, const char **, size_t *); +typedef void (*stdout_handler_fn)(int out); static int run_and_feed_hook(const char *hook_name, feed_fn feed, + stdout_handler_fn stdout_handler, struct receive_hook_feed_state *feed_state) { struct child_process proc = CHILD_PROCESS_INIT; @@ -716,9 +719,15 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, proc.argv = argv; proc.in = -1; - proc.stdout_to_stderr = 1; + if (stdout_handler) + proc.out = -1; + else + proc.stdout_to_stderr = 1; proc.trace2_hook_name = hook_name; + if (!strcmp(hook_name, "post-receive") && post_receive_env_array.argc > 0) + argv_array_pushv(&proc.env_array, post_receive_env_array.argv); + if (feed_state->push_options) { int i; for (i = 0; i < feed_state->push_options->nr; i++) @@ -763,6 +772,10 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, break; } close(proc.in); + + if (stdout_handler) + stdout_handler(proc.out); + if (use_sideband) finish_async(&muxer); @@ -819,7 +832,7 @@ static int run_receive_hook(struct command *commands, return 0; state.cmd = commands; state.push_options = push_options; - status = run_and_feed_hook(hook_name, feed_receive_hook, &state); + status = run_and_feed_hook(hook_name, feed_receive_hook, NULL, &state); strbuf_release(&state.buf); return status; } @@ -853,6 +866,23 @@ static int run_update_hook(struct command *cmd) return finish_command(&proc); } +static void prepare_post_receive_env(int in) +{ + struct strbuf stdout_buf = STRBUF_INIT; + + while (strbuf_getwholeline_fd(&stdout_buf, in, '\n') != EOF) { + char *p = stdout_buf.buf + stdout_buf.len -1; + if (*p =='\n') + *p = '\0'; + p = strchr(stdout_buf.buf, '='); + if (p == NULL) + continue; + argv_array_push(&post_receive_env_array, stdout_buf.buf); + strbuf_reset(&stdout_buf); + } + strbuf_release(&stdout_buf); +} + static int run_proc_receive_hook(struct command *commands, const struct string_list *push_options) { @@ -869,7 +899,8 @@ static int run_proc_receive_hook(struct command *commands, return 0; state.cmd = commands; state.push_options = push_options; - status = run_and_feed_hook("proc-receive", feed_receive_hook, &state); + status = run_and_feed_hook("proc-receive", + feed_receive_hook, prepare_post_receive_env, &state); strbuf_release(&state.buf); return status; } @@ -2040,6 +2071,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) }; string_list_init(&proc_receive_refs, 0); + argv_array_init(&post_receive_env_array); packet_trace_identity("receive-pack"); diff --git a/t/t5411-proc-receive-hook.sh b/t/t5411-proc-receive-hook.sh index e3bb421078..6f35867e85 100755 --- a/t/t5411-proc-receive-hook.sh +++ b/t/t5411-proc-receive-hook.sh @@ -687,4 +687,74 @@ test_expect_success "(6) push both a normal and a special refs (declined)" ' test_cmp expect actual ' +test_expect_success "(7) restore pre-receive hook" ' + mv $bare/hooks/pre-receive $bare/hooks/pre-receive.fail && + mv $bare/hooks/pre-receive.ok $bare/hooks/pre-receive +' + +test_expect_success "(7) new proc-receive and post-receive hooks (pass environment variables)" ' + ## proc-receive hook + mv $bare/hooks/proc-receive $bare/hooks/proc-receive.ok && + cat >$bare/hooks/proc-receive <<-EOF && + #!/bin/sh + + printf >&2 "execute: proc-receive hook\n" + + while read old new ref + do + printf >&2 ">> old: \$old, new: \$new, ref: \$ref.\n" + done + + printf "GIT_VAR1=var1\n" + printf "GIT_VAR2=var2\n" + printf "AGIT_VAR1=foo\n" + printf "AGIT_VAR2=bar\n" + EOF + chmod a+x $bare/hooks/proc-receive && + + ## post-receive hook + mv $bare/hooks/post-receive $bare/hooks/post-receive.ok && + cat >$bare/hooks/post-receive <<-EOF && + #!/bin/sh + + printf >&2 "execute: post-receive hook\n" + + while read old new ref + do + printf >&2 ">> old: \$old, new: \$new, ref: \$ref.\n" + done + + for k in GIT_VAR1 GIT_VAR2 AGIT_VAR1 AGIT_VAR2 + do + if test -n "\$(eval echo \\"\\\$\$k\")" + then + printf >&2 ">> has env: \$k=\$(eval echo \\"\\\$\$k\").\n" + fi + done + EOF + chmod a+x $bare/hooks/post-receive +' + +test_expect_success "(7) push one special ref (show environments)" ' + ( + cd work && + git push origin \ + HEAD:refs/for/master/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: execute: pre-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: execute: proc-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: execute: post-receive hook + remote: >> old: $ZERO_OID, new: $B, ref: refs/for/master/my/topic. + remote: >> has env: GIT_VAR1=var1. + remote: >> has env: GIT_VAR2=var2. + remote: >> has env: AGIT_VAR1=foo. + remote: >> has env: AGIT_VAR2=bar. + EOF + test_cmp expect actual +' + test_done From patchwork Sun Mar 8 15:38:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 11425713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C56F5138D for ; Sun, 8 Mar 2020 15:38:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9D79D206D7 for ; Sun, 8 Mar 2020 15:38:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YhNHsA5q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726330AbgCHPiX (ORCPT ); Sun, 8 Mar 2020 11:38:23 -0400 Received: from mail-pj1-f65.google.com ([209.85.216.65]:40650 "EHLO mail-pj1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726260AbgCHPiW (ORCPT ); Sun, 8 Mar 2020 11:38:22 -0400 Received: by mail-pj1-f65.google.com with SMTP id gv19so3301646pjb.5 for ; Sun, 08 Mar 2020 08:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QCRoykGyqvBYbGdGdBEY+MSzZ9WPPSm9L8vfS0EeIzg=; b=YhNHsA5qZM8cGHaU8o97Aqb/BdCiMCVnR0QKnHCIftQFI9Ynb9mvgzHU1YJYrG75km vt5rEse0cgYOg8TVCmuCuH+0387bzwK9RVuKiLu63AP3eWsApudrZQHM+qD7hPc97lw0 enH+uw71I3XfGZbZ89Fi+1cPcPuqlKwPO+ugppBPbwY53Jrg6EbWAi24Gm9col9hwQNs O/v1O65mT22A0S4ji/63DADCRRDpIFPjmZIe9fqqzbfB7WRSw96dH3b1onlJ7y2Ph/yy UhRvWaz8W8+0hb4soFcnzgzfY35T6ApDeDvhTuZfgpYD1YssZo6/hdxBPhUHgQAXo7M9 ynfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QCRoykGyqvBYbGdGdBEY+MSzZ9WPPSm9L8vfS0EeIzg=; b=H3XIfbr/DgBpJ34zPy8fMbEM1l0NAKYUEsL4x3zR7nTkH3FIFD9xiWAmqqNT2kzFIT DyilMFXTJGFKQ9UXpVZCNmyi/Vln9cx21lY9SNHF2LLi3AOA6gByzolF7ntLymGYvNy8 BPsoS3BNKnadFFDed5PEuE0dvDF/7JJo9nHR85HwMSael1I9SctFFUdXC3vIhkrEQUqB DWP5CvA9V+2uA45BPG5WfaStP3Dwo+1KFZZrgHfAuqeMCsDgRCSj75CcgFAVKPHa6e6A hy6WaAKdqqZaI5sNHCTV1CaPy473OQu8MB4SePtYs2FODGG1XSiuIYKwLFm++5THnLl8 jBfA== X-Gm-Message-State: ANhLgQ3+vxYXoK0kEbbxSQzma7gdFIZBZPYi3xfDLHvLVao1ZWY/jbjk QBKsanZaPVoc4uEjUkTqO3w= X-Google-Smtp-Source: ADFU+vs+Nfi/FMrEeN/D3YzN9KZkiy+pWF3eWT76NzCyjB/GS9dK2LCXyMhlPtgqYc7qhV78k6QJLg== X-Received: by 2002:a17:90a:ab0a:: with SMTP id m10mr889552pjq.105.1583681901665; Sun, 08 Mar 2020 08:38:21 -0700 (PDT) Received: from tigtog.localdomain.localdomain ([144.34.163.219]) by smtp.gmail.com with ESMTPSA id a143sm18141508pfd.108.2020.03.08.08.38.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2020 08:38:21 -0700 (PDT) From: Jiang Xin To: Junio C Hamano , Git List Cc: Jiang Xin Subject: [PATCH v2 5/5] hook: add document and example for "proc-receive" hook Date: Sun, 8 Mar 2020 11:38:16 -0400 Message-Id: <20200308153816.4690-3-worldhello.net@gmail.com> X-Mailer: git-send-email 2.26.0.rc0 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Jiang Xin Signed-off-by: Jiang Xin --- Documentation/githooks.txt | 22 +++++ t/t5411-proc-receive-hook.sh | 71 ++++++++++++++++ templates/hooks--proc-receive.sample | 119 +++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100755 templates/hooks--proc-receive.sample diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 3dccab5375..1af213bad8 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -333,6 +333,28 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +[[proc-receive]] +proc-receive +~~~~~~~~~~~~ +This hook is invoked by linkgit:git-receive-pack[1] when it reacts to +special `git push` command. According to refnames of the commands which +`git push` sends to 'git-receive-pack', the commands will be devided +into two groups by matching what the `receive.procReceiveRefs` +configuration variable defines. One group of the commands will execute +the internal `execute_commands` function to update the corresponding +refnames, and the other group of commands which have matching refnames +will execute this 'proc-receive' hook to create pull requests, etc. +If there is no `receive.procReceiveRefs` settings, this hook won't +execute at all, and all commands are sent to the internal +`execute_commands` function. + +Its exit status only determines the success or failure of the group of +commands with special refnames, unless atomic push is in use. + +This hook executes once for the receive operation. It takes no +arguments, but gets the same information as the +<> hook does on its standard input. + [[post-receive]] post-receive ~~~~~~~~~~~~ diff --git a/t/t5411-proc-receive-hook.sh b/t/t5411-proc-receive-hook.sh index 6f35867e85..caedd49f2e 100755 --- a/t/t5411-proc-receive-hook.sh +++ b/t/t5411-proc-receive-hook.sh @@ -757,4 +757,75 @@ test_expect_success "(7) push one special ref (show environments)" ' test_cmp expect actual ' +test_expect_success "(8) install proc-receive from proc-receive.sample" ' + mv $bare/hooks/pre-receive $bare/hooks/pre-receive.ok && + mv $bare/hooks/post-receive $bare/hooks/post-receive.env && + mv $bare/hooks/proc-receive $bare/hooks/proc-receive.env && + cp ../../templates/hooks--proc-receive.sample $bare/hooks/proc-receive && + chmod a+x $bare/hooks/proc-receive +' + +test_expect_success "(8) proc-receive.sample: show push result" ' + ( + cd work && + git push origin \ + HEAD:refs/for/a/b/c/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: [proc-receive] ******************************************************* + remote: [proc-receive] * Pull request #12345678901 created/updated * + remote: [proc-receive] * URL: https://... ... * + remote: [proc-receive] ******************************************************* + EOF + test_cmp expect actual +' + +test_expect_success "(8) proc-receive.sample: show debug info" ' + ( + cd work && + git push -o debug=1 -o reviewers=user1,user2 \ + origin \ + HEAD:refs/for/a/b/c/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: [DEBUG] [proc-receive] push-option: AGIT_DEBUG=1 + remote: [DEBUG] [proc-receive] push-option: AGIT_REVIEWERS=user1,user2 + remote: [DEBUG] [proc-receive] command from stdin: $ZERO_OID $B refs/for/a/b/c/my/topic + remote: [DEBUG] [proc-receive] call API (AGIT_PR_TARGET=a/b/c, AGIT_PR_TOPIC=)... + remote: [DEBUG] [proc-receive] parse API result, and get AGIT_PR_ID, etc. + remote: [proc-receive] ******************************************************* + remote: [proc-receive] * Pull request #12345678901 created/updated * + remote: [proc-receive] * URL: https://... ... * + remote: [proc-receive] ******************************************************* + remote: [DEBUG] [proc-receive] output kv pairs to stdout for git to parse. + EOF + test_cmp expect actual +' + +test_expect_success "(8) proc-receive.sample: fail to push non-exist branch" ' + ( + cd work && + test_must_fail git push -o reviewers=user1,user2 \ + origin \ + HEAD:refs/for/a/b/x/my/topic + ) >out 2>&1 && + grep "^remote:" out | sed -e "s/ *\$//g" >actual && + cat >expect <<-EOF && + remote: [proc-receive] cannot find target branch from ref: refs/for/a/b/x/my/topic + EOF + test_cmp expect actual && + ( + cd $bare && + git show-ref + ) >actual && + cat >expect <<-EOF && + $A refs/heads/a/b/c + $A refs/heads/maint + $A refs/heads/master + EOF + test_cmp expect actual +' + test_done diff --git a/templates/hooks--proc-receive.sample b/templates/hooks--proc-receive.sample new file mode 100755 index 0000000000..31fa85ffe1 --- /dev/null +++ b/templates/hooks--proc-receive.sample @@ -0,0 +1,119 @@ +#!/bin/sh +# +# This is an example hook script, DO NOT use it on production service. + +debug() { + case "$AGIT_DEBUG" in + "yes" | "true" | "1") + ;; + *) + return + esac + + echo >&2 "[DEBUG] $@" +} + +# Parse push options +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + i=$((i + 1)) + + k=$(echo ${value%=*} | tr [a-z] [A-Z]) + v=${value#*=} + if test -n "$v" && test -n "$k" + then + k="AGIT_$k" + else + continue + fi + eval "$k=$v" + debug "[proc-receive] push-option: $k=$v" + done +fi + +# Read push commands. +count=0 +while read old new refname +do + debug "[proc-receive] command from stdin: $old $new $refname" + count=$(( count + 1 )) + # Only one special refname is allowed for each push + if test $count -gt 1 + then + echo >&2 "[proc-receive]: cannot handle more than one push commands" + exit 1 + fi + + # Parse refname, and set envrionment + remains= + if test "${refname#refs/for/}" != "$refname" + then + AGIT_PR_IS_DRAFT=false + remains=${refname#refs/for/} + elif test "${refname#refs/drafts/}" != "$refname" + then + AGIT_PR_IS_DRAFT=true + remains=${refname#refs/drafts/} + else + echo >&2 "[proc-receive] unknown refname: $refname" + exit 1 + fi + + ref= + found_ref= + for i in $(echo $remains | tr "/" "\n") + do + if test -z "$ref" + then + ref=$i + else + ref=$ref/$i + fi + if git rev-parse --verify $ref -- 2>/dev/null + then + found_ref=yes + break + fi + done + if test -z "$found_ref" + then + echo >&2 "[proc-receive] cannot find target branch from ref: $refname" + exit 1 + fi + AGIT_PR_TARGET=$ref + AGIT_PR_SOURCE=${remains#$ref/} +done + +if test -z "$AGIT_PR_TARGET" +then + echo >&2 "[proc-receive] fail to parse refname, no target found" + exit 1 +fi + +# Call API to generate code review. +debug "[proc-receive] call API (AGIT_PR_TARGET=$AGIT_PR_TARGET, AGIT_PR_TOPIC=$AGIT_PR_TOPIC)..." + +# Parse result of API. +debug "[proc-receive] parse API result, and get AGIT_PR_ID, etc." +AGIT_PR_ID="12345678901" +AGIT_PR_LOCAL_ID="23" + +# Show message. +if test -n "$AGIT_PR_ID" +then + echo >&2 "[proc-receive] *******************************************************" + echo >&2 "[proc-receive] * Pull request #$AGIT_PR_ID created/updated *" + echo >&2 "[proc-receive] * URL: https://... ... *" + echo >&2 "[proc-receive] *******************************************************" +fi + +# Show envs to stdout, and will be exported as envs for "post-receive" hook. +debug "[proc-receive] output kv pairs to stdout for git to parse." +echo "AGIT_PR_ID=$AGIT_PR_ID" +echo "AGIT_PR_LOCAL_ID=$AGIT_PR_LOCAL_ID" + +exit 0