From patchwork Thu Jun 17 10:22:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327323 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 DBF7BC2B9F4 for ; Thu, 17 Jun 2021 10:23:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B136661245 for ; Thu, 17 Jun 2021 10:23:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231680AbhFQKZT (ORCPT ); Thu, 17 Jun 2021 06:25:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbhFQKZS (ORCPT ); Thu, 17 Jun 2021 06:25:18 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5C44C06175F for ; Thu, 17 Jun 2021 03:23:10 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id c7so2331057edn.6 for ; Thu, 17 Jun 2021 03:23:10 -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=rs54EexwIUW7+/OU2Anl7o2UQVXtLRfDxKe+aWVgoe8=; b=tCsDwtDczwLeSm2haovFMc8bJNmi7XHUi+cxAmxlVk+jbbhZK2IR2o/+fBeRPkY60/ qS5X0YjUzJw8/cwbaqicsHO5ZncoWeRkzFJ4EPL+IojXEDPdCrOtDLWa7/cgHXaKaFMG jP8FJ4EwVBsOOhscnUgNkZwDeYiQZG9WhydHn1TD65chpIP5tCIkoING+SNhzfYFM9T9 ALm4xM+FGyQ52FA2YKL/KMxfGyf03eQR3+aNZHdEPO/ZdN7boNWcaGarMssaQbrXTI2B HKmR1LL2agVpXV9tXDFaVt9pta7Cv2qQoM/zA9Xm9owxZM1KC2Pa2yYqkrYg7W68gMNq Autw== 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=rs54EexwIUW7+/OU2Anl7o2UQVXtLRfDxKe+aWVgoe8=; b=Febzt2B9bk8vtN747b6gjawRT1cCRmcvzVSdp0Isw29KeZZSmDwRivzQrePwgk9gbm q60MFV6eLfPkOgGYOf0Bs9oy1JxVLcGDb0qtZ++vEOL4ZNTWf6BMXQ97SdwdzCvTW3aa HJ+o6GIul+qZB2YgXn11UkO5shrUA1KQpWzpSk87NjE6sP4DI+gMqVuBBHO9CBrhh3NL jub3TDy/urFD+zva2nUU73DViTIqPdfIt/qgfc4ZE0xSXdh9qPy464m0h1COiBQ8fx6H gm3FclmcxU2br+S26LjSqpp4ipMtOKX6v1knnKniyKw3cUhqwhmsnjOK2pgaXxLPPz3t QzhA== X-Gm-Message-State: AOAM532ykH9BW4hou21G+EkI+0YSQ1682QDN9h2MS1vsoWrgIH2QHbPl 72EkGqTDUCBVHYUqOOn/V8jvqbEGQBECrg== X-Google-Smtp-Source: ABdhPJzxJvPO1TPr8xP+jXyfC1uB4yXQUUsVFk6MZ96H5Spp4nVGoaXnPnXglF1PjWOydMg9JIT9rA== X-Received: by 2002:a05:6402:100e:: with SMTP id c14mr3873874edu.51.1623925389171; Thu, 17 Jun 2021 03:23:09 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:08 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 01/27] hook: add 'run' subcommand Date: Thu, 17 Jun 2021 12:22:35 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer In order to enable hooks to be run as an external process, by a standalone Git command, or by tools which wrap Git, provide an external means to run all configured hook commands for a given hook event. Most of our hooks require more complex functionality than this, but let's start with the bare minimum required to support our simplest hooks. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- .gitignore | 1 + Documentation/git-hook.txt | 38 +++++++++++ Documentation/githooks.txt | 4 ++ Makefile | 1 + builtin.h | 1 + builtin/hook.c | 81 +++++++++++++++++++++++ command-list.txt | 1 + git.c | 1 + hook.c | 119 +++++++++++++++++++++++++++++++++ hook.h | 53 +++++++++++++++ t/t1800-hook.sh | 131 +++++++++++++++++++++++++++++++++++++ 11 files changed, 431 insertions(+) create mode 100644 Documentation/git-hook.txt create mode 100644 builtin/hook.c create mode 100755 t/t1800-hook.sh diff --git a/.gitignore b/.gitignore index 6be9de41ae..66189ca3cd 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,7 @@ /git-grep /git-hash-object /git-help +/git-hook /git-http-backend /git-http-fetch /git-http-push diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt new file mode 100644 index 0000000000..660d6a992a --- /dev/null +++ b/Documentation/git-hook.txt @@ -0,0 +1,38 @@ +git-hook(1) +=========== + +NAME +---- +git-hook - run git hooks + +SYNOPSIS +-------- +[verse] +'git hook' run [-- ] + +DESCRIPTION +----------- + +This command is an interface to git hooks (see linkgit:githooks[5]). +Currently it only provides a convenience wrapper for running hooks for +use by git itself. In the future it might gain other functionality. + +SUBCOMMANDS +----------- + +run:: + Run the `` hook. See linkgit:githooks[5] for + the hook names we support. ++ +Any positional arguments to the hook should be passed after an +optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The +arguments (if any) differ by hook name, see linkgit:githooks[5] for +what those are. + +SEE ALSO +-------- +linkgit:githooks[5] + +GIT +--- +Part of the linkgit:git[1] suite diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index b51959ff94..a16e62bc8c 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -698,6 +698,10 @@ and "0" meaning they were not. Only one parameter should be set to "1" when the hook runs. The hook running passing "1", "1" should not be possible. +SEE ALSO +-------- +linkgit:git-hook[1] + GIT --- Part of the linkgit:git[1] suite diff --git a/Makefile b/Makefile index 9bd31e1ac5..a52d509162 100644 --- a/Makefile +++ b/Makefile @@ -1106,6 +1106,7 @@ BUILTIN_OBJS += builtin/get-tar-commit-id.o BUILTIN_OBJS += builtin/grep.o BUILTIN_OBJS += builtin/hash-object.o BUILTIN_OBJS += builtin/help.o +BUILTIN_OBJS += builtin/hook.o BUILTIN_OBJS += builtin/index-pack.o BUILTIN_OBJS += builtin/init-db.o BUILTIN_OBJS += builtin/interpret-trailers.o diff --git a/builtin.h b/builtin.h index 16ecd5586f..91740c1514 100644 --- a/builtin.h +++ b/builtin.h @@ -164,6 +164,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix); int cmd_grep(int argc, const char **argv, const char *prefix); int cmd_hash_object(int argc, const char **argv, const char *prefix); int cmd_help(int argc, const char **argv, const char *prefix); +int cmd_hook(int argc, const char **argv, const char *prefix); int cmd_index_pack(int argc, const char **argv, const char *prefix); int cmd_init_db(int argc, const char **argv, const char *prefix); int cmd_interpret_trailers(int argc, const char **argv, const char *prefix); diff --git a/builtin/hook.c b/builtin/hook.c new file mode 100644 index 0000000000..7714d31ef1 --- /dev/null +++ b/builtin/hook.c @@ -0,0 +1,81 @@ +#include "cache.h" +#include "builtin.h" +#include "config.h" +#include "hook.h" +#include "parse-options.h" +#include "strbuf.h" +#include "strvec.h" + +static const char * const builtin_hook_usage[] = { + N_("git hook [...]"), + N_("git hook run [-- ]"), + NULL +}; + +static const char * const builtin_hook_run_usage[] = { + N_("git hook run [-- ]"), + NULL +}; + +static int run(int argc, const char **argv, const char *prefix) +{ + int i; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + int rc = 0; + const char *hook_name; + const char *hook_path; + + struct option run_options[] = { + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, run_options, + builtin_hook_run_usage, + PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); + + if (argc > 1) { + if (strcmp(argv[1], "--") && + strcmp(argv[1], "--end-of-options")) + /* Having a -- for "run" is mandatory */ + usage_with_options(builtin_hook_usage, run_options); + /* Add our arguments, start after -- */ + for (i = 2 ; i < argc; i++) + strvec_push(&opt.args, argv[i]); + } + + /* Need to take into account core.hooksPath */ + git_config(git_default_config, NULL); + + /* + * We are not using run_hooks() because we'd like to detect + * missing hooks. Let's find it ourselves and call + * run_found_hooks() instead. + */ + hook_name = argv[0]; + hook_path = find_hook(hook_name); + if (!hook_path) { + error("cannot find a hook named %s", hook_name); + return 1; + } + rc = run_found_hooks(hook_name, hook_path, &opt); + + run_hooks_opt_clear(&opt); + + return rc; +} + +int cmd_hook(int argc, const char **argv, const char *prefix) +{ + struct option builtin_hook_options[] = { + OPT_END(), + }; + argc = parse_options(argc, argv, NULL, builtin_hook_options, + builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION); + if (!argc) + usage_with_options(builtin_hook_usage, builtin_hook_options); + + if (!strcmp(argv[0], "run")) + return run(argc, argv, prefix); + else + usage_with_options(builtin_hook_usage, builtin_hook_options); +} diff --git a/command-list.txt b/command-list.txt index a289f09ed6..9ccd8e5aeb 100644 --- a/command-list.txt +++ b/command-list.txt @@ -103,6 +103,7 @@ git-grep mainporcelain info git-gui mainporcelain git-hash-object plumbingmanipulators git-help ancillaryinterrogators complete +git-hook mainporcelain git-http-backend synchingrepositories git-http-fetch synchelpers git-http-push synchelpers diff --git a/git.c b/git.c index 18bed9a996..540909c391 100644 --- a/git.c +++ b/git.c @@ -538,6 +538,7 @@ static struct cmd_struct commands[] = { { "grep", cmd_grep, RUN_SETUP_GENTLY }, { "hash-object", cmd_hash_object }, { "help", cmd_help }, + { "hook", cmd_hook, RUN_SETUP }, { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, { "init", cmd_init_db }, { "init-db", cmd_init_db }, diff --git a/hook.c b/hook.c index 1f1db1ec9b..4d87e98255 100644 --- a/hook.c +++ b/hook.c @@ -2,11 +2,14 @@ #include "hook.h" #include "run-command.h" #include "hook-list.h" +#include "config.h" static int known_hook(const char *name) { const char **p; size_t len = strlen(name); + static int test_hooks_ok = -1; + for (p = hook_name_list; *p; p++) { const char *hook = *p; @@ -14,6 +17,14 @@ static int known_hook(const char *name) return 1; } + if (test_hooks_ok == -1) + test_hooks_ok = git_env_bool("GIT_TEST_FAKE_HOOKS", 0); + + if (test_hooks_ok && + (!strcmp(name, "test-hook") || + !strcmp(name, "does-not-exist"))) + return 1; + return 0; } @@ -59,3 +70,111 @@ int hook_exists(const char *name) { return !!find_hook(name); } + +void run_hooks_opt_clear(struct run_hooks_opt *o) +{ + strvec_clear(&o->env); + strvec_clear(&o->args); +} + +static int pick_next_hook(struct child_process *cp, + struct strbuf *out, + void *pp_cb, + void **pp_task_cb) +{ + struct hook_cb_data *hook_cb = pp_cb; + struct hook *run_me = hook_cb->run_me; + + cp->no_stdin = 1; + cp->env = hook_cb->options->env.v; + cp->stdout_to_stderr = 1; + cp->trace2_hook_name = hook_cb->hook_name; + + /* add command */ + strvec_push(&cp->args, run_me->hook_path); + + /* + * add passed-in argv, without expanding - let the user get back + * exactly what they put in + */ + strvec_pushv(&cp->args, hook_cb->options->args.v); + + /* Provide context for errors if necessary */ + *pp_task_cb = run_me; + + return 1; +} + +static int notify_start_failure(struct strbuf *out, + void *pp_cb, + void *pp_task_cp) +{ + struct hook_cb_data *hook_cb = pp_cb; + struct hook *attempted = pp_task_cp; + + hook_cb->rc |= 1; + + strbuf_addf(out, _("Couldn't start hook '%s'\n"), + attempted->hook_path); + + return 1; +} + +static int notify_hook_finished(int result, + struct strbuf *out, + void *pp_cb, + void *pp_task_cb) +{ + struct hook_cb_data *hook_cb = pp_cb; + + hook_cb->rc |= result; + + return 1; +} + +int run_found_hooks(const char *hook_name, const char *hook_path, + struct run_hooks_opt *options) +{ + struct hook my_hook = { + .hook_path = hook_path, + }; + struct hook_cb_data cb_data = { + .rc = 0, + .hook_name = hook_name, + .options = options, + }; + cb_data.run_me = &my_hook; + + if (options->jobs != 1) + BUG("we do not handle %d or any other != 1 job number yet", options->jobs); + + run_processes_parallel_tr2(options->jobs, + pick_next_hook, + notify_start_failure, + notify_hook_finished, + &cb_data, + "hook", + hook_name); + + return cb_data.rc; +} + +int run_hooks(const char *hook_name, struct run_hooks_opt *options) +{ + const char *hook_path; + int ret; + if (!options) + BUG("a struct run_hooks_opt must be provided to run_hooks"); + + hook_path = find_hook(hook_name); + + /* + * If you need to act on a missing hook, use run_found_hooks() + * instead + */ + if (!hook_path) + return 0; + + ret = run_found_hooks(hook_name, hook_path, options); + return ret; +} diff --git a/hook.h b/hook.h index 4c547ac15e..dcc2cc2112 100644 --- a/hook.h +++ b/hook.h @@ -1,5 +1,8 @@ #ifndef HOOK_H #define HOOK_H +#include "strbuf.h" +#include "strvec.h" +#include "run-command.h" /* * Returns the path to the hook file, or NULL if the hook is missing @@ -13,4 +16,54 @@ const char *find_hook(const char *name); */ int hook_exists(const char *hookname); +struct hook { + /* The path to the hook */ + const char *hook_path; +}; + +struct run_hooks_opt +{ + /* Environment vars to be set for each hook */ + struct strvec env; + + /* Args to be passed to each hook */ + struct strvec args; + + /* + * Number of threads to parallelize across, currently a stub, + * we use the parallel API for future-proofing, but we always + * have one hook of a given name, so this is always an + * implicit 1 for now. + */ + int jobs; +}; + +#define RUN_HOOKS_OPT_INIT { \ + .jobs = 1, \ + .env = STRVEC_INIT, \ + .args = STRVEC_INIT, \ +} + +struct hook_cb_data { + /* rc reflects the cumulative failure state */ + int rc; + const char *hook_name; + struct hook *run_me; + struct run_hooks_opt *options; +}; + +void run_hooks_opt_clear(struct run_hooks_opt *o); + +/* + * Calls find_hook(hookname) and runs the hooks (if any) with + * run_found_hooks(). + */ +int run_hooks(const char *hook_name, struct run_hooks_opt *options); + +/* + * Takes an already resolved hook and runs it. Internally the simpler + * run_hooks() will call this. + */ +int run_found_hooks(const char *hookname, const char *hook_path, + struct run_hooks_opt *options); #endif diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh new file mode 100755 index 0000000000..ecd517b162 --- /dev/null +++ b/t/t1800-hook.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +test_description='git-hook command' + +. ./test-lib.sh + +test_expect_success 'git hook usage' ' + test_expect_code 129 git hook && + test_expect_code 129 git hook -h && + test_expect_code 129 git hook run -h +' + +test_expect_success 'setup GIT_TEST_FAKE_HOOKS=true to permit "test-hook" and "does-not-exist" names"' ' + GIT_TEST_FAKE_HOOKS=true && + export GIT_TEST_FAKE_HOOKS +' + +test_expect_success 'git hook run: nonexistent hook' ' + cat >stderr.expect <<-\EOF && + error: cannot find a hook named test-hook + EOF + test_expect_code 1 git hook run test-hook 2>stderr.actual && + test_cmp stderr.expect stderr.actual +' + +test_expect_success 'git hook run: basic' ' + write_script .git/hooks/test-hook <<-EOF && + echo Test hook + EOF + + cat >expect <<-\EOF && + Test hook + EOF + git hook run test-hook 2>actual && + test_cmp expect actual +' + +test_expect_success 'git hook run: stdout and stderr both write to our stderr' ' + write_script .git/hooks/test-hook <<-EOF && + echo >&1 Will end up on stderr + echo >&2 Will end up on stderr + EOF + + cat >stderr.expect <<-\EOF && + Will end up on stderr + Will end up on stderr + EOF + git hook run test-hook >stdout.actual 2>stderr.actual && + test_cmp stderr.expect stderr.actual && + test_must_be_empty stdout.actual +' + +test_expect_success 'git hook run: exit codes are passed along' ' + write_script .git/hooks/test-hook <<-EOF && + exit 1 + EOF + + test_expect_code 1 git hook run test-hook && + + write_script .git/hooks/test-hook <<-EOF && + exit 2 + EOF + + test_expect_code 2 git hook run test-hook && + + write_script .git/hooks/test-hook <<-EOF && + exit 128 + EOF + + test_expect_code 128 git hook run test-hook && + + write_script .git/hooks/test-hook <<-EOF && + exit 129 + EOF + + test_expect_code 129 git hook run test-hook +' + +test_expect_success 'git hook run arg u ments without -- is not allowed' ' + test_expect_code 129 git hook run test-hook arg u ments +' + +test_expect_success 'git hook run -- pass arguments' ' + write_script .git/hooks/test-hook <<-\EOF && + echo $1 + echo $2 + EOF + + cat >expect <<-EOF && + arg + u ments + EOF + + git hook run test-hook -- arg "u ments" 2>actual && + test_cmp expect actual +' + +test_expect_success 'git hook run -- out-of-repo runs excluded' ' + write_script .git/hooks/test-hook <<-EOF && + echo Test hook + EOF + + nongit test_must_fail git hook run test-hook +' + +test_expect_success 'git -c core.hooksPath= hook run' ' + mkdir my-hooks && + write_script my-hooks/test-hook <<-\EOF && + echo Hook ran $1 >>actual + EOF + + cat >expect <<-\EOF && + Test hook + Hook ran one + Hook ran two + Hook ran three + Hook ran four + EOF + + # Test various ways of specifying the path. See also + # t1350-config-hooks-path.sh + >actual && + git hook run test-hook -- ignored 2>>actual && + git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual && + git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual && + git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual && + git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual && + test_cmp expect actual +' + +test_done From patchwork Thu Jun 17 10:22:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327331 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 5D472C2B9F4 for ; Thu, 17 Jun 2021 10:23:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 37B8A61263 for ; Thu, 17 Jun 2021 10:23:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232137AbhFQKZj (ORCPT ); Thu, 17 Jun 2021 06:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231840AbhFQKZ2 (ORCPT ); Thu, 17 Jun 2021 06:25:28 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E66C9C061574 for ; Thu, 17 Jun 2021 03:23:11 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id t3so3247023edc.7 for ; Thu, 17 Jun 2021 03:23:11 -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=MHlwt0RA73zXBfMOn4s+GaK7BhhC8YxCI9VDEXg5Et8=; b=l40UAvggmyawyZAQbYftxTONb/jXfW+F0z19cImtzdaOjjI+pWra8h0cFtPiGYQ6zP ZTED1exg4fAmHAB0ZOoELYRzpd++O8M2Z5ftT2LFL5QOwSUOX9u1Cs7m/2Gd+T/Oh+2V vM0i0vqEMovlApbuGZeNYBJwE91M+MN9pMVOLux97uwkp0cKTDkX40Sluj7MYle7uFuQ MvXWUq7RHmeV4RfWNTP3bSzb9rG9n0l/A+A3hft/ea6Q1rtcLhadN7xbE9i3ULx89/ZP tCEh0Knf3+0qQLchaR2DJd3Fxz92MCpQoh+wnd337QG93eAUzr+r8BtEg5WG5A7mUwgz K9BA== 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=MHlwt0RA73zXBfMOn4s+GaK7BhhC8YxCI9VDEXg5Et8=; b=km1LixhJhUtjsD8QmBj7XyyZqJqNEovY7Tmvy7BjI/2yrrPWuNGzvP+w4Es6fKl3O8 d73vronJys322BJQbxLAG0z5dzhboK2D7ZPuue8L4Gezq/lzqS5dF3i9K/07ehp9yn9G G19isKQFP9lbCzquy+xpGoWiOnS8mUBlgD76ImnoSw46WdVRYpFDEXwc43b4gvoFgH9Q 0ewsVBUsJeb+8DHgTfJwfu9tTR69x1nxP0MOXG70e+rd1uvZnZ+N1EM8JYnw5fEKyiR9 Q93WdgsbuvKHXuXMQSnl8DiZycia2jvY826T3+aCjuF3Z/hJwhtagbBxmwy8PUNm5jMh 54qQ== X-Gm-Message-State: AOAM532iqkFXEHaLd1NUC1niahXs/DS5KY9XqJCbH8BPd2jAorlYhli6 Q2h2OCokMx2bKKxF8COEqK6Rps5QVIfafw== X-Google-Smtp-Source: ABdhPJw3dqnSeQ6JVA3CDwll85cdcwPdLf98bz4ibbrwu0DZwTusoXGhxehTIZ5M6b5h/qZE4oBNhA== X-Received: by 2002:a05:6402:344:: with SMTP id r4mr5428497edw.226.1623925390348; Thu, 17 Jun 2021 03:23:10 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:09 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 02/27] gc: use hook library for pre-auto-gc hook Date: Thu, 17 Jun 2021 12:22:36 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Using the hook.h library instead of the run-command.h library to run pre-auto-gc means that those hooks can be set up in config files, as well as in the hookdir. pre-auto-gc is called only from builtin/gc.c. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/gc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/builtin/gc.c b/builtin/gc.c index f05d2f0a1a..a12641a691 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -32,6 +32,7 @@ #include "remote.h" #include "object-store.h" #include "exec-cmd.h" +#include "hook.h" #define FAILED_RUN "failed to run %s" @@ -348,6 +349,8 @@ static void add_repack_incremental_option(void) static int need_to_gc(void) { + struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; + /* * Setting gc.auto to 0 or negative can disable the * automatic gc. @@ -394,8 +397,11 @@ static int need_to_gc(void) else return 0; - if (run_hook_le(NULL, "pre-auto-gc", NULL)) + if (run_hooks("pre-auto-gc", &hook_opt)) { + run_hooks_opt_clear(&hook_opt); return 0; + } + run_hooks_opt_clear(&hook_opt); return 1; } From patchwork Thu Jun 17 10:22:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327333 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 83F47C2B9F4 for ; Thu, 17 Jun 2021 10:23:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 669B961263 for ; Thu, 17 Jun 2021 10:23:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232178AbhFQKZk (ORCPT ); Thu, 17 Jun 2021 06:25:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231861AbhFQKZ2 (ORCPT ); Thu, 17 Jun 2021 06:25:28 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8653C06175F for ; Thu, 17 Jun 2021 03:23:12 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id d7so3339178edx.0 for ; Thu, 17 Jun 2021 03:23:12 -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=ikPP/KaiGSCEe/M5dnOvxYdnI0jeLBzD0GOE9+f1t7o=; b=IUlHzvzMCqN8q2rlvaZhkNEqBXa4wirlABu3RlpmHMElp8Gr8yoO8B1R8NXq3KWlNB 13yEIFNMEp6Entzf+6K5JcbFdJuk52GKDTf5jx8IHWkNQ1WxRXvc+VXt7vlRt60MLH7T 7Qx1yO/d7mNosT5w8r1JP1uTwm2Jnjk7gqCXm62yQqz74p7Pzb71Hvgri2w10qMKpUQZ bOn0RxKvFDjZX/8SZabGB09V6dHXSZQwlK6aUDxHHs3IlfdWKu1w0vNn5/3PyGlyiLd1 cjPrfI5YIVc1rnG56/KVkGaQgoDy0+CKZq88uljRzRXTsP+aKG9uJSxeHDl8qUCaRWtG G1+Q== 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=ikPP/KaiGSCEe/M5dnOvxYdnI0jeLBzD0GOE9+f1t7o=; b=I/lmFMACupYwNWiwSmZMwVEX4XFyQCqqg0VP65bnfWm3p6fmhNC0Iuyz4010zVahTP /YbhanMZ2XW03pZLyUdfEE4s4N1KEGRKsVTeZjN86lK0LvG1Z6hGKCXpUJFXyb5THvRG ag8MVrQp5klqaQEzsQCfZB2ZowwTd0qG8dOPGE+DT6RZjVB6JVMcK8QSX7ezLkGcLZyf K94uFXPIN1fiT+fDmwj8x6Bp1pKX/vzuHJkgicUJvUtvg4x4ST5z//ZJrmVyTdG9UzJ8 icrZMS11KZBofZnqw73VNBFKnMYL+yravxZI9X7jWjhJZeKaFADyN5SpOmdBTfMLK2vj IoXA== X-Gm-Message-State: AOAM532pAKu08mzTI5bjsyevEWVPl2d6mYLK61fVZ7tjmdeMntpDWhXb MGBU69xIlgy2u66PR5fmKLqZER34PNrQwA== X-Google-Smtp-Source: ABdhPJzVzYnfk7bIfpuxHpjGE3a2S6JhoylR5+50RIkEPNOKIzq5ubPGOgZzpA4uiCEPdk3xqNDQZQ== X-Received: by 2002:aa7:d713:: with SMTP id t19mr5381060edq.144.1623925391235; Thu, 17 Jun 2021 03:23:11 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:10 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 03/27] rebase: teach pre-rebase to use hook.h Date: Thu, 17 Jun 2021 12:22:37 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Move the pre-rebase hook away from run-command.h to and over to the new hook.h library. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/rebase.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 12f093121d..2081f6fa8d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -28,6 +28,7 @@ #include "sequencer.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define DEFAULT_REFLOG_ACTION "rebase" @@ -1313,6 +1314,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) char *squash_onto_name = NULL; int reschedule_failed_exec = -1; int allow_preemptive_ff = 1; + struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"), @@ -2022,10 +2024,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } /* If a hook exists, give it a chance to interrupt*/ + strvec_pushl(&hook_opt.args, options.upstream_arg, argc ? argv[0] : NULL, NULL); if (!ok_to_skip_pre_rebase && - run_hook_le(NULL, "pre-rebase", options.upstream_arg, - argc ? argv[0] : NULL, NULL)) + run_hooks("pre-rebase", &hook_opt)) { + run_hooks_opt_clear(&hook_opt); die(_("The pre-rebase hook refused to rebase.")); + } + run_hooks_opt_clear(&hook_opt); if (options.flags & REBASE_DIFFSTAT) { struct diff_options opts; From patchwork Thu Jun 17 10:22:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327327 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 0584BC48BE5 for ; Thu, 17 Jun 2021 10:23:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CEDC561166 for ; Thu, 17 Jun 2021 10:23:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231949AbhFQKZa (ORCPT ); Thu, 17 Jun 2021 06:25:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231273AbhFQKZ1 (ORCPT ); Thu, 17 Jun 2021 06:25:27 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B941C061760 for ; Thu, 17 Jun 2021 03:23:13 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id z12so3300238edc.1 for ; Thu, 17 Jun 2021 03:23:13 -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=rFIzz+YV9rwqDHJ90Swqsb73lFK2nN/fXrEnUPBnKyg=; b=uqE+cVBkqGxMTCPFbaht4lD7cfdW95lyYFnK05ldiBolyJYpY6P32R2O8NnZZ3MNWU WnBBtVL8AsBJ/75AnO3P48sGEL/3qWIyqyPn052viWWAjEQa172njhZjKN2UUbID4QrA 8TyLm0yygiIni8cbqYYroCf65mfyRm8CD93s/aZ74k8XLpnBDX7j1vpRXsG1+biejynQ 5rSDoboJ1NBSazz/zNqD1BlU5Nf+fxA0SQGykydfH0G+J/lzVC8/UaO650yfKR5qKelo EAVEjLTVnthiAHYcJeP48VYmvy4Ji63USRaMV7gpvHnrMaksxONkcJwcPiueM33lKt6+ c2cA== 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=rFIzz+YV9rwqDHJ90Swqsb73lFK2nN/fXrEnUPBnKyg=; b=ROWNxaTvoCY+LmB0q+CLToFXFVm7CDJ4dprMurmGzS5e3QrilyEgiLOPCtqfxZhF85 fT1aFQHW4NeKFbZ1I0enQ97yEvf8QkzzNBZ4lhhHqcmJH5Atl6LUk8mBoBdXs+NRUl99 rMNGPtp5iWj0guG+Oxrcs3Uog6e/0Ovfb/AEGQUCnltMxrnZTRbydvVEiUlnzELfxzXX ohHv1NIyJb4EpcgXew+6o9kzwwlJN7u6jfmGzqpqvQPuNwmw19KSARAwiYsQRfleNqlv XGjrwKPKcWYhxsMccrelBwUrbzLhXGCXs7WwdViVjTQGEX8usLuxpRYSN4U+3TwnXngg tQvg== X-Gm-Message-State: AOAM532JJwJUwJH0cBIb0kvPyIghb+uwxrE7zqpatNBtrajiIJeeDftO cpeiCCUVyoADF13uyLupsLPzz4S9SBXvyA== X-Google-Smtp-Source: ABdhPJzgm3vQbrxEovXpwGXXRUCRk+5dU+Z7nFiyec2dG15K4kxuGsGM8/PZN0eLlT7TaQgbXscOZA== X-Received: by 2002:a05:6402:c92:: with SMTP id cm18mr5495223edb.29.1623925392054; Thu, 17 Jun 2021 03:23:12 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:11 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 04/27] am: convert applypatch hooks to use config Date: Thu, 17 Jun 2021 12:22:38 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Teach pre-applypatch, post-applypatch, and applypatch-msg to use the hook.h library instead of the run-command.h library. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/am.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 1c8a548903..9e9c1b5e9f 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -445,9 +445,12 @@ static void am_destroy(const struct am_state *state) static int run_applypatch_msg_hook(struct am_state *state) { int ret; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; assert(state->msg); - ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL); + strvec_push(&opt.args, am_path(state, "final-commit")); + ret = run_hooks("applypatch-msg", &opt); + run_hooks_opt_clear(&opt); if (!ret) { FREE_AND_NULL(state->msg); @@ -1607,9 +1610,13 @@ static void do_commit(const struct am_state *state) struct commit_list *parents = NULL; const char *reflog_msg, *author, *committer = NULL; struct strbuf sb = STRBUF_INIT; + struct run_hooks_opt hook_opt_pre = RUN_HOOKS_OPT_INIT; + struct run_hooks_opt hook_opt_post = RUN_HOOKS_OPT_INIT; - if (run_hook_le(NULL, "pre-applypatch", NULL)) + if (run_hooks("pre-applypatch", &hook_opt_pre)) { + run_hooks_opt_clear(&hook_opt_pre); exit(1); + } if (write_cache_as_tree(&tree, 0, NULL)) die(_("git write-tree failed to write a tree")); @@ -1660,8 +1667,10 @@ static void do_commit(const struct am_state *state) fclose(fp); } - run_hook_le(NULL, "post-applypatch", NULL); + run_hooks("post-applypatch", &hook_opt_post); + run_hooks_opt_clear(&hook_opt_pre); + run_hooks_opt_clear(&hook_opt_post); strbuf_release(&sb); } From patchwork Thu Jun 17 10:22:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327335 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 C5AAAC2B9F4 for ; Thu, 17 Jun 2021 10:23:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A7BA861245 for ; Thu, 17 Jun 2021 10:23:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232182AbhFQKZp (ORCPT ); Thu, 17 Jun 2021 06:25:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231882AbhFQKZ2 (ORCPT ); Thu, 17 Jun 2021 06:25:28 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BFB2C061767 for ; Thu, 17 Jun 2021 03:23:14 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id nb6so8906655ejc.10 for ; Thu, 17 Jun 2021 03:23:14 -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=BMzAH59OIj9FTzeiEnLJdGcEqzfCjHVnSrjGAjZbwB8=; b=McwNcQ7hY90XmfFWK4SavgJ0jgTjp7qNL9BeZOUFW6pHfww2wsH6cIICFkI8KkDwfL C20+L3Hw1ymeYykkpuMprngelamHLTL6h3GeEdyw9p2lkixbYQx73oFXzBkkZrvANaFG ZW2diHRNnMGPwWeXr2eG7j5fs3Qh8KRcIdjec7YrCw1rl+XTn5IIQn6J8fWmnHBdceNB fkEhrSxf/qZ/3va/i2hGfvxhvnLfRWLdqmxv+NN/5w6n0JSoGfFrbAWABAkK+j0bbVSR 8k1D6TwPJGUBmlEYpLB4nI/dmBU2b2JLfjYZgW5s9YqIjttw3Fd2Fr49SE4+Qz8mM2OS sB+Q== 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=BMzAH59OIj9FTzeiEnLJdGcEqzfCjHVnSrjGAjZbwB8=; b=Yjp9Sut8Ro8z7MdeWPoh1/ZBgjI8ILRz7foHBT2slzgqzOgn4/tyYzxH3S6HWAkKS6 xVXGWZJFiL1BlUwLfMiMXaFJXj0SYJx51WxMYTg6CxQ/ZvShv2WTA9ab+n2ZYMlXctJp u1+lmzIcVLhcHlSgA8IaoRiRG4cUUnQIz+oPaR5zt9c7UcWy3ztaCYjgVne/LgLzJnkC jKFfF4GvwctgEQ0HNQYwJVppD+pLGEJPOh2unLGdVfuUhH6POnA4M/nR5aQWGIkjqYLO s2kUGQvzzoOgfDFqpHZ4sDzV+PEKtjvF3BJO0mhfRwt83WeV7hmw3HgmXwPcm0JRbqKu bmgA== X-Gm-Message-State: AOAM531Xwqsh9VIqQl2uhRmefbzJQv4pcmbr2CeCtTCfcR+KDtmOeoYh NokiSbY+5SFJWK5rbfdLpiZbEyktLOIpqw== X-Google-Smtp-Source: ABdhPJwCNAL/HhWibIa31cBEBi8Mpp8Rnv5fHomOOjMDOV2A+L0ls4wI7k79aj///njTeMvbs8q0tA== X-Received: by 2002:a17:906:2a8e:: with SMTP id l14mr4331305eje.549.1623925392902; Thu, 17 Jun 2021 03:23:12 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:12 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 05/27] hooks: convert 'post-checkout' hook to hook library Date: Thu, 17 Jun 2021 12:22:39 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Move the running of the 'post-checkout' hook away from run-command.h to the new hook.h library. For "worktree" this requires a change to it to run the hooks from a given directory. We could strictly speaking skip the "absolute_path" flag and just check if "dir" is specified, but let's split them up for clarity, as well as for any future user who'd like to set "dir" but not implicitly change the argument to an absolute path. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/checkout.c | 17 ++++++++++++----- builtin/clone.c | 7 +++++-- builtin/worktree.c | 30 ++++++++++++++---------------- hook.c | 8 ++++++++ hook.h | 9 +++++++++ read-cache.c | 1 + reset.c | 15 +++++++++++---- 7 files changed, 60 insertions(+), 27 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index f4cd7747d3..6205ace09f 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -9,6 +9,7 @@ #include "config.h" #include "diff.h" #include "dir.h" +#include "hook.h" #include "ll-merge.h" #include "lockfile.h" #include "merge-recursive.h" @@ -106,13 +107,19 @@ struct branch_info { static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, int changed) { - return run_hook_le(NULL, "post-checkout", - oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()), - oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()), - changed ? "1" : "0", NULL); + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + int rc; + /* "new_commit" can be NULL when checking out from the index before a commit exists. */ - + strvec_pushl(&opt.args, + oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()), + oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()), + changed ? "1" : "0", + NULL); + rc = run_hooks("post-checkout", &opt); + run_hooks_opt_clear(&opt); + return rc; } static int update_some(const struct object_id *oid, struct strbuf *base, diff --git a/builtin/clone.c b/builtin/clone.c index 66fe66679c..de57a3119b 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -32,6 +32,7 @@ #include "connected.h" #include "packfile.h" #include "list-objects-filter-options.h" +#include "hook.h" /* * Overall FIXMEs: @@ -775,6 +776,7 @@ static int checkout(int submodule_progress) struct tree *tree; struct tree_desc t; int err = 0; + struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; if (option_no_checkout) return 0; @@ -820,8 +822,9 @@ static int checkout(int submodule_progress) if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) die(_("unable to write new index file")); - err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()), - oid_to_hex(&oid), "1", NULL); + strvec_pushl(&hook_opt.args, oid_to_hex(null_oid()), oid_to_hex(&oid), "1", NULL); + err |= run_hooks("post-checkout", &hook_opt); + run_hooks_opt_clear(&hook_opt); if (!err && (option_recurse_submodules.nr > 0)) { struct strvec args = STRVEC_INIT; diff --git a/builtin/worktree.c b/builtin/worktree.c index b1350640fe..2ad26a76f4 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -382,22 +382,20 @@ static int add_worktree(const char *path, const char *refname, * is_junk is cleared, but do return appropriate code when hook fails. */ if (!ret && opts->checkout) { - const char *hook = find_hook("post-checkout"); - if (hook) { - const char *env[] = { "GIT_DIR", "GIT_WORK_TREE", NULL }; - cp.git_cmd = 0; - cp.no_stdin = 1; - cp.stdout_to_stderr = 1; - cp.dir = path; - cp.env = env; - cp.argv = NULL; - cp.trace2_hook_name = "post-checkout"; - strvec_pushl(&cp.args, absolute_path(hook), - oid_to_hex(null_oid()), - oid_to_hex(&commit->object.oid), - "1", NULL); - ret = run_command(&cp); - } + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + + strvec_pushl(&opt.env, "GIT_DIR", "GIT_WORK_TREE", NULL); + strvec_pushl(&opt.args, + oid_to_hex(null_oid()), + oid_to_hex(&commit->object.oid), + "1", + NULL); + opt.dir = path; + opt.absolute_path = 1; + + ret = run_hooks("post-checkout", &opt); + + run_hooks_opt_clear(&opt); } strvec_clear(&child_env); diff --git a/hook.c b/hook.c index 4d87e98255..cb39404ac8 100644 --- a/hook.c +++ b/hook.c @@ -89,6 +89,7 @@ static int pick_next_hook(struct child_process *cp, cp->env = hook_cb->options->env.v; cp->stdout_to_stderr = 1; cp->trace2_hook_name = hook_cb->hook_name; + cp->dir = hook_cb->options->dir; /* add command */ strvec_push(&cp->args, run_me->hook_path); @@ -135,6 +136,7 @@ static int notify_hook_finished(int result, int run_found_hooks(const char *hook_name, const char *hook_path, struct run_hooks_opt *options) { + struct strbuf abs_path = STRBUF_INIT; struct hook my_hook = { .hook_path = hook_path, }; @@ -143,6 +145,10 @@ int run_found_hooks(const char *hook_name, const char *hook_path, .hook_name = hook_name, .options = options, }; + if (options->absolute_path) { + strbuf_add_absolute_path(&abs_path, hook_path); + my_hook.hook_path = abs_path.buf; + } cb_data.run_me = &my_hook; if (options->jobs != 1) @@ -155,6 +161,8 @@ int run_found_hooks(const char *hook_name, const char *hook_path, &cb_data, "hook", hook_name); + if (options->absolute_path) + strbuf_release(&abs_path); return cb_data.rc; } diff --git a/hook.h b/hook.h index dcc2cc2112..8bd0fc8d1f 100644 --- a/hook.h +++ b/hook.h @@ -36,6 +36,15 @@ struct run_hooks_opt * implicit 1 for now. */ int jobs; + + /* Resolve and run the "absolute_path(hook)" instead of + * "hook". Used for "git worktree" hooks + */ + int absolute_path; + + /* Path to initial working directory for subprocess */ + const char *dir; + }; #define RUN_HOOKS_OPT_INIT { \ diff --git a/read-cache.c b/read-cache.c index 77961a3885..af5b97104c 100644 --- a/read-cache.c +++ b/read-cache.c @@ -27,6 +27,7 @@ #include "progress.h" #include "sparse-index.h" #include "csum-file.h" +#include "hook.h" /* Mask for the name length in ce_flags in the on-disk index */ diff --git a/reset.c b/reset.c index 4bea758053..e6af33b901 100644 --- a/reset.c +++ b/reset.c @@ -7,6 +7,7 @@ #include "tree-walk.h" #include "tree.h" #include "unpack-trees.h" +#include "hook.h" int reset_head(struct repository *r, struct object_id *oid, const char *action, const char *switch_to_branch, unsigned flags, @@ -126,10 +127,16 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, ret = create_symref("HEAD", switch_to_branch, reflog_head); } - if (run_hook) - run_hook_le(NULL, "post-checkout", - oid_to_hex(orig ? orig : null_oid()), - oid_to_hex(oid), "1", NULL); + if (run_hook) { + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + strvec_pushl(&opt.args, + oid_to_hex(orig ? orig : null_oid()), + oid_to_hex(oid), + "1", + NULL); + run_hooks("post-checkout", &opt); + run_hooks_opt_clear(&opt); + } leave_reset_head: strbuf_release(&msg); From patchwork Thu Jun 17 10:22:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327329 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 1569DC49361 for ; Thu, 17 Jun 2021 10:23:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E3BD361263 for ; Thu, 17 Jun 2021 10:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231959AbhFQKZb (ORCPT ); Thu, 17 Jun 2021 06:25:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231791AbhFQKZ2 (ORCPT ); Thu, 17 Jun 2021 06:25:28 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 766A6C061768 for ; Thu, 17 Jun 2021 03:23:15 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id d7so3339328edx.0 for ; Thu, 17 Jun 2021 03:23:15 -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=CEFqbkcyoN1dYwRjQG/l3c7WOwsfBIe/4hyHq1K4CQA=; b=EpIM9utIc3onG4Pxfxtrg2x+aJCR6OUDtldRPcljvN4XNOTUO+OsIpzLOHb9ucNk17 kp2o5oDsEGS+y/2D/LbeL1UzkuaTDVFC+HtJsun8fMUfqzDwDxlIPRY/fZT2zEoAAbWW VDSo8dayOTiW0MDBIHAtsLlZa+U4Tn8N2Phk2CEaP6x6DQ+BsrcQimXbClkaWABY/co0 o5CQgEHDMH8AXFZH7bnI0Knz401cgUIxOjMJXV/GrRFbv9X6Nex0TcYSsfwK1dj8eCCy VEA1E7ixTseII5kxjVQ0nM+QDuM+jkjqfLVOJYDeVY75Yi49yPPBQwVbis43xjxeRPfU y1YA== 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=CEFqbkcyoN1dYwRjQG/l3c7WOwsfBIe/4hyHq1K4CQA=; b=Wje90wXql/JT5GihmwmZJPfXH7BAIIGSOgGFzbePfMdO0sAXDq6HH58mzpLLPWD418 f+W6WE1foG917Bqvf5MNNw5JGN2GVoNW/V7Tyeh7fxGIPltm9OWdmvsILMSguaBfY/z6 nt+1nODqMBI4J5YfNz5ubYpIVsxGcKMcUyhIfwmOcB1UH4NtNa167Y3mvoD6AaKnSg0V BQD7VhK/pvWxbzjdBghfXn4EbbKOusBdkvXl4I7nS0g4OI5WXQCLzCJ/3xifxwZLJ/GS 9PA2rikj9gzkUuiSGlfALgvK8+cIruJAXWIoA5nvVuJ1sdThvIHFL9ieGR0ky2KHmjew 4sKA== X-Gm-Message-State: AOAM530+0IxwNCIVT1E+687LdQprLZRT+TRnmhycPzXFimi3Ixvemetj zOHVU11NaFt6E10YJ5GgO1q12UinVZUYsw== X-Google-Smtp-Source: ABdhPJw1ynPnx7UNw+I5gsvqBxuvt13gqeKbjEjfmvQS6plVwKO+IxA2Em7wGcqYc+O3MxdCrwmzeA== X-Received: by 2002:aa7:c845:: with SMTP id g5mr5489761edt.306.1623925393845; Thu, 17 Jun 2021 03:23:13 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:13 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 06/27] merge: use config-based hooks for post-merge hook Date: Thu, 17 Jun 2021 12:22:40 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Teach post-merge to use the hook.h library instead of the run-command.h library to run hooks. This means that post-merge hooks can come from the config as well as from the hookdir. post-merge is invoked only from builtin/merge.c. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/merge.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/builtin/merge.c b/builtin/merge.c index be98d66b0a..6128b60942 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -448,6 +448,7 @@ static void finish(struct commit *head_commit, const struct object_id *new_head, const char *msg) { struct strbuf reflog_message = STRBUF_INIT; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; const struct object_id *head = &head_commit->object.oid; if (!msg) @@ -489,7 +490,9 @@ static void finish(struct commit *head_commit, } /* Run a post-merge hook */ - run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL); + strvec_push(&opt.args, squash ? "1" : "0"); + run_hooks("post-merge", &opt); + run_hooks_opt_clear(&opt); apply_autostash(git_path_merge_autostash(the_repository)); strbuf_release(&reflog_message); @@ -849,7 +852,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) * and write it out as a tree. We must do this before we invoke * the editor and after we invoke run_status above. */ - if (find_hook("pre-merge-commit")) + if (hook_exists("pre-merge-commit")) discard_cache(); read_cache_from(index_file); strbuf_addbuf(&msg, &merge_msg); From patchwork Thu Jun 17 10:22:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327339 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 DC91AC2B9F4 for ; Thu, 17 Jun 2021 10:23:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BF20361263 for ; Thu, 17 Jun 2021 10:23:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232208AbhFQKZu (ORCPT ); Thu, 17 Jun 2021 06:25:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231926AbhFQKZa (ORCPT ); Thu, 17 Jun 2021 06:25:30 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53032C0617A8 for ; Thu, 17 Jun 2021 03:23:16 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id d7so3339385edx.0 for ; Thu, 17 Jun 2021 03:23:16 -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=Es4wf1ATb0YeoWwa+zop/EEDuFJX675pSI+Mdm7uM9A=; b=H5Nv+/x98wVtJ4dKWgoPjZ91Klh4oWr0P3g9KwdoQ8k0CyzNl40Ealh7un62GyvqRR rn8gndRnoypXRfCSC41WHRfJDXuk9x5qjAvL5E9eeBjtMbeNJHwO04qsMjYAfbH8E1KO pUqktpMugZ89UfSl9LKiVqIeO3pK7rcG+vu0ZQ1upbaP7lYX0mswNITyGiFAo6HtpQ4g l278NpHT7MtKrryCk3kH8yKsjDz6EcVptMkPTWz1fMSxJmt3rbelkMkGzi7nkQRmb4nE NX6MME6Y0un7onJHm8DJs6OSbetPxDiY2xZ9U+Q/qjyha3yWQcuR9+gRnyIsxHKFgTsI EXvQ== 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=Es4wf1ATb0YeoWwa+zop/EEDuFJX675pSI+Mdm7uM9A=; b=HXxnnu/FF2cS56kUibg0weU0/T5WM+3juTNwr+TZmD/4D7sd0waG/Pnt65/WxE+Uqo 188gJoxr2jQKCCVilQBwG4kKsfkgVl2TJK7/MGSgvER9zFbcKRttIpnD4Kz5lNU+MrXx R/Yo5xFJaPvhbuOvI0Y980vxZFhY7SqTZmBxlBtTzwMb/4rWKMVWb5eBEbLMhxOuFm7Y 9Da8kGpIGnM2rHmSzsauu0glzgPPBn1Nig3MzJJ+FHD7tLFpaSpS+aNvu+To7GJyRt0D J3gWCuvZ+xyCRxIAISa1VNN5QIsDpqvgMeWn/dVhL9IelhaZGkeHL2OmCZMB1g3GU2Wg D37w== X-Gm-Message-State: AOAM530albGPp51unax1Ja7wab/ofUthXb5sl1O9gD2XbYeWe6ehLALG 6sKp3wTQS1MEbHhj7iP6+hv1T4xMimdDbg== X-Google-Smtp-Source: ABdhPJxG9W93xkLY9Sn2KwcR6OpkVreJqpQorP7YylDJke71Rq1LlAnKDJLKUaYYcMpRSkVluB2Ivw== X-Received: by 2002:a05:6402:151:: with SMTP id s17mr5405073edu.233.1623925394692; Thu, 17 Jun 2021 03:23:14 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:14 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 07/27] git hook run: add an --ignore-missing flag Date: Thu, 17 Jun 2021 12:22:41 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org For certain one-shot hooks we'd like to optimistically run them, and not complain if they don't exist. This will be used by send-email in a subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- Documentation/git-hook.txt | 10 +++++++++- builtin/hook.c | 8 +++++++- t/t1800-hook.sh | 7 ++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt index 660d6a992a..097fb9de63 100644 --- a/Documentation/git-hook.txt +++ b/Documentation/git-hook.txt @@ -8,7 +8,7 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run [-- ] +'git hook' run [--ignore-missing] [-- ] DESCRIPTION ----------- @@ -29,6 +29,14 @@ optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The arguments (if any) differ by hook name, see linkgit:githooks[5] for what those are. +OPTIONS +------- + +--ignore-missing:: + Ignore any missing hook by quietly returning zero. Used for + tools that want to do a blind one-shot run of a hook that may + or may not be present. + SEE ALSO -------- linkgit:githooks[5] diff --git a/builtin/hook.c b/builtin/hook.c index 7714d31ef1..47e0de7bbc 100644 --- a/builtin/hook.c +++ b/builtin/hook.c @@ -22,10 +22,13 @@ static int run(int argc, const char **argv, const char *prefix) int i; struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; int rc = 0; + int ignore_missing = 0; const char *hook_name; const char *hook_path; struct option run_options[] = { + OPT_BOOL(0, "ignore-missing", &ignore_missing, + N_("exit quietly with a zero exit code if the requested hook cannot be found")), OPT_END(), }; @@ -49,11 +52,14 @@ static int run(int argc, const char **argv, const char *prefix) /* * We are not using run_hooks() because we'd like to detect * missing hooks. Let's find it ourselves and call - * run_found_hooks() instead. + * run_found_hooks() instead... */ hook_name = argv[0]; hook_path = find_hook(hook_name); if (!hook_path) { + /* ... act like run_hooks() under --ignore-missing */ + if (ignore_missing) + return 0; error("cannot find a hook named %s", hook_name); return 1; } diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index ecd517b162..542e551628 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -23,7 +23,12 @@ test_expect_success 'git hook run: nonexistent hook' ' test_cmp stderr.expect stderr.actual ' -test_expect_success 'git hook run: basic' ' +test_expect_success 'git hook run: nonexistent hook with --ignore-missing' ' + git hook run --ignore-missing does-not-exist 2>stderr.actual && + test_must_be_empty stderr.actual +' + +test_expect_success 'git hook run -- basic' ' write_script .git/hooks/test-hook <<-EOF && echo Test hook EOF From patchwork Thu Jun 17 10:22:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327341 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 C896EC49361 for ; Thu, 17 Jun 2021 10:23:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF09F61057 for ; Thu, 17 Jun 2021 10:23:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232209AbhFQKZv (ORCPT ); Thu, 17 Jun 2021 06:25:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231791AbhFQKZb (ORCPT ); Thu, 17 Jun 2021 06:25:31 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 283E9C0617AD for ; Thu, 17 Jun 2021 03:23:17 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id df12so771988edb.2 for ; Thu, 17 Jun 2021 03:23:17 -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=G8dO6wJi/v/+/qmdQwVbTmJWMQp/M50ti1LojmYp6C4=; b=ZjvUvmZyXsKl1q38AdfZU2hOLkinFCha4i94p7eAEUo6PMx0vZnGzc1tE4bSG5CC+F EZLS0U1yGcJWb4gmnI4DYkl/H9PaxCzKP27am9W+/EZwA93uZjUvin1LtJyDX/zBdvJZ ToXG9KhsY1WeJ9kki/fb68LrnPfoJJrQ49jcG5IHo442NJXtfS8pva4zFWl37srfLFQ/ 05Ikzotog/4x1sFHqNhMf2ZaKqdX2NGph+EmxPMiR29bkCdFnzPXxub+bNWlMX5vzhiT rYGRjeZ8s9f+KqAs3uoh9qpT+5HI2LB/N0a9nGUqgCmijmxImiVZ8fBBJh/jyQGKLXZ7 MzIw== 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=G8dO6wJi/v/+/qmdQwVbTmJWMQp/M50ti1LojmYp6C4=; b=cSuJ+28FXdgX5a1jTCDvpovTZrGmyYuEp5RrTkBs1vDg/Y/Wgns/Xh5UByOW6YDTgU bmi8t2vq3o2ypvhtyG8Kx7OTUw56kXXOwJp9j5uDIlVZVVPKRVCei7v1ugzibqXaQj2a jMkSRkLCWS4FLzaHmE817+g6jc9304UdRc+4bO9/JK6OJ6enEtpAKCzwfy+6dja1SJfF BClALhnxZE+o1HQFPrVZz9gKx8Kmb/vEEt931yXXEtfybCfChLkgbXFGUPyXaOEMlZuI sMrTvQQS/sT1g8KN3qlT+819crXXq238sGv7BKohiJY696jLZSHtBJNmnGGCNiQ0cuqM YbdA== X-Gm-Message-State: AOAM5307rfCUUAWh1s1tJk8p3Fwye8DrIG3q7oqKwIbF/Fr74/3xN4ey qIYzPn8NhqzZVeD0bVYICFCwkjI4N7Nf2g== X-Google-Smtp-Source: ABdhPJwXBGj2g657wdNog7OIjVsa+VS4o3x9q+Z3bG/UENny/hdP9ZkRYyQ8FbwiijZAfVpqTHacXA== X-Received: by 2002:a05:6402:1cad:: with SMTP id cz13mr5439196edb.67.1623925395504; Thu, 17 Jun 2021 03:23:15 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:15 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 08/27] send-email: use 'git hook run' for 'sendemail-validate' Date: Thu, 17 Jun 2021 12:22:42 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Change the "sendmail-validate" hook to be run via the "git hook run" wrapper instead of via a direct invocation. This is the smallest possibly change to get "send-email" using "git hook run". We still check the hook itself with "-x", and set a "GIT_DIR" variable, both of which are asserted by our tests. We'll need to get rid of this special behavior if we start running N hooks, but for now let's be as close to bug-for-bug compatible as possible. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- git-send-email.perl | 20 ++++++++++++-------- t/t9001-send-email.sh | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/git-send-email.perl b/git-send-email.perl index 7ba0b3433d..9e47430403 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -214,13 +214,13 @@ sub format_2822_time { my $editor; sub system_or_msg { - my ($args, $msg) = @_; + my ($args, $msg, $cmd_name) = @_; system(@$args); my $signalled = $? & 127; my $exit_code = $? >> 8; return unless $signalled or $exit_code; - my @sprintf_args = ($args->[0], $exit_code); + my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code); if (defined $msg) { # Quiet the 'redundant' warning category, except we # need to support down to Perl 5.8, so we can't do a @@ -1979,9 +1979,9 @@ sub validate_patch { my ($fn, $xfer_encoding) = @_; if ($repo) { + my $hook_name = 'sendemail-validate'; my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks'); - my $validate_hook = catfile($hooks_path, - 'sendemail-validate'); + my $validate_hook = catfile($hooks_path, $hook_name); my $hook_error; if (-x $validate_hook) { my $target = abs_path($fn); @@ -1990,13 +1990,17 @@ sub validate_patch { chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); local $ENV{"GIT_DIR"} = $repo->repo_path(); - $hook_error = system_or_msg([$validate_hook, $target]); + my @validate_hook = ("git", "hook", "run", "--ignore-missing", $hook_name, "--", $target); + $hook_error = system_or_msg(\@validate_hook, undef, + "git hook run $hook_name -- "); chdir($cwd_save) or die("chdir: $!"); } if ($hook_error) { - die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" . - "%s\n" . - "warning: no patches were sent\n"), $fn, $hook_error); + $hook_error = sprintf(__("fatal: %s: rejected by %s hook\n" . + $hook_error . "\n" . + "warning: no patches were sent\n"), + $fn, $hook_name); + die $hook_error; } } diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh index 30eff725a9..6d4e25df8d 100755 --- a/t/t9001-send-email.sh +++ b/t/t9001-send-email.sh @@ -539,7 +539,7 @@ test_expect_success $PREREQ "--validate respects relative core.hooksPath path" ' test_path_is_file my-hooks.ran && cat >expect <<-EOF && fatal: longline.patch: rejected by sendemail-validate hook - fatal: command '"'"'my-hooks/sendemail-validate'"'"' died with exit code 1 + fatal: command '"'"'git hook run sendemail-validate -- '"'"' died with exit code 1 warning: no patches were sent EOF test_cmp expect actual @@ -558,7 +558,7 @@ test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" ' test_path_is_file my-hooks.ran && cat >expect <<-EOF && fatal: longline.patch: rejected by sendemail-validate hook - fatal: command '"'"'$hooks_path/sendemail-validate'"'"' died with exit code 1 + fatal: command '"'"'git hook run sendemail-validate -- '"'"' died with exit code 1 warning: no patches were sent EOF test_cmp expect actual From patchwork Thu Jun 17 10:22:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327337 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 520BEC48BE5 for ; Thu, 17 Jun 2021 10:23:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E0F861245 for ; Thu, 17 Jun 2021 10:23:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232127AbhFQKZt (ORCPT ); Thu, 17 Jun 2021 06:25:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231929AbhFQKZa (ORCPT ); Thu, 17 Jun 2021 06:25:30 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EB23C0617AE for ; Thu, 17 Jun 2021 03:23:18 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id b11so3269787edy.4 for ; Thu, 17 Jun 2021 03:23:17 -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=DL6XCi/03qNcT3N6dN9SuwpfB6ZZ2iEg6jAUskaW268=; b=abJX1WGMpJHqPtxgt2y7GimiqjqeIcCW2TVelfQcfMr6EIFeiZnxkoWkEEnpE0/8xd MpU8paTVZbhJas8G6To/0hG2mCxNYfDMOvRQQvyNdSW4ERV/zJ45/WZC/L6u0f7hSiWa r1qbynCWcygRzIxaXPI+ZxIVIfhCwxbTg3y9UYaQc9wBnD9hcfTe80SVstEK//RJiu8k IY4fRNwt0jWHxtxEN/6lRdH4VevJyYt5wqvOq0jN3vAIIwVXAlK81//SnPEa6ebDcp8Z wmE0NvLyXWrLdmanAqtiD32Ha1oLmRhN1JWOLOav9FDaDZOMVxbg/KBxmSUuB7V+nDfG NGLw== 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=DL6XCi/03qNcT3N6dN9SuwpfB6ZZ2iEg6jAUskaW268=; b=dPKmq55UwecNTfxIPuJbKMb9Efe5lv19DejJ0VguZUDYqQIKAVxZyUVKys+3Zm5IFl +f6rt0W7hb0caNnj4di/cKjVVZJYU3NlHtAVlo4mXYrfnDfjel8EkOyPm3QudLWHfLcE NxL4z5hUfn70Cpz9N6PVYMqKOfH1S0eyJAMGXvxqPFY3ZMXoagO6lxGomlUs1RKUI9Ih JXCEVwh2HaS5q7od+XWcE7KtUzMBQCIHx0OqWKP8Z2CkbgkiibAcy+DqzA/TP2fKInyu wj0V1dxnb0gmRgbJS45pBokyNKMw1Psk2G/UGhau0lD0NWUjYepkvZ9IhOnzvf4P4zWg BK6w== X-Gm-Message-State: AOAM533LytHffaENy6Yvx1C9cbh0exk++q0WEFTlAm6xF+1QwVpZnfnt 2lNtjLjAgwjFz34cVfd6GpuTmq2/KPBc8w== X-Google-Smtp-Source: ABdhPJz1Ov28LCUqao69C7X3yhLGQTyXmGWLx2ef6AwlFbnDCDVwaMnjd0h5spDwhnNRMGimMNk0ow== X-Received: by 2002:a05:6402:759:: with SMTP id p25mr5579048edy.146.1623925396339; Thu, 17 Jun 2021 03:23:16 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:15 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 09/27] git-p4: use 'git hook' to run hooks Date: Thu, 17 Jun 2021 12:22:43 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Instead of duplicating the behavior of run-command.h:run_hook_le() in Python, we can directly call 'git hook run'. We emulate the existence check with the --ignore-missing flag. As this is the last hook execution in git.git to not go through "git hook run" or the hook.[ch] library we can now be absolutely sure that our assertion in hook.c that only hooks known by the generated (from githooks(5)) hook-list.h are permitted. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- git-p4.py | 72 ++++++------------------------------------------------- 1 file changed, 7 insertions(+), 65 deletions(-) diff --git a/git-p4.py b/git-p4.py index d34a1946b7..e76d8df313 100755 --- a/git-p4.py +++ b/git-p4.py @@ -207,71 +207,13 @@ def decode_path(path): return path def run_git_hook(cmd, param=[]): - """Execute a hook if the hook exists.""" - if verbose: - sys.stderr.write("Looking for hook: %s\n" % cmd) - sys.stderr.flush() - - hooks_path = gitConfig("core.hooksPath") - if len(hooks_path) <= 0: - hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks") - - if not isinstance(param, list): - param=[param] - - # resolve hook file name, OS depdenent - hook_file = os.path.join(hooks_path, cmd) - if platform.system() == 'Windows': - if not os.path.isfile(hook_file): - # look for the file with an extension - files = glob.glob(hook_file + ".*") - if not files: - return True - files.sort() - hook_file = files.pop() - while hook_file.upper().endswith(".SAMPLE"): - # The file is a sample hook. We don't want it - if len(files) > 0: - hook_file = files.pop() - else: - return True - - if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK): - return True - - return run_hook_command(hook_file, param) == 0 - -def run_hook_command(cmd, param): - """Executes a git hook command - cmd = the command line file to be executed. This can be - a file that is run by OS association. - - param = a list of parameters to pass to the cmd command - - On windows, the extension is checked to see if it should - be run with the Git for Windows Bash shell. If there - is no file extension, the file is deemed a bash shell - and will be handed off to sh.exe. Otherwise, Windows - will be called with the shell to handle the file assocation. - - For non Windows operating systems, the file is called - as an executable. - """ - cli = [cmd] + param - use_shell = False - if platform.system() == 'Windows': - (root,ext) = os.path.splitext(cmd) - if ext == "": - exe_path = os.environ.get("EXEPATH") - if exe_path is None: - exe_path = "" - else: - exe_path = os.path.join(exe_path, "bin") - cli = [os.path.join(exe_path, "SH.EXE")] + cli - else: - use_shell = True - return subprocess.call(cli, shell=use_shell) - + """args are specified with -a -a -a """ + args = ['git', 'hook', 'run', '--ignore-missing', cmd] + if param: + args.append("--") + for p in param: + args.append(p) + return subprocess.call(args) == 0 def write_pipe(c, stdin): if verbose: From patchwork Thu Jun 17 10:22:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327343 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 B98D8C2B9F4 for ; Thu, 17 Jun 2021 10:23:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 989FE61263 for ; Thu, 17 Jun 2021 10:23:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232216AbhFQKZz (ORCPT ); Thu, 17 Jun 2021 06:25:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231808AbhFQKZi (ORCPT ); Thu, 17 Jun 2021 06:25:38 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E72B7C0617AF for ; Thu, 17 Jun 2021 03:23:18 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id b11so3269842edy.4 for ; Thu, 17 Jun 2021 03:23:18 -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=TGNXQs5KcZYZ57foWIBK6vEeJYRgxZ7UI+ID4j51V8A=; b=NhfDSPKLksq9//YPPM7/hkS4yfAP1e5Wgb58QpwWAA7g1l+yaT7Y5N6FE+Dvo6eru8 LyLI+/Q7sSyq6g+6KCJE1T6y5ybqc57ldEN7ezpTHRCb/1DkxvGQPDjWNbCmiTNwFsP1 ySOiAdaEIVsV2Wk/h+PNsCEdICTgX6DWiQ6ryw22g2KmZhgA9ruwewOxM+Lt5btoBXQ5 bQG0wwbKD73yGBvCZmXSKFfC8o9jYpxy4b9SklLQG+U0crWMRuuVQLFSuV8CYLObQcfl YdhlSxj5N8OyUT5ZCq9VSU11sRLA8+qHVt3B1aOBCEaBcZVaBOZjXFDTV945x9EivZSm oeOQ== 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=TGNXQs5KcZYZ57foWIBK6vEeJYRgxZ7UI+ID4j51V8A=; b=hwHi/aPtnA6vrunuLXAKevMHzzCvsZ379B0KDlmimHKsB8LisBeLySYTPoyLjCu0pB jKZ0IMM65c8LGheCsbmYk6T8ToxdtzL2XKInl0WP+6u5pF6BLfyAklK/29fn2ui1CzDR IniZVXx1GOX+YaP4y0SU4ilzjX3uVysmfl0qpUAuxvAn+TCiwI0uWcJ5aYzq1kMGxq05 8FA3AcdvS6BCn7KG5l6R64XRcYShDNFIppbZMafmoP8q4j2EqqqqeeqSDbrmAQwgJ8iU /6gkIgtESCkpxfleuOx04w7dSFDF8JO4plr2HhvOWvw2fMiRoYKKs2wdpCW4ujhugCwm 0Ucg== X-Gm-Message-State: AOAM533kNwMELvzPJeaoCpV+0CIGeK1LzYBrR7wZqMlnNiXPLIK+n4Lx CMwpY0tzsQeeEX4mqs1lmEEcLsj67obOTw== X-Google-Smtp-Source: ABdhPJzyUZCXxkmpwzDfP2VsJPx1sHNBq2BqECtfmIMDdxj3iBRKNUwCGcQmkCjF48eL0GtVYg/CcQ== X-Received: by 2002:a05:6402:1644:: with SMTP id s4mr5502864edx.190.1623925397216; Thu, 17 Jun 2021 03:23:17 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:16 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 10/27] commit: use hook.h to execute hooks Date: Thu, 17 Jun 2021 12:22:44 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Teach run_commit_hook() to call hook.h instead of run-command.h. This covers 'pre-commit', 'commit-msg', and 'prepare-commit-msg'. Additionally, ask the hook library - not run-command - whether any hooks will be run, as it's possible hooks may exist in the config but not the hookdir. Because all but 'post-commit' hooks are expected to make some state change, force all but 'post-commit' hook to run in series. 'post-commit' "is meant primarily for notification, and cannot affect the outcome of `git commit`," so it is fine to run in parallel. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/commit.c | 2 +- commit.c | 16 ++++++++++------ sequencer.c | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index f1aafd67d4..dad4e56544 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1045,7 +1045,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, return 0; } - if (!no_verify && find_hook("pre-commit")) { + if (!no_verify && hook_exists("pre-commit")) { /* * Re-read the index as pre-commit hook could have updated it, * and write it out as a tree. We must do this before we invoke diff --git a/commit.c b/commit.c index 8ea55a447f..e8147a88fc 100644 --- a/commit.c +++ b/commit.c @@ -21,6 +21,7 @@ #include "commit-reach.h" #include "run-command.h" #include "shallow.h" +#include "hook.h" static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **); @@ -1698,22 +1699,25 @@ size_t ignore_non_trailer(const char *buf, size_t len) int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...) { - struct strvec hook_env = STRVEC_INIT; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; va_list args; + const char *arg; int ret; - - strvec_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file); + strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file); /* * Let the hook know that no editor will be launched. */ if (!editor_is_used) - strvec_push(&hook_env, "GIT_EDITOR=:"); + strvec_push(&opt.env, "GIT_EDITOR=:"); va_start(args, name); - ret = run_hook_ve(hook_env.v, name, args); + while ((arg = va_arg(args, const char *))) + strvec_push(&opt.args, arg); va_end(args); - strvec_clear(&hook_env); + + ret = run_hooks(name, &opt); + run_hooks_opt_clear(&opt); return ret; } diff --git a/sequencer.c b/sequencer.c index 3de479f90e..8f46984ffb 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1446,7 +1446,7 @@ static int try_to_commit(struct repository *r, } } - if (find_hook("prepare-commit-msg")) { + if (hook_exists("prepare-commit-msg")) { res = run_prepare_commit_msg_hook(r, msg, hook_commit); if (res) goto out; From patchwork Thu Jun 17 10:22:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327353 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 4FC55C2B9F4 for ; Thu, 17 Jun 2021 10:23:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3653F61245 for ; Thu, 17 Jun 2021 10:23:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232244AbhFQK0D (ORCPT ); Thu, 17 Jun 2021 06:26:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231840AbhFQKZj (ORCPT ); Thu, 17 Jun 2021 06:25:39 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B557FC0613A2 for ; Thu, 17 Jun 2021 03:23:19 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id w21so3272467edv.3 for ; Thu, 17 Jun 2021 03:23:19 -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=7Mc0ML94HBE2oL0F6HPGqBHkkiAAZSTF7Onuu1wSZkQ=; b=N1gpR+hR5TPNpRGuyLrk3sOAiTbQAngpLyD0OKwIY9FkakuJzw+uSunTiRytvmaTdh ks/yXzc/rIaDNcEFinSo6jScan8RjBqefVNL92OKIN7US2/H4/j6tBR11WQaGSdNb8WC aLz7161fAuwLzW4KifB8SWE42RxKgZVR5TJnBHZsRg7sfc5pzy29DIes7tyKuL3pEAVe 0mRbSGFTDLIMtsS8Gc65/Pev2fbELUG+41kZwMNnnGg38NBoInkoTMmI84C3iLvxrOZo J+rq4cEalRLpTV3zFafnw89eI5S/qcMzLdGkBS5mMuhexjKkZc2BrsYOK8vq0bI2CK78 efeA== 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=7Mc0ML94HBE2oL0F6HPGqBHkkiAAZSTF7Onuu1wSZkQ=; b=hjI34CdFOgrwJuatdbNyurVAunQDesRa1Ssn7gKHH3EkyoNkWUPqu9zcDgixPKhxED +8nRjlPYy35fe5dpuwWkdn2Iy8mJCEetbyJmiMNwwOh6X0LKa/2ALQetjE8qquq9VEWb 1M97+HHprw+gvlDYnLkYPaOoCE7uxN9Xf3LYm0J8css2Ri3MysguF4rXUi3q8advGsBi 7VmsYZJJLh9pwv7gGkaeMpcN3t5ZGuZZITL5m21YMxyRVktlhJTwbMVZuLk9+ePDPd58 yo+IHiDdkEOxA91r3a4P0I7lTDs7op1EikSgkLitM9OP1XM+6RCxJeAuNn0kIB/cicCy sIMA== X-Gm-Message-State: AOAM530vpKWRaCqh4S1B4sVPvCBA0Q0C1M1vSK5xCDbYP4GOHSafGdBa UQWSFS0RHBf/iLWndFFeMhcSXKebYihP1w== X-Google-Smtp-Source: ABdhPJyMbkoU9L8gdwXtKdmjCB+AJXYQinBQ37vKUI031WSaptX4tAbCHxToKWV0vJF03auC85kQlA== X-Received: by 2002:aa7:dc42:: with SMTP id g2mr5477828edu.362.1623925398098; Thu, 17 Jun 2021 03:23:18 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:17 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 11/27] read-cache: convert post-index-change hook to use config Date: Thu, 17 Jun 2021 12:22:45 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using hook.h instead of run-command.h to run, post-index-change hooks can now be specified in the config in addition to the hookdir. post-index-change is not run anywhere besides in read-cache.c. This removes the last direct user of run_hook_ve(), so we can make the function static now. It'll be removed entirely soon. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- read-cache.c | 11 ++++++++--- run-command.c | 2 +- run-command.h | 1 - 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/read-cache.c b/read-cache.c index af5b97104c..f801313cc9 100644 --- a/read-cache.c +++ b/read-cache.c @@ -3063,6 +3063,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l { int ret; int was_full = !istate->sparse_index; + struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; ret = convert_to_sparse(istate); @@ -3091,9 +3092,13 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l else ret = close_lock_file_gently(lock); - run_hook_le(NULL, "post-index-change", - istate->updated_workdir ? "1" : "0", - istate->updated_skipworktree ? "1" : "0", NULL); + strvec_pushl(&hook_opt.args, + istate->updated_workdir ? "1" : "0", + istate->updated_skipworktree ? "1" : "0", + NULL); + run_hooks("post-index-change", &hook_opt); + run_hooks_opt_clear(&hook_opt); + istate->updated_workdir = 0; istate->updated_skipworktree = 0; diff --git a/run-command.c b/run-command.c index 82fdf29656..eecdef5a0c 100644 --- a/run-command.c +++ b/run-command.c @@ -1321,7 +1321,7 @@ int async_with_fork(void) #endif } -int run_hook_ve(const char *const *env, const char *name, va_list args) +static int run_hook_ve(const char *const *env, const char *name, va_list args) { struct child_process hook = CHILD_PROCESS_INIT; const char *p; diff --git a/run-command.h b/run-command.h index b58531a7eb..24ab5d63c4 100644 --- a/run-command.h +++ b/run-command.h @@ -216,7 +216,6 @@ int run_command(struct child_process *); */ LAST_ARG_MUST_BE_NULL int run_hook_le(const char *const *env, const char *name, ...); -int run_hook_ve(const char *const *env, const char *name, va_list args); /* * Trigger an auto-gc From patchwork Thu Jun 17 10:22:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327345 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 AA060C48BE5 for ; Thu, 17 Jun 2021 10:23:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AD4261057 for ; Thu, 17 Jun 2021 10:23:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232225AbhFQKZ5 (ORCPT ); Thu, 17 Jun 2021 06:25:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232064AbhFQKZi (ORCPT ); Thu, 17 Jun 2021 06:25:38 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A00EAC0613A4 for ; Thu, 17 Jun 2021 03:23:20 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id nd37so9013016ejc.3 for ; Thu, 17 Jun 2021 03:23: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=0Wvheu7R7cHGNLPnvKAA3iJwQQQ144pCjkwvtfW4N7Y=; b=JD75JJUjIWibMxWC2mB7diwdvWCMRdpII130KScs3ffkkOVKj098Kcw5qjG6wK1DvF 3Ggm30jz2H9tAnlGMaQJi1O38dlvanNfBE4RTq/Bj2C21JtwE9wpiouraGqhAJCU0zzK FHqNE5O5cJIaeORvE1nRhPfg9aIGZFZGJhHqr8GexPqK3bEcZanj/IRng6FcF62wc8Qx Vc1TiBw7EX0oMmNi5mMUafZUuI6bF6s81xsu1Xtn/gVovsXKGSkEpAS3ZPEl9z0mfJSg 8rD4iBWgjef+McOqJ/+xoCIf3dXl07DPR+UU3hC7nALdXgPCgTKPVUYl0sdVtx+CRSKT 1XfQ== 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=0Wvheu7R7cHGNLPnvKAA3iJwQQQ144pCjkwvtfW4N7Y=; b=bmQqOnKoX1TPYUhZo9pnrdZ9iLkEkO+FTfODOI6Ax9U/tDdngyvhiqGAGEDDH7VPu5 DtyuHCypNxxqBLSQ71eR0hzcPIsCLflnX3wveVPIcsPFyHe3suBc3m4cu74KdBVmbnz5 S/SuW/lcGdiRy53AKqSwktSI0A/aKc9HTN0ZO1KjNWjWLq0+G5CxWu7NCYcM3r1eGJjl DeDMKox8r71vGuZuzRZTNqy5+ZyiXdiWPDi3oyNo9eKrUESL69ouUxGvXYL/wZ/lB+hj sR/paW5rsURwJ/JubBOQ/nE5RYEIha3TlrFq56tGm3RJFoA0ldmhf6S1f8tAX0zNGGh/ 7hwQ== X-Gm-Message-State: AOAM533lY0fU1jDXr+V4FcLhtM06eYKPB3mOxcwEIOppbicznZomDm1g o3/efQcO+qeJu2wz3roxRbYGAOHrr7PcOQ== X-Google-Smtp-Source: ABdhPJztzgdqGzl4jomSCPLkYDeZy/ATnIUNfipXFBtAN8OuROeAmfwpvQkK1EkfDuTD5ldNC/EQHQ== X-Received: by 2002:a17:906:b141:: with SMTP id bt1mr4476786ejb.498.1623925398942; Thu, 17 Jun 2021 03:23:18 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:18 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 12/27] receive-pack: convert push-to-checkout hook to hook.h Date: Thu, 17 Jun 2021 12:22:46 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using hook.h instead of run-command.h to invoke push-to-checkout, hooks can now be specified in the config as well as in the hookdir. push-to-checkout is not called anywhere but in builtin/receive-pack.c. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 1e0e04c62f..5248228ebf 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1436,12 +1436,18 @@ static const char *push_to_checkout(unsigned char *hash, struct strvec *env, const char *work_tree) { + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree)); - if (run_hook_le(env->v, push_to_checkout_hook, - hash_to_hex(hash), NULL)) + strvec_pushv(&opt.env, env->v); + strvec_push(&opt.args, hash_to_hex(hash)); + if (run_hooks(push_to_checkout_hook, &opt)) { + run_hooks_opt_clear(&opt); return "push-to-checkout hook declined"; - else + } else { + run_hooks_opt_clear(&opt); return NULL; + } } static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree) @@ -1465,7 +1471,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir)); - if (!find_hook(push_to_checkout_hook)) + if (!hook_exists(push_to_checkout_hook)) retval = push_to_deploy(sha1, &env, work_tree); else retval = push_to_checkout(sha1, &env, work_tree); From patchwork Thu Jun 17 10:22:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327347 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 A56DBC49EA2 for ; Thu, 17 Jun 2021 10:23:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7522C61245 for ; Thu, 17 Jun 2021 10:23:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232172AbhFQKZ5 (ORCPT ); Thu, 17 Jun 2021 06:25:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232113AbhFQKZi (ORCPT ); Thu, 17 Jun 2021 06:25:38 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 823B8C061283 for ; Thu, 17 Jun 2021 03:23:21 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id gb32so794306ejc.2 for ; Thu, 17 Jun 2021 03:23: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=wkoP/cJDwE1I+78BvbNe+HvpElmd1BUlc71OLYDtOds=; b=JiOSEYNJrtO3GFI9zwWm76Ozkq3VRaf3CV0OvQJOxaFFw1MAI7eAnjaOtvrvpYdUAi w32Tvjjg05Ai/HORmEO0M1Gok1Z/7mogWb1+s1Q66xstW96oR5b7yBxd51+AfEDhK5qs OXdDUFGEkpUNK+wyc9y+dXrX18gVllYxRkoWiTIachKlf0sljP5WE7i86OsczHGssHfu nw4QpQ0Fgk/mox2OV4tv9MTE6mvWhg7PVTgk61D3T4WbJ7S1FwKvfV4E30TJR3F+ZYmU mUx97BGm+ImbdadvUPMsPxOeWCGE2axlKOtPlLd60c1AAKT+nzoa2jiqi970VrRKFn6U V+FQ== 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=wkoP/cJDwE1I+78BvbNe+HvpElmd1BUlc71OLYDtOds=; b=VkaLPRvnVhrwYgyYC8VRoh1YcZjMWEClYH2FazZrVXXeaRhZiC7CKzdJl9TFTmluly 52880h1Q/OklMu0y/VjRmgqb9eAUV7KmaHukehySPifOQapBB9MQbULlJOOgz9+l8k0X 01IpiGPPPhytpiKcOvMpLWNBrWXc4v7kn2k2sAhRo54twEMu1QwJiNd7t2c0+3qlGPXG RQbf14Yd4eF9J/GEsT/KPq4YrJrcrvaT8jDpsF4STxILQGuZu9RkEtBJqzBAHa7I4lWn JbZOeFqyxF3FWcdVAB8pcc38SGtmQkcDqgkChPRsRGM5zFjfmFoKVPKbB5doYvKbOhKq srNg== X-Gm-Message-State: AOAM5302+B0DQXOJpTJ+u5p+bELi5YIP6MA3PgYJqjFLX7yMaymCJAwU cgDbUTV4PxLTftvy+SMaBD91AktY+sHoXw== X-Google-Smtp-Source: ABdhPJxM1W8eSskBMC2CO9E+p9GfiQTos5zwkydBY9p4WoZQQm6XhgGBI6C7CQPn+BOCA6iFscABQg== X-Received: by 2002:a17:907:c02:: with SMTP id ga2mr4407593ejc.215.1623925399852; Thu, 17 Jun 2021 03:23:19 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:19 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 13/27] run-command: remove old run_hook_{le,ve}() hook API Date: Thu, 17 Jun 2021 12:22:47 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer The new hook.h library has replaced all run-command.h hook-related functionality. So let's delete this dead code. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- run-command.c | 32 -------------------------------- run-command.h | 16 ---------------- 2 files changed, 48 deletions(-) diff --git a/run-command.c b/run-command.c index eecdef5a0c..95c950a4a2 100644 --- a/run-command.c +++ b/run-command.c @@ -1321,38 +1321,6 @@ int async_with_fork(void) #endif } -static int run_hook_ve(const char *const *env, const char *name, va_list args) -{ - struct child_process hook = CHILD_PROCESS_INIT; - const char *p; - - p = find_hook(name); - if (!p) - return 0; - - strvec_push(&hook.args, p); - while ((p = va_arg(args, const char *))) - strvec_push(&hook.args, p); - hook.env = env; - hook.no_stdin = 1; - hook.stdout_to_stderr = 1; - hook.trace2_hook_name = name; - - return run_command(&hook); -} - -int run_hook_le(const char *const *env, const char *name, ...) -{ - va_list args; - int ret; - - va_start(args, name); - ret = run_hook_ve(env, name, args); - va_end(args); - - return ret; -} - struct io_pump { /* initialized by caller */ int fd; diff --git a/run-command.h b/run-command.h index 24ab5d63c4..748d4fc2a7 100644 --- a/run-command.h +++ b/run-command.h @@ -201,22 +201,6 @@ int finish_command_in_signal(struct child_process *); */ int run_command(struct child_process *); -/** - * Run a hook. - * The first argument is a pathname to an index file, or NULL - * if the hook uses the default index file or no index is needed. - * The second argument is the name of the hook. - * The further arguments correspond to the hook arguments. - * The last argument has to be NULL to terminate the arguments list. - * If the hook does not exist or is not executable, the return - * value will be zero. - * If it is executable, the hook will be executed and the exit - * status of the hook is returned. - * On execution, .stdout_to_stderr and .no_stdin will be set. - */ -LAST_ARG_MUST_BE_NULL -int run_hook_le(const char *const *env, const char *name, ...); - /* * Trigger an auto-gc */ From patchwork Thu Jun 17 10:22:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327349 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 BF12CC2B9F4 for ; Thu, 17 Jun 2021 10:23:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A578661057 for ; Thu, 17 Jun 2021 10:23:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232234AbhFQKZ6 (ORCPT ); Thu, 17 Jun 2021 06:25:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232119AbhFQKZj (ORCPT ); Thu, 17 Jun 2021 06:25:39 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 479E2C06124C for ; Thu, 17 Jun 2021 03:23:22 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id s6so3223929edu.10 for ; Thu, 17 Jun 2021 03:23: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=ZBrjnoRSnoHbAnuAzfd9UwOW6z8IPdZ5pp0fPqGzxL0=; b=o9CTrup6bT0tQ0uJ4SBIHPPYJlmbtVcTLr6hGzaDBqReHuyoMdhHX4GQWZp3P74vZw NxGTfV/lkZwqsYX4ptsllzA6zhrwUVJnMOkIjfqQ1Ov3roGdYqD5MnibhIuZ2y5YDEIM f/WXwlFqkncyEpNDvBpUqD73ySHGLrr0fnVzL5Oj68z8fjao4wxHzXGf0RQAE9nWRlDM 8XTWdfSlsynaFx/oEbqAe7Sjxf3r9i2IHkTGTkTbsh7w/fi5w2zOwZ9yADRAQTxY/ViU beecapoOjgdmNGNljYcLPEH2GP7VAu+MZfYW5gzwoyTxwftRqhBjvjZiOmeuZjK736No kyUQ== 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=ZBrjnoRSnoHbAnuAzfd9UwOW6z8IPdZ5pp0fPqGzxL0=; b=h+YaHgCchXLILbgStE69knkQ0dLN4XuCCosZY/RlQ52HrU7NWSCnUjNDBLnz+YKpab Ot4XxvrHAF6WyB3NPbANoga4woKBkz5qjyokfLY6RZsDoI6bd9ZP2Cod05gYwdagHK9f E+43152cV9alfhFkqCKoohkbUtY4Hud28Sl1kEVK+Siu/KAwPuryeFJbYAL7VpnRdUUt A2s0kkUTr3YGxZ+YR861be3rLYSnY8/FCb9HacfSjol8aYnbwhBjNTxYahG2jIQFDJdL D2BsefZAEVG5KTROFTCVJRcl1I3kCcQpTtRNfxCR+mAvnYrZNQTYZV+0QtNsa1qI3jC8 Dvgg== X-Gm-Message-State: AOAM5304bB3/kXI5EFuAqdlZHyVMu+9pT+lFu/P04Wr7SqtXoOBzNUBp eEUgTiaFr6L/zoensGya4uElYD/ioJW5Vg== X-Google-Smtp-Source: ABdhPJz2yC4dbrG6gGSb6zMpqwSbTKE8IqLMNAUy4ZG6MW0W0io7JquIG/UitCrLnhG+FAUFMYUwYQ== X-Received: by 2002:aa7:c594:: with SMTP id g20mr5377818edq.193.1623925400667; Thu, 17 Jun 2021 03:23:20 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:20 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 14/27] run-command: allow stdin for run_processes_parallel Date: Thu, 17 Jun 2021 12:22:48 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer While it makes sense not to inherit stdin from the parent process to avoid deadlocking, it's not necessary to completely ban stdin to children. An informed user should be able to configure stdin safely. By setting `some_child.process.no_stdin=1` before calling `get_next_task()` we provide a reasonable default behavior but enable users to set up stdin streaming for themselves during the callback. `some_child.process.stdout_to_stderr`, however, remains unmodifiable by `get_next_task()` - the rest of the run_processes_parallel() API depends on child output in stderr. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- run-command.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/run-command.c b/run-command.c index 95c950a4a2..0bf771845e 100644 --- a/run-command.c +++ b/run-command.c @@ -1628,6 +1628,14 @@ static int pp_start_one(struct parallel_processes *pp) if (i == pp->max_processes) BUG("bookkeeping is hard"); + /* + * By default, do not inherit stdin from the parent process - otherwise, + * all children would share stdin! Users may overwrite this to provide + * something to the child's stdin by having their 'get_next_task' + * callback assign 0 to .no_stdin and an appropriate integer to .in. + */ + pp->children[i].process.no_stdin = 1; + code = pp->get_next_task(&pp->children[i].process, &pp->children[i].err, pp->data, @@ -1639,7 +1647,6 @@ static int pp_start_one(struct parallel_processes *pp) } pp->children[i].process.err = -1; pp->children[i].process.stdout_to_stderr = 1; - pp->children[i].process.no_stdin = 1; if (start_command(&pp->children[i].process)) { code = pp->start_failure(&pp->children[i].err, From patchwork Thu Jun 17 10:22:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327351 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 68754C48BE5 for ; Thu, 17 Jun 2021 10:23:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BDDF61245 for ; Thu, 17 Jun 2021 10:23:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232176AbhFQK0A (ORCPT ); Thu, 17 Jun 2021 06:26:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232126AbhFQKZj (ORCPT ); Thu, 17 Jun 2021 06:25:39 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 146C5C061760 for ; Thu, 17 Jun 2021 03:23:23 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id gb32so794425ejc.2 for ; Thu, 17 Jun 2021 03:23:23 -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=w56WOd37/leZn9F3J/DFkDPgkOgsnZPLn249OK3Eluc=; b=TyGBR1A6LJ6TOhQO0+hqri/0wFz/LQRkfJl6Ve/xPYjL/ztcP9E+e0K5wtcsUW78I4 2ME+xXXxyNKLOUWe1YRiJHiOQyZHNHjhJe/ScDKbhdHKo/buDpF758pFQKlSb7PdqgIi y4I6SGashMn0YoKYME2SrbiXUrREqeP5rcqY6kF2atItTGt4vdZnhWs1jKQvV1ACWHX+ I+iFmx24X0qsOUJFsbiNb/96aSUZOyT91xQnIqoCWwH+P27EHgjsW6fydUlEp/huk9Tt BdtGBp/M4F135sauYaAqrxc79ndPmGUa73x5vkh4yrqTumddFJ88IpXwj7KdIXDk2Uc8 So9A== 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=w56WOd37/leZn9F3J/DFkDPgkOgsnZPLn249OK3Eluc=; b=n72QyFQlhUal/4pUoi7BGqLpUmtawlOA4mg1YBHgVYAIXo2nVAZKe3tzjRCbEeXKQT 96nktsJY8BMGC3te2JM+X+dBojqUSr3UyCMq3c+/HRkt+sPgtZ/RNecVoc8uzFmg4VYy 1Uv/QlsRk2av2uLwfuoh6NE83LrmH8vXEqHY01ommQLDRYxL+qk9fwAiyZQrXWXq0zrE rwhgCrSGm3QIf5uPhJdtSG3O/76KS2pkzM2ScR7Swjc67jMIxwqCJmsiDLKe8+Scwzjo HVjOfEWnTbNHyYzyQcU1mdo9C4AQPWvNvLSBLU1SVls28SIYGgnDnNRNxXuMvc6ecwaF psxQ== X-Gm-Message-State: AOAM531++PAKj3SH3b+pqmHZ8dEeIB4orbiZfKAS8Pf1wsl7OjCbr9rT 2nnHOXVzyVa/WG6u5kixVPZbG/lkU8jnRw== X-Google-Smtp-Source: ABdhPJwemHsgdLaZgsk6NDLKQDH0LuUs2JYymLnorzNABclO8chbO66Af7JRcc8b+dip9orzFUjtOA== X-Received: by 2002:a17:906:488a:: with SMTP id v10mr4350593ejq.383.1623925401482; Thu, 17 Jun 2021 03:23:21 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:21 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 15/27] hook: support passing stdin to hooks Date: Thu, 17 Jun 2021 12:22:49 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Some hooks (such as post-rewrite) need to take input via stdin. Previously, callers provided stdin to hooks by setting run-command.h:child_process.in, which takes a FD. Callers would open the file in question themselves before calling run-command(). However, since we will now need to seek to the front of the file and read it again for every hook which runs, hook.h:run_command() takes a path and handles FD management itself. Since this file is opened for read only, it should not prevent later parallel execution support. On the frontend, this is supported by asking for a file path, rather than by reading stdin. Reading directly from stdin would involve caching the entire stdin (to memory or to disk) and reading it back from the beginning to each hook. We'd want to support cases like insufficient memory or storage for the file. While this may prove useful later, for now the path of least resistance is to just ask the user to make this interim file themselves. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- Documentation/git-hook.txt | 7 ++++++- builtin/hook.c | 5 ++++- hook.c | 9 ++++++++- hook.h | 2 ++ t/t1800-hook.sh | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt index 097fb9de63..fa68c1f391 100644 --- a/Documentation/git-hook.txt +++ b/Documentation/git-hook.txt @@ -8,7 +8,7 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run [--ignore-missing] [-- ] +'git hook' run [--to-stdin=] [--ignore-missing] [-- ] DESCRIPTION ----------- @@ -32,6 +32,11 @@ what those are. OPTIONS ------- +--to-stdin:: + For "run"; Specify a file which will be streamed into the + hook's stdin. The hook will receive the entire file from + beginning to EOF. + --ignore-missing:: Ignore any missing hook by quietly returning zero. Used for tools that want to do a blind one-shot run of a hook that may diff --git a/builtin/hook.c b/builtin/hook.c index 47e0de7bbc..169a8dd08f 100644 --- a/builtin/hook.c +++ b/builtin/hook.c @@ -8,12 +8,13 @@ static const char * const builtin_hook_usage[] = { N_("git hook [...]"), - N_("git hook run [-- ]"), + N_("git hook run [] [-- ]"), NULL }; static const char * const builtin_hook_run_usage[] = { N_("git hook run [-- ]"), + N_("git hook run [--to-stdin=] [-- ]"), NULL }; @@ -29,6 +30,8 @@ static int run(int argc, const char **argv, const char *prefix) struct option run_options[] = { OPT_BOOL(0, "ignore-missing", &ignore_missing, N_("exit quietly with a zero exit code if the requested hook cannot be found")), + OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"), + N_("file to read into hooks' stdin")), OPT_END(), }; diff --git a/hook.c b/hook.c index cb39404ac8..cab9cced82 100644 --- a/hook.c +++ b/hook.c @@ -85,7 +85,14 @@ static int pick_next_hook(struct child_process *cp, struct hook_cb_data *hook_cb = pp_cb; struct hook *run_me = hook_cb->run_me; - cp->no_stdin = 1; + + /* reopen the file for stdin; run_command closes it. */ + if (hook_cb->options->path_to_stdin) { + cp->no_stdin = 0; + cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY); + } else { + cp->no_stdin = 1; + } cp->env = hook_cb->options->env.v; cp->stdout_to_stderr = 1; cp->trace2_hook_name = hook_cb->hook_name; diff --git a/hook.h b/hook.h index 8bd0fc8d1f..c4ac054ee3 100644 --- a/hook.h +++ b/hook.h @@ -45,6 +45,8 @@ struct run_hooks_opt /* Path to initial working directory for subprocess */ const char *dir; + /* Path to file which should be piped to stdin for each hook */ + const char *path_to_stdin; }; #define RUN_HOOKS_OPT_INIT { \ diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index 542e551628..f3510379e0 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -133,4 +133,22 @@ test_expect_success 'git -c core.hooksPath= hook run' ' test_cmp expect actual ' +test_expect_success 'stdin to hooks' ' + write_script .git/hooks/test-hook <<-\EOF && + echo BEGIN stdin + cat + echo END stdin + EOF + + cat >expect <<-EOF && + BEGIN stdin + hello + END stdin + EOF + + echo hello >input && + git hook run --to-stdin=input test-hook 2>actual && + test_cmp expect actual +' + test_done From patchwork Thu Jun 17 10:22:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327355 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 1CC57C49361 for ; Thu, 17 Jun 2021 10:24:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0754861057 for ; Thu, 17 Jun 2021 10:24:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232254AbhFQK0G (ORCPT ); Thu, 17 Jun 2021 06:26:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232154AbhFQKZk (ORCPT ); Thu, 17 Jun 2021 06:25:40 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF9A7C06121D for ; Thu, 17 Jun 2021 03:23:23 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id my49so8940498ejc.7 for ; Thu, 17 Jun 2021 03:23:23 -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=Lofb6OkhsrELY81zghL1eWlAZIqumh/y2CEHnNKEPr8=; b=aTICkkyWfG+T0zcB0ULZLkAAlSIMXcaSq70vlbCPjorqmvSY+mZJ1VQTkdrjJUvmd5 SGdSGGIh7LRp5TwTeTjqZcMtaDQxJqCrzqlYgxh/0SRs2gUkesbM+Kn5Gw6AAG8HSsBA lA0xctsj9+efXoxnIwPZBXN6kEP2AxXpxY1YNzuEZ3AseSTbBJiSrzqFnucrGDqa0HMr YcGnjiYDjnCWF2kTnvBjhNxL8IBu2UoyE0bJ2g6msBev8UGLnIRe0L1OQI7nj/zsrxoh AM+kG+KaLEZjVMkGlEPi8Pa5g1j0m4QV2/YC7FBsGgB0TF4TVYFGXVDpfQUjNPhnS//c ifIw== 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=Lofb6OkhsrELY81zghL1eWlAZIqumh/y2CEHnNKEPr8=; b=lzDJ3RHBMQzmN0yfz1YlPu5LaDszIVhX728LA8hlMKos38L6SdDZRaqCOdhbJ5BSif 1954lWMUfw1ldksGkUvXwMLUkMlgqR/6Sxg4CDSd81H1hQm0gT0QBFxoHKqpn8dfXGFA SYyuvhIsno2wOd4E7rz3Z17+R9kqLpV2dbsCRYLw4TbySWeKN70f5EpaPdT8ee9bjQW0 /o7JdE/nAUtorfRMfjNs8aTCXN3egNZ2qYM5o6rDg0iRI+YSjBEJ6ZFiQf7dHQ4g/HA0 M8axis5mj/aRcRqqimEBCf2UoQVugvilEit4zs/vCvDV0he5CP26EBe9QeTwbFYPzscI iBCg== X-Gm-Message-State: AOAM5319Ix5aiQlblCc7VULZKpIKc7DkszX58mMdNwKDO0iTjEHM5YiP uQBwHbZYnU5gDJysNQb9K81xvNYsEQqYeg== X-Google-Smtp-Source: ABdhPJz4oe/yF4R22Uc6JaftEoV/XIm2EorP8Eu/HXFIUdwBo1yHW6wWVYY78+AibXw6VFq45kv+qA== X-Received: by 2002:a17:906:2bd0:: with SMTP id n16mr4495627ejg.110.1623925402299; Thu, 17 Jun 2021 03:23:22 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:21 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 16/27] am: convert 'post-rewrite' hook to hook.h Date: Thu, 17 Jun 2021 12:22:50 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/am.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 9e9c1b5e9f..6e4f9c8036 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -467,23 +467,15 @@ static int run_applypatch_msg_hook(struct am_state *state) */ static int run_post_rewrite_hook(const struct am_state *state) { - struct child_process cp = CHILD_PROCESS_INIT; - const char *hook = find_hook("post-rewrite"); + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; int ret; - if (!hook) - return 0; - - strvec_push(&cp.args, hook); - strvec_push(&cp.args, "rebase"); - - cp.in = xopen(am_path(state, "rewritten"), O_RDONLY); - cp.stdout_to_stderr = 1; - cp.trace2_hook_name = "post-rewrite"; + strvec_push(&opt.args, "rebase"); + opt.path_to_stdin = am_path(state, "rewritten"); - ret = run_command(&cp); + ret = run_hooks("post-rewrite", &opt); - close(cp.in); + run_hooks_opt_clear(&opt); return ret; } From patchwork Thu Jun 17 10:22:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327357 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 A3727C2B9F4 for ; Thu, 17 Jun 2021 10:24:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8C02B61057 for ; Thu, 17 Jun 2021 10:24:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232193AbhFQK0H (ORCPT ); Thu, 17 Jun 2021 06:26:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232155AbhFQKZk (ORCPT ); Thu, 17 Jun 2021 06:25:40 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0F0CC061226 for ; Thu, 17 Jun 2021 03:23:24 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id s15so3218774edt.13 for ; Thu, 17 Jun 2021 03:23:24 -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=VwaTOf7nmcCfVeSSxmfQsSLuyD053WNP0b0qelhyihU=; b=GXQd4BFINIOYioS+HxBAX+BCyDMD/dvOARRjgokI749ibXp6OsMO44wCDWy1J68AqA NUoFc6iwqNFTIc5rHUfoC3isXdKg/1nWXgCEQBmy8jC4ozJxsZ+4hiuHjqDgeL3WyaHJ h8YkJaDUOi21RQa+sbqqPzeSAxwmDSwBYR7qrGKKi/k5WPCb8zgFHfVjV9eqVADpwHtD r1T6Q0Qg8ZykXv/9m3px/H/GjxGnh3SxXLBV1x8hfeDst+GhLC8/4e5gymUReBdk+44f UQHCc+OSNMj2IhiNiHog3dyEO8+eDyPuwKeZbwKMf5HqCEEPLNiGSiiOXEFc3ts6Agvd oW/w== 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=VwaTOf7nmcCfVeSSxmfQsSLuyD053WNP0b0qelhyihU=; b=b7xiA7Xhmd2Rn1O0WQKH1RJ1a7woffBuNQ8gZdGHd4pacfl3eRVBJ/LW0eO07sZaf5 L4YDHzZuH4ohgzZjv9hnOj/hEGLWq2RO9KLKnjpM9qU4SQW4npB/Qq1+U0AYUhgOrv0S iJPyjd0ehNaZ/UfuM64Kfn+tVGEBynyvMmEz7P94aW5U138wT3s6COEFuUyt3iqRN/jg +AAFzFgVJj9egAyN4OnvbDA+TERciAUT02t26gfN26Q8XD5UTtx+jVG3N0w5E2rMeWo+ ysHQBSZMVBaCMX0BmrT6Ol7fvfxhh5iKC3BSE60XRSOXnvGIdd1W7tNiymiro6vNx0Kh o65Q== X-Gm-Message-State: AOAM533oM5UbQpulBYB+xAcdMHv9mxj75UwwdaDrEcgGkUcZiOXhCLoC dhiSFUklk9vTtLaHRk+MPysJ/9UPmPYl4w== X-Google-Smtp-Source: ABdhPJw5/pPpUNwWlix/3bxHLJEtM2JoEAtr074JAMUkgwwbbJrV2jaDRIz0RaIXcuwMnSmZ38TXNg== X-Received: by 2002:aa7:c3ce:: with SMTP id l14mr5450330edr.99.1623925403160; Thu, 17 Jun 2021 03:23:23 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:22 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 17/27] run-command: add stdin callback for parallelization Date: Thu, 17 Jun 2021 12:22:51 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer If a user of the run_processes_parallel() API wants to pipe a large amount of information to stdin of each parallel command, that information could exceed the buffer of the pipe allocated for that process's stdin. Generally this is solved by repeatedly writing to child_process.in between calls to start_command() and finish_command(); run_processes_parallel() did not provide users an opportunity to access child_process at that time. Because the data might be extremely large (for example, a list of all refs received during a push from a client) simply taking a string_list or strbuf is not as scalable as using a callback; the rest of the run_processes_parallel() API also uses callbacks, so making this feature match the rest of the API reduces mental load on the user. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fetch.c | 1 + builtin/submodule--helper.c | 2 +- hook.c | 1 + run-command.c | 54 +++++++++++++++++++++++++++++++++++-- run-command.h | 17 +++++++++++- submodule.c | 1 + t/helper/test-run-command.c | 31 ++++++++++++++++++--- t/t0061-run-command.sh | 30 +++++++++++++++++++++ 8 files changed, 129 insertions(+), 8 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index 9191620e50..3d8f04b392 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1817,6 +1817,7 @@ static int fetch_multiple(struct string_list *list, int max_children) result = run_processes_parallel_tr2(max_children, &fetch_next_remote, &fetch_failed_to_start, + NULL, &fetch_finished, &state, "fetch", "parallel/fetch"); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index ae6174ab05..818494dd18 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2295,7 +2295,7 @@ static int update_submodules(struct submodule_update_clone *suc) int i; run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task, - update_clone_start_failure, + update_clone_start_failure, NULL, update_clone_task_finished, suc, "submodule", "parallel/update"); diff --git a/hook.c b/hook.c index cab9cced82..a012c3d458 100644 --- a/hook.c +++ b/hook.c @@ -164,6 +164,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure, + NULL, notify_hook_finished, &cb_data, "hook", diff --git a/run-command.c b/run-command.c index 0bf771845e..3392640f17 100644 --- a/run-command.c +++ b/run-command.c @@ -1493,6 +1493,7 @@ struct parallel_processes { get_next_task_fn get_next_task; start_failure_fn start_failure; + feed_pipe_fn feed_pipe; task_finished_fn task_finished; struct { @@ -1520,6 +1521,13 @@ static int default_start_failure(struct strbuf *out, return 0; } +static int default_feed_pipe(struct strbuf *pipe, + void *pp_cb, + void *pp_task_cb) +{ + return 1; +} + static int default_task_finished(int result, struct strbuf *out, void *pp_cb, @@ -1550,6 +1558,7 @@ static void pp_init(struct parallel_processes *pp, int n, get_next_task_fn get_next_task, start_failure_fn start_failure, + feed_pipe_fn feed_pipe, task_finished_fn task_finished, void *data) { @@ -1568,6 +1577,7 @@ static void pp_init(struct parallel_processes *pp, pp->get_next_task = get_next_task; pp->start_failure = start_failure ? start_failure : default_start_failure; + pp->feed_pipe = feed_pipe ? feed_pipe : default_feed_pipe; pp->task_finished = task_finished ? task_finished : default_task_finished; pp->nr_processes = 0; @@ -1665,6 +1675,37 @@ static int pp_start_one(struct parallel_processes *pp) return 0; } +static void pp_buffer_stdin(struct parallel_processes *pp) +{ + int i; + struct strbuf sb = STRBUF_INIT; + + /* Buffer stdin for each pipe. */ + for (i = 0; i < pp->max_processes; i++) { + if (pp->children[i].state == GIT_CP_WORKING && + pp->children[i].process.in > 0) { + int done; + strbuf_reset(&sb); + done = pp->feed_pipe(&sb, pp->data, + pp->children[i].data); + if (sb.len) { + if (write_in_full(pp->children[i].process.in, + sb.buf, sb.len) < 0) { + if (errno != EPIPE) + die_errno("write"); + done = 1; + } + } + if (done) { + close(pp->children[i].process.in); + pp->children[i].process.in = 0; + } + } + } + + strbuf_release(&sb); +} + static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout) { int i; @@ -1729,6 +1770,7 @@ static int pp_collect_finished(struct parallel_processes *pp) pp->nr_processes--; pp->children[i].state = GIT_CP_FREE; pp->pfd[i].fd = -1; + pp->children[i].process.in = 0; child_process_init(&pp->children[i].process); if (i != pp->output_owner) { @@ -1762,6 +1804,7 @@ static int pp_collect_finished(struct parallel_processes *pp) int run_processes_parallel(int n, get_next_task_fn get_next_task, start_failure_fn start_failure, + feed_pipe_fn feed_pipe, task_finished_fn task_finished, void *pp_cb) { @@ -1770,7 +1813,9 @@ int run_processes_parallel(int n, int spawn_cap = 4; struct parallel_processes pp; - pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb); + sigchain_push(SIGPIPE, SIG_IGN); + + pp_init(&pp, n, get_next_task, start_failure, feed_pipe, task_finished, pp_cb); while (1) { for (i = 0; i < spawn_cap && !pp.shutdown && @@ -1787,6 +1832,7 @@ int run_processes_parallel(int n, } if (!pp.nr_processes) break; + pp_buffer_stdin(&pp); pp_buffer_stderr(&pp, output_timeout); pp_output(&pp); code = pp_collect_finished(&pp); @@ -1798,11 +1844,15 @@ int run_processes_parallel(int n, } pp_cleanup(&pp); + + sigchain_pop(SIGPIPE); + return 0; } int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task, start_failure_fn start_failure, + feed_pipe_fn feed_pipe, task_finished_fn task_finished, void *pp_cb, const char *tr2_category, const char *tr2_label) { @@ -1812,7 +1862,7 @@ int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task, ((n < 1) ? online_cpus() : n)); result = run_processes_parallel(n, get_next_task, start_failure, - task_finished, pp_cb); + feed_pipe, task_finished, pp_cb); trace2_region_leave(tr2_category, tr2_label, NULL); diff --git a/run-command.h b/run-command.h index 748d4fc2a7..41e36d26cb 100644 --- a/run-command.h +++ b/run-command.h @@ -419,6 +419,20 @@ typedef int (*start_failure_fn)(struct strbuf *out, void *pp_cb, void *pp_task_cb); +/** + * This callback is called repeatedly on every child process who requests + * start_command() to create a pipe by setting child_process.in < 0. + * + * pp_cb is the callback cookie as passed into run_processes_parallel, and + * pp_task_cb is the callback cookie as passed into get_next_task_fn. + * The contents of 'send' will be read into the pipe and passed to the pipe. + * + * Return nonzero to close the pipe. + */ +typedef int (*feed_pipe_fn)(struct strbuf *pipe, + void *pp_cb, + void *pp_task_cb); + /** * This callback is called on every child process that finished processing. * @@ -453,10 +467,11 @@ typedef int (*task_finished_fn)(int result, int run_processes_parallel(int n, get_next_task_fn, start_failure_fn, + feed_pipe_fn, task_finished_fn, void *pp_cb); int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn, - task_finished_fn, void *pp_cb, + feed_pipe_fn, task_finished_fn, void *pp_cb, const char *tr2_category, const char *tr2_label); #endif diff --git a/submodule.c b/submodule.c index 0b1d9c1dde..ea026a8195 100644 --- a/submodule.c +++ b/submodule.c @@ -1645,6 +1645,7 @@ int fetch_populated_submodules(struct repository *r, run_processes_parallel_tr2(max_parallel_jobs, get_next_submodule, fetch_start_failure, + NULL, fetch_finish, &spf, "submodule", "parallel/fetch"); diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index 7ae03dc712..9348184d30 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -32,8 +32,13 @@ static int parallel_next(struct child_process *cp, return 0; strvec_pushv(&cp->args, d->argv); + cp->in = d->in; + cp->no_stdin = d->no_stdin; strbuf_addstr(err, "preloaded output of a child\n"); number_callbacks++; + + *task_cb = xmalloc(sizeof(int)); + *(int*)(*task_cb) = 2; return 1; } @@ -55,6 +60,17 @@ static int task_finished(int result, return 1; } +static int test_stdin(struct strbuf *pipe, void *cb, void *task_cb) +{ + int *lines_remaining = task_cb; + + if (*lines_remaining) + strbuf_addf(pipe, "sample stdin %d\n", --(*lines_remaining)); + + return !(*lines_remaining); +} + + struct testsuite { struct string_list tests, failed; int next; @@ -185,7 +201,7 @@ static int testsuite(int argc, const char **argv) suite.tests.nr, max_jobs); ret = run_processes_parallel(max_jobs, next_test, test_failed, - test_finished, &suite); + test_stdin, test_finished, &suite); if (suite.failed.nr > 0) { ret = 1; @@ -413,15 +429,22 @@ int cmd__run_command(int argc, const char **argv) if (!strcmp(argv[1], "run-command-parallel")) exit(run_processes_parallel(jobs, parallel_next, - NULL, NULL, &proc)); + NULL, NULL, NULL, &proc)); if (!strcmp(argv[1], "run-command-abort")) exit(run_processes_parallel(jobs, parallel_next, - NULL, task_finished, &proc)); + NULL, NULL, task_finished, &proc)); if (!strcmp(argv[1], "run-command-no-jobs")) exit(run_processes_parallel(jobs, no_job, - NULL, task_finished, &proc)); + NULL, NULL, task_finished, &proc)); + + if (!strcmp(argv[1], "run-command-stdin")) { + proc.in = -1; + proc.no_stdin = 0; + exit (run_processes_parallel(jobs, parallel_next, NULL, + test_stdin, NULL, &proc)); + } fprintf(stderr, "check usage\n"); return 1; diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 7d599675e3..87759482ad 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -143,6 +143,36 @@ test_expect_success 'run_command runs in parallel with more tasks than jobs avai test_cmp expect actual ' +cat >expect <<-EOF +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +EOF + +test_expect_success 'run_command listens to stdin' ' + write_script stdin-script <<-\EOF && + echo "listening for stdin:" + while read line; do + echo "$line" + done + EOF + test-tool run-command run-command-stdin 2 ./stdin-script 2>actual && + test_cmp expect actual +' + cat >expect <<-EOF preloaded output of a child asking for a quick stop From patchwork Thu Jun 17 10:22:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327359 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 C7D6EC49361 for ; Thu, 17 Jun 2021 10:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B402B61057 for ; Thu, 17 Jun 2021 10:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232260AbhFQK0J (ORCPT ); Thu, 17 Jun 2021 06:26:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232161AbhFQKZk (ORCPT ); Thu, 17 Jun 2021 06:25:40 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2041C0611C0 for ; Thu, 17 Jun 2021 03:23:25 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id l1so8998002ejb.6 for ; Thu, 17 Jun 2021 03:23:25 -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=mN56wNKC0YYPqfoGbyBU2Gj4SFbIHRwVr17bU+eBLUo=; b=VFWGdYo47A5nsIqm2jo6tssbWVzlPioYSQAGEyFbmIrBUsaFdSZfGYIDoj03yQZFt4 deQYMWMjnSTg9cFW5FGBMZ0qa6xqpdMcRLXXjAa+KTv1VVZr/4ztCSHI1Tv17hbqSeeg 2y2Vf9v5Zl1hWIChKRlF5hbAYOmMBIjRICAoE0K0CVPkpqPd66pWpEv+F98NWQa7h8Pe XyOVLMKIpqPB4NW2dc2ZjxYRqmsOOna9+Rh8nc+6k+tFgWSZdm3k9irwen6bfxgnb0V4 klGljloJTS2w5QqVp14gneqAVGDkEJ0AAmFfiCShNnl3Fk+XTnPLG69uOGepbWLxpq6B SMAA== 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=mN56wNKC0YYPqfoGbyBU2Gj4SFbIHRwVr17bU+eBLUo=; b=RV3fjLrPloZTHp3OKF82P0nNHD9JDaRqB6kaRmYTE5VvIah67kZ6G5XVAqtzdZqaJZ A5NUqyjVSdTF/NiVuXWZ2b88pTaWFgaQ+4FZeWGynpJ4E7pdRAleLKl/if2QxuZCottn Db96Zha0Jl466zgCgtPROPDA1JajKWqMJzyEpIWrlJTX0O3VZWU5is9kjnv0KfApMM5i ctPV6tqSkQ1IjzSismyXwrS+nElbnHUJuOrWPzdxwg1YzgtStUD/9YnmIE3d2zCt5TiX NAsslRiUpLZ9iYDPL/mSIHXHQiJJK2U1dCNVJ3vncsVufhFM7ihIrd6FrCh+P5F5U6oj CZtQ== X-Gm-Message-State: AOAM531m3ha5XuwAVwQFtbBLg47ratBtwoOsgn1b/EqV2lo90c9HnkSu sh35an1RT/Ef9TVtlLrfLT4+7qN/+yhk/g== X-Google-Smtp-Source: ABdhPJxd/9YhES6sTuAvW4AzS2maroZvfwVkAbkUJk/PoRtS4vpsODyKK2iMS79Q8EoE6msuIXjCXQ== X-Received: by 2002:a17:906:9713:: with SMTP id k19mr4508970ejx.516.1623925404046; Thu, 17 Jun 2021 03:23:24 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:23 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 18/27] hook: provide stdin by string_list or callback Date: Thu, 17 Jun 2021 12:22:52 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer In cases where a hook requires only a small amount of information via stdin, it should be simple for users to provide a string_list alone. But in more complicated cases where the stdin is too large to hold in memory, let's instead provide a callback the users can populate line after line. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- hook.c | 32 +++++++++++++++++++++++++++++++- hook.h | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/hook.c b/hook.c index a012c3d458..c4df787f13 100644 --- a/hook.c +++ b/hook.c @@ -77,6 +77,29 @@ void run_hooks_opt_clear(struct run_hooks_opt *o) strvec_clear(&o->args); } +int pipe_from_string_list(struct strbuf *pipe, void *pp_cb, void *pp_task_cb) +{ + int *item_idx; + struct hook *ctx = pp_task_cb; + struct hook_cb_data *hook_cb = pp_cb; + struct string_list *to_pipe = hook_cb->options->feed_pipe_ctx; + + /* Bootstrap the state manager if necessary. */ + if (!ctx->feed_pipe_cb_data) { + ctx->feed_pipe_cb_data = xmalloc(sizeof(unsigned int)); + *(int*)ctx->feed_pipe_cb_data = 0; + } + + item_idx = ctx->feed_pipe_cb_data; + + if (*item_idx < to_pipe->nr) { + strbuf_addf(pipe, "%s\n", to_pipe->items[*item_idx].string); + (*item_idx)++; + return 0; + } + return 1; +} + static int pick_next_hook(struct child_process *cp, struct strbuf *out, void *pp_cb, @@ -90,6 +113,10 @@ static int pick_next_hook(struct child_process *cp, if (hook_cb->options->path_to_stdin) { cp->no_stdin = 0; cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY); + } else if (hook_cb->options->feed_pipe) { + /* ask for start_command() to make a pipe for us */ + cp->in = -1; + cp->no_stdin = 0; } else { cp->no_stdin = 1; } @@ -164,7 +191,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure, - NULL, + options->feed_pipe, notify_hook_finished, &cb_data, "hook", @@ -182,6 +209,9 @@ int run_hooks(const char *hook_name, struct run_hooks_opt *options) if (!options) BUG("a struct run_hooks_opt must be provided to run_hooks"); + if (options->path_to_stdin && options->feed_pipe) + BUG("choose only one method to populate stdin"); + hook_path = find_hook(hook_name); /* diff --git a/hook.h b/hook.h index c4ac054ee3..b78a8e4805 100644 --- a/hook.h +++ b/hook.h @@ -19,6 +19,12 @@ int hook_exists(const char *hookname); struct hook { /* The path to the hook */ const char *hook_path; + + /* + * Use this to keep state for your feed_pipe_fn if you are using + * run_hooks_opt.feed_pipe. Otherwise, do not touch it. + */ + void *feed_pipe_cb_data; }; struct run_hooks_opt @@ -47,6 +53,19 @@ struct run_hooks_opt /* Path to file which should be piped to stdin for each hook */ const char *path_to_stdin; + + /* + * Callback and state pointer to ask for more content to pipe to stdin. + * Will be called repeatedly, for each hook. See + * hook.c:pipe_from_stdin() for an example. Keep per-hook state in + * hook.feed_pipe_cb_data (per process). Keep initialization context in + * feed_pipe_ctx (shared by all processes). + * + * See 'pipe_from_string_list()' for info about how to specify a + * string_list as the stdin input instead of writing your own handler. + */ + feed_pipe_fn feed_pipe; + void *feed_pipe_ctx; }; #define RUN_HOOKS_OPT_INIT { \ @@ -55,6 +74,14 @@ struct run_hooks_opt .args = STRVEC_INIT, \ } +/* + * To specify a 'struct string_list', set 'run_hooks_opt.feed_pipe_ctx' to the + * string_list and set 'run_hooks_opt.feed_pipe' to 'pipe_from_string_list()'. + * This will pipe each string in the list to stdin, separated by newlines. (Do + * not inject your own newlines.) + */ +int pipe_from_string_list(struct strbuf *pipe, void *pp_cb, void *pp_task_cb); + struct hook_cb_data { /* rc reflects the cumulative failure state */ int rc; From patchwork Thu Jun 17 10:22:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327361 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 33ABFC2B9F4 for ; Thu, 17 Jun 2021 10:24:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1BFC661263 for ; Thu, 17 Jun 2021 10:24:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232266AbhFQK0K (ORCPT ); Thu, 17 Jun 2021 06:26:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232177AbhFQKZl (ORCPT ); Thu, 17 Jun 2021 06:25:41 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6596EC0611C6 for ; Thu, 17 Jun 2021 03:23:26 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id r7so3217781edv.12 for ; Thu, 17 Jun 2021 03:23:26 -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=5KbqqglF5L+7KCmYzQq1fd+B0jm+IIL9LVoFGfz/8fc=; b=LI1ZfT6452wS4raDBRJaZ61QhUqgsSdldHOIisKVUz1HyAf6iH25g/F1OTu65BoJrR +fF4VTrITz+vHjfJnfI4sgDbujX4UE/bBiSZ42yGjTeEWo0ICOJparVS2sg0zHYpuHtL tVEQPK42zjsUI/j9edadUeynMupu62IyjmtWpmMW/+BuTZQ5E93Rie0/Hxg6XgiTKI11 4mihRhwNf5/4s1UDMyE3IxpJvcx7vESC9qmZXgTxUsn5gZDen87PXxw97sgPb9gbEufD rH3DFjhmWDPfrh8SrNnyBiEF4sg2XmBZ38SbzS5EaBRTYOQiaHtJOXdvxSqh4pGrFk/9 aA6Q== 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=5KbqqglF5L+7KCmYzQq1fd+B0jm+IIL9LVoFGfz/8fc=; b=PpMrOkBSOxP+b5xN10724eaEU9BDIJlMvSBBiKfPNITq4Pq/fEUU7u10Cu06dn3Rt1 8pnSyq5LY2f4be4Tt633NuU3X+LbWMaSeVoeyNwRjLLrdiN6WYyEiutIJcAShq1VFoK4 dj860Z/yAflHiQnh73hhYPKf1+d700BNmN6w6rJixYqZxy9l0qQ2ff5ssK4IntRmP+5Y hYtRqZmi0Ym61UaC2FIiQkbSLEmeY2kstz2CCnJEj/wqcE5kmhR9UrKFpH8cH5Ya+9wU T6m1n2Hgfag4zEA4Sc+CkmAsNtZ/aCNgJjerL7Y0J4LG7+boqXXZzjRFUmAZxO4MvD5P STHw== X-Gm-Message-State: AOAM533e4nTYZM8yhrBopOvzqHTVoDI16viPnMbkE2KVmEr0lQtQpDqf qUdOhDgjwlmicOEbPsqULzZ7rnJlTnfWmQ== X-Google-Smtp-Source: ABdhPJy5sUT+5U/Z/32ns4myuTNiw6DcC0edYU6yEc5vIQ6cFaMfzQbbh2dhzplUqEQnktFoVu+ehw== X-Received: by 2002:a05:6402:100e:: with SMTP id c14mr3874997edu.51.1623925404819; Thu, 17 Jun 2021 03:23:24 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:24 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 19/27] hook: convert 'post-rewrite' hook in sequencer.c to hook.h Date: Thu, 17 Jun 2021 12:22:53 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using 'hook.h' for 'post-rewrite', we simplify hook invocations by not needing to put together our own 'struct child_process'. The signal handling that's being removed by this commit now takes place in run-command.h:run_processes_parallel(), so it is OK to remove them here. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- sequencer.c | 81 ++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/sequencer.c b/sequencer.c index 8f46984ffb..ec2761e47d 100644 --- a/sequencer.c +++ b/sequencer.c @@ -35,6 +35,7 @@ #include "commit-reach.h" #include "rebase-interactive.h" #include "reset.h" +#include "string-list.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" @@ -1147,33 +1148,28 @@ int update_head_with_reflog(const struct commit *old_head, static int run_rewrite_hook(const struct object_id *oldoid, const struct object_id *newoid) { - struct child_process proc = CHILD_PROCESS_INIT; - const char *argv[3]; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct strbuf tmp = STRBUF_INIT; + struct string_list to_stdin = STRING_LIST_INIT_DUP; int code; - struct strbuf sb = STRBUF_INIT; - argv[0] = find_hook("post-rewrite"); - if (!argv[0]) - return 0; + strvec_push(&opt.args, "amend"); - argv[1] = "amend"; - argv[2] = NULL; - - proc.argv = argv; - proc.in = -1; - proc.stdout_to_stderr = 1; - proc.trace2_hook_name = "post-rewrite"; - - code = start_command(&proc); - if (code) - return code; - strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); - sigchain_push(SIGPIPE, SIG_IGN); - write_in_full(proc.in, sb.buf, sb.len); - close(proc.in); - strbuf_release(&sb); - sigchain_pop(SIGPIPE); - return finish_command(&proc); + strbuf_addf(&tmp, + "%s %s", + oid_to_hex(oldoid), + oid_to_hex(newoid)); + string_list_append(&to_stdin, tmp.buf); + + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; + + code = run_hooks("post-rewrite", &opt); + + run_hooks_opt_clear(&opt); + strbuf_release(&tmp); + string_list_clear(&to_stdin, 0); + return code; } void commit_post_rewrite(struct repository *r, @@ -4527,30 +4523,21 @@ static int pick_commits(struct repository *r, flush_rewritten_pending(); if (!stat(rebase_path_rewritten_list(), &st) && st.st_size > 0) { - struct child_process child = CHILD_PROCESS_INIT; - const char *post_rewrite_hook = - find_hook("post-rewrite"); - - child.in = open(rebase_path_rewritten_list(), O_RDONLY); - child.git_cmd = 1; - strvec_push(&child.args, "notes"); - strvec_push(&child.args, "copy"); - strvec_push(&child.args, "--for-rewrite=rebase"); + struct child_process notes_cp = CHILD_PROCESS_INIT; + struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT; + + notes_cp.in = open(rebase_path_rewritten_list(), O_RDONLY); + notes_cp.git_cmd = 1; + strvec_push(¬es_cp.args, "notes"); + strvec_push(¬es_cp.args, "copy"); + strvec_push(¬es_cp.args, "--for-rewrite=rebase"); /* we don't care if this copying failed */ - run_command(&child); - - if (post_rewrite_hook) { - struct child_process hook = CHILD_PROCESS_INIT; - - hook.in = open(rebase_path_rewritten_list(), - O_RDONLY); - hook.stdout_to_stderr = 1; - hook.trace2_hook_name = "post-rewrite"; - strvec_push(&hook.args, post_rewrite_hook); - strvec_push(&hook.args, "rebase"); - /* we don't care if this hook failed */ - run_command(&hook); - } + run_command(¬es_cp); + + hook_opt.path_to_stdin = rebase_path_rewritten_list(); + strvec_push(&hook_opt.args, "rebase"); + run_hooks("post-rewrite", &hook_opt); + run_hooks_opt_clear(&hook_opt); } apply_autostash(rebase_path_autostash()); From patchwork Thu Jun 17 10:22:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327363 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 6AE4DC48BE5 for ; Thu, 17 Jun 2021 10:24:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 51F4661245 for ; Thu, 17 Jun 2021 10:24:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232267AbhFQK0M (ORCPT ); Thu, 17 Jun 2021 06:26:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232011AbhFQKZl (ORCPT ); Thu, 17 Jun 2021 06:25:41 -0400 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D496C0611F8 for ; Thu, 17 Jun 2021 03:23:27 -0700 (PDT) Received: by mail-ej1-x62d.google.com with SMTP id my49so8940792ejc.7 for ; Thu, 17 Jun 2021 03:23:27 -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=S9Ik6qLoHV2+sMYlUg3Jydq/czJGR92aSCTNKp3pY3Y=; b=klIh9huhqvp3zi11VUXoprN5thm0Tg+yIqxU/2Z/QJ+qzKEaW+LmOjCWPNhryRM3Tn 2/bSNYOlginLOVvGTl7FwCP0/2ZgngOc1V0gEfPN11KcwluxejT2gRjemm1vfW6tbRyQ JqhCF2SsdGUZ3xYK6uG0OFsXmkHMOhCFrFbSqrvEMdzfpHsdGXNu03EP6zgVXRjjwKIR wJ46sKUQpeWGGuw7WIdJ62tVDsnMvUcLG65im7rbF8BF3O8CYOED3/0UHZ8gH9/mSBKt jCMuXc4uC3iX9Yb5lJaNo5cABgzGPnKh2C9uLsHOvLJNpNqT09GUsHxN5U8Ow56ydB+Q t5ag== 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=S9Ik6qLoHV2+sMYlUg3Jydq/czJGR92aSCTNKp3pY3Y=; b=Z8JliuIozU8AOC44/SY6qM7IgCcksg96E5ArJGpCSIH9+ic16df0vVVqNMKHwXnzvF Z7HCmzVXJdEhO4cUPmXj5n9vZCV7akUj92xukXhd8BqCOKvYRpS/9HY3GeiIrMl7JU1R eyX9CcTzHzf1jrPfVj7zoS52aTR3Adcn03+a7HfnyIYqiTsQlIcs1NVxfFJ/Jbow3YTd NlsbtaWoYgPvhFHsHNO/QJmudgOqwuAK0mtHuHWZfUnsMjvhzW4+nGDxhBmjIjx5sIwu s7OTLyTjuaWTvEJZfCUtFKrdlcJ/0lc8MRetqdO5bg7HePKfn9q/cXJTvy3cdEmkuy9x ThXA== X-Gm-Message-State: AOAM533jQvgxC5+I8JhjaDXZNV//sDFwPMqJwPnJNJGOydn9s6Qnofta s+37tbI8yBd+tRHO2RoDfa3C26GNOmPhbg== X-Google-Smtp-Source: ABdhPJyNwAaZGVIUv99m7HMemokmrzWoCpcg3leh+SLQ9WIVorztk4Qnxf/+tFaNhyUjdasu5yhX9w== X-Received: by 2002:a17:906:6847:: with SMTP id a7mr898176ejs.268.1623925405802; Thu, 17 Jun 2021 03:23:25 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:25 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 20/27] transport: convert pre-push hook to use config Date: Thu, 17 Jun 2021 12:22:54 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using the hook.h:run_hooks API, pre-push hooks can be specified in the config as well as in the hookdir. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- transport.c | 57 ++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 42 deletions(-) diff --git a/transport.c b/transport.c index 2ed270171f..9969ed2cdd 100644 --- a/transport.c +++ b/transport.c @@ -1199,31 +1199,14 @@ static void die_with_unpushed_submodules(struct string_list *needs_pushing) static int run_pre_push_hook(struct transport *transport, struct ref *remote_refs) { - int ret = 0, x; + int ret = 0; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct strbuf tmp = STRBUF_INIT; struct ref *r; - struct child_process proc = CHILD_PROCESS_INIT; - struct strbuf buf; - const char *argv[4]; - - if (!(argv[0] = find_hook("pre-push"))) - return 0; - - argv[1] = transport->remote->name; - argv[2] = transport->url; - argv[3] = NULL; - - proc.argv = argv; - proc.in = -1; - proc.trace2_hook_name = "pre-push"; - - if (start_command(&proc)) { - finish_command(&proc); - return -1; - } - - sigchain_push(SIGPIPE, SIG_IGN); + struct string_list to_stdin = STRING_LIST_INIT_DUP; - strbuf_init(&buf, 256); + strvec_push(&opt.args, transport->remote->name); + strvec_push(&opt.args, transport->url); for (r = remote_refs; r; r = r->next) { if (!r->peer_ref) continue; @@ -1232,30 +1215,20 @@ static int run_pre_push_hook(struct transport *transport, if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue; if (r->status == REF_STATUS_UPTODATE) continue; - strbuf_reset(&buf); - strbuf_addf( &buf, "%s %s %s %s\n", + strbuf_reset(&tmp); + strbuf_addf(&tmp, "%s %s %s %s", r->peer_ref->name, oid_to_hex(&r->new_oid), r->name, oid_to_hex(&r->old_oid)); - - if (write_in_full(proc.in, buf.buf, buf.len) < 0) { - /* We do not mind if a hook does not read all refs. */ - if (errno != EPIPE) - ret = -1; - break; - } + string_list_append(&to_stdin, tmp.buf); } - strbuf_release(&buf); - - x = close(proc.in); - if (!ret) - ret = x; - - sigchain_pop(SIGPIPE); + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; - x = finish_command(&proc); - if (!ret) - ret = x; + ret = run_hooks("pre-push", &opt); + run_hooks_opt_clear(&opt); + strbuf_release(&tmp); + string_list_clear(&to_stdin, 0); return ret; } From patchwork Thu Jun 17 10:22:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327367 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 A704EC49361 for ; Thu, 17 Jun 2021 10:24:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9374E61057 for ; Thu, 17 Jun 2021 10:24:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232274AbhFQK0O (ORCPT ); Thu, 17 Jun 2021 06:26:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232164AbhFQKZo (ORCPT ); Thu, 17 Jun 2021 06:25:44 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48775C0611F9 for ; Thu, 17 Jun 2021 03:23:28 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id gb32so794785ejc.2 for ; Thu, 17 Jun 2021 03:23:28 -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=l8NsYz3rSr29PAhRjRC2o3IiZMJtHxbwQP29AB1FPMQ=; b=oyHcvrxdGFDPKPz21VOOSICvOcFyntVeiDKH4kOBXB6uNhsGHJ3V+LEmdkKH/HNzv4 JIlKHZLqFNl/rhgTdIr4O4ISvpN+x1qh+FybcaCHl2YWMRgRySZ14/CMxOZS3qF7VkOF fcg8Wcvh8LQisyYhDZ0L2NxTvFKCMmnMXJJ0PLyR0/yYrLW7l5dc7uIzeyUY4CXXyW1B e55gPNvEbvWOH8OKHmjhxAZG0tJturs0GChjfESSIE0Ly446rm/UL9Bwaa/N4cnVO1Y3 LnMNWrle5cwxTjh4H69LUDdcTEqZs1aJDlsNVf+ea99qnJ2tfjdaR8GACtgg78ww6iRP k9Og== 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=l8NsYz3rSr29PAhRjRC2o3IiZMJtHxbwQP29AB1FPMQ=; b=ouSL5/diojqD3hKxygIalmCJ9NBENECcAFzIp1yzlWHsbYPOxYUWGnUzieXYN8Sk2k VMhL+jdGgl/DSn6KO+VfKeCu3L/z7RBSWjnqsQ1e6PCJYRBtl6iFqpSz4uG7a7fBo2/n EJKdOzciPNr76pchH6OQfGhbPl3G01DTPlPfS6GpoCpG/pNsI4fjzhmG+D7/BkPhngpU 9SyBa0HFxZ3QXCbki2H811I76fvCRY3MNBbBVuNiwv/hdhuUH22VLG5c3143eKmGklXF JjD3xllelEmJnt4iSTwx102Ac41R4M6uajiwU7/yD2t2BkGLfAT3yMbQcLoY+F5h1VtD uypA== X-Gm-Message-State: AOAM531y4sdLW9Rr14iOCreKPejHm78TxMWbPVKmwAOYoD+UVFO7a06X /RMCJOir8ptAIASKtDEVEMXTz0ukoZpwHQ== X-Google-Smtp-Source: ABdhPJwm/WL3t9rbJqVkzT2Ei1OxrVmMRfg8jnR6asd6jbhn7mAPTPMwOyB9ACiIJM9PZzSfovYb/A== X-Received: by 2002:a17:907:6f2:: with SMTP id yh18mr4439241ejb.5.1623925406607; Thu, 17 Jun 2021 03:23:26 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:26 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 21/27] reference-transaction: use hook.h to run hooks Date: Thu, 17 Jun 2021 12:22:55 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- refs.c | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/refs.c b/refs.c index 59be29cf08..1149e7e7dc 100644 --- a/refs.c +++ b/refs.c @@ -2062,47 +2062,35 @@ int ref_update_reject_duplicates(struct string_list *refnames, static int run_transaction_hook(struct ref_transaction *transaction, const char *state) { - struct child_process proc = CHILD_PROCESS_INIT; struct strbuf buf = STRBUF_INIT; - const char *hook; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct string_list to_stdin = STRING_LIST_INIT_DUP; int ret = 0, i; + char o[GIT_MAX_HEXSZ + 1], n[GIT_MAX_HEXSZ + 1]; - hook = find_hook("reference-transaction"); - if (!hook) + if (!hook_exists("reference-transaction")) return ret; - strvec_pushl(&proc.args, hook, state, NULL); - proc.in = -1; - proc.stdout_to_stderr = 1; - proc.trace2_hook_name = "reference-transaction"; - - ret = start_command(&proc); - if (ret) - return ret; - - sigchain_push(SIGPIPE, SIG_IGN); + strvec_push(&opt.args, state); for (i = 0; i < transaction->nr; i++) { struct ref_update *update = transaction->updates[i]; + oid_to_hex_r(o, &update->old_oid); + oid_to_hex_r(n, &update->new_oid); strbuf_reset(&buf); - strbuf_addf(&buf, "%s %s %s\n", - oid_to_hex(&update->old_oid), - oid_to_hex(&update->new_oid), - update->refname); - - if (write_in_full(proc.in, buf.buf, buf.len) < 0) { - if (errno != EPIPE) - ret = -1; - break; - } + strbuf_addf(&buf, "%s %s %s", o, n, update->refname); + string_list_append(&to_stdin, buf.buf); } - close(proc.in); - sigchain_pop(SIGPIPE); + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; + + ret = run_hooks("reference-transaction", &opt); + run_hooks_opt_clear(&opt); strbuf_release(&buf); + string_list_clear(&to_stdin, 0); - ret |= finish_command(&proc); return ret; } From patchwork Thu Jun 17 10:22:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327365 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 A8CFDC2B9F4 for ; Thu, 17 Jun 2021 10:24:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4FCDA61057 for ; Thu, 17 Jun 2021 10:24:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232200AbhFQK0N (ORCPT ); Thu, 17 Jun 2021 06:26:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232052AbhFQKZn (ORCPT ); Thu, 17 Jun 2021 06:25:43 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29FC0C0611FA for ; Thu, 17 Jun 2021 03:23:29 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id l1so8998266ejb.6 for ; Thu, 17 Jun 2021 03:23:29 -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=kFJeV17qGyARyccj49s4e3wij8+nEK0KT0IfkNYY6sE=; b=Knhx7gLa6Ngsaqeh1Bork7lG+H5m9k4avN5IurUvBSK/Zstcy2mwa2hFxO8ww6wZUL HrH7K53cMoU6N3jiYTPBsmb2W3GNDzA/Mmnt6kc4GqkJSfrFkQhL9a7YzacyiCqOHiT0 k31GkULYCWQ3mamaTMP0pCpwV0Dqy8qRCUso5owGRpFdelXwT4XrRwjeA+lmXq7qr59b acwo2w4xF9Gtww5qxhi2qrOzlw5lHflJRT2CrDe4TOfMInIGyDsOeJgvO+YhPmSU1SNB rGYm/7GSxWMqoMMiGwltCqyd2v8eRRQzL8u94Ps7WhaUinPTd6SeYPM2HRnepsyuuWMI legQ== 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=kFJeV17qGyARyccj49s4e3wij8+nEK0KT0IfkNYY6sE=; b=ew2bmFgs1koy0FGVYVJPB0l2rMLkrz0q2V6Gv5jfgk3QZ3O7yXs/lNADg6yYJgyAu5 1bKW/zlOfOe2CNNntWmeY14s8okODfnqFC3yY9ABltU0d8xovivZ7Y2LSFgK5e/uBC4s /7Hejtc0oF+uaMqjB53X0EhJPXacYqeFeZC+BCeL12vdtJiw6KbdUYG1vePG2N4x9Yjo jxMF59IcfpMVVsDRiaSUtlwsQ30hcVXV6cZsRO4S577sU0kKviDtf/yUzKGSD9Z5vhMQ 1tkfnIdUUKTqYa+ThMpmKjrncyROdo+T9/qwweqQJgZGhiNTDDNpZPYuw+RWXEb61yRJ Zp/A== X-Gm-Message-State: AOAM533RJ5rMYIuHoZUCbJVxi+e7JVubYZrwzZsv9NZuYtfpSxI6rjOb CV6keYqYvissod040j6E0eLZ6mxkebH4UA== X-Google-Smtp-Source: ABdhPJx0cPR/qMCM5nMWrWJxEz03QjEJeZU6Kq6GOroFxJhWQhZ8xLgunEhWr2GPtL5NAL6xRljyow== X-Received: by 2002:a17:906:488f:: with SMTP id v15mr4358724ejq.428.1623925407477; Thu, 17 Jun 2021 03:23:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:27 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 22/27] run-command: allow capturing of collated output Date: Thu, 17 Jun 2021 12:22:56 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Some callers, for example server-side hooks which wish to relay hook output to clients across a transport, want to capture what would normally print to stderr and do something else with it. Allow that via a callback. By calling the callback regardless of whether there's output available, we allow clients to send e.g. a keepalive if necessary. Because we expose a strbuf, not a fd or FILE*, there's no need to create a temporary pipe or similar - we can just skip the print to stderr and instead hand it to the caller. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fetch.c | 2 +- builtin/submodule--helper.c | 2 +- hook.c | 1 + run-command.c | 33 +++++++++++++++++++++++++-------- run-command.h | 18 +++++++++++++++++- submodule.c | 2 +- t/helper/test-run-command.c | 25 ++++++++++++++++++++----- t/t0061-run-command.sh | 7 +++++++ 8 files changed, 73 insertions(+), 17 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index 3d8f04b392..0595b88b7c 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -1817,7 +1817,7 @@ static int fetch_multiple(struct string_list *list, int max_children) result = run_processes_parallel_tr2(max_children, &fetch_next_remote, &fetch_failed_to_start, - NULL, + NULL, NULL, &fetch_finished, &state, "fetch", "parallel/fetch"); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 818494dd18..69782f6204 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -2295,7 +2295,7 @@ static int update_submodules(struct submodule_update_clone *suc) int i; run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task, - update_clone_start_failure, NULL, + update_clone_start_failure, NULL, NULL, update_clone_task_finished, suc, "submodule", "parallel/update"); diff --git a/hook.c b/hook.c index c4df787f13..2dca969f9e 100644 --- a/hook.c +++ b/hook.c @@ -192,6 +192,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, pick_next_hook, notify_start_failure, options->feed_pipe, + NULL, notify_hook_finished, &cb_data, "hook", diff --git a/run-command.c b/run-command.c index 3392640f17..4a1a7a1082 100644 --- a/run-command.c +++ b/run-command.c @@ -1494,6 +1494,7 @@ struct parallel_processes { get_next_task_fn get_next_task; start_failure_fn start_failure; feed_pipe_fn feed_pipe; + consume_sideband_fn consume_sideband; task_finished_fn task_finished; struct { @@ -1559,6 +1560,7 @@ static void pp_init(struct parallel_processes *pp, get_next_task_fn get_next_task, start_failure_fn start_failure, feed_pipe_fn feed_pipe, + consume_sideband_fn consume_sideband, task_finished_fn task_finished, void *data) { @@ -1579,6 +1581,7 @@ static void pp_init(struct parallel_processes *pp, pp->start_failure = start_failure ? start_failure : default_start_failure; pp->feed_pipe = feed_pipe ? feed_pipe : default_feed_pipe; pp->task_finished = task_finished ? task_finished : default_task_finished; + pp->consume_sideband = consume_sideband; pp->nr_processes = 0; pp->output_owner = 0; @@ -1615,7 +1618,10 @@ static void pp_cleanup(struct parallel_processes *pp) * When get_next_task added messages to the buffer in its last * iteration, the buffered output is non empty. */ - strbuf_write(&pp->buffered_output, stderr); + if (pp->consume_sideband) + pp->consume_sideband(&pp->buffered_output, pp->data); + else + strbuf_write(&pp->buffered_output, stderr); strbuf_release(&pp->buffered_output); sigchain_pop_common(); @@ -1736,9 +1742,13 @@ static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout) static void pp_output(struct parallel_processes *pp) { int i = pp->output_owner; + if (pp->children[i].state == GIT_CP_WORKING && pp->children[i].err.len) { - strbuf_write(&pp->children[i].err, stderr); + if (pp->consume_sideband) + pp->consume_sideband(&pp->children[i].err, pp->data); + else + strbuf_write(&pp->children[i].err, stderr); strbuf_reset(&pp->children[i].err); } } @@ -1777,11 +1787,15 @@ static int pp_collect_finished(struct parallel_processes *pp) strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); strbuf_reset(&pp->children[i].err); } else { - strbuf_write(&pp->children[i].err, stderr); + /* Output errors, then all other finished child processes */ + if (pp->consume_sideband) { + pp->consume_sideband(&pp->children[i].err, pp->data); + pp->consume_sideband(&pp->buffered_output, pp->data); + } else { + strbuf_write(&pp->children[i].err, stderr); + strbuf_write(&pp->buffered_output, stderr); + } strbuf_reset(&pp->children[i].err); - - /* Output all other finished child processes */ - strbuf_write(&pp->buffered_output, stderr); strbuf_reset(&pp->buffered_output); /* @@ -1805,6 +1819,7 @@ int run_processes_parallel(int n, get_next_task_fn get_next_task, start_failure_fn start_failure, feed_pipe_fn feed_pipe, + consume_sideband_fn consume_sideband, task_finished_fn task_finished, void *pp_cb) { @@ -1815,7 +1830,7 @@ int run_processes_parallel(int n, sigchain_push(SIGPIPE, SIG_IGN); - pp_init(&pp, n, get_next_task, start_failure, feed_pipe, task_finished, pp_cb); + pp_init(&pp, n, get_next_task, start_failure, feed_pipe, consume_sideband, task_finished, pp_cb); while (1) { for (i = 0; i < spawn_cap && !pp.shutdown && @@ -1853,6 +1868,7 @@ int run_processes_parallel(int n, int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task, start_failure_fn start_failure, feed_pipe_fn feed_pipe, + consume_sideband_fn consume_sideband, task_finished_fn task_finished, void *pp_cb, const char *tr2_category, const char *tr2_label) { @@ -1862,7 +1878,8 @@ int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task, ((n < 1) ? online_cpus() : n)); result = run_processes_parallel(n, get_next_task, start_failure, - feed_pipe, task_finished, pp_cb); + feed_pipe, consume_sideband, + task_finished, pp_cb); trace2_region_leave(tr2_category, tr2_label, NULL); diff --git a/run-command.h b/run-command.h index 41e36d26cb..7150da851a 100644 --- a/run-command.h +++ b/run-command.h @@ -433,6 +433,20 @@ typedef int (*feed_pipe_fn)(struct strbuf *pipe, void *pp_cb, void *pp_task_cb); +/** + * If this callback is provided, instead of collating process output to stderr, + * they will be collated into a new pipe. consume_sideband_fn will be called + * repeatedly. When output is available on that pipe, it will be contained in + * 'output'. But it will be called with an empty 'output' too, to allow for + * keepalives or similar operations if necessary. + * + * pp_cb is the callback cookie as passed into run_processes_parallel. + * + * Since this callback is provided with the collated output, no task cookie is + * provided. + */ +typedef void (*consume_sideband_fn)(struct strbuf *output, void *pp_cb); + /** * This callback is called on every child process that finished processing. * @@ -468,10 +482,12 @@ int run_processes_parallel(int n, get_next_task_fn, start_failure_fn, feed_pipe_fn, + consume_sideband_fn, task_finished_fn, void *pp_cb); int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn, - feed_pipe_fn, task_finished_fn, void *pp_cb, + feed_pipe_fn, consume_sideband_fn, + task_finished_fn, void *pp_cb, const char *tr2_category, const char *tr2_label); #endif diff --git a/submodule.c b/submodule.c index ea026a8195..7fe0c8f7c9 100644 --- a/submodule.c +++ b/submodule.c @@ -1645,7 +1645,7 @@ int fetch_populated_submodules(struct repository *r, run_processes_parallel_tr2(max_parallel_jobs, get_next_submodule, fetch_start_failure, - NULL, + NULL, NULL, fetch_finish, &spf, "submodule", "parallel/fetch"); diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index 9348184d30..d53db6d11c 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -51,6 +51,16 @@ static int no_job(struct child_process *cp, return 0; } +static void test_consume_sideband(struct strbuf *output, void *cb) +{ + FILE *sideband; + + sideband = fopen("./sideband", "a"); + + strbuf_write(output, sideband); + fclose(sideband); +} + static int task_finished(int result, struct strbuf *err, void *pp_cb, @@ -201,7 +211,7 @@ static int testsuite(int argc, const char **argv) suite.tests.nr, max_jobs); ret = run_processes_parallel(max_jobs, next_test, test_failed, - test_stdin, test_finished, &suite); + test_stdin, NULL, test_finished, &suite); if (suite.failed.nr > 0) { ret = 1; @@ -429,23 +439,28 @@ int cmd__run_command(int argc, const char **argv) if (!strcmp(argv[1], "run-command-parallel")) exit(run_processes_parallel(jobs, parallel_next, - NULL, NULL, NULL, &proc)); + NULL, NULL, NULL, NULL, &proc)); if (!strcmp(argv[1], "run-command-abort")) exit(run_processes_parallel(jobs, parallel_next, - NULL, NULL, task_finished, &proc)); + NULL, NULL, NULL, task_finished, &proc)); if (!strcmp(argv[1], "run-command-no-jobs")) exit(run_processes_parallel(jobs, no_job, - NULL, NULL, task_finished, &proc)); + NULL, NULL, NULL, task_finished, &proc)); if (!strcmp(argv[1], "run-command-stdin")) { proc.in = -1; proc.no_stdin = 0; exit (run_processes_parallel(jobs, parallel_next, NULL, - test_stdin, NULL, &proc)); + test_stdin, NULL, NULL, &proc)); } + if (!strcmp(argv[1], "run-command-sideband")) + exit(run_processes_parallel(jobs, parallel_next, NULL, NULL, + test_consume_sideband, NULL, + &proc)); + fprintf(stderr, "check usage\n"); return 1; } diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 87759482ad..e99f6c7f44 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -143,6 +143,13 @@ test_expect_success 'run_command runs in parallel with more tasks than jobs avai test_cmp expect actual ' +test_expect_success 'run_command can divert output' ' + test_when_finished rm sideband && + test-tool run-command run-command-sideband 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual && + test_must_be_empty actual && + test_cmp expect sideband +' + cat >expect <<-EOF preloaded output of a child listening for stdin: From patchwork Thu Jun 17 10:22:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327369 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 3E645C2B9F4 for ; Thu, 17 Jun 2021 10:24:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2957061263 for ; Thu, 17 Jun 2021 10:24:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232164AbhFQK0Q (ORCPT ); Thu, 17 Jun 2021 06:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232014AbhFQKZp (ORCPT ); Thu, 17 Jun 2021 06:25:45 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58416C0611BC for ; Thu, 17 Jun 2021 03:23:30 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id nb6so8907786ejc.10 for ; Thu, 17 Jun 2021 03:23:30 -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=2q84uGasL8ocPuaf06RJk4OoAS9Os898GqdlZKBJydM=; b=drNoiDc1zDV7CfZLbneoIgqX2ZDzXPcTG8eFuPuesJwF/DZKMBl++PpOasB3L8zz3V RqdMUbSRQQCQWf8h3ZK20Gv8OmM1Ddkd2Qsaxt5y1vsbfeDHTJHEHVxmCkm+6P9PcLNS Rf2PA97Mf4L6JiBSo2n+4wHK8xHR2xkZBNqOM5GkYN/E915JNakXFbEJPz/QFkdDngZl jO1NG6e5EG8pSHCJ3a0Mqbv3Ja3QrXnAxH7JMidgErVlFS8kPLaGiGKQhpueg65Wz+PQ g9W2Kt7/IvTuzLxaOO0OaaeiQQtxoErRJKfEj3+Hf5gjhmQhbadMcZ4iINXh+kABUTWE DGTg== 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=2q84uGasL8ocPuaf06RJk4OoAS9Os898GqdlZKBJydM=; b=HJjgcRrvk6uUgj1i3Z+OTEuG3CfW05+8//6XDVBEhJ/NagTqkt66ODZeiQ8Q+kvp01 FwsTywpjXfJINes92vpcTgRP+JWukBcgs9N2yZ+7P3+mTgqkVl82L2MAvCWwR3jJvU3S miOw1W9z/EtlROgi1TTDFYTypAuCe0tdQftRGjz8/Vxg6ShglXR2hz65WO3IHUP44AzA Nqo2mVTJA8pjSrTcksxnbgYCN8uMzdPSmcri6wFHbXVWyU+JFR+j1l4qq5Xad5Ijv7iz 3REkwqLBlqouIVjDECN++Y1sLKWqe+c8+o9TPuzKl/dYfJWvPsIsEPh4xTrOXJMUmmew 1HaQ== X-Gm-Message-State: AOAM532Bc2DlMCerpqBkwotSP2PTtebV8GRZFxqKVNZDLDtL8tT6RYjF lvuO2GUkmke0EXjII3xCewDmZ65yoDLZOw== X-Google-Smtp-Source: ABdhPJyv/lqxX4VhIBqLA60AMyg1JEpMrWDlX4VDTBWWxn9MlYvikHD8aFOTe8TIhRQ5RjCcQe9Leg== X-Received: by 2002:a17:906:744:: with SMTP id z4mr4470904ejb.347.1623925408762; Thu, 17 Jun 2021 03:23:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:27 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 23/27] hooks: allow callers to capture output Date: Thu, 17 Jun 2021 12:22:57 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Some server-side hooks will require capturing output to send over sideband instead of printing directly to stderr. Expose that capability. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- hook.c | 2 +- hook.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/hook.c b/hook.c index 2dca969f9e..e7f3b468ea 100644 --- a/hook.c +++ b/hook.c @@ -192,7 +192,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, pick_next_hook, notify_start_failure, options->feed_pipe, - NULL, + options->consume_sideband, notify_hook_finished, &cb_data, "hook", diff --git a/hook.h b/hook.h index b78a8e4805..1302efa590 100644 --- a/hook.h +++ b/hook.h @@ -66,6 +66,14 @@ struct run_hooks_opt */ feed_pipe_fn feed_pipe; void *feed_pipe_ctx; + + /* + * Populate this to capture output and prevent it from being printed to + * stderr. This will be passed directly through to + * run_command:run_parallel_processes(). See t/helper/test-run-command.c + * for an example. + */ + consume_sideband_fn consume_sideband; }; #define RUN_HOOKS_OPT_INIT { \ From patchwork Thu Jun 17 10:22:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327371 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 61B2CC49361 for ; Thu, 17 Jun 2021 10:24:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4367D61245 for ; Thu, 17 Jun 2021 10:24:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232283AbhFQK0R (ORCPT ); Thu, 17 Jun 2021 06:26:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232195AbhFQKZq (ORCPT ); Thu, 17 Jun 2021 06:25:46 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3484FC0611BD for ; Thu, 17 Jun 2021 03:23:31 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id i13so3242146edb.9 for ; Thu, 17 Jun 2021 03:23:31 -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=6XGkB4nZ+AQpvz3PnGx+LTGeUuRHYf+xnAxUcuc5hFM=; b=IKKPVSSkkG7z6H7o7x7JNIlAcHcvscX/YZ2zKH6jCE7AcAlLibauJj5nHGIqrdbgCf lQ59+MSszXJ7tfHCBOFE82dxeLJaOo2p0QX1ocTVlqg5EjQqPCjqCTsaZmunv2zqI12K OeBCvsYuayZsR9tTCpgkzbIipcS/3GYBKR5dpFxIcoLz0+R3vV1lm9zyvK4pxg7g2UmD Z8LlgSsEcAV7mKzuWgmKdyCmCBJV7z200PTPtU62MBkSffH8bfTymTjRNogL1U66ev93 gPHcSGeeLtUerS2EyXd5i4t4dvxboT4NzEZ+kpS/P95FM7u+DKuHi1sISN/df3B24fyK Nsow== 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=6XGkB4nZ+AQpvz3PnGx+LTGeUuRHYf+xnAxUcuc5hFM=; b=AqIY44PTevNrj4HFud0jEnTueURXxNX2m3evEWK7QPR6Tp7D212tX5/I88ueIeI5Ru Cwu6bUHfph8adWmJ6fEfqwekJx6FZgcVq5Zo5X271GotM3zvt3IBicklAwS6ZuSJ+guT OMdVOlD8Bb5MInxBgBmlVf/TrckwmR6TveVPF6DRM6v3q8Tw0xgMOEEN4K5CVyHdRHiT xaVMyGThTolV2RwXNBixDUyqFYJ7S5uYlLz0DLyaDMwE9O24gZyNpl1DdQ0NCzCHnuTJ ylaBSV5WUv4JphvwukyzqHunU/CeHa8dpieNGc6REQiguWfRRlC/2S+BauduM+SkcQhv DR8Q== X-Gm-Message-State: AOAM532flShdQaZmrTIss1alVyVESZ/5v2hlEtzy2jNFMNoA1TIy2C/o om+6rUYM4W9EArso3RMtTRN4oM0U1eBt+A== X-Google-Smtp-Source: ABdhPJyyMBzeM4I5f+EGCCN0wRMaWslOQ6PCDdag5089OAr6VFpVRL6qmNL3k+WAbTPJLLCyNoKmnw== X-Received: by 2002:aa7:d713:: with SMTP id t19mr5382333edq.144.1623925409592; Thu, 17 Jun 2021 03:23:29 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:29 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 24/27] receive-pack: convert 'update' hook to hook.h Date: Thu, 17 Jun 2021 12:22:58 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer This makes use of the new sideband API in hook.h added in the preceding commit. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 5248228ebf..378f8f6b5d 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -938,33 +938,56 @@ static int run_receive_hook(struct command *commands, return status; } -static int run_update_hook(struct command *cmd) +static void hook_output_to_sideband(struct strbuf *output, void *cb_data) { - const char *argv[5]; - struct child_process proc = CHILD_PROCESS_INIT; - int code; + int keepalive_active = 0; - argv[0] = find_hook("update"); - if (!argv[0]) - return 0; + if (keepalive_in_sec <= 0) + use_keepalive = KEEPALIVE_NEVER; + if (use_keepalive == KEEPALIVE_ALWAYS) + keepalive_active = 1; - argv[1] = cmd->ref_name; - argv[2] = oid_to_hex(&cmd->old_oid); - argv[3] = oid_to_hex(&cmd->new_oid); - argv[4] = NULL; + /* send a keepalive if there is no data to write */ + if (keepalive_active && !output->len) { + static const char buf[] = "0005\1"; + write_or_die(1, buf, sizeof(buf) - 1); + return; + } - proc.no_stdin = 1; - proc.stdout_to_stderr = 1; - proc.err = use_sideband ? -1 : 0; - proc.argv = argv; - proc.trace2_hook_name = "update"; + if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) { + const char *first_null = memchr(output->buf, '\0', output->len); + if (first_null) { + /* The null bit is excluded. */ + size_t before_null = first_null - output->buf; + size_t after_null = output->len - (before_null + 1); + keepalive_active = 1; + send_sideband(1, 2, output->buf, before_null, use_sideband); + send_sideband(1, 2, first_null + 1, after_null, use_sideband); + + return; + } + } + + send_sideband(1, 2, output->buf, output->len, use_sideband); +} + +static int run_update_hook(struct command *cmd) +{ + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + int code; + + strvec_pushl(&opt.args, + cmd->ref_name, + oid_to_hex(&cmd->old_oid), + oid_to_hex(&cmd->new_oid), + NULL); - code = start_command(&proc); - if (code) - return code; if (use_sideband) - copy_to_sideband(proc.err, -1, NULL); - return finish_command(&proc); + opt.consume_sideband = hook_output_to_sideband; + + code = run_hooks("update", &opt); + run_hooks_opt_clear(&opt); + return code; } static struct command *find_command_by_refname(struct command *list, From patchwork Thu Jun 17 10:22:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327373 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 66EC6C48BE5 for ; Thu, 17 Jun 2021 10:24:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F24B61057 for ; Thu, 17 Jun 2021 10:24:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232213AbhFQK0S (ORCPT ); Thu, 17 Jun 2021 06:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231925AbhFQKZq (ORCPT ); Thu, 17 Jun 2021 06:25:46 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2509C06175F for ; Thu, 17 Jun 2021 03:23:31 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id ji1so2838679ejc.4 for ; Thu, 17 Jun 2021 03:23:31 -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=mlEQsBswfm9PJHjnBmRO4pSRm8aTdjk9znLndlRKjsk=; b=so6CgctMgHqUPyMdd/Ie9q9BXYFzvFb5JIRJOTpWN++QNnRmdc5Lv1gy7XKiRTN6N/ dfEleZTcCuXPVgmGfp5vlb9XrrcdAQPmdcYEK1yJUAhtQZChCXXTvSr2iAky/VKExvl/ T7YWLui6ts9qsboZebBOuHAPO/u/VUWjyEZDxHXaq8yiqMvxQY4hcuKDYJpg+Ip4QRKz ej34h5DS39Wr+JNa9yxlhyG2n+H8MhFs9SHKISSWpI7qJuaYja/OY80p3rZdFAi7Mou4 X79RHULjhPmhlnBa6BpB7zBynQQArSvwP/rSLBCBLsWkWnYz3NCtehI2g87S9NmPcmhC RBSQ== 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=mlEQsBswfm9PJHjnBmRO4pSRm8aTdjk9znLndlRKjsk=; b=QM65sfxuxAKhcsHAegORREjkEik2krANSr+bBCFxHJ7+lZhjiQAcnWyNw0Fq4SYpGs F+MtAeiDQuRqT7Yfhei5GwA0I0aKJSv9ePQXqE8iHMU/CvK0Hrp4Av3DxLuDVsZ9bDOC kvmtNcbRsN8cMLXn9uL2cJUlTsppTPUd0YTkxT8NGNVcN74JJbScXJ7/mRAt9yMafd4e G9Td8ESd5AywDahD2wPql5jL7RfLPeXPVWKoBGsFEf+qDlm1pHMGu4+CzQWRURvt4L6X Gnp52/SoRZhnMrDPDQlKxyDC86wUsJHuWOJaR/UtORxuseJ4Xz8p+JMZhYXgEDTZmm7K OTEA== X-Gm-Message-State: AOAM533ZO9JtUIVaqJwBaos2wA0T4/SySy+S2SGxZclmkC0mxGTU1Qzq DSGqWdxa90tgWEdAHOXjpct/0Fuaj+r1yw== X-Google-Smtp-Source: ABdhPJwRuIpLoVJoCzOUIQbZlsV2QIh6R3KmeLUEcYDeprmf/jW3rCTrwLPMXZGOioXvV+eMu4zyGg== X-Received: by 2002:a17:906:c211:: with SMTP id d17mr4436797ejz.247.1623925410404; Thu, 17 Jun 2021 03:23:30 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:30 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 25/27] post-update: use hook.h library Date: Thu, 17 Jun 2021 12:22:59 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 378f8f6b5d..b2ccdb66da 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1657,33 +1657,21 @@ static const char *update(struct command *cmd, struct shallow_info *si) static void run_update_post_hook(struct command *commands) { struct command *cmd; - struct child_process proc = CHILD_PROCESS_INIT; - const char *hook; - - hook = find_hook("post-update"); - if (!hook) - return; + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; for (cmd = commands; cmd; cmd = cmd->next) { if (cmd->error_string || cmd->did_not_exist) continue; - if (!proc.args.nr) - strvec_push(&proc.args, hook); - strvec_push(&proc.args, cmd->ref_name); + strvec_push(&opt.args, cmd->ref_name); } - if (!proc.args.nr) + if (!opt.args.nr) return; - proc.no_stdin = 1; - proc.stdout_to_stderr = 1; - proc.err = use_sideband ? -1 : 0; - proc.trace2_hook_name = "post-update"; + if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; - if (!start_command(&proc)) { - if (use_sideband) - copy_to_sideband(proc.err, -1, NULL); - finish_command(&proc); - } + run_hooks("post-update", &opt); + run_hooks_opt_clear(&opt); } static void check_aliased_update_internal(struct command *cmd, From patchwork Thu Jun 17 10:23:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327375 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 CD2C5C49361 for ; Thu, 17 Jun 2021 10:24:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B57D661057 for ; Thu, 17 Jun 2021 10:24:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232291AbhFQK0T (ORCPT ); Thu, 17 Jun 2021 06:26:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232117AbhFQKZs (ORCPT ); Thu, 17 Jun 2021 06:25:48 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E883FC061767 for ; Thu, 17 Jun 2021 03:23:32 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id gb32so795124ejc.2 for ; Thu, 17 Jun 2021 03:23:32 -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=pJwjMp0mHjk/pFDx+636Av/tPIYEG95gqv562PmZZEU=; b=O4o1uwoi9tlK1D9g5gkrtKM9KKE5ws9Y/MjqIAUrw2c678arTT9pI8D3x0CugC9QcW fT0wWUTRAWRvrPe3sJ9ZmxVtFEaLqN77QIcUtS3F8D/c5aK5/T/j2uU3iMbfWTnn9Gp9 OpETryokugPaoprbHD7otSW2D4/S61fnHXfjBHVNLV47A1yprfrgQ0Pl8PGrmBJFsB31 hevHGkEL2aFObatPtmRbFx/vCAWqS9WwDzAtgosg47MkO2dX8Svk53COfIaA5eg03IQo +qgDCN/dinmOX4mZW4fjjeeCRECIU6KB/fIT/Nefj7SlzwMkvq9Q2BrptGT7LBkXFky/ kXEg== 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=pJwjMp0mHjk/pFDx+636Av/tPIYEG95gqv562PmZZEU=; b=IIAUTE1VDa3VZq30HHMZuQEFJOewwmSyNIrymuvH/VDw2ZdbV2GHJYRFycBPJCM5RS 6fBrrUyFk1m+rwYaD6jEQR4wvaVHu/UmqIl0GhpkkZcOVDLUHVM2l1EhPiVhuc83webM kKwua+GvHCHnKjn7L/QS75dlIZGbY/dii7X5suzmGuiGKQRIpxPw/dX90qj+NjJme6o1 053TwerhDdYQAn+2a4/egs3Y+OlKBYWxbqt/3RqbtCLHbkQ1anI9ZWMOoODa1cBMtKPR N1eOkvuCrAkKtll0e8pDLJz9LkwWT5C47gHUXImD9dNW5I4ciUn4MrcUbWwe9a1wsrrK u86Q== X-Gm-Message-State: AOAM533w1EiIRqVOoT+LeC9/N+gy++/ti6ayU1Sx7Dor7Z0O+RhlDITU oYRldkt4XSqSJzSO8O5F6X5zwexQgq2vSA== X-Google-Smtp-Source: ABdhPJz48RqwRrvnqWZ7NjYnvlOvVKAZdffuYPXAtDrmyti1hZaVNBfprlyrPbYul4pqlxF+D2qeMg== X-Received: by 2002:a17:906:234e:: with SMTP id m14mr2341175eja.217.1623925411307; Thu, 17 Jun 2021 03:23:31 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:30 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 26/27] receive-pack: convert receive hooks to hook.h Date: Thu, 17 Jun 2021 12:23:00 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 197 +++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 107 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index b2ccdb66da..ec90e10477 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -748,7 +748,7 @@ static int check_cert_push_options(const struct string_list *push_options) return retval; } -static void prepare_push_cert_sha1(struct child_process *proc) +static void prepare_push_cert_sha1(struct run_hooks_opt *opt) { static int already_done; @@ -772,110 +772,42 @@ static void prepare_push_cert_sha1(struct child_process *proc) nonce_status = check_nonce(push_cert.buf, bogs); } if (!is_null_oid(&push_cert_oid)) { - strvec_pushf(&proc->env_array, "GIT_PUSH_CERT=%s", + strvec_pushf(&opt->env, "GIT_PUSH_CERT=%s", oid_to_hex(&push_cert_oid)); - strvec_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s", + strvec_pushf(&opt->env, "GIT_PUSH_CERT_SIGNER=%s", sigcheck.signer ? sigcheck.signer : ""); - strvec_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s", + strvec_pushf(&opt->env, "GIT_PUSH_CERT_KEY=%s", sigcheck.key ? sigcheck.key : ""); - strvec_pushf(&proc->env_array, "GIT_PUSH_CERT_STATUS=%c", + strvec_pushf(&opt->env, "GIT_PUSH_CERT_STATUS=%c", sigcheck.result); if (push_cert_nonce) { - strvec_pushf(&proc->env_array, + strvec_pushf(&opt->env, "GIT_PUSH_CERT_NONCE=%s", push_cert_nonce); - strvec_pushf(&proc->env_array, + strvec_pushf(&opt->env, "GIT_PUSH_CERT_NONCE_STATUS=%s", nonce_status); if (nonce_status == NONCE_SLOP) - strvec_pushf(&proc->env_array, + strvec_pushf(&opt->env, "GIT_PUSH_CERT_NONCE_SLOP=%ld", nonce_stamp_slop); } } } +struct receive_hook_feed_context { + struct command *cmd; + int skip_broken; +}; + struct receive_hook_feed_state { struct command *cmd; struct ref_push_report *report; int skip_broken; struct strbuf buf; - const struct string_list *push_options; }; -typedef int (*feed_fn)(void *, const char **, size_t *); -static int run_and_feed_hook(const char *hook_name, feed_fn feed, - struct receive_hook_feed_state *feed_state) -{ - struct child_process proc = CHILD_PROCESS_INIT; - struct async muxer; - const char *argv[2]; - int code; - - argv[0] = find_hook(hook_name); - if (!argv[0]) - return 0; - - argv[1] = NULL; - - proc.argv = argv; - proc.in = -1; - proc.stdout_to_stderr = 1; - proc.trace2_hook_name = hook_name; - - if (feed_state->push_options) { - int i; - for (i = 0; i < feed_state->push_options->nr; i++) - strvec_pushf(&proc.env_array, - "GIT_PUSH_OPTION_%d=%s", i, - feed_state->push_options->items[i].string); - strvec_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d", - feed_state->push_options->nr); - } else - strvec_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT"); - - if (tmp_objdir) - strvec_pushv(&proc.env_array, tmp_objdir_env(tmp_objdir)); - - if (use_sideband) { - memset(&muxer, 0, sizeof(muxer)); - muxer.proc = copy_to_sideband; - muxer.in = -1; - code = start_async(&muxer); - if (code) - return code; - proc.err = muxer.in; - } - - prepare_push_cert_sha1(&proc); - - code = start_command(&proc); - if (code) { - if (use_sideband) - finish_async(&muxer); - return code; - } - - sigchain_push(SIGPIPE, SIG_IGN); - - while (1) { - const char *buf; - size_t n; - if (feed(feed_state, &buf, &n)) - break; - if (write_in_full(proc.in, buf, n) < 0) - break; - } - close(proc.in); - if (use_sideband) - finish_async(&muxer); - - sigchain_pop(SIGPIPE); - - return finish_command(&proc); -} - -static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) +static int feed_receive_hook(void *state_) { struct receive_hook_feed_state *state = state_; struct command *cmd = state->cmd; @@ -884,9 +816,7 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) state->skip_broken && (cmd->error_string || cmd->did_not_exist)) cmd = cmd->next; if (!cmd) - return -1; /* EOF */ - if (!bufp) - return 0; /* OK, can feed something. */ + return 1; /* EOF - close the pipe*/ strbuf_reset(&state->buf); if (!state->report) state->report = cmd->report; @@ -910,32 +840,36 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) cmd->ref_name); state->cmd = cmd->next; } - if (bufp) { - *bufp = state->buf.buf; - *sizep = state->buf.len; - } return 0; } -static int run_receive_hook(struct command *commands, - const char *hook_name, - int skip_broken, - const struct string_list *push_options) +static int feed_receive_hook_cb(struct strbuf *pipe, void *pp_cb, void *pp_task_cb) { - struct receive_hook_feed_state state; - int status; - - strbuf_init(&state.buf, 0); - state.cmd = commands; - state.skip_broken = skip_broken; - state.report = NULL; - if (feed_receive_hook(&state, NULL, NULL)) - return 0; - state.cmd = commands; - state.push_options = push_options; - status = run_and_feed_hook(hook_name, feed_receive_hook, &state); - strbuf_release(&state.buf); - return status; + struct hook *hook = pp_task_cb; + struct receive_hook_feed_state *feed_state = hook->feed_pipe_cb_data; + int rc; + + /* first-time setup */ + if (!feed_state) { + struct hook_cb_data *hook_cb = pp_cb; + struct run_hooks_opt *opt = hook_cb->options; + struct receive_hook_feed_context *ctx = opt->feed_pipe_ctx; + if (!ctx) + BUG("run_hooks_opt.feed_pipe_ctx required for receive hook"); + + feed_state = xmalloc(sizeof(struct receive_hook_feed_state)); + strbuf_init(&feed_state->buf, 0); + feed_state->cmd = ctx->cmd; + feed_state->skip_broken = ctx->skip_broken; + feed_state->report = NULL; + + hook->feed_pipe_cb_data = feed_state; + } + + rc = feed_receive_hook(feed_state); + if (!rc) + strbuf_addbuf(pipe, &feed_state->buf); + return rc; } static void hook_output_to_sideband(struct strbuf *output, void *cb_data) @@ -971,6 +905,55 @@ static void hook_output_to_sideband(struct strbuf *output, void *cb_data) send_sideband(1, 2, output->buf, output->len, use_sideband); } +static int run_receive_hook(struct command *commands, + const char *hook_name, + int skip_broken, + const struct string_list *push_options) +{ + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct receive_hook_feed_context ctx; + int rc; + struct command *iter = commands; + + /* if there are no valid commands, don't invoke the hook at all. */ + while (iter && skip_broken && (iter->error_string || iter->did_not_exist)) + iter = iter->next; + if (!iter) + return 0; + + /* pre-receive hooks should run in series as the hook updates refs */ + if (!strcmp(hook_name, "pre-receive")) + opt.jobs = 1; + + if (push_options) { + int i; + for (i = 0; i < push_options->nr; i++) + strvec_pushf(&opt.env, "GIT_PUSH_OPTION_%d=%s", i, + push_options->items[i].string); + strvec_pushf(&opt.env, "GIT_PUSH_OPTION_COUNT=%d", push_options->nr); + } else + strvec_push(&opt.env, "GIT_PUSH_OPTION_COUNT"); + + if (tmp_objdir) + strvec_pushv(&opt.env, tmp_objdir_env(tmp_objdir)); + + prepare_push_cert_sha1(&opt); + + /* set up sideband printer */ + if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; + + /* set up stdin callback */ + ctx.cmd = commands; + ctx.skip_broken = skip_broken; + opt.feed_pipe = feed_receive_hook_cb; + opt.feed_pipe_ctx = &ctx; + + rc = run_hooks(hook_name, &opt); + run_hooks_opt_clear(&opt); + return rc; +} + static int run_update_hook(struct command *cmd) { struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; From patchwork Thu Jun 17 10:23:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12327377 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 CEF84C48BE5 for ; Thu, 17 Jun 2021 10:24:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B5CFF61245 for ; Thu, 17 Jun 2021 10:24:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232224AbhFQK0U (ORCPT ); Thu, 17 Jun 2021 06:26:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232217AbhFQKZz (ORCPT ); Thu, 17 Jun 2021 06:25:55 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E515C0617A6 for ; Thu, 17 Jun 2021 03:23:34 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id ji1so2838810ejc.4 for ; Thu, 17 Jun 2021 03:23:33 -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=nt/lqeZPuN1sptc7xcpmdMuNc+0BI/dvWdf2Uxbk3iU=; b=WwgIK3CCtidJPkDt/RMxr8dQrB6ogRIdhF11XRB5y4mW9WNOR2BeOUL+SZVMQDcDBW obVwe/4x77slPgDN3XA0BFyt+8iDi/7PbIbmbOfQ/vGHNgLyh7kNKh1sl9GeEX/aa9iC 32RwlrE87KZ14Dw9ftCb2+rUfiJ9WquKxOWF5uBZ0E45kyV2GtFRN7+UF8K/sXsNIHgW irZP4xi3/ro2I77pN8tjJc+QPhwRDHAFxPQbtfnshpmdCd5FugOeG7kk7j8/exvg1par 2dZqLVrv2aw/QvJVytBVxawIyKxKzFcdf+7J2DmZamX1ASbk0Je4S4SQcUZPBxTFHNCL x/JA== 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=nt/lqeZPuN1sptc7xcpmdMuNc+0BI/dvWdf2Uxbk3iU=; b=eUvsb8oGIDw7/s8YOt285xhjQWrqAIm6Qjgjb7TWupckhJuUrYH01uxOkoH6Ntwm65 1eSXMpy8ACLF52iBbP2lMCwCUJlANLakT7hgjSDLvt3RidfXcNdKB9fkniBNx88CRzCZ duL9jTCLTUhcfjbqJveqGviRxuaOmlZAfDkMprrVrXZANeOtEzZfbxHhQ5PZG9m8zOJ+ 16XD7B1xA0l1SbUIzE/x31MNeB+gx7eNl53pfCXr/8hy1trs7h1FNj4/SzmE2RRJNWBZ WmPO0Di6/tnm9B0b+IUP2w/q3Qu8jsj56JQYZlGYQBFVbx5VKawQvr07u2w52QCArbZp hxMg== X-Gm-Message-State: AOAM530HnEpLv4CEeY4piGFscn75X/h+IRpOMnDANtQApUFpv5CgTIbi O/ApcUBgs2uA+8J9JwMcaJg8MD9XflKwJg== X-Google-Smtp-Source: ABdhPJx7VdXOMOV8Qsxi3sDI4yfzPkVHQInRibfIUK+wrd2Yl5mUQtNoHIiA/UesXhgIJCFqrJULyA== X-Received: by 2002:a17:906:28ce:: with SMTP id p14mr1089379ejd.344.1623925412374; Thu, 17 Jun 2021 03:23:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id jo13sm3447293ejb.91.2021.06.17.03.23.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 03:23:31 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King , Taylor Blau , Felipe Contreras , Eric Sunshine , "brian m . carlson" , Josh Steadmon , Jonathan Tan , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 27/27] hooks: fix a TOCTOU in "did we run a hook?" heuristic Date: Thu, 17 Jun 2021 12:23:01 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.576.g59759b6ca7d In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee550d72 (commit: skip discarding the index if there is no pre-commit hook, 2017-08-14). We can fix the race passing around information about whether or not we ran the hook in question, instead of running hook_exists() after the fact to check if the hook in question exists. This problem has been noted on-list when 680ee550d72 was discussed[1], but had not been fixed. In addition to fixing this for the pre-commit hook as suggested there I'm also fixing this for the pre-merge-commit hook. See 6098817fd7f (git-merge: honor pre-merge-commit hook, 2019-08-07) for the introduction of its previous behavior. Let's also change this for the push-to-checkout hook. Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout. If the hook doesn't exist we'll fall back on push to deploy. The same behavior as before, without the TOCTOU race. See 0855331941b (receive-pack: support push-to-checkout hook, 2014-12-01) for the introduction of the previous behavior. This leaves uses of hook_exists() in two places that matter. The "reference-transaction" check in refs.c, see 67541597670 (refs: implement reference transaction hook, 2020-06-19), and the prepare-commit-msg hook, see 66618a50f9c (sequencer: run 'prepare-commit-msg' hook, 2018-01-24). In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook. So using this "invoked_hook" pattern doesn't make sense in those cases. More importantly, in those cases the worst we'll do is miss that we "should" run the hook because a new hook appeared, whereas in the pre-commit and pre-merge-commit cases we'll skip an important discard_cache() on the bases of our faulty guess. I do think none of these races really matter in practice. It would be some one-off issue as a hook was added or removed. I did think it was stupid that we didn't pass a "did this run?" flag instead of doing this guessing at a distance though, so now we're not guessing anymore. 1. https://lore.kernel.org/git/20170810191613.kpmhzg4seyxy3cpq@sigill.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/commit.c | 18 +++++++++++------- builtin/merge.c | 16 ++++++++++------ builtin/receive-pack.c | 8 +++++--- commit.c | 1 + commit.h | 3 ++- hook.c | 4 ++++ hook.h | 10 ++++++++++ sequencer.c | 4 ++-- 8 files changed, 45 insertions(+), 19 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index dad4e56544..a66727a612 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -725,11 +725,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix, int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE); int old_display_comment_prefix; int merge_contains_scissors = 0; + int invoked_hook = 0; /* This checks and barfs if author is badly specified */ determine_author_info(author_ident); - if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL)) + if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook, + "pre-commit", NULL)) return 0; if (squash_message) { @@ -1045,10 +1047,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix, return 0; } - if (!no_verify && hook_exists("pre-commit")) { + if (!no_verify && invoked_hook) { /* - * Re-read the index as pre-commit hook could have updated it, - * and write it out as a tree. We must do this before we invoke + * Re-read the index as the pre-commit-commit hook was invoked + * and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ discard_cache(); @@ -1060,7 +1062,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, return 0; } - if (run_commit_hook(use_editor, index_file, "prepare-commit-msg", + if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg", git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL)) return 0; @@ -1077,7 +1079,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix, } if (!no_verify && - run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) { + run_commit_hook(use_editor, index_file, NULL, "commit-msg", + git_path_commit_editmsg(), NULL)) { return 0; } @@ -1830,7 +1833,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix) repo_rerere(the_repository, 0); run_auto_maintenance(quiet); - run_commit_hook(use_editor, get_index_file(), "post-commit", NULL); + run_commit_hook(use_editor, get_index_file(), NULL, "post-commit", + NULL); if (amend && !no_post_rewrite) { commit_post_rewrite(the_repository, current_head, &oid); } diff --git a/builtin/merge.c b/builtin/merge.c index 6128b60942..0425c9bf2b 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -844,15 +844,18 @@ static void prepare_to_commit(struct commit_list *remoteheads) { struct strbuf msg = STRBUF_INIT; const char *index_file = get_index_file(); + int invoked_hook = 0; - if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL)) + if (!no_verify && run_commit_hook(0 < option_edit, index_file, + &invoked_hook, "pre-merge-commit", + NULL)) abort_commit(remoteheads, NULL); /* - * Re-read the index as pre-merge-commit hook could have updated it, - * and write it out as a tree. We must do this before we invoke + * Re-read the index as the pre-merge-commit hook was invoked + * and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ - if (hook_exists("pre-merge-commit")) + if (invoked_hook) discard_cache(); read_cache_from(index_file); strbuf_addbuf(&msg, &merge_msg); @@ -873,7 +876,8 @@ static void prepare_to_commit(struct commit_list *remoteheads) append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0); write_merge_heads(remoteheads); write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len); - if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg", + if (run_commit_hook(0 < option_edit, get_index_file(), NULL, + "prepare-commit-msg", git_path_merge_msg(the_repository), "merge", NULL)) abort_commit(remoteheads, NULL); if (0 < option_edit) { @@ -882,7 +886,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) } if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(), - "commit-msg", + NULL, "commit-msg", git_path_merge_msg(the_repository), NULL)) abort_commit(remoteheads, NULL); diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index ec90e10477..cd658f41d5 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1439,10 +1439,12 @@ static const char *push_to_deploy(unsigned char *sha1, static const char *push_to_checkout_hook = "push-to-checkout"; static const char *push_to_checkout(unsigned char *hash, + int *invoked_hook, struct strvec *env, const char *work_tree) { struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + opt.invoked_hook = invoked_hook; strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree)); strvec_pushv(&opt.env, env->v); @@ -1460,6 +1462,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w { const char *retval, *work_tree, *git_dir = NULL; struct strvec env = STRVEC_INIT; + int invoked_hook = 0; if (worktree && worktree->path) work_tree = worktree->path; @@ -1477,10 +1480,9 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir)); - if (!hook_exists(push_to_checkout_hook)) + retval = push_to_checkout(sha1, &invoked_hook, &env, work_tree); + if (!invoked_hook) retval = push_to_deploy(sha1, &env, work_tree); - else - retval = push_to_checkout(sha1, &env, work_tree); strvec_clear(&env); return retval; diff --git a/commit.c b/commit.c index e8147a88fc..cf62ebceae 100644 --- a/commit.c +++ b/commit.c @@ -1697,6 +1697,7 @@ size_t ignore_non_trailer(const char *buf, size_t len) } int run_commit_hook(int editor_is_used, const char *index_file, + int *invoked_hook, const char *name, ...) { struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; diff --git a/commit.h b/commit.h index df42eb434f..b5a542993c 100644 --- a/commit.h +++ b/commit.h @@ -363,7 +363,8 @@ int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused) int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused); LAST_ARG_MUST_BE_NULL -int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...); +int run_commit_hook(int editor_is_used, const char *index_file, + int *invoked_hook, const char *name, ...); /* Sign a commit or tag buffer, storing the result in a header. */ int sign_with_header(struct strbuf *buf, const char *keyid); diff --git a/hook.c b/hook.c index e7f3b468ea..31e822bf51 100644 --- a/hook.c +++ b/hook.c @@ -164,6 +164,9 @@ static int notify_hook_finished(int result, hook_cb->rc |= result; + if (hook_cb->invoked_hook) + *hook_cb->invoked_hook = 1; + return 1; } @@ -178,6 +181,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, .rc = 0, .hook_name = hook_name, .options = options, + .invoked_hook = options->invoked_hook, }; if (options->absolute_path) { strbuf_add_absolute_path(&abs_path, hook_path); diff --git a/hook.h b/hook.h index 1302efa590..9d9171672d 100644 --- a/hook.h +++ b/hook.h @@ -74,6 +74,15 @@ struct run_hooks_opt * for an example. */ consume_sideband_fn consume_sideband; + + /* + * A pointer which if provided will be set to 1 or 0 depending + * on if a hook was invoked (i.e. existed), regardless of + * whether or not that was successful. Used for avoiding + * TOCTOU races in code that would otherwise call hook_exist() + * after a "maybe hook run" to see if a hook was invoked. + */ + int *invoked_hook; }; #define RUN_HOOKS_OPT_INIT { \ @@ -96,6 +105,7 @@ struct hook_cb_data { const char *hook_name; struct hook *run_me; struct run_hooks_opt *options; + int *invoked_hook; }; void run_hooks_opt_clear(struct run_hooks_opt *o); diff --git a/sequencer.c b/sequencer.c index ec2761e47d..2440b9dccd 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1204,7 +1204,7 @@ static int run_prepare_commit_msg_hook(struct repository *r, } else { arg1 = "message"; } - if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name, + if (run_commit_hook(0, r->index_file, NULL, "prepare-commit-msg", name, arg1, arg2, NULL)) ret = error(_("'prepare-commit-msg' hook failed")); @@ -1534,7 +1534,7 @@ static int try_to_commit(struct repository *r, goto out; } - run_commit_hook(0, r->index_file, "post-commit", NULL); + run_commit_hook(0, r->index_file, NULL, "post-commit", NULL); if (flags & AMEND_MSG) commit_post_rewrite(r, current_head, oid);