From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 078D0C2BA19 for ; Mon, 13 Apr 2020 16:48:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CA16B2063A for ; Mon, 13 Apr 2020 16:48:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mgHTm33q" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731793AbgDMQso (ORCPT ); Mon, 13 Apr 2020 12:48:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1731783AbgDMQsk (ORCPT ); Mon, 13 Apr 2020 12:48:40 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1EE9C0A3BE2 for ; Mon, 13 Apr 2020 09:48:40 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id cl8so2865535pjb.3 for ; Mon, 13 Apr 2020 09:48:40 -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=Qih7/Un8icVIOrUFd/ywK4bWXgYUGCpd1DApMjMYCE4=; b=mgHTm33qo4mHhq4jPixYv44FArUkdhg0BQ3BsVmOLmmCqPWoOj9pHhyYXmtipBtyY9 LgH48p+y58AyKSE8f3/EfpRWDiqM0eMkzXR3eLo2I0kMNSSmOokhYbiAuHRzootrQWcQ t39E4LAbMdslkvNhMfG+GPmhyZE3uezQXos9ftx1VNy1tvN3aYhIT8AOXY0YQv2V8NKb Al2Jq5e3BpD2Edrysf9Z6Y8tdlwKxStHdEHco2paPsRbS0mYwMzdcIh4CT752YqK7xgS 8CO5NlVrgXGIaW1gHAgSoHobXssJo/wR/W2Gvhq5ZeFTEUyxCzjJYAeANTTINn+ulnBe o6ng== 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=Qih7/Un8icVIOrUFd/ywK4bWXgYUGCpd1DApMjMYCE4=; b=VFlUHUpqgC/ylhyOIsJdGiDMffZ3lC0KRpY117lmfdkWFMdjEFOGIZqUMtkGqIHIPe p916FxRNMp4Z5rhvlJvCA6GE01ayhcjch2gp5GKhXcPEvMHWodp5e1Uye8N6DS6MErES UCZHLVq7/FUNdcUPPSWUhxwnF5GQxrKNPAVRBeyb2nNXgPU6olLTgsPtLde5JyiXS8by gIjqsqV+Z5rmZpdAEIYGDkeJ/Wy0E/WVU/sAjf2sJsx4k95i84TveHSvJhbMkbyjrl1j zo48CBKdS9W4ZhSv7KLoAkC+cAcydYC6tY5g3O5Q9l2Rq/pCPMofitF5aEnIm1dpgWc0 jvew== X-Gm-Message-State: AGi0PuZVutmMZf+Pj6jyW54TDaLNFuip6bfdWx6kTDp7B6d7y4+svQUd oBXw6qEVqHyR3lC7qc13U5I= X-Google-Smtp-Source: APiQypKHVWxThrIBF5qOVG9Ou1TiV4udG2+4lnsVkp77fmpMYX/gonPcBjAyMp9SyCrCWwDEQ0wiYw== X-Received: by 2002:a17:902:222:: with SMTP id 31mr18607176plc.108.1586796520394; Mon, 13 Apr 2020 09:48:40 -0700 (PDT) Received: from tigtog.localdomain.localdomain ([144.34.163.219]) by smtp.gmail.com with ESMTPSA id e11sm9254885pfh.117.2020.04.13.09.48.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Apr 2020 09:48:39 -0700 (PDT) From: Jiang Xin To: Junio C Hamano , Git List Cc: Jiang Xin Subject: [PATCH v11 6/7] receive-pack: new config receive.procReceiveRefs Date: Mon, 13 Apr 2020 12:48:26 -0400 Message-Id: <20200413164828.23260-7-worldhello.net@gmail.com> X-Mailer: git-send-email 2.26.0.rc0 In-Reply-To: <20200412133022.17590-1-worldhello.net@gmail.com> References: <20200412133022.17590-1-worldhello.net@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 from 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 hook "proc-receive". Signed-off-by: Jiang Xin --- Documentation/config/receive.txt | 14 +++++ builtin/receive-pack.c | 43 ++++++++++++--- t/t5411/common-test-cases.sh | 94 +++++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 15 deletions(-) diff --git a/Documentation/config/receive.txt b/Documentation/config/receive.txt index 65f78aac37..e7b967feeb 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 hook "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 the hook "proc-receive". + 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 fd2dc23c2e..7f39f8db9a 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_append(&proc_receive_refs, prefix); + return 0; + } + return git_default_config(var, value, cb); } @@ -1750,15 +1765,26 @@ 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->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED; - run_proc_receive = 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_matches(&proc_receive_refs, cmd->ref_name, refname_full.buf)) { + cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED; + run_proc_receive = 1; + } } + + strbuf_release(&refname_full); } if (run_receive_hook(commands, "pre-receive", 0, push_options)) { @@ -2223,6 +2249,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); @@ -2338,5 +2366,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/common-test-cases.sh b/t/t5411/common-test-cases.sh index 7fa7211ce4..ff73549a8a 100644 --- a/t/t5411/common-test-cases.sh +++ b/t/t5411/common-test-cases.sh @@ -53,6 +53,14 @@ 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/review/ + ) +' + # Refs of upstream : master(A) # Refs of workbench: master(A) tags/v123 # git push : next(A) refs/for/master/topic(A) @@ -607,7 +615,7 @@ test_expect_success "setup proc-receive hook (with extended status)" ' write_script "$upstream/hooks/proc-receive" <<-EOF printf >&2 "# proc-receive hook\n" test-tool proc-receive -v \ - -r "$ZERO_OID $A refs/for/a/b/c/topic ok" \ + -r "$ZERO_OID $A refs/review/a/b/c/topic ok" \ -r "$ZERO_OID $A refs/for/next/topic ok\0ref=refs/pull/123/head" \ -r "$B $A refs/for/master/topic ok\0ref=refs/pull/124/head forced-update" EOF @@ -615,33 +623,33 @@ test_expect_success "setup proc-receive hook (with extended status)" ' # Refs of upstream : master(A) # Refs of workbench: master(A) tags/v123 -# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/master/topic(A) +# git push : refs/for/next/topic(A) refs/review/a/b/c/topic(A) refs/for/master/topic(A) test_expect_success "proc-receive: report with extended status" ' git -C workbench push origin \ HEAD:refs/for/next/topic \ - HEAD:refs/for/a/b/c/topic \ + HEAD:refs/review/a/b/c/topic \ HEAD:refs/for/master/topic \ >out 2>&1 && make_user_friendly_and_stable_output actual && cat >expect <<-EOF && remote: # pre-receive hook remote: pre-receive< refs/for/next/topic - remote: pre-receive< refs/for/a/b/c/topic + remote: pre-receive< refs/review/a/b/c/topic remote: pre-receive< refs/for/master/topic remote: # proc-receive hook remote: proc-receive< refs/for/next/topic - remote: proc-receive< refs/for/a/b/c/topic + remote: proc-receive< refs/review/a/b/c/topic remote: proc-receive< refs/for/master/topic - remote: proc-receive> refs/for/a/b/c/topic ok + remote: proc-receive> refs/review/a/b/c/topic ok remote: proc-receive> refs/for/next/topic ok\0ref=refs/pull/123/head remote: proc-receive> refs/for/master/topic ok\0ref=refs/pull/124/head forced-update remote: # post-receive hook remote: post-receive< refs/for/next/topic - remote: post-receive< refs/for/a/b/c/topic + remote: post-receive< refs/review/a/b/c/topic remote: post-receive< refs/for/master/topic To * [new reference] HEAD -> refs/pull/123/head - * [new reference] HEAD -> refs/for/a/b/c/topic + * [new reference] HEAD -> refs/review/a/b/c/topic + ... HEAD -> refs/pull/124/head (forced update) EOF test_cmp expect actual && @@ -714,3 +722,73 @@ test_expect_success "proc-receive: report update of mixed refs" ' EOF test_cmp expect actual ' + +test_expect_success "config add receive.procReceiveRefs = refs" ' + git -C "$upstream" config --add receive.procReceiveRefs refs +' + +test_expect_success "setup proc-receive hook" ' + write_script "$upstream/hooks/proc-receive" <<-EOF + printf >&2 "# proc-receive hook\n" + test-tool proc-receive -v \ + -r "$B $A refs/heads/master ft" \ + -r "$A $ZERO_OID refs/heads/foo ft" \ + -r "$A $B refs/heads/bar ft" \ + -r "$A $B refs/for/master/topic ok\0ref=refs/pull/123/head" \ + -r "$B $A refs/for/next/topic ok\0ref=refs/pull/124/head forced-update" + EOF +' + +# Refs of upstream : master(B) foo(A) bar(A)) baz(A) +# Refs of workbench: master(A) tags/v123 +# git push -f : master(A) (NULL) (B) refs/for/master/topic(A) refs/for/next/topic(A) +test_expect_success "proc-receive: process all refs" ' + git -C workbench push -f origin \ + HEAD:refs/heads/master \ + :refs/heads/foo \ + $B:refs/heads/bar \ + HEAD:refs/for/master/topic \ + HEAD:refs/for/next/topic \ + >out 2>&1 && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< refs/heads/bar + remote: pre-receive< refs/heads/foo + remote: pre-receive< refs/heads/master + remote: pre-receive< refs/for/master/topic + remote: pre-receive< refs/for/next/topic + remote: # proc-receive hook + remote: proc-receive< refs/heads/bar + remote: proc-receive< refs/heads/foo + remote: proc-receive< refs/heads/master + remote: proc-receive< refs/for/master/topic + remote: proc-receive< refs/for/next/topic + remote: proc-receive> refs/heads/master ft + remote: proc-receive> refs/heads/foo ft + remote: proc-receive> refs/heads/bar ft + remote: proc-receive> refs/for/master/topic ok\0ref=refs/pull/123/head + remote: proc-receive> refs/for/next/topic ok\0ref=refs/pull/124/head forced-update + remote: # post-receive hook + remote: post-receive< refs/heads/bar + remote: post-receive< refs/heads/foo + remote: post-receive< refs/heads/master + remote: post-receive< refs/for/master/topic + remote: post-receive< refs/for/next/topic + To + .. -> bar + - [deleted] foo + + ... HEAD -> master (forced update) + .. HEAD -> refs/pull/123/head + + ... HEAD -> refs/pull/124/head (forced update) + EOF + test_cmp expect actual && + git -C "$upstream" show-ref >out && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + refs/heads/bar + refs/heads/baz + refs/heads/master + EOF + test_cmp expect actual +' -- 2.24.1.15.g448c31058d.agit.4.5