From patchwork Mon Jun 14 10:32: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: 12318509 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.7 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,URIBL_BLOCKED,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 74DDFC4743C for ; Mon, 14 Jun 2021 10:36:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E4256143B for ; Mon, 14 Jun 2021 10:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233320AbhFNKib (ORCPT ); Mon, 14 Jun 2021 06:38:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233621AbhFNKgT (ORCPT ); Mon, 14 Jun 2021 06:36:19 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04B9CC0611BC for ; Mon, 14 Jun 2021 03:33:47 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id i94so13946135wri.4 for ; Mon, 14 Jun 2021 03:33:46 -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=9bqX+LiL1PpM1s5e5YwGhGH9yR72VRc/oypzZflRFNk=; b=ZmgzTvUHZ/lilID3C7G9DwcT3i42xsNZDdCgKDdLyAdwVB13a97AMRjDNvJ1kmojiz yyYykdfjH9Z3Ca2ycOW+1qvDkStAEqtjW9xSf9daSDn6oGPa2xrOu6tEwqlMH8HiAaId FVu2ae/WP0jyNMS+XxmXcj4b0wHxkT9dB8vgcgVIBE+NyNB8qdm4tTcxMp+1q29HmCpE GVltzEguAPkGtRPcZT/U6HluIFf9wAR4gu7aeWLwjj1ZpIjZroyZ+oOH7Iix37QVU0bY ef9mvVgTKphLK5nImtEe0rGaEOeKmUos8Q2v9ZKVs3EVldSrunj8y63XiasegvwUWMEN v5Sw== 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=9bqX+LiL1PpM1s5e5YwGhGH9yR72VRc/oypzZflRFNk=; b=fVJ0ElgR9xO4VyRXFurvpI3rJ9K7mr1fw/PqUM7DFeB8Dyd6ONULpNboQMDbHO+QJH klRo0FQW0iEQJVwsaDXVZCd164uLvmrP0gYZ0lCqyQ+KAR/749BLYRp/Yc/w0m0GoElb 1XgliTSMTeDLEU+3oDYrp87Ajsy+vx/628dhp9gnRA6w59jh6BqgceB0JdmSWhI8Lul0 oCLbVfyZ4br0PSFXPIFzy0Cf1jFr75aEkDwuBjK3lEbvb4ahnY77sTS8AoXBKjtewa76 W7pDMKQuqjLqyrACBvajh1cr6vsWRynFSAQkBHSMtVX4etl4HGfNlx06vpJ9fzmK4Vxy HZ1Q== X-Gm-Message-State: AOAM533SF0ufsrzgG3haxouedmZnRGPwMiCbp9i43HcB/f+pZi7Lbufh vBqCn29Uw/niXIeuq4oUjtr0ZOIVFLJ4+g== X-Google-Smtp-Source: ABdhPJz/MtWonCmlXX6IC+OWNzsvdv4tdfUeck4QKgQ07lgnixmIKfaO57Kh9aWAJiHefYTgrPvaGg== X-Received: by 2002:adf:f1ca:: with SMTP id z10mr18564557wro.396.1623666825281; Mon, 14 Jun 2021 03:33:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:44 -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 v2 01/30] hook: add 'run' subcommand Date: Mon, 14 Jun 2021 12:32:50 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 | 36 ++++++++++ Documentation/githooks.txt | 4 ++ Makefile | 2 + builtin.h | 1 + builtin/hook.c | 65 ++++++++++++++++++ command-list.txt | 1 + git.c | 1 + hook.c | 114 ++++++++++++++++++++++++++++++++ hook.h | 54 +++++++++++++++ t/t1800-hook.sh | 131 +++++++++++++++++++++++++++++++++++++ 11 files changed, 410 insertions(+) create mode 100644 Documentation/git-hook.txt create mode 100644 builtin/hook.c create mode 100644 hook.c create mode 100644 hook.h create mode 100755 t/t1800-hook.sh diff --git a/.gitignore b/.gitignore index 311841f9bed..de39dc9961b 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 00000000000..902b9cffaef --- /dev/null +++ b/Documentation/git-hook.txt @@ -0,0 +1,36 @@ +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. Any positional arguments to the + hook should be passed after an optional "--" (or + "--end-of-options"). See "OPTIONS" below for the arguments + this accepts. + +SEE ALSO +-------- +linkgit:githooks[5] + +GIT +--- +Part of the linkgit:git[1] suite diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index b51959ff941..a16e62bc8c8 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 c3565fc0f8f..a6b71a0fbed 100644 --- a/Makefile +++ b/Makefile @@ -901,6 +901,7 @@ LIB_OBJS += hash-lookup.o LIB_OBJS += hashmap.o LIB_OBJS += help.o LIB_OBJS += hex.o +LIB_OBJS += hook.o LIB_OBJS += ident.o LIB_OBJS += json-writer.o LIB_OBJS += kwset.o @@ -1101,6 +1102,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 16ecd5586f0..91740c15149 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 00000000000..1b1a594fd00 --- /dev/null +++ b/builtin/hook.c @@ -0,0 +1,65 @@ +#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 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_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); + + if (argc > 2) { + if (strcmp(argv[2], "--") && + strcmp(argv[2], "--end-of-options")) + /* Having a -- for "run" is mandatory */ + usage_with_options(builtin_hook_usage, run_options); + /* Add our arguments, start after -- */ + for (i = 3 ; i < argc; i++) + strvec_push(&opt.args, argv[i]); + } + + /* Need to take into account core.hooksPath */ + git_config(git_default_config, NULL); + + hook_name = argv[1]; + 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(), + }; + + if (!strcmp(argv[1], "run")) + return run(argc, argv, prefix); + usage_with_options(builtin_hook_usage, builtin_hook_options); + return 1; +} diff --git a/command-list.txt b/command-list.txt index a289f09ed6f..9ccd8e5aebe 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 18bed9a9964..540909c391f 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 new file mode 100644 index 00000000000..aa66c968186 --- /dev/null +++ b/hook.c @@ -0,0 +1,114 @@ +#include "cache.h" +#include "hook.h" +#include "run-command.h" + +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; + + if (!run_me) + BUG("did we not return 1 in notify_hook_finished?"); + + 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; + + /* |= rc in cb */ + 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; + + /* |= rc in 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); + + /* Care about nonexistence? Use run_found_hooks() */ + if (!hook_path) + return 0; + + ret = run_found_hooks(hook_name, hook_path, options); + return ret; +} diff --git a/hook.h b/hook.h new file mode 100644 index 00000000000..ebfee26bcf2 --- /dev/null +++ b/hook.h @@ -0,0 +1,54 @@ +#ifndef HOOK_H +#define HOOK_H +#include "strbuf.h" +#include "strvec.h" +#include "run-command.h" + +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 */ + int jobs; +}; + +#define RUN_HOOKS_OPT_INIT { \ + .jobs = 1, \ + .env = STRVEC_INIT, \ + .args = STRVEC_INIT, \ +} + +/* + * Callback provided to feed_pipe_fn and consume_sideband_fn. + */ +struct hook_cb_data { + 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 00000000000..f6ff6c4a493 --- /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 run -- nonexistent hook' ' + cat >stderr.expect <<-\EOF && + error: cannot find a hook named does-not-exist + EOF + test_expect_code 1 git hook run does-not-exist 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 handling' ' + 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 >>actual + EOF + + cat >expect <<-\EOF && + Test hook + Hook ran + Hook ran + Hook ran + Hook ran + EOF + + # Test various ways of specifying the path. See also + # t1350-config-hooks-path.sh + >actual && + git hook run test-hook 2>>actual && + git -c core.hooksPath=my-hooks hook run test-hook 2>>actual && + git -c core.hooksPath=my-hooks/ hook run test-hook 2>>actual && + git -c core.hooksPath="$PWD/my-hooks" hook run test-hook 2>>actual && + git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook 2>>actual && + test_cmp expect actual +' + +test_expect_success 'set up a pre-commit hook in core.hooksPath' ' + >actual && + mkdir -p .git/custom-hooks .git/hooks && + write_script .git/custom-hooks/pre-commit <<-\EOF && + echo CUSTOM >>actual + EOF + write_script .git/hooks/pre-commit <<-\EOF + echo NORMAL >>actual + EOF +' + +test_done From patchwork Mon Jun 14 10:32: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: 12318511 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 1A149C48BE8 for ; Mon, 14 Jun 2021 10:36:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F1BF1613D3 for ; Mon, 14 Jun 2021 10:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233470AbhFNKid (ORCPT ); Mon, 14 Jun 2021 06:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233452AbhFNKga (ORCPT ); Mon, 14 Jun 2021 06:36:30 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9308C0611BD for ; Mon, 14 Jun 2021 03:33:47 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id d184so11861678wmd.0 for ; Mon, 14 Jun 2021 03:33:47 -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=7BpEL71EQnjnOXfyfoe1LPfsOQuAL5fbTlyR8FIKMAQ=; b=BedbhMXFa99i8gnBybXVbNKVqciuDWluEumBVPohCRKtMM1/HZns4XVBXWRKymsl22 uqM4g/ShCsSQChAoLJMhmzHhjYZu7yHSzDgKqK7C7xW+3SfvW/cvRuseZzsqwmeQ+FI1 NcTXTJv9ZKT/1CXJSCTftRx29h310yl/3HWtL8rOVwiCF8PzfC8D8DpUAeGo9l4sZy7r NDWhN1YJlJpwVLhFJyQs1scETYvyuXfwN4drOSlIZGaX2bk2gV84Wozbfwnrn8pl2YEe iG7hJ4Xx3GPYQ9cMlv6bFLN6pApJRu6sTyrPbGNWQ7ziUb6cFHh25+naAXBq0LTtoOya dWZA== 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=7BpEL71EQnjnOXfyfoe1LPfsOQuAL5fbTlyR8FIKMAQ=; b=c9tPOSfjz2dAemuui6RM3dZc98vTwvKvIeTG5WmqgM+kihSrBduHXWHiYMAgaGTxqV WLs/guaQCrZ31eLkt1FDUvp5F+7oWXqqbu/I7HTBt8JzXbUC2TEFoty6Hbi4kDFZ0Vuv VsE8W2APehzOe0VqnTJrYRyju9EnYzRvlVsXNOGk6rG/oxSlaWpHDR0oAjf/X5aEp45N 7YJT0KvkoxMufc40dgn2RmYw4ABmh5a5gq0QBw2oe72iRVxILZIbvZfNwN8Euy/Mm7Kk aEFgO2S63Bz2pUOjT9X2BHYJt6sw2xO2CU9oEPoTT6588ROenbKOBcUF1g4I6ey/AG5j khMw== X-Gm-Message-State: AOAM533EsyIk0g0xzmLLYvyDQ+/XjOFYq1rhuh8CYUMVU8o/eK/fLm96 awwSPGXMOoqHHeYchf9fN/rlvNgZOjZg2w== X-Google-Smtp-Source: ABdhPJyuBNLNkYepLCcX+8B3IW2DdeoUFwTsFmKVxm8kpY0+BCRfRbqCzJKwJfMgHCuT7KL0QpDbXQ== X-Received: by 2002:a1c:7210:: with SMTP id n16mr31071749wmc.75.1623666826267; Mon, 14 Jun 2021 03:33:46 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:45 -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 v2 02/30] run-command.h: move find_hook() to hook.h Date: Mon, 14 Jun 2021 12:32:51 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move the find_hook() command to hook.h. Eventually all the hook related code will live there, let's move this function over as-is. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/am.c | 1 + builtin/bugreport.c | 2 +- builtin/commit.c | 1 + builtin/merge.c | 1 + builtin/receive-pack.c | 1 + builtin/worktree.c | 1 + hook.c | 36 ++++++++++++++++++++++++++++++++++++ hook.h | 7 +++++++ refs.c | 1 + run-command.c | 35 +---------------------------------- run-command.h | 7 ------- sequencer.c | 1 + transport.c | 1 + 13 files changed, 53 insertions(+), 42 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 0b2d886c81b..1c8a5489035 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -11,6 +11,7 @@ #include "parse-options.h" #include "dir.h" #include "run-command.h" +#include "hook.h" #include "quote.h" #include "tempfile.h" #include "lockfile.h" diff --git a/builtin/bugreport.c b/builtin/bugreport.c index 9915a5841de..596f079a7f9 100644 --- a/builtin/bugreport.c +++ b/builtin/bugreport.c @@ -3,7 +3,7 @@ #include "strbuf.h" #include "help.h" #include "compat/compiler.h" -#include "run-command.h" +#include "hook.h" static void get_system_info(struct strbuf *sys_info) diff --git a/builtin/commit.c b/builtin/commit.c index 190d215d43b..f1aafd67d46 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -19,6 +19,7 @@ #include "revision.h" #include "wt-status.h" #include "run-command.h" +#include "hook.h" #include "refs.h" #include "log-tree.h" #include "strbuf.h" diff --git a/builtin/merge.c b/builtin/merge.c index a8a843b1f54..be98d66b0a8 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -13,6 +13,7 @@ #include "builtin.h" #include "lockfile.h" #include "run-command.h" +#include "hook.h" #include "diff.h" #include "diff-merges.h" #include "refs.h" diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index a34742513ac..1e0e04c62fc 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -7,6 +7,7 @@ #include "pkt-line.h" #include "sideband.h" #include "run-command.h" +#include "hook.h" #include "exec-cmd.h" #include "commit.h" #include "object.h" diff --git a/builtin/worktree.c b/builtin/worktree.c index 976bf8ed063..b1350640fed 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -8,6 +8,7 @@ #include "branch.h" #include "refs.h" #include "run-command.h" +#include "hook.h" #include "sigchain.h" #include "submodule.h" #include "utf8.h" diff --git a/hook.c b/hook.c index aa66c968186..06842f50e5c 100644 --- a/hook.c +++ b/hook.c @@ -2,6 +2,42 @@ #include "hook.h" #include "run-command.h" +const char *find_hook(const char *name) +{ + static struct strbuf path = STRBUF_INIT; + + strbuf_reset(&path); + strbuf_git_path(&path, "hooks/%s", name); + if (access(path.buf, X_OK) < 0) { + int err = errno; + +#ifdef STRIP_EXTENSION + strbuf_addstr(&path, STRIP_EXTENSION); + if (access(path.buf, X_OK) >= 0) + return path.buf; + if (errno == EACCES) + err = errno; +#endif + + if (err == EACCES && advice_ignored_hook) { + static struct string_list advise_given = STRING_LIST_INIT_DUP; + + if (!string_list_lookup(&advise_given, name)) { + string_list_insert(&advise_given, name); + advise(_("The '%s' hook was ignored because " + "it's not set as executable.\n" + "You can disable this warning with " + "`git config advice.ignoredHook false`."), + path.buf); + } + } + return NULL; + } + return path.buf; +} + + + void run_hooks_opt_clear(struct run_hooks_opt *o) { strvec_clear(&o->env); diff --git a/hook.h b/hook.h index ebfee26bcf2..291ee19469a 100644 --- a/hook.h +++ b/hook.h @@ -37,6 +37,13 @@ struct hook_cb_data { struct run_hooks_opt *options; }; +/* + * Returns the path to the hook file, or NULL if the hook is missing + * or disabled. Note that this points to static storage that will be + * overwritten by further calls to find_hook and run_hook_*. + */ +const char *find_hook(const char *name); + void run_hooks_opt_clear(struct run_hooks_opt *o); /* diff --git a/refs.c b/refs.c index 8c9490235ea..59be29cf081 100644 --- a/refs.c +++ b/refs.c @@ -10,6 +10,7 @@ #include "refs.h" #include "refs/refs-internal.h" #include "run-command.h" +#include "hook.h" #include "object-store.h" #include "object.h" #include "tag.h" diff --git a/run-command.c b/run-command.c index be6bc128cd9..82fdf296569 100644 --- a/run-command.c +++ b/run-command.c @@ -8,6 +8,7 @@ #include "string-list.h" #include "quote.h" #include "config.h" +#include "hook.h" void child_process_init(struct child_process *child) { @@ -1320,40 +1321,6 @@ int async_with_fork(void) #endif } -const char *find_hook(const char *name) -{ - static struct strbuf path = STRBUF_INIT; - - strbuf_reset(&path); - strbuf_git_path(&path, "hooks/%s", name); - if (access(path.buf, X_OK) < 0) { - int err = errno; - -#ifdef STRIP_EXTENSION - strbuf_addstr(&path, STRIP_EXTENSION); - if (access(path.buf, X_OK) >= 0) - return path.buf; - if (errno == EACCES) - err = errno; -#endif - - if (err == EACCES && advice_ignored_hook) { - static struct string_list advise_given = STRING_LIST_INIT_DUP; - - if (!string_list_lookup(&advise_given, name)) { - string_list_insert(&advise_given, name); - advise(_("The '%s' hook was ignored because " - "it's not set as executable.\n" - "You can disable this warning with " - "`git config advice.ignoredHook false`."), - path.buf); - } - } - return NULL; - } - return path.buf; -} - int run_hook_ve(const char *const *env, const char *name, va_list args) { struct child_process hook = CHILD_PROCESS_INIT; diff --git a/run-command.h b/run-command.h index d08414a92e7..b58531a7eb3 100644 --- a/run-command.h +++ b/run-command.h @@ -201,13 +201,6 @@ int finish_command_in_signal(struct child_process *); */ int run_command(struct child_process *); -/* - * Returns the path to the hook file, or NULL if the hook is missing - * or disabled. Note that this points to static storage that will be - * overwritten by further calls to find_hook and run_hook_*. - */ -const char *find_hook(const char *name); - /** * Run a hook. * The first argument is a pathname to an index file, or NULL diff --git a/sequencer.c b/sequencer.c index 0bec01cf38e..3de479f90e1 100644 --- a/sequencer.c +++ b/sequencer.c @@ -8,6 +8,7 @@ #include "sequencer.h" #include "tag.h" #include "run-command.h" +#include "hook.h" #include "exec-cmd.h" #include "utf8.h" #include "cache-tree.h" diff --git a/transport.c b/transport.c index 50f5830eb6b..2ed270171f0 100644 --- a/transport.c +++ b/transport.c @@ -2,6 +2,7 @@ #include "config.h" #include "transport.h" #include "run-command.h" +#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" #include "remote.h" From patchwork Mon Jun 14 10:32: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: 12318507 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 DE055C2B9F4 for ; Mon, 14 Jun 2021 10:36:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C420F6143C for ; Mon, 14 Jun 2021 10:36:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233833AbhFNKiT (ORCPT ); Mon, 14 Jun 2021 06:38:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233599AbhFNKgP (ORCPT ); Mon, 14 Jun 2021 06:36:15 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6281C0611BE for ; Mon, 14 Jun 2021 03:33:48 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id o3so13977165wri.8 for ; Mon, 14 Jun 2021 03:33:48 -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=9xGP99aGKF6yjV0zJ5oCa3cs1y2yVY9yQanaHDRG3WE=; b=Hy7acSDkuVjy6usUMwpmbRxpLLPvrgvtcvZcpO6LO+1F/zORKN6GU2G5MRahh3vTS4 mKBg9mOzvpq8dubub+BcC/ZA1B528vOKC+fWbkcaFzk+GdSMgy4kQX2tt4zM2vKwJzYQ PBo67Lr8k9QoErpMH9MhZxNgFYwqMRE9nhq7xVcRR0IqY/UlvWR8lMuHoiuqyjdwprfB UtAQGXF0VOdR68SmTrF4zD8j7SXhFtnKE2kVikkOv1fYgLFYurmCynrRL63jeuaMYvTz VN0p9jFBS1zw0WyNQpSnDrko0iDBf75clOjhezFZw/6DcR0B+NbB5W+LgArm6RnWA39x 61sw== 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=9xGP99aGKF6yjV0zJ5oCa3cs1y2yVY9yQanaHDRG3WE=; b=H8TxqO5SD+wI1In1nwbFWH3e3JeOHzSgZl/4KGlS1Mll/f9tz68NcMw88kvMOS30qE +LcDw83hz3OSLx8lNoxeiswW2L8/vZ1PLKR2WwiLzQv8jSnr9fLU9W8bwgnyXzV/+Vz5 ZPr1vsDoPizK3wlbmbm46+pDUafkPT3oxQylhEjD48NXSt6sbvI/j3FoyEtf2VaPR4Qi FT949SUHN06rgUq0kK/1RsLisLBdIiQ4INNkMa/cTTNx47FkHnoslaErxC7rcPOvi8v5 iKiayQPKfW0ZaokjtY8zKPo8IrjkdQIB/zLulbFCHgMleLjdWHoj4IE0dbfoSGCxrGaQ gNeg== X-Gm-Message-State: AOAM531txA8X3z2wTnwwF3mDiR6ZTvcZU9eSYPzQwZON1M/IEaQ4j9eh 8EHuIMaB1tIFscqPPPCxD7gl58Jw9T0ISw== X-Google-Smtp-Source: ABdhPJxx5vMCP1yTYmaNtrt1bTpFCykDdveFUwm0g/IGNRzvCO/9DStaR4A/sVg8+H41AQkGPifdxw== X-Received: by 2002:adf:fe4a:: with SMTP id m10mr17968150wrs.332.1623666827182; Mon, 14 Jun 2021 03:33:47 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:46 -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 v2 03/30] hook.c: add a hook_exists() wrapper and use it in bugreport.c Date: Mon, 14 Jun 2021 12:32:52 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer Add a boolean version of the find_hook() function for those callers who are only interested in checking whether the hook exists, not what the path to it is. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/bugreport.c | 2 +- hook.c | 5 ++++- hook.h | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/builtin/bugreport.c b/builtin/bugreport.c index 596f079a7f9..941c8d5e270 100644 --- a/builtin/bugreport.c +++ b/builtin/bugreport.c @@ -82,7 +82,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit) } for (i = 0; i < ARRAY_SIZE(hook); i++) - if (find_hook(hook[i])) + if (hook_exists(hook[i])) strbuf_addf(hook_info, "%s\n", hook[i]); } diff --git a/hook.c b/hook.c index 06842f50e5c..c7da273822d 100644 --- a/hook.c +++ b/hook.c @@ -36,7 +36,10 @@ const char *find_hook(const char *name) return path.buf; } - +int hook_exists(const char *name) +{ + return !!find_hook(name); +} void run_hooks_opt_clear(struct run_hooks_opt *o) { diff --git a/hook.h b/hook.h index 291ee19469a..cbda7746a5d 100644 --- a/hook.h +++ b/hook.h @@ -44,6 +44,11 @@ struct hook_cb_data { */ const char *find_hook(const char *name); +/* + * A boolean version of find_hook() + */ +int hook_exists(const char *hookname); + void run_hooks_opt_clear(struct run_hooks_opt *o); /* From patchwork Mon Jun 14 10:32: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: 12318525 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 A6997C2B9F4 for ; Mon, 14 Jun 2021 10:36:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 91AF16144F for ; Mon, 14 Jun 2021 10:36:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233372AbhFNKix (ORCPT ); Mon, 14 Jun 2021 06:38:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233631AbhFNKgX (ORCPT ); Mon, 14 Jun 2021 06:36:23 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD433C0619F6 for ; Mon, 14 Jun 2021 03:33:49 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id h11-20020a05600c350bb02901b59c28e8b4so12205776wmq.1 for ; Mon, 14 Jun 2021 03:33:49 -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=CpCb373suqu9uZ+XOdqIZHCL/vvYRRFgJmucadMTSv8=; b=GM665Cx6GviwzdW1bHMpRaBlulxk0LUigue0vGcuqkbV//UUZH2va01GsJSCF15V0J jPe8THPOi3JroRXokRqWUW0AHtE9OpjrKkPE4V3Fyc7tyI6t45efGFIC0zoG6XmQwPBD E8lYp5OBUaS7KgOfTZwtzEwGZ61snne6mn5gsxvlpA25ytiCSPZDWROsrNgYEVSxAwLk OiImkp1GPFJX03Ncv77oZ8xnQPJQb6CNcid0GNsGMqCf8Y29eMb2Tqdhq16Z9rS88jY5 9/1UVBJYl/zoGvjOZxwWIV1WtQJZfES0D/jsVFACJDHONZKc32r+1rp9ISbf9KLCoHWJ Bpzg== 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=CpCb373suqu9uZ+XOdqIZHCL/vvYRRFgJmucadMTSv8=; b=NUsyHS2O3OEJI30WcfPalRKeZ2yGmCS4wT0TNwtbJeL5qeq69v4vdFYARLkH8OyAxT bhvibAGzs1+2eAd4VuwTqqHix5/KAt6oHtfc7LdKTrDGjBQp5FUQAxJxvkTCV0IXSi9/ yKTYrG4Sve33WFERAlBa5ydqKeF/WQiJc2WHnic1LeZs9JyT99ZmK+dTN6mY2QhOz101 VVz4YnJmf0TpzAp7TOvndKIGHQsGbF+usEi0B1vs+zDkFA6BqygthVJwaVHxruXuHF4+ 78VqrZB919ZtW3DJD1yrCn9wJ7JYBwDwhdV1KA/291DaB7ZsOM+UD4v3FeB5f6+beG/N /nvg== X-Gm-Message-State: AOAM533OPO6c9K0HBmeAnqKi7AXFjb3FjYUE9EJh1rXH3hqw+Q29oIJB SYUXz+v1UQryCylBxMvTfeln+VRuL/g5ag== X-Google-Smtp-Source: ABdhPJzuykclBMJw3eszXytZa3+JCk4JmkZTQzuWdi5iu9szMuI0NkTnReibRD00liAfdYDoLhDBvA== X-Received: by 2002:a05:600c:154:: with SMTP id w20mr16250676wmm.185.1623666827945; Mon, 14 Jun 2021 03:33:47 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:47 -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 v2 04/30] gc: use hook library for pre-auto-gc hook Date: Mon, 14 Jun 2021 12:32:53 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 f05d2f0a1ac..a12641a691d 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 Mon Jun 14 10:32: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: 12318523 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 870E5C2B9F4 for ; Mon, 14 Jun 2021 10:36:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5F8676144D for ; Mon, 14 Jun 2021 10:36:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233653AbhFNKiu (ORCPT ); Mon, 14 Jun 2021 06:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233632AbhFNKgX (ORCPT ); Mon, 14 Jun 2021 06:36:23 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81E16C0619FB for ; Mon, 14 Jun 2021 03:33:50 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id l2so14015825wrw.6 for ; Mon, 14 Jun 2021 03:33:50 -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=0HrAaJ6IS93jhZY5XTLzxNtXyu5QKUvm92WhBzzxi/8=; b=AKJgtUZGGyF3GYI4hRR+vcjbCQojzSyU2Abe1bK1dtCyPCY9EJdAZlkV44e8u+hq0r uchUL2dXErbVB28WYU75tWg8cgphBwojS50AkCvHdf4IQUI5GuQ5gQLI80bblYeP+jky iA0ZCCKP1yjB3Mq5bNNiUfVWe7gCm1myMahxyfGaZeBrvd0gcgFDF1XYXtImaYxKcW0x Fazk5ljauTi1TvM0jmIr8bS+K4sQiikFjxrYZFE1Kz7HKmtwBIlXaVecWOH2KNc2LY+W uDCC7kzwjLuukteoQXDyLVVoGw5QqEJHOSX3A0HaeH3OWiDATdLJwqVY1rYNvc+NBd27 F0pA== 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=0HrAaJ6IS93jhZY5XTLzxNtXyu5QKUvm92WhBzzxi/8=; b=ahNTx7hCFERLZlcoU/Gn40lGoft/D/yqOPSHwc5XHdmpPUDzVU6Mns4fGChgjyfvF3 FcaiXzVcwbQe7qGX3PKY77SuKHLR7LY7FKzqvkPIdRMK+aFPGdUwLvRsb97ijB8GU4SM xCIMHFWtc9qtF+7P0ObqYInxl6y8JGOSghoT+NV26kZ9hhwd7r3ONnu7Fom2odTi93f0 nT8y2Pn2a5I/xuQu967lRWJOsorD5ahHv8VB09pQPf17KVBHG/uTKvP36tmllOXR3HsL p0lnlFS1BpkK7ipIdjLm+GBLghzodEIiTSLlqi0LWLw55GD5836amffPpez5nvd/FnAF GzRA== X-Gm-Message-State: AOAM530lC6Fnv1A7NVwxcOiLMxBf3XAlxsGZDhkxlBDIUKCdE1zNaB84 sWv9oNh/GmE+XVWQU3xbXr9BVd6Rem3kJg== X-Google-Smtp-Source: ABdhPJzVsq1IRWBosCJ+awThxbVFi8gMFygTt1uvvN21Eoio0ZgnKvA87DzJoLsA+ZeKkg//vbScgw== X-Received: by 2002:adf:f942:: with SMTP id q2mr17484533wrr.427.1623666828874; Mon, 14 Jun 2021 03:33:48 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:48 -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 v2 05/30] rebase: teach pre-rebase to use hook.h Date: Mon, 14 Jun 2021 12:32:54 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 12f093121d9..2081f6fa8db 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 Mon Jun 14 10:32: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: 12318521 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 69563C2B9F4 for ; Mon, 14 Jun 2021 10:36:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 53B6E61444 for ; Mon, 14 Jun 2021 10:36:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233089AbhFNKin (ORCPT ); Mon, 14 Jun 2021 06:38:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233297AbhFNKgY (ORCPT ); Mon, 14 Jun 2021 06:36:24 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 542D3C0619FE for ; Mon, 14 Jun 2021 03:33:51 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id h22-20020a05600c3516b02901a826f84095so12472556wmq.5 for ; Mon, 14 Jun 2021 03:33:51 -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=AkpgrQ+La2deYDtCTCnPIvK7pnSftp/7hwIbv7uDy/c=; b=aVwlWQtEL/ZADyYU+eYz0w50nMmZxEUnCtdcuTSeEoA9nBJqsk/brORHZPjq3Pkosz osIsiOzDwVr0TyEsiuWuUoO4a/EXoHWQcDDMkbhCznFfrgV6OZsftTPFj8CjFijmIIxJ fjevPITxfsmJhMutRetcyl9c6rkc5tNd4SHRE7bDX84d05PyBbfE6CDGjXzpVyg9BW5v HRQBi3Pm7FQIEO5JDTon49rjfBVysCiMbwz7CKjCboO0iIo4tN09i3VuUFt0YKXTp8X1 hbEANvUOd0fa9s6KAWL2u2biar2PIh6RmDWS6MmtlXjx7bGc4X9J+wOpPmOBOc0mYQVr UtJA== 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=AkpgrQ+La2deYDtCTCnPIvK7pnSftp/7hwIbv7uDy/c=; b=KSitWzoR08X/UeYog381GQMVpCbsTn/ETqnDA/uKACkcCFfitMd16xX/LCh09tR/LH R3LnrpmBhn6EC6tNDzVvoG9bdjcbzK+i1a8XAVXCsiZFFoamob4uR37Dxm21MG81mfVc 8m6u5AprYAl4MsNk6iI/zM1RD9qAFxdBt02KtNk9cLTuQlcP2urYrU8HTXloBRw25Hrw 3KAvHA3gedKdFPz9VnaT7qriaz8rNmGWdMTXZVZaMWGrKFBnh5jnIJ2zjnCbsMkvvbzL EjaNebu2OcRQuDkgAc6AT5T2nyTX9TmC4RikvxbGVsmF+xBLLX/1BqnZw082V9GECnOs 2lKA== X-Gm-Message-State: AOAM532Gb26rh3cjWFFv2Sy7lGGqafo+Ma8UIfgzG/ZIe+I1qDMrB/mx XUznmZjf6WViUlM5TGh6oOPGLVqHCdpmcQ== X-Google-Smtp-Source: ABdhPJymWCVV7TYXqExqYToDfS1X/io/2q7oA22ctJufcCKpCIHmEPxfLGel0q2oDCxQAZy/doZ7AQ== X-Received: by 2002:a05:600c:154f:: with SMTP id f15mr15743472wmg.22.1623666829719; Mon, 14 Jun 2021 03:33:49 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:49 -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 v2 06/30] am: convert applypatch hooks to use config Date: Mon, 14 Jun 2021 12:32:55 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 1c8a5489035..9e9c1b5e9f2 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 Mon Jun 14 10:32: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: 12318519 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.7 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,URIBL_BLOCKED,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 042FCC48BE6 for ; Mon, 14 Jun 2021 10:36:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E58A26144A for ; Mon, 14 Jun 2021 10:36:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233885AbhFNKil (ORCPT ); Mon, 14 Jun 2021 06:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233215AbhFNKgZ (ORCPT ); Mon, 14 Jun 2021 06:36:25 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21CB1C061141 for ; Mon, 14 Jun 2021 03:33:52 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id k5-20020a05600c1c85b02901affeec3ef8so12719492wms.0 for ; Mon, 14 Jun 2021 03:33:52 -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=POIely+Tnfzm4pVn4ntvzxTV14RM9zmxAXBIfMVWRJM=; b=RbvVv/MLsnhMnO3ENAlU/9NY3zdtpbS+62AGUq0u9FWslMgrzGsaXc4weY1HPx74v8 pQ2XWZSKR+mPlXcAFQG7e5L5g1MkPC0Sr+UguQh3DNaq2dn4ZxVLCCAhuA/T9mx1eQSh qTy7ENxCAgS1zI3WbP2X9rU9JYr5b0edOVktB91/voFIhl+jCEW+v3YSN0nYEMbfjwIo yJ/6pKAbvkCFPpDg10KYXbDCgxb246lsJOWCj+IjdUMXvtQ6oLC76PjqQ11lM1b6UAze d3pCnHXUa0/AMO3qEmboTHDUIqmZ/1CZsYqwkTQI3XapMcMgy7wP3DVY7yrWvD8VtaZs cXjg== 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=POIely+Tnfzm4pVn4ntvzxTV14RM9zmxAXBIfMVWRJM=; b=O7m3kLo6FByniNK4opkPm9HZcsvYTiAlj1axIEw+DkJrRmHUq3LHDQHaA3x7wo1CdH pgMbagP43QgxQiwfkwJyjoT4A9AOAR4yA94kI+jfzD4RFlIaJNKSA9pi6PD+2sRtJsWJ 8XSIYBQKNOnc8XqeQ1N7q3SgF2ooyy3GCyC50gOHQIOY0GT+ybSbb9jUxc/N57OmGnSB feq6cTEE4ikNCp8IDPo/zNjWpEfY5OfFu3wW4dmGQr1BdLiI3xVdilfSlgoDmcnwxW2f N81JodBKrE3M2JIqLiWqIJFjLarYloVOlvawm6KDMjL3ss8NUjqxLhGO6cpJceO+eC5e DxzA== X-Gm-Message-State: AOAM530Ri16NcGzRYHorIVVtOKtSmhW5+HZozYrH41BP4HmuYD+NlhAn pQJegotjCOSHgsEC+gElz4mI8aQKJOo3zA== X-Google-Smtp-Source: ABdhPJx9iCuQlE4GzgY8JajH+fCew7+ktxr9r69cis9u3tOzAQBnMwMxikDTs2vK8V++m8ATQFJu/w== X-Received: by 2002:a05:600c:22d7:: with SMTP id 23mr31103976wmg.119.1623666830539; Mon, 14 Jun 2021 03:33:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:50 -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 v2 07/30] hooks: convert 'post-checkout' hook to hook library Date: Mon, 14 Jun 2021 12:32:56 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 f4cd7747d35..6205ace09f6 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 66fe66679c8..de57a3119b7 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 b1350640fed..2ad26a76f4c 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 c7da273822d..51337f9798f 100644 --- a/hook.c +++ b/hook.c @@ -62,6 +62,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); @@ -111,6 +112,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, }; @@ -119,6 +121,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) @@ -131,6 +137,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 cbda7746a5d..2d7724bbb50 100644 --- a/hook.h +++ b/hook.h @@ -19,6 +19,15 @@ struct run_hooks_opt /* Number of threads to parallelize across */ 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 77961a38854..af5b97104cf 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 4bea758053b..e6af33b901c 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 Mon Jun 14 10:32: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: 12318527 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 1FAD2C4743C for ; Mon, 14 Jun 2021 10:36:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0A13C61451 for ; Mon, 14 Jun 2021 10:36:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233165AbhFNKi4 (ORCPT ); Mon, 14 Jun 2021 06:38:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233481AbhFNKgi (ORCPT ); Mon, 14 Jun 2021 06:36:38 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5984C061145 for ; Mon, 14 Jun 2021 03:33:52 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id f2so13966462wri.11 for ; Mon, 14 Jun 2021 03:33:52 -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=kxTOlilJr02pS+bL23nf+Nd5rH4EnQu2BKg+lQcykEA=; b=obtgBGnxvBBixR64vhi2HPAyxwyNd5TyemL0H3bDXHCpah2RbjeTN3J559xfy9unDR 9tDVuIlYkPoGI7it/tZHwzwIGT64XbnQ21KCNb57ZNl+dEGH5Q83WdNVQ9lzbIax/9TO pFrVODPU7L2NXNUf5lOjelJvI3/BpwzOu4OtWWobqo21vfosgBNlWPjqOMMdVOh0HyPJ IO3ffi6ywm2YXKVbsVOnsZfnH0uWCl26Cic+Y9TwfaTrR+a71MWLcKojho6h/CEvnPE8 xuYmeJzVLvR668SGTfRMLH8wVO27HesEBEfAkdMeX3xaSzeVGKvqwhzsS3uZOdl8zxSG nssQ== 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=kxTOlilJr02pS+bL23nf+Nd5rH4EnQu2BKg+lQcykEA=; b=bmFs7bMpkpPwOt6lm+TUIdCC10R5WW47obyjcnk0rgKXnH7+/Pb0Q5K/KlkIeFVLP7 dztNHwWDheJQnB8548EfjcJswVoIwlXWDXT/+rqTXD7fR9dggBKPOB2pSWd7bPHlNf9R xa+yjsaDWt50z9GKmAD82oH3A7Ff2VavbM4AsqojVdmvkwHKHcL4bN0W93vO9DMRRLXG AGXjuct6DA15ARR741C7aDIE+Wzh9iWYyBiA7/CA6bx1was29lBQQVjP0YsRIiFgZjWP DuvR03QY6MzjKqlSHNGy05ip8D/PpCk9mJCnB+539X7Q6iJ1kiX9yMZj/J5DTu9iCxKv N7Uw== X-Gm-Message-State: AOAM530ktUeDCRy10sc0SbS9YdYN5sByouTWvSFlrLcIhkxx5/RjspIK uihwJIm1Pc1kvFWe565qDW9z4/UQ62vN+w== X-Google-Smtp-Source: ABdhPJy9ep3kjdl3q6SADT2D9v4WMGO2L9C5Ph97ZCmctQneVFG5t4bzp3U+W93ns6GOlayypgOECQ== X-Received: by 2002:adf:c442:: with SMTP id a2mr17850913wrg.277.1623666831394; Mon, 14 Jun 2021 03:33:51 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:51 -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 v2 08/30] merge: use config-based hooks for post-merge hook Date: Mon, 14 Jun 2021 12:32:57 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 be98d66b0a8..6128b60942f 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 Mon Jun 14 10:32: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: 12318517 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.7 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,URIBL_BLOCKED,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 21A0AC4743C for ; Mon, 14 Jun 2021 10:36:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 044BF61448 for ; Mon, 14 Jun 2021 10:36:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233880AbhFNKij (ORCPT ); Mon, 14 Jun 2021 06:38:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232825AbhFNKg0 (ORCPT ); Mon, 14 Jun 2021 06:36:26 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8D2CC061144 for ; Mon, 14 Jun 2021 03:33:53 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id t4-20020a1c77040000b029019d22d84ebdso12675426wmi.3 for ; Mon, 14 Jun 2021 03:33:53 -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=ZMSa6zLBMU4KYRnb5uZFshCsRnDXbbt0OVu+2+yx0lE=; b=LMTZ6zLwOhXETRfbeaUhE/nPMTTy3oVnDfL+DsXgtnuSFSg4dj6GDWwiIDOTP6HvSS U8JdeIjzk4rU50aDNukJrIlNwwIQNLbXj49f3s3W1+Zbr6KduzysUdN0xQ3WGbbBVRWm LJYupVpoxt8ksDtIG40Wo5yDbS1IX16qELR1gnzIKuQ7at8Kuir8do4Hlqti0pABI2Ag Xwr6GPbV9OXHx0gHz3NhN/3vj67d6PFBU0QLTetBRl3dZZ339iKAoJDQ9o9tT6A4ICrf AYmzDU1V9hh1nlCBFwcdxi/Cc909KIfIwpDMYGD8K6GuNGfoGoh4AT3SQJtSZwIUZI+O 2now== 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=ZMSa6zLBMU4KYRnb5uZFshCsRnDXbbt0OVu+2+yx0lE=; b=LleqyE5j9It8tC8Hkrx6K0cfgDB8+Di612xA+4LE6bEA/qLn4rt90XOt+samBuztt1 wdoW/NHIrBAq5pTLHUjdF5dbD6xtX6Iip9O3RtIkLERkhqwC/Cu9Q/5LL9CVOTn/Vefv GKsPHqVfQU3zCRZBjC5Exw12Hq97T69ZpHGdN4XepUm/7t75LpA25ojpOcw0edXH0KEc l1ZmJHmak8e7e9vY8qfbcDHceOA6MBcN3pGXPRAUXU2oGzmq2Z7uUn8qYkv0mNUKqsbU zTF9/LfbtSgIis6qEcFSxT+Yy9mJD/G762WabZhtXM8+wJB1DESFzJE7CMexdmn67Wa7 +MOw== X-Gm-Message-State: AOAM530pupf+gPIKrY8i3ZMU5ybqVfvFxTPaddlAop8tINKGdl+HEbAJ ZVKL1QroBU8ByCmgsoMCsvmcNQAxr0WMfg== X-Google-Smtp-Source: ABdhPJwFSch7Ma6SOtp/kNeSqGcqKqTuY1k4CEvSrEJJHfnCWMQSEmERpoy9y976qqniBwBKVVcBtw== X-Received: by 2002:a05:600c:204:: with SMTP id 4mr32045654wmi.95.1623666832248; Mon, 14 Jun 2021 03:33:52 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:51 -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 v2 09/30] git hook run: add an --ignore-missing flag Date: Mon, 14 Jun 2021 12:32:58 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 | 5 +++++ t/t1800-hook.sh | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt index 902b9cffaef..1528c860cf1 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 ----------- @@ -27,6 +27,14 @@ run:: "--end-of-options"). See "OPTIONS" below for the arguments this accepts. +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 1b1a594fd00..275dd5b0ed0 100644 --- a/builtin/hook.c +++ b/builtin/hook.c @@ -16,10 +16,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(), }; @@ -42,6 +45,8 @@ static int run(int argc, const char **argv, const char *prefix) hook_name = argv[1]; hook_path = find_hook(hook_name); if (!hook_path) { + 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 f6ff6c4a493..714dfd08cd8 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -12,6 +12,11 @@ test_expect_success 'git hook run -- nonexistent hook' ' test_cmp stderr.expect stderr.actual ' +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 From patchwork Mon Jun 14 10:32: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: 12318537 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.7 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,URIBL_BLOCKED,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 F1EBEC4743C for ; Mon, 14 Jun 2021 10:38:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DF3646145C for ; Mon, 14 Jun 2021 10:38:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233492AbhFNKkR (ORCPT ); Mon, 14 Jun 2021 06:40:17 -0400 Received: from mail-wm1-f54.google.com ([209.85.128.54]:42930 "EHLO mail-wm1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhFNKhK (ORCPT ); Mon, 14 Jun 2021 06:37:10 -0400 Received: by mail-wm1-f54.google.com with SMTP id l7-20020a05600c1d07b02901b0e2ebd6deso12495033wms.1 for ; Mon, 14 Jun 2021 03:34:53 -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=WqB5TOSPl2zwh8YNxAlEV2KiLZYYnpFRMKrG+o1Ixfg=; b=qSL7IVsFpSF6YGXPRK7DqbFkF6KprrBhwC8MkqOD9pz60zs+oO6+AFHWLZYu5PZBbR tUgPI35fzKVUG2R2gWZ/LOM860bivUR0QCBnMXdyK7ptBniO84F1IZ+lYgNFYh6QbzNi blRXwcODRjNAwAUHAayyJGezpkigeAlK4jwgQck6qt80RJ6qi1h7UDaSA/ce+lWOL3Y6 brNnZVwsQdP86NrQQ4tI8ltVxQ0l+8vNTX5avwpzn62ZIFd8RDTdtR4QJZciLxXshtQH 1yK0speehHTQLshhDMNbFo0hl33lu3EwEEsmdC7dMDVzt6fz0ch7agJ8QkwkGwCL8i8G KTtw== 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=WqB5TOSPl2zwh8YNxAlEV2KiLZYYnpFRMKrG+o1Ixfg=; b=kvfIZ42g3H/QJcP82U01PD0ngJQFMRZxuag/9EC/LzKclpAt8UewpFQE5R4mXr2CuQ f9hik6RswrqJV6VMTTt1Oux91zGORXWbQr5AnO3a9V4hf8WtlNBW/D/PPLfiUkZjzova L/P867a0PGnnT6Vz3DTwHXssprXEZqRxvxJHPYZIP/c3dXJI0HrwCtWJBR0lDwnGkkOv qUMo4lzPEjh0MZSqTpFi3vFjPgn6kTnhAvq6e7XqZ28d/Z3mMlMYqpfyxyXA8ltzSmUU U5QmUKcrairdESdmiqnZUb2b9l37+O/GXkoNF58YlaHtk5sG7a6Q5CABwo0QlkFRC0qb nl8Q== X-Gm-Message-State: AOAM530lyZ1xqPABVYmC5dToz4qyzjWTaVcAoi6EdWzbr+uXHeZNTIWn SKKEgfLmN9E8LzSi4nWlwmL/C7xuj1n/3Q== X-Google-Smtp-Source: ABdhPJyV1vjHmNuhZ8ojAWk9cmmtZxvWYW759g65XC4gzus9L6u14kPHgkyUqLDM5rwhYwpKkdMe2g== X-Received: by 2002:a1c:bb45:: with SMTP id l66mr32295920wmf.29.1623666833026; Mon, 14 Jun 2021 03:33:53 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:52 -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 v2 10/30] send-email: use 'git hook run' for 'sendemail-validate' Date: Mon, 14 Jun 2021 12:32:59 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 7ba0b3433d7..9e474304036 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 30eff725a96..6d4e25df8dc 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 Mon Jun 14 10:33: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: 12318529 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.7 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,URIBL_BLOCKED,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 3683BC4743C for ; Mon, 14 Jun 2021 10:36:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1BCBF6144F for ; Mon, 14 Jun 2021 10:36:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233500AbhFNKi6 (ORCPT ); Mon, 14 Jun 2021 06:38:58 -0400 Received: from mail-wm1-f48.google.com ([209.85.128.48]:53180 "EHLO mail-wm1-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233497AbhFNKg5 (ORCPT ); Mon, 14 Jun 2021 06:36:57 -0400 Received: by mail-wm1-f48.google.com with SMTP id f17so11839890wmf.2 for ; Mon, 14 Jun 2021 03:34:54 -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=CWoqdfr50plwmlkBtSy1F3aFaYVY5KaFBSGL1KzvXCQ=; b=p2yMFWynYA6tODZgp2UJQV5M1lIqhlCMb/Bk5SodqZ/1Dh2LnfBRHR4o2WHClXwxGj Pkv12JUDCai3TdxFrkCbYxIjEpZqatBOks3t6HrKng5TQnP4KTHRc8wr2a8nJvJChMgO m1bfo1TfN15OFjb/+Y9laq4p2ScYY5QrMsudwi7OEYr570qFD4ZesxjBfaKrpxIOKUZn MJY36LLDYe9Xj3Dh3uTC05EnFw5khB/7PnMaPA9rmiExMrvbeYdcPlIcWb4I23daz2/8 byLzesbrjFwSPs7szFfV/7UBx88ZxgOoleIpYrVnUC5YNJcz/GNGOv+snfeZKBL31l26 4Z8g== 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=CWoqdfr50plwmlkBtSy1F3aFaYVY5KaFBSGL1KzvXCQ=; b=T2BvJKZP9ffWu5C1fgvZRk5Yt4XE2iksUqABGkgNE7bhBnrU+StP6PjT3SqMQcgWjB 0arxOex6ONHhqhUiJbDE+UuStNyny1So8oKx0H44YGYsEvIcKXCLh6klIke/71eBW5ZH w3cHk7SEaXXNLZ6Th4og29MwZt4kqxLeRA0HWUnu6kpl9H7gJ7USfq9+YjzkYaMV7Mio yyzznHERFFqpJT0mM34ImdvWTw0m8ddF77rQra2YhxWjNN4pmpzffYSS1uYOqm5C8GAp TPd3nHKKH/ULQaXBsEKpLK6qSIWKoXh3AFzpota3b6s+Sk9KjnyapqqP2Fz6XvWC8df+ QDmw== X-Gm-Message-State: AOAM531l1hxqkqZMNHFikVkXoqQm0O/cgZSGUNnhmgoNsEeSMj9rZiJM BMPM0zQOt3SSaj5UhU6JPDwMlwo569qetw== X-Google-Smtp-Source: ABdhPJy80kA6yOxtcEYlb5cGMSvdJm4cWnGHfwTNvnIeiU+WkJgYFACkkf7bsR+rsOLzmt9uFBYYhg== X-Received: by 2002:a05:600c:5128:: with SMTP id o40mr31307616wms.43.1623666833945; Mon, 14 Jun 2021 03:33:53 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:53 -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 v2 11/30] git-p4: use 'git hook' to run hooks Date: Mon, 14 Jun 2021 12:33:00 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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. 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 d34a1946b75..e76d8df3139 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 Mon Jun 14 10:33: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: 12318513 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 B3FD2C48BE6 for ; Mon, 14 Jun 2021 10:36:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 963B761442 for ; Mon, 14 Jun 2021 10:36:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233638AbhFNKie (ORCPT ); Mon, 14 Jun 2021 06:38:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233312AbhFNKg0 (ORCPT ); Mon, 14 Jun 2021 06:36:26 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47A8CC061146 for ; Mon, 14 Jun 2021 03:33:56 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id k5-20020a05600c1c85b02901affeec3ef8so12719610wms.0 for ; Mon, 14 Jun 2021 03:33:56 -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=XDj19W/ZMPD1pE1RrzzE8ywI3DvZNTt9FGRMV/zGuOs=; b=Eie817kywaAve2NKs+60zCm+X8nVvuVtGMB4eYQ3/J108JvAt2AHEMTgva/+k4sQRV Xqv3ZDYsU1HNdgD/OxVDF8NBIGodCVDTYoHGWRXUHLCefihLEGKbgKnaWD5rrj7sYEUh NjrdAIyxjnxjoU6Ue8GpA1YkeHWIcmsI0cTjvvl9TB+216OFTtTmWYt92xjlpf3i0BWr CVZjORD6Y9q5R2s8ZpRtFKEu1E3+4ssVgwzHFfBSAlsxgeckXF2Scb/V68UGGMT+9CpD UppKZX0BIOeDHW9t/GcIqHFaTcktR5TnwUijP5cdpzZHlVAe2QXKWcTv7F5oVFcpzh/9 NXkA== 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=XDj19W/ZMPD1pE1RrzzE8ywI3DvZNTt9FGRMV/zGuOs=; b=OZrtCgf3pFp6esYjS6wkttnwOX2g4YCCiVvPfUxa7jFM8tHWHIaLo9hK0KGnskI3eq 3t1KsCOb8MIgDPyPnKnQYzcdmBU9egzdkLKX/9TELzr1XlWj9W5GP4myoSn/tPavwPxo UQmnDLThufgwWRRO3THOF3xboOK9DmaSoRwcrYcMvmWV5Vo4vz+9Jf/N2V1IeejekWKf G+1nHg465kZvZ0V6A2xyqMCTJNicWS6mrFGdTMM0HkEPWZ/pvytTjmM+CbpAiTDHVSD1 mqJ+scL9muMGrAaDDlDXhF8V+SLuzPpuLNtzW5rRbufwzbF4MIiHkN/8OcWokm5BJakU Ei+w== X-Gm-Message-State: AOAM530e8iYYVxIOzoUXzZC6HPmCSnjFjVWJryUtsjDB7pilINHGfMFg NxkgUH8bmq59q4sNUw1yuMFtz1bWOzJNRQ== X-Google-Smtp-Source: ABdhPJw9FUxTzOogh6BOlTY3inGbV5F76n6PG5fVcrA/epAHbR+QPgyMOxoQXbzJDwnxvwa0y58Oaw== X-Received: by 2002:a1c:c90f:: with SMTP id f15mr32161774wmb.142.1623666834699; Mon, 14 Jun 2021 03:33:54 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:54 -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 v2 12/30] commit: use hook.h to execute hooks Date: Mon, 14 Jun 2021 12:33:01 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 f1aafd67d46..dad4e565443 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 8ea55a447fa..e8147a88fc6 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 3de479f90e1..8f46984ffb7 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 Mon Jun 14 10:33:02 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: 12318515 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 B89A6C2B9F4 for ; Mon, 14 Jun 2021 10:36:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9E6C661442 for ; Mon, 14 Jun 2021 10:36:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233868AbhFNKif (ORCPT ); Mon, 14 Jun 2021 06:38:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233439AbhFNKg0 (ORCPT ); Mon, 14 Jun 2021 06:36:26 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AA0DC061147 for ; Mon, 14 Jun 2021 03:33:57 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id c9so13967612wrt.5 for ; Mon, 14 Jun 2021 03:33:57 -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=WxJtRfYKBIfljHxwAjBgXgZ9c9/ivqgRseAI29LMbt8=; b=Djeg0B1w5c/udnh+5GBTXI2H+GNK61nq5HM82xYQ1HAHIwmKQ9YL+OW78Q9/75NhvP BN56sHnQVX1gmshTwL7vIuYhw3iuQFYRT00qZiKlyP2Snfec4ssN6jREsQ2o5/2fmGPG XSz/+w1K3i9knmPs4Xj6u0/UM0UT1FDBvy971n+/RVzeuyVVQd1WC5NrhF6xTDLEzrTe yuL8m6peBhEoeU/gyKa9qJIFMSHT7eLnuyF0ZIz2Pbc062CRZoiM1/vRYFxCrhdWpxZQ Zdm3IYhiBcAtBs+zNv/piTF7DmGmxf4VpU4GFgJCN4lcGMs7/PNS/aMAjGe1AQDqCNzK 822g== 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=WxJtRfYKBIfljHxwAjBgXgZ9c9/ivqgRseAI29LMbt8=; b=sjUX30fTikwiSmEDs6qcpEshCPf0zoB0fYKpkXbktPTN6XbT4tCejlRak886tM3Xff jbNNFhWsoDL0LfpQSZg5Hkg/96+5yHlDGMtVVsgBGSN8I03nuUJFDrMumMczWybyn3TW /e53Q3CqN/ejjTDPPV9T5GmgEc2R5gDdCrQvlOC/Rqc+r9RXediSDvrkEOjNOOkfQDsQ Ljs9qYJZCAxDnnm9gyKm3/KLpKRd36X8t7Dx3/O3OJLp6sOxhfAAI/I9R02qUMuaQIow qaAraJPAr1rvmIyYL1ifwgGxPfcNl18h9Bx2EEW75w25W+oABd/JcA7qP9/VV/BYoXGL lL8w== X-Gm-Message-State: AOAM5332kFYyIlIcbKnkkE46mkQTUI7tfu4NyaD8DHXnH3Z0DyQCdLOf XPsOXZKBGYDesvIrbGFr47kMxLHnH3grNA== X-Google-Smtp-Source: ABdhPJxXxLBk+nl6Q59i5JD9/2w6Rn7r81Xwe48pxtUVocRNidNbfZkTaeC4po9ENDrWWvsRo0QFYg== X-Received: by 2002:adf:d209:: with SMTP id j9mr17646804wrh.328.1623666835580; Mon, 14 Jun 2021 03:33:55 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:55 -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 v2 13/30] read-cache: convert post-index-change hook to use config Date: Mon, 14 Jun 2021 12:33:02 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 af5b97104cf..f801313cc95 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 82fdf296569..eecdef5a0c8 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 b58531a7eb3..24ab5d63c4c 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 Mon Jun 14 10:33:03 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: 12318545 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 504FDC2B9F4 for ; Mon, 14 Jun 2021 10:38:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B94C61465 for ; Mon, 14 Jun 2021 10:38:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233389AbhFNKkd (ORCPT ); Mon, 14 Jun 2021 06:40:33 -0400 Received: from mail-wm1-f41.google.com ([209.85.128.41]:39727 "EHLO mail-wm1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232997AbhFNKhN (ORCPT ); Mon, 14 Jun 2021 06:37:13 -0400 Received: by mail-wm1-f41.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso12674284wmh.4 for ; Mon, 14 Jun 2021 03:34:57 -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=WR7PwsMwRCfbNoF+4AXiGruf8+rvLuVkrOyBQKA1lGU=; b=WK3vSBTN1f127u0sgXVQoSjTx/zrVmEsPidoxv2UgxUW2mX45KsQO5U8lJZKnRB62V Kq6lK9yZVc4Ax0eciy2rnDQvH1y4K/TyEqETx7o9Z/dJJrbqp3gmLY4H0bd7rwNPudow JPVZbfa5BkclZuW/fzRUGlz9fJ61Glgv1bmCnXr5QT8DEqlpRAKDUkF9kn7xOk5SKdHe eJg1IzRLZn0DPwPx7k9cEs/rf3tjR+X0TyrrrZcKtr+cDvSc2+oUr+FBfUPtnhI1iNHT 2lRTKeIY7y1X6Bgt33xLRl/uCvxpM2I0Osk0ulXzFKLVtc8pnXKe6bxRuGcakAmZ/hSr sR3w== 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=WR7PwsMwRCfbNoF+4AXiGruf8+rvLuVkrOyBQKA1lGU=; b=iHXIm2cIOTBx5aTh4kZrWEa5Vpz+qjXt7qX5jbXs8CNyxrn+3g3CAdgTvs76gztfto i+GWcXsecStiL0b4aTNXQJres6nWvE5Nj9oXkO09oMl2z8FG8uqQLK0yv2QQ0TEFEfUf SgVWRl/5Ji2wnL51Cy+6c9807FyB8FOyX40c0vG9b8NMgARVw/4h1u58Z/ZS3AwxGkfl u+60um/lwzqLZTfogXHyGb9iUEC2I+egmit9fPYz4ol7PSPlarJydFfYqaLahGLGqZKd ZUNCtlbHe56kjZUCA8h4Mg9ZgH6uuquxn+VdD2Z+8ZzFOPa0LJ6AkkKXvbsvYyYwnYNt n1UA== X-Gm-Message-State: AOAM5302Z1U0UV+gcAraFTzf7al7wEAvZ1eUUqPTnt5obmtC/OjnXbiB iu1WOZ9VXtfsaPqZ0BLnwHdvwY43mWsigg== X-Google-Smtp-Source: ABdhPJw+sSxuRuyUxwgB6UTeEPaDKdWJGw35YmNNU292tQpBT94fqqRXpC/PUElBKnKOzqlf9wbtUw== X-Received: by 2002:a05:600c:c9:: with SMTP id u9mr15332697wmm.156.1623666836393; Mon, 14 Jun 2021 03:33:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:56 -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 v2 14/30] receive-pack: convert push-to-checkout hook to hook.h Date: Mon, 14 Jun 2021 12:33:03 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 1e0e04c62fc..5248228ebfe 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 Mon Jun 14 10:33:04 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: 12318541 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 5559DC48BE8 for ; Mon, 14 Jun 2021 10:38:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4189061469 for ; Mon, 14 Jun 2021 10:38:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233701AbhFNKkY (ORCPT ); Mon, 14 Jun 2021 06:40:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233705AbhFNKhK (ORCPT ); Mon, 14 Jun 2021 06:37:10 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A473BC061148 for ; Mon, 14 Jun 2021 03:33:58 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id u5-20020a7bc0450000b02901480e40338bso9097007wmc.1 for ; Mon, 14 Jun 2021 03:33:58 -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=6fFontTwR5y5vMRitA8g33Aprg/qaLpm4iJrlCgRw50=; b=lQhrUzghLbrtFfkyvHTiS1U4cDdkddhZZUTpc6EKwalwTbj24+BoddtoQkb6B9g5iT kfyT5kcZ3MUHcSkw1jZXnx1u5y79nzUDSXwpCpToCJN1aT+E50QHHb/nVctKbfe6T33G gzAxrI9eAY71B2Y9xns455kh07KPha3T+DNquBwAwuBg0ZNox1jLV2Stlh5CAJkjtFs/ k6cn8b1Uyl9yuz+OkX7eNToUQ/4+3kllZrhWYNBtCnykvCCd5QvBJEvhHZ4NwMQTfGSj kFD4In8N9poPIU6Rzn5FuSZMOim9MBsUN35qCU6h4pmeay6Qj8+aSmmlg4YtmaXNmjOT DzIw== 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=6fFontTwR5y5vMRitA8g33Aprg/qaLpm4iJrlCgRw50=; b=jkJbjybDTcpbmPcVn8dkzVH4M3lkBgOafT0l1yJDpTmGGEWkVKdPFFKbQkKKZdzwNs ZCfWCzYtYC86NcPDJtiY50dNIXgGqBUNWyGivfZ3X4UEZf8nyAMvFu4+Cp9KPAYsMBEU gyhLgWPQjnN9hOthDCajQkX4tugTWfQsw96Ogk0MQE+1RnEYJUFTkD1F7dmncOtxS/zq i3TeFKB8VzsrCS98fpL6QJsrn4q7JrpkYvlO1WOleBcgbOIX/uLmujtI0vH7OQKI/3pN iU5tjfmbjdgRt3yCnV+M6vcH1PXc8NmflTp408TlSKQ8ZtVZLzI4m0pIDaUe2xbgFxTP yDVA== X-Gm-Message-State: AOAM533rnXmHMOzLGncUGbLPv0s10WRvLQa9Ex+1/vhYCCzr3LW5w7Mj JXVC7ZwUbeehev2qn076GhDsn7oTfFc+rA== X-Google-Smtp-Source: ABdhPJx586Edr6qShKck6HMbLCwEN103Rys2ItuQ7/R+KOYq5uJP4l26jh3kWfJmHTmxKYHOYEGPAQ== X-Received: by 2002:a05:600c:21cf:: with SMTP id x15mr31494320wmj.174.1623666837096; Mon, 14 Jun 2021 03:33:57 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:56 -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 v2 15/30] run-command: remove old run_hook_{le,ve}() hook API Date: Mon, 14 Jun 2021 12:33:04 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 eecdef5a0c8..95c950a4a2b 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 24ab5d63c4c..748d4fc2a72 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 Mon Jun 14 10:33:05 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: 12318531 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 72B83C2B9F4 for ; Mon, 14 Jun 2021 10:37:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5DC7C61451 for ; Mon, 14 Jun 2021 10:37:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233894AbhFNKjC (ORCPT ); Mon, 14 Jun 2021 06:39:02 -0400 Received: from mail-wr1-f41.google.com ([209.85.221.41]:46726 "EHLO mail-wr1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232875AbhFNKhB (ORCPT ); Mon, 14 Jun 2021 06:37:01 -0400 Received: by mail-wr1-f41.google.com with SMTP id a11so13963020wrt.13 for ; Mon, 14 Jun 2021 03:34:58 -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=c583cjIJKRhFUjc4wOuKeRSYLzv0VZfpzMGrECZj7uY=; b=GBuLw2t1fiD7gUO22FwnICFCFRA9FcYqbDggIyaN+DFTuAlZPstiBxRcrpTvC4HAsT lbpJgxVGY1g2iE8T0LE14F3aLiYtih8UZ00NPyKtZIKDYIQ6qru6+n+m3VMS+65w0Tqt 1c3/0BSDbnMK3nbuUTtM679zgRKtNAuQwM6j1XXv7oMyjB21rGyai2EQGpv7qY2kuugz ME55v5bXc6p/KnQj5R8hAqTX/GAHQm1WElIDPLcmG8s8lgbvohzRpFURNmkbagrmOBSm lZeeXNTY8xuny9MVYbepwV23y47Es0xd8BSYDIGTyPoe3IDPBp3pctBiKJXB6mCpbbDl mzAw== 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=c583cjIJKRhFUjc4wOuKeRSYLzv0VZfpzMGrECZj7uY=; b=bieEe9CWP+MHC4cbrgezF7hurK5d1URamRY0YswONpFLNWyidAh2wwHs0f0KJPHMOE 0T7g/VU/elMe85o66lOzPdrB9QMf0D5VboKynVq1QJWzJ2yJiYt5umZdfz/R2boekrUn 4O5CNTWuyeBwXcY+9mutPlVxVHIfYSej+I83nr/XSs9Gcnps/76qKo4slhoiD0giwxrb D+l6oiI0SFJK9miuQjsYatKKKMb0/zVv/J6Q0s7tL4x7ZiQOquJzjY0BO9/HBiqwAWGj eFM8fwgo+1D61X9+S1b91GRaJG9Iku0gzlNUd57GoZGybro9Fe31yRq7SGxnmi1Bmw8Z PC0w== X-Gm-Message-State: AOAM532f5D6aWKDvtb76T4YNtczds0SFCmcxuxsBvqSQwDr7V7Q8SUcH esb/J7zN3dpzIbt37oZnvyQaSLVIjpuyEA== X-Google-Smtp-Source: ABdhPJxjFwfFc2LMKHsaAtPSseHDyevrvIkzUN1x3sub1S27aW1K1Rm2wg11U9GoiF7PYvOmyHMOXg== X-Received: by 2002:a5d:4408:: with SMTP id z8mr18249846wrq.336.1623666837849; Mon, 14 Jun 2021 03:33:57 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:57 -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 v2 16/30] run-command: allow stdin for run_processes_parallel Date: Mon, 14 Jun 2021 12:33:05 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 95c950a4a2b..0bf771845e4 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 Mon Jun 14 10:33:06 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: 12318533 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.7 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,URIBL_BLOCKED,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 147C4C2B9F4 for ; Mon, 14 Jun 2021 10:37:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F12C361452 for ; Mon, 14 Jun 2021 10:37:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232911AbhFNKjE (ORCPT ); Mon, 14 Jun 2021 06:39:04 -0400 Received: from mail-wr1-f44.google.com ([209.85.221.44]:44866 "EHLO mail-wr1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233066AbhFNKhD (ORCPT ); Mon, 14 Jun 2021 06:37:03 -0400 Received: by mail-wr1-f44.google.com with SMTP id f2so13966929wri.11 for ; Mon, 14 Jun 2021 03:34:59 -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=hs61v+o9NuN8dXH1/E/hJ7rrZ9IiRob9QFRiR5JubfQ=; b=S0Jcq387mQn3ptIhQBLmuUvnpmzZDHHsbfXHKMnv0X6I/hgWMhZBD3M/f1XDSMQdIv cIL/olxPq2kaoVd20gTL02SGPzRjbIn94/b2+h5miCRCyMEBjt8cmFKTcPfMstfiWtGe /R0m8mggat6/C6Irk340WWdTDI0db0WSXyapko5BduUj006BYCOFkvEPfqAc1oNw9Y0U wp1mUdbYmSC9WqvM6tnuQiGDlGuG0+NzezSHF3TIhD3vo9FZo9N7kidAbghbj7xPH/Hh 1z8qXK7jAHKJP/Ib6E1+qRbeaLSAq1OP4UXFQbHkKtDxXruB3AyHUJmoEQvyceytjYeW KMwg== 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=hs61v+o9NuN8dXH1/E/hJ7rrZ9IiRob9QFRiR5JubfQ=; b=pCHhk0TxCkejaq6qq/Win1UwN5DLhcOh98VZGw73yMGEBve8U2JH/d7itLm3BgMwUr ktcW1ce3nXcPI4YzC8z3xr+vDmM0knuzWF7MREuM9udj6ipTeuIw4+ahT7Rl+qSnUOq+ 6kE/lf+DK6K7jcOwQWUBpIg84Xo5ymeLJr08IO0MWHcFWQqp43daindQrb0Fci7FiOXG UYb+HJBzoAZnUU60xBx3SEFBXFUuLVj6yrY+Gfm4o7ry7KePUfQyACLhbgFPBNWL4yxj DWw3nOgsu1acf5GhEGvmOted2HkTpA+GxMogwoHqYyjxM5kJyJAYzCFVLufDbF2+e7t8 CfYA== X-Gm-Message-State: AOAM532PXTimJ57xFMygZrHtpAPp7YZWQs7txQlCesDV8tJtJX229hUR e/gWZVH7FH8prigvX50jDGAYyYrNtyh5Wg== X-Google-Smtp-Source: ABdhPJwPV2DfVYzJsJaMgLW+HAoAy8fb0fz9+jjhw/FHUVCmA1MyGZ3pKE3KpcezODqP2bi3hdgxdQ== X-Received: by 2002:adf:fe8c:: with SMTP id l12mr18085823wrr.26.1623666838700; Mon, 14 Jun 2021 03:33:58 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:58 -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 v2 17/30] hook: support passing stdin to hooks Date: Mon, 14 Jun 2021 12:33:06 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 | 4 +++- hook.c | 8 +++++++- hook.h | 2 ++ t/t1800-hook.sh | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt index 1528c860cf1..816b3eda460 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 ----------- @@ -30,6 +30,11 @@ run:: 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 275dd5b0ed0..baaef4dce49 100644 --- a/builtin/hook.c +++ b/builtin/hook.c @@ -7,7 +7,7 @@ #include "strvec.h" static const char * const builtin_hook_usage[] = { - N_("git hook run [-- ]"), + N_("git hook run [--to-stdin=] [-- ]"), NULL }; @@ -23,6 +23,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 51337f9798f..daf39f61741 100644 --- a/hook.c +++ b/hook.c @@ -58,7 +58,13 @@ static int pick_next_hook(struct child_process *cp, if (!run_me) BUG("did we not return 1 in notify_hook_finished?"); - 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 2d7724bbb50..74a8c76a94c 100644 --- a/hook.h +++ b/hook.h @@ -28,6 +28,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 714dfd08cd8..1a6b708137e 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -133,4 +133,22 @@ test_expect_success 'set up a pre-commit hook in core.hooksPath' ' EOF ' +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 Mon Jun 14 10:33:07 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: 12318547 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.7 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,URIBL_BLOCKED,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 03346C48BE6 for ; Mon, 14 Jun 2021 10:38:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E04CB6140F for ; Mon, 14 Jun 2021 10:38:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233544AbhFNKkq (ORCPT ); Mon, 14 Jun 2021 06:40:46 -0400 Received: from mail-wr1-f53.google.com ([209.85.221.53]:35433 "EHLO mail-wr1-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233120AbhFNKhR (ORCPT ); Mon, 14 Jun 2021 06:37:17 -0400 Received: by mail-wr1-f53.google.com with SMTP id m18so13982870wrv.2 for ; Mon, 14 Jun 2021 03:35:00 -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=YJLZb8O9lw3uH9CyeykuLkCsjf6FqX8V3WkuSOlv+sQ=; b=pQEx2dWFVLp0VCREwrWNlnobMEKQISpGKpw6esWa23mHTaY8EERt0j0+DuOqJNFl5W xZCl8UMlJ7cNljTY8QOuY1quxdjDn7hv2NkMFdB9QiXsQ/UWOxL/SLQBpmI6+J9s1Gu1 hG/5mLGfzNYHVO4MRiBQGhIHFVj6V/Nm6LKPJvXo6Nz//B2/4lwK2pdwtoxQMEIosqNT fjdS163hTkZV3odCtWatjtqdeckJqtrK0VxEvpLfVZNQ0zbOqkJ//HJdXTJiLKnKFXaa UpPXfuLrm8PK/DCdRjhr3MqXstKoeYAqbV0hdZ9qE4xsHErPbkepJt9GzHegbSC3GGMs VkdQ== 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=YJLZb8O9lw3uH9CyeykuLkCsjf6FqX8V3WkuSOlv+sQ=; b=oTQl2dHioklctSdDBVBTZybkNRnwHNCcAoDySEO3zCWOcFX+3230EhzeRQXe7jl3MN IvBsmfuSvt0uzDeehpGi3VIoUQ9nnzWWNAJ0XvyMVC3n7W2dhvM/zcPJ2e1Mw8QLrzQb w0y+RWQN64KmFSfxdjs5blROel1KkUE1OLIhVch6Ja44KIlDuxrMDS56z9i7V/3Sj4go EWZhwjawoKo9oTKF+JFjVujUnuG+E24WJuIneO5Hm+RM7eY4WYU9z0oPBebbKpu6od+J 435/GCXCMC4I1xvhg6/ekw3NDTfp1tLH7MfQnty6HY1kZJX010GwExmue2Lhcf1CH0Ht X/4g== X-Gm-Message-State: AOAM533/X6RckZRDFQeitnwXsQlU/gSbPjsvB21X4K2ENdMiJFfUc1oN fTmqCKH1TUGAa7yHiktZokqO0rXBI1roFA== X-Google-Smtp-Source: ABdhPJyTKeW4w5fcyvHhCa/vgHnFycvd0XoYmWdIHfd0Pto6GNvL4Nxqa24V7OFZLnMeZB8KLiPmHA== X-Received: by 2002:a5d:6b0e:: with SMTP id v14mr17833543wrw.297.1623666839506; Mon, 14 Jun 2021 03:33:59 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:59 -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 v2 18/30] am: convert 'post-rewrite' hook to hook.h Date: Mon, 14 Jun 2021 12:33:07 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 9e9c1b5e9f2..6e4f9c80360 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 Mon Jun 14 10:33:08 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: 12318555 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.7 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,URIBL_BLOCKED,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 B345BC4743C for ; Mon, 14 Jun 2021 10:39:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A0EBA61477 for ; Mon, 14 Jun 2021 10:39:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233574AbhFNKlC (ORCPT ); Mon, 14 Jun 2021 06:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233253AbhFNKiR (ORCPT ); Mon, 14 Jun 2021 06:38:17 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A01AC06114B for ; Mon, 14 Jun 2021 03:34:02 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id g204so11821159wmf.5 for ; Mon, 14 Jun 2021 03:34:02 -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=PjjzdYZTj76sUHgHSRZ+xD/7yQHLQ1TOZ+Rb5LYGxtA=; b=GMRo5cxzsHtJ2+ypVBSMygeL+Y5NY8FLZu9o/UL5jdsqzSrlbbI3kYh5G+wyWhv7CD YiB9PydE9U9whIOdFxwMt8bgPDZec3MqdNMruZh+pwUERPb+8t6AKdKpsLOk0/Q9a4Dk g5X9hOnrO+QfEntm0jXAEzgTGuAWRj4/ZDjKwFXBkVl1NLqk3qzJ2nKJBM0/c8/ilXlD 02tzgxkHgA8HaN0aRfjAe887mbYBdDREliYjij5LqJ2expj/sNJ9KUcnc0w1X7325SlI Z75/MKJ+vidm/NfMzqv65a2//KZVpRAMQ0iw6NLGZ38yjWaInpgHPkuFk3CFyKBw3kp+ S0Hg== 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=PjjzdYZTj76sUHgHSRZ+xD/7yQHLQ1TOZ+Rb5LYGxtA=; b=HFhgP8OzJoA/WMD+CbiZhcoJRjy1mO4xsym+oBKmC2ZKwiSXmKvNqesZtqmF21tlYD t/5sp3bubbeF9Bc0NfFPrA3Q4123ZaIqa3r86Oe0lwt64umMm63f50/cRcrmDFTNAgyo ELanjNXHBeP5cXa3gGXQTRCHlNsJoDR2iTyZxj06YW6nlXHaK/qFtJegt/iiTuAKo9CK uUWa3xPfEIovw/VBXCEDKpT4a8lbpvceU+1Kaa3d8l2ZyiAxpiPcG/DJGJw4EZptdP3w qXZ4t98K5a1wsf1TSj6ZWzg/kgrXFmMNO/p3yw9eYklluDLj/O9ClbSFo0dsSndDjcbV Xrlg== X-Gm-Message-State: AOAM533ngTe5LKI1rHLO/1o7mlzBp6hTd7po5RE007hdVbzGdub4TkBb ZtQn49hz4DAZlxp/sP4Th3zDhIfUJgSFVA== X-Google-Smtp-Source: ABdhPJyuAqJ3MUHr7JrjCd0L/9q4E+fcbHMlvZBZ3t7Y7lJ7SHFZ0ayw1z1+cb08q16kDbkGbJ2JMg== X-Received: by 2002:a1c:6455:: with SMTP id y82mr8398466wmb.66.1623666840469; Mon, 14 Jun 2021 03:34:00 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.33.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:33:59 -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 v2 19/30] run-command: add stdin callback for parallelization Date: Mon, 14 Jun 2021 12:33:08 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 9191620e50c..3d8f04b392d 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 ae6174ab05b..818494dd18e 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 daf39f61741..5f6335bac3f 100644 --- a/hook.c +++ b/hook.c @@ -139,6 +139,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 0bf771845e4..3392640f17b 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 748d4fc2a72..41e36d26cb1 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 0b1d9c1dde5..ea026a8195f 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 7ae03dc7123..9348184d303 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 7d599675e35..87759482ad1 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 Mon Jun 14 10:33:09 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: 12318551 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 16474C4743C for ; Mon, 14 Jun 2021 10:38:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 03FC461466 for ; Mon, 14 Jun 2021 10:38:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233750AbhFNKkv (ORCPT ); Mon, 14 Jun 2021 06:40:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233812AbhFNKiH (ORCPT ); Mon, 14 Jun 2021 06:38:07 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2223EC06114E for ; Mon, 14 Jun 2021 03:34:03 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id y13-20020a1c4b0d0000b02901c20173e165so4579497wma.0 for ; Mon, 14 Jun 2021 03:34:03 -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=0CjckoTRrYVle/J6enZG1j4QRqr8mtNff9jGBcP5JrA=; b=HQ1KD1yDeyu//C2y0abqLsbLm9OzHQqN052JzXgNUUd4FrU2WYs+rZHOP7byPJbzvl 4FbOhAKeJp0VEeTG8eVPVbiEr270R8JDlSYLT0tptzC+5ziDeMPhuEC0qDOoX4m21BAb gFD3g4zP4CAbIIKsFszivvfNQN+XVKg6nUgH3CI1DpEsuhSGV6MGrQpOjW4hUw4Hu2yR 2O57f1IJLm7lkwDU3H5Td7cf9KqvhX6SncaCfK83/OzskFhFv1wE9GjFNKz6I2FWdcPh w4bAjnsJ6rV6bh8yc4HB/TAKLOCtJZ/H/JEK6HPoXzgKHC1hEz7g/uBxbot+gKk1WQ1x 3ong== 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=0CjckoTRrYVle/J6enZG1j4QRqr8mtNff9jGBcP5JrA=; b=SZ5U7SyOZA9sV4dSmT+dY/+0ViRF3u4OzcaacrRTtwPUvmuh//aVnHYBAF6C1MG42r zIhWlRDNnCVVnC/6pznwNh5GaRjc3zKHnOVIZi9fvG39hPudtHnLBDbG4ITS2ucE6Uhn Pa7KH8JZWfb4hcl9tu4S11ctK/X5kujhVoEa06TfV/tn9urFpELbz+sp6Uvd8vXH8yQT i3g39MAQ44v4Ls5IrEqAzU2T/iuR8pE6OjI/HBfwWjva8/Mf473W1MibbNR09FHQBg4R dXMK35BhEPzxwb3r4LGD79QWK3Um1BqvslRK44k0FDzcxxBeq7cW0wibU/l/+P5udy7I BNNQ== X-Gm-Message-State: AOAM531dZDmrqpL3u67ZkRutuVMs6HZ47Zf/GvXLVHK3bgCgTo7IQCaQ gikka1WTjgP8J3W/yr3zghajWd0aJKR4RA== X-Google-Smtp-Source: ABdhPJzhCNzVYzcjrfmEs5mJkm6+Xe0Jvs7XFV9rV/YieGXkDhbifBjVBt0bCKyN5YFIiPAKrMQfgw== X-Received: by 2002:a05:600c:4b87:: with SMTP id e7mr15751778wmp.101.1623666841385; Mon, 14 Jun 2021 03:34:01 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:00 -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 v2 20/30] hook: provide stdin by string_list or callback Date: Mon, 14 Jun 2021 12:33:09 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 | 33 +++++++++++++++++++++++++++++++-- hook.h | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/hook.c b/hook.c index 5f6335bac3f..ac5e3988fec 100644 --- a/hook.c +++ b/hook.c @@ -47,6 +47,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, @@ -62,6 +85,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; } @@ -114,7 +141,6 @@ static int notify_hook_finished(int result, return 1; } - int run_found_hooks(const char *hook_name, const char *hook_path, struct run_hooks_opt *options) { @@ -139,7 +165,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", @@ -157,6 +183,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); /* Care about nonexistence? Use run_found_hooks() */ diff --git a/hook.h b/hook.h index 74a8c76a94c..ff1697d1087 100644 --- a/hook.h +++ b/hook.h @@ -7,6 +7,12 @@ 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 @@ -30,6 +36,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 { \ @@ -38,6 +57,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); + /* * Callback provided to feed_pipe_fn and consume_sideband_fn. */ From patchwork Mon Jun 14 10:33:10 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: 12318557 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.7 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,URIBL_BLOCKED,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 4735BC49EA2 for ; Mon, 14 Jun 2021 10:39:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 367A561469 for ; Mon, 14 Jun 2021 10:39:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233694AbhFNKlD (ORCPT ); Mon, 14 Jun 2021 06:41:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233821AbhFNKiS (ORCPT ); Mon, 14 Jun 2021 06:38:18 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CC1EC0617AF for ; Mon, 14 Jun 2021 03:34:04 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id d184so11862145wmd.0 for ; Mon, 14 Jun 2021 03:34:04 -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=Me5IR44UbEIIVzwoWkHzyQI0SIlKx1CvIlEV7hgtNZQ=; b=eT0+qHTdRH3pGD6j3bZ7RAjLlPg3DDKOwpcKM73XKRwFe6lTYafnxlN0wrVXfCPcSS PecVsYAjTpRWxbYqdSNU80sgG4hGOIZbgp4mO0ymMQsh20yHvDXVPedYEN/hDfADdDs3 F1cIrvc3+dqxTuwKKpxFPJYlXngg22NFYkRWeGbazlg2DMBISFmEY5pJo5nbhpwE1XJP 3AnzHdZq5mjCYX6SoxivNSYrS8eRMFJBspMfXvv3s50tppiV0WVTtRAbOAuH2UE0mEKr hDrnCmZMBtwVRsd6An5dB83F2b29x2A+ZMUjTu4M1O3lo4vh1Rmjvnh1/2K3jFPjvTbt O1nw== 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=Me5IR44UbEIIVzwoWkHzyQI0SIlKx1CvIlEV7hgtNZQ=; b=Drdv1N1O3iazV+G8Oy0QDFOE8dxniwOjpUf/XE8t1s2z5qQRaE0vUse4tsPt+tTIqI HQlhDb7aedKGkop0P2SoSx40FkcejACVxUfr1kowpNKGgwrRTxI2opaeVw05A2lf8v8M 3SztHHRzP+W/1J6t7EEM5S/somDYP7iachu9+g8pB7HMgZoHdVL9gHhwJHlK7IhL+WL4 P3pgv290FsvXNsjkN4hkwOFoTaE9nuO4NsYkgnPEhidA13AE4K8ijANCJcjxox67NqHV zQgZdq2srB6P3sU6lbCRNJeRaL51UMZuH/DH/lrk+axjRIq1JNfBew+OfyKKwXXV4RlL q2Vw== X-Gm-Message-State: AOAM530RWokUQOEqjZhZjvdKM/OYisyhI9soiNnUGpWuxV3xsUu40FcE vM982odBRZChDHpKlRBtiCwvWoIfUHb5zg== X-Google-Smtp-Source: ABdhPJz3WiGu0G5VhLs+RygDgclJCgUO7eXr810PdRCzkyZ4LmcrPrFzWqPYQO+Jgd2xI9IB0DriZg== X-Received: by 2002:a1c:7402:: with SMTP id p2mr31559773wmc.88.1623666842425; Mon, 14 Jun 2021 03:34:02 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:01 -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 v2 21/30] hook: convert 'post-rewrite' hook in sequencer.c to hook.h Date: Mon, 14 Jun 2021 12:33:10 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 8f46984ffb7..ec2761e47d9 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 Mon Jun 14 10:33:11 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: 12318553 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.7 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,URIBL_BLOCKED,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 E87E2C2B9F4 for ; Mon, 14 Jun 2021 10:39:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D62CA61469 for ; Mon, 14 Jun 2021 10:38:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233169AbhFNKk7 (ORCPT ); Mon, 14 Jun 2021 06:40:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233064AbhFNKiP (ORCPT ); Mon, 14 Jun 2021 06:38:15 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF117C061787 for ; Mon, 14 Jun 2021 03:34:04 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id y7so13970892wrh.7 for ; Mon, 14 Jun 2021 03:34:04 -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=ypHfSi7SXV5NcfJn+7pVEt9nZfmts64RKFBGRgUZ5R0=; b=Y4UR2G2ObYHBs41yMbUk6Y5RZj+BpNlO5D/BpFhnPvtgVCNTSMyr2InUSCzcLxjVSr uMu/fhLqOWwGrqaq0KV0M3PSMYehIHkyLoHwCLvfxK6j1OyLc15zz9BIq7EYFCvIWZmf f3d/J4McZf7ZIAu7OL7o1RzzX+ZsV31HZTdHDQ/hQHR2/Ciel9OFjzyn6QtsRzSB18vV 5skJ58INzUjRpkpkvyufO3QJ54QOuBh9oKabJBHUJrzU07bGuYiICtKv7z/ureiT+47j qMJQlPHKS5ZnoBBnMLaecllRvyIvfF9oknltYoSPAZ13hO0Y7oDIR7dTTmHX7tF8w35l ILnw== 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=ypHfSi7SXV5NcfJn+7pVEt9nZfmts64RKFBGRgUZ5R0=; b=k5iqZcO/+JqFheU2ASP6kS8VGdrQ14qTKyk8h6XQDKWa/kb6Z63ij0vWYtoE7V5Enz qjdrYuBRB+ob01cWMnP28Szj9B4/tfaHLDz4S0s9bVbYN1tWdeEU7nW65WTn/k6FJ92a tw/R5O48UCHz5YVT+arPrslf0PNokKJfWvLnZ2Cnv/t2XRwy+jVec0cyGjfHKnzrOqKd b3/ljIRFI4rNWPJL0hxpy+74RmTEsNNyWwwbTVtjqEgqoLtItXvmuMSdH0uhbnYuZvPe EWxgQ/hjeI9GVuezDRaFCH2+aK5x6dcZ372APbDX1tcTEk7sFOsGvilcEhEvRPkkvpzo hK6A== X-Gm-Message-State: AOAM533Z9H5Vf88+7lHwQw36Cvsfi5GZqoTo8iHd/WiGthRw5Lx483VE 9F40RHb9of4u8ZM28kqEKk+4Pf4gBj/z6g== X-Google-Smtp-Source: ABdhPJwmeRA6lX5D1WXPeYmK1QOXRMpmvH6RwIHOfV/xDhX9ioaIz3epLtDIFS/FVtMkwLOAbVm6sg== X-Received: by 2002:a05:6000:1049:: with SMTP id c9mr17650827wrx.416.1623666843308; Mon, 14 Jun 2021 03:34:03 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:02 -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 v2 22/30] transport: convert pre-push hook to use config Date: Mon, 14 Jun 2021 12:33:11 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 2ed270171f0..9969ed2cdde 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 Mon Jun 14 10:33:12 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: 12318549 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.7 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,URIBL_BLOCKED,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 8595CC48BE8 for ; Mon, 14 Jun 2021 10:38:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 738CB61464 for ; Mon, 14 Jun 2021 10:38:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233451AbhFNKks (ORCPT ); Mon, 14 Jun 2021 06:40:48 -0400 Received: from mail-wm1-f41.google.com ([209.85.128.41]:51775 "EHLO mail-wm1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233739AbhFNKhV (ORCPT ); Mon, 14 Jun 2021 06:37:21 -0400 Received: by mail-wm1-f41.google.com with SMTP id l9so11842291wms.1 for ; Mon, 14 Jun 2021 03:35:04 -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=ilzOW3GnKIopL42EeHdRGSokwhnkmREW+LSjof5wwa0=; b=lT7dk782ijuxrqOXBWjjkQ/dMKPOXEg7w547zLgt1POqufbiqJUbPezrOdGnJ0vpIm s/XEhNtRAwEH14DyGrar6GaQq/e9XCYuXlwuCAbCBw00ApREmsua8GG26ASlYOru11v3 lpT9QdvzqTTjU9d+7o5CqZ8ry994R3noobrmtD2hzbVVlwQ+nRESD98Z8yeQWaDVZTiJ oZ5UWSbBabvaUSuix2kVYIUX2kZ8WZpoGt5IWFYkmVHGrC3NZsvNjznbNoEaPhMEvv8w N159G+cciiHJgWMwNthMCsh/HWBWjkY1adbNiTp3FjokzYy7wbUNbTk25GUHwvTaICnV 27IA== 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=ilzOW3GnKIopL42EeHdRGSokwhnkmREW+LSjof5wwa0=; b=MGtq3kPOhnJkGNVV8vX1QTmnpKUIWXn0IDJZVDXeyF+qmv+PGoQSYLw+SWrWCP+2rh JoHjtiA5Kd19yyEvP77LwnX7CQFmCpEbCTXRU0QxnwPXZMwQz5y3XXhZaLwk3Udnm7rM eueSl+B52K9ALYwx/UKFNQq/iezJRGVfWFyh5wlS0pz1aYAwx0JrpOEp0AYpL68CLE3E vH8xr0vbY7eaWj9+JRLwCSUDznQPKwQgHkddpbpH7lIM5yVfCdPJA/09nVixVTAqzkgK 5HrPfQmpOa43BOH9HheT30KQrl66h3n1CsPzpdlSkyRpb2+gExAc2qZJP64m1MU5q8UI aziA== X-Gm-Message-State: AOAM530q/JxXXUp/JmDxA1IR9yjUORybfjDMFQOXkwtr703Fh4a0IUWH bDOiExPwIPvkLxRdaxh2rURRcKPoIVuF7g== X-Google-Smtp-Source: ABdhPJxRdqQ6walbdS3wY/Ws+2UMmMQi11Mfrh6/8AAzN9i7FnFMcEyDxX4ki6WGVukyMOdcF3MnYQ== X-Received: by 2002:a05:600c:204:: with SMTP id 4mr32046580wmi.95.1623666844216; Mon, 14 Jun 2021 03:34:04 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:03 -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 v2 23/30] reference-transaction: use hook.h to run hooks Date: Mon, 14 Jun 2021 12:33:12 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 59be29cf081..1149e7e7dcb 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 Mon Jun 14 10:33:13 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: 12318565 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.7 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,URIBL_BLOCKED,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 230D4C49EA3 for ; Mon, 14 Jun 2021 10:39:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0742861477 for ; Mon, 14 Jun 2021 10:39:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233463AbhFNKlM (ORCPT ); Mon, 14 Jun 2021 06:41:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233446AbhFNKic (ORCPT ); Mon, 14 Jun 2021 06:38:32 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8102C061224 for ; Mon, 14 Jun 2021 03:34:06 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id l9so11842332wms.1 for ; Mon, 14 Jun 2021 03:34:06 -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=FJRuNTvpbdAQr8hG67thKHCa/UsNc8dhSrkBxYNcx3E=; b=PXkmS8K6hQxJ0A681o+CNlcRd97U/wEwdA8Cc1yUZVhWpMZYfUdEXsGJN9KVQPHzmt oCjcRDR9lZnOQdHsMIFk3so0/qZkiuTsiBjcGP4TaeR1kR6NyN05tT2RqnvGOA0Ei45h 3rdBSPWsNWJaeFdj5kKI6VWVdp+39p+jW0+sUMT3CjcgSWHIjd636r1p8Id/X6Bug93L yGE9D6w862D1eIzYCQijfBb9n36MWRCunskykoCKXOIhM8l4QqPLWs4Gofq+thV15Lt2 6Sb3aU891gu0q3NnBl0BXIJDxw2SrxWPhNfVDQ7E2MW5N+6hrFe0SDSX+XdORmHqWblp +HXg== 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=FJRuNTvpbdAQr8hG67thKHCa/UsNc8dhSrkBxYNcx3E=; b=ar7m1OcmxPK9//fghkyihyu014OtORuva6fjaeUJVfGYyqE9M5OwwPDhmJhLCxZ1QJ +Qd5uxcmolzl1J91jitifIVPJRvS6dwKR3PQZjkDEADQKVAtbIIvSZcH1JqDEDCbY5bd KW5OZAFsFXvdYGmDD/6UmswY0msnbHg3xlfge7qLERGHIwOyH6DaYIO504dNcHWqLitH Hvib7CAjPfh/zlPbYunqO3EbJx16swW7sLoEEFvJQ8K+Pbfkl2nmieHcY0KzBmeN8ETT fZS2nHNlIcKqQA3+LtjIRbZsF1tygmIadZVDhNiu8AAboxpS37xNravN29M0PA79Ghgt +rBg== X-Gm-Message-State: AOAM53398MnD7oYqVm6e7QDlMlQ0bnkAoM0sqx2FAoPQqQ8fjHbzA5cn yNr/0VKyjgaBcKcwR+XG/sd4tQqQyTRtrg== X-Google-Smtp-Source: ABdhPJxHdogAJP0oRbFcnpB1+A9Zi3JsS/kEwgD6iQ2Q1QVaPcIYokrFpVOTgAoeYbszPragPO2rUQ== X-Received: by 2002:a05:600c:4ec7:: with SMTP id g7mr15451113wmq.157.1623666845085; Mon, 14 Jun 2021 03:34:05 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:04 -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 v2 24/30] run-command: allow capturing of collated output Date: Mon, 14 Jun 2021 12:33:13 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 3d8f04b392d..0595b88b7cc 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 818494dd18e..69782f62044 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 ac5e3988fec..0faa24ec825 100644 --- a/hook.c +++ b/hook.c @@ -166,6 +166,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 3392640f17b..4a1a7a10820 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 41e36d26cb1..7150da851a8 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 ea026a8195f..7fe0c8f7c9f 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 9348184d303..d53db6d11c4 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 87759482ad1..e99f6c7f445 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 Mon Jun 14 10:33:14 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: 12318563 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 A5FDDC48BE8 for ; Mon, 14 Jun 2021 10:39:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 91B6261474 for ; Mon, 14 Jun 2021 10:39:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233215AbhFNKlK (ORCPT ); Mon, 14 Jun 2021 06:41:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233875AbhFNKii (ORCPT ); Mon, 14 Jun 2021 06:38:38 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2977C0613A3 for ; Mon, 14 Jun 2021 03:34:07 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id n7so7813962wri.3 for ; Mon, 14 Jun 2021 03:34:07 -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=tKdHbjwNT2h1uxYqxzxZOgPsrToLQVFrXqodsNNOvzk=; b=UgX6C2X1HTdmjo18atcJKbjTim0gHlxO4MyeASwCP+bL5SRHGSRGPW8KLbgqIeFwdK JjB6NKrsLUkQuiKU+r3fYRSRNSdWG7OHNESg1dKk08TEmCSyzsl8xEJ2en0BXL3C8Xz0 //tguSDOPqB3jfF/VCAaE40aBHKZRIpcaDuJRz+t5KQO5VUUPvkY09NMHzSrryDThCFQ bWXiSRlKMSOpZDhMTXekHWGskv8Howo5/3lN1tuSQajhowYRMp8SGH5lg26WF7OCbkr9 Jl75gZKCFdwBCNMYwI9cbTmm6e7Uz3IYgoTS8bKGUvoXISvJiE5LvkNwV3rhBKmU3l4k /dfw== 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=tKdHbjwNT2h1uxYqxzxZOgPsrToLQVFrXqodsNNOvzk=; b=fzzfGo4D9zKwbTVFNRL7VRinBJCB6lmYTxjpCQQlT1Cqh/l+qYQT1JMjrQ7MYRLkbB /9rU0fYsr22ffibYK8XJZhnPUTQw4zdJRDH7jSIOVM2Dg9EfwlU6br04gsFKXWw82YNK WpRR/LdI1cNzmJd74Yyi2uTBk9SNF29xnJAfb2UJJaT+X8wjPczry/DaFY1ICEcliEqf PH4KAhKnt0ZMfMNzxKSboL5jyBLre1OM2NUAUFW6XdcHLfyrC/FadMZZtee9hBB43DvP Wv1iZCKA9lvcSAQSSH5LRYb7vdj3VcFKxgfw0VYhFyhjPXH1xNPKevKGzr7hn4ME0sah Qf1g== X-Gm-Message-State: AOAM533NOIGZ+Iv17Mxg7/k+ltZMNdFdjllWMlqMPN4ZWp4q5ojg6JQf 9ya9xHhlFwQjyH0mu8hheoOc6LL2Yqg/CA== X-Google-Smtp-Source: ABdhPJynl9zVYOq+mhpxd/IQkDxkQobmtQIcIrqTjGYsBYgJaJXtAVX5bxTsWvxjGoEq0lK/EaZldA== X-Received: by 2002:adf:d1e8:: with SMTP id g8mr18212993wrd.309.1623666846295; Mon, 14 Jun 2021 03:34:06 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:05 -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 v2 25/30] hooks: allow callers to capture output Date: Mon, 14 Jun 2021 12:33:14 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 0faa24ec825..17ae65eca31 100644 --- a/hook.c +++ b/hook.c @@ -166,7 +166,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 ff1697d1087..5f895032341 100644 --- a/hook.h +++ b/hook.h @@ -49,6 +49,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 Mon Jun 14 10:33:15 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: 12318539 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 A28BDC2B9F4 for ; Mon, 14 Jun 2021 10:38:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D4EA6141B for ; Mon, 14 Jun 2021 10:38:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233613AbhFNKkW (ORCPT ); Mon, 14 Jun 2021 06:40:22 -0400 Received: from mail-wm1-f46.google.com ([209.85.128.46]:46754 "EHLO mail-wm1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233707AbhFNKhK (ORCPT ); Mon, 14 Jun 2021 06:37:10 -0400 Received: by mail-wm1-f46.google.com with SMTP id h22-20020a05600c3516b02901a826f84095so12473092wmq.5 for ; Mon, 14 Jun 2021 03:35:07 -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=LESMR97jcTsx8/DDjybCC6129jMzHgP48FHyDQmD1QE=; b=QvsE6ds/LQlewS6EOQKD1SdIiTdqafzwrAD0ja05ly1a4LTrFiNQUMZN8Xxn9Ga/VE 2oDODJVGh8lnC/hdUR2UarlsbMyg6MS/zSoejCdvjyBs6D+K+TFcCYVubLxV7mB4KxSI GmoYpaT0AmyXwlVCvnXtA0mJJjLlJ4/TUtlL2ITcRnpUHrmFhjMMHGZKxp06DFsYcy3O F0sRm+wGnbThpfzwvPFu4mBRZmv8oZoqpbeilujMr1bzGR2UdSeM45IvCObMayrq86b3 fMdT4C1vM4oKyIOBLF4sLzvJDZJLfwhwy1CyrqPeVfRvwXD46IQ33LDCcw1aOYl21po1 g7oA== 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=LESMR97jcTsx8/DDjybCC6129jMzHgP48FHyDQmD1QE=; b=BM4AH/n8j2yc3rY0d90z4Y7anAwF61+k2Wi/+6ZAqTi1Qs7BloNjsMr5MVN1KKnHAY HDQFmjcpzDmc5uzkSmgejRJq+pnS7lkP83UySAIT0Dm8HkfKA4fMwQHdxOXiYOAW8eIl yPgPC8toOtoiBMEECySvEhmKs69GHH7O6S69TwTvuTHum6Xol/QE0Jav9EMTLpxG5dpR A3HF1/qRF3HGBaVASimKaTRobhtmMxcFjeOQLlwSSOuZTstnnZZ7ZntDxN/TAIqgJNIG xqPn1uTZLp89zHUVUiSj8Yi9Ix8XxU5cPK6Yg8TZO8v3ZJz0edlaISxzxAjdar5Vfbui YmBA== X-Gm-Message-State: AOAM530XlA3AaahfmEMSwAWXOYmpl85UDx1a/f9GJwy/FlB1ee0qlAEe KBpjIQ/KTWyYC59bNdWCMWIlzLHPKwFUSA== X-Google-Smtp-Source: ABdhPJyTxllVJdS3d8w8/n2DzwXaEt0hL1xSsoyvo5Ar8KCCYr8P3jSURw3HxREgf85aXO9RrsXGug== X-Received: by 2002:a7b:c24a:: with SMTP id b10mr31702919wmj.25.1623666847123; Mon, 14 Jun 2021 03:34:07 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:06 -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 v2 26/30] receive-pack: convert 'update' hook to hook.h Date: Mon, 14 Jun 2021 12:33:15 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 5248228ebfe..378f8f6b5d1 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 Mon Jun 14 10:33:16 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: 12318561 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 33304C2B9F4 for ; Mon, 14 Jun 2021 10:39:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 144B361476 for ; Mon, 14 Jun 2021 10:39:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233297AbhFNKlH (ORCPT ); Mon, 14 Jun 2021 06:41:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233874AbhFNKii (ORCPT ); Mon, 14 Jun 2021 06:38:38 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70099C061226 for ; Mon, 14 Jun 2021 03:34:09 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id q5so14003573wrm.1 for ; Mon, 14 Jun 2021 03:34:09 -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=Csbtn5cRttyubGogbl9HJqZG9OFQrrDlf8OCzLI6sJM=; b=B8QT0LXiH+E1zBU6V+iu6Lbqg9LUnCGjQSC/h+4ixHce3XxlxfmKeGC9ofV984O2tP FDitflKk46FsYEY7yF8l6JdT9Oq5Q/eQpXZr1ssmCZseb4MbnLadxSDyVlDwUYsFFFD8 0rCo+AWK9a2FfNG3fcSc4OzrhD522DdQYvFvord9rpF7P8p74I9+58KadI+gkqGwf22r 1BSRpBiCHSsL4gWYwhPrjkZ3rIbXSWxxI1spk9ELvVCtfqtqiV1OveWk+CD+TPxAy8rm fjt0PRiv8vzFMqYRulQgXivrwyLGg4c1TD7nclLB2j2k+ZwySqZK+aejmtAraH5My44n oEeg== 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=Csbtn5cRttyubGogbl9HJqZG9OFQrrDlf8OCzLI6sJM=; b=qWj4gOF4VmBTfONSywk53fb1XYGO3VOABqgLgogJ7Rzw7sNRmuEIeQ408eAMbffN9c jEsHUu7Fo3C84H76bZNxLL2KAPjUL9mLuFvTRbn7eyherqPPcBkhgwbnZ7V04kFhXs+r UDSbPVHyOiq6nrebXK4J4HgX7Xdz/4vkMfNzVOvk4bmzSdB+/YWCWtuzUg5FI0tZQ3Xy T9q9vs3oB3U/Q4BLJ/Gy28k5vZaOysjaUeN54+kKOYerPCyhqEhqzOHJYbwWVbuyO0cn usSZwxbBtz00XU+12SGxblIuytL9kMjdyYoVz/b6QuPyzo7I05dyMhbVxOXMonRr9lAf 2H8g== X-Gm-Message-State: AOAM530JYgSz9XzzyyCtq8Z2lI36MgHkId+Y5aldPMvdCg68PclCvzGJ 7sKJwXvm4r1b6BSBMwq0kq6oj5UC2Kzbkg== X-Google-Smtp-Source: ABdhPJxRdWI8bBkiT0Gmabl4kHDKJbdQ7TVlz0yaTBVtMP6TMoTz7nfEtV93BpkKyMPpLXwVhx5O0g== X-Received: by 2002:adf:fe8c:: with SMTP id l12mr18086642wrr.26.1623666847860; Mon, 14 Jun 2021 03:34:07 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34:07 -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 v2 27/30] post-update: use hook.h library Date: Mon, 14 Jun 2021 12:33:16 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 378f8f6b5d1..b2ccdb66daa 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 Mon Jun 14 10:33:17 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: 12318567 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.7 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,URIBL_BLOCKED,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 B7AC9C2B9F4 for ; Mon, 14 Jun 2021 10:39:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A271B6147E for ; Mon, 14 Jun 2021 10:39:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233814AbhFNKlS (ORCPT ); Mon, 14 Jun 2021 06:41:18 -0400 Received: from mail-wm1-f47.google.com ([209.85.128.47]:43583 "EHLO mail-wm1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233714AbhFNKhM (ORCPT ); Mon, 14 Jun 2021 06:37:12 -0400 Received: by mail-wm1-f47.google.com with SMTP id 3-20020a05600c0243b029019f2f9b2b8aso12483453wmj.2 for ; Mon, 14 Jun 2021 03:35:09 -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=9Ro5rHts4hw/ry0Xv8bJ+87ugrvD9L7WktYG6axhJ2w=; b=ESKy6OD7iimKgq7cG0pvu6Kz/asIldXcPXt8WUhvnGGr6ZCmPfmU1xn78vQl9xQtaS XW0977h+burrd2ZYytg+HcDcbeCYaoVTPfm892weTKiocy9lYLuV4MpQx9Ip17oxgLWL eba/5yLeWKLkKkGSxrci5gl9tME9ZhSGnVx8yiHeto5g/ndCYJJVEBjxPMe4z375X8VC AXfZjTFo6Uftnn0Uit7t739T6+7DldPN0oxYYWBApCimUHeqIH7klM4MObe2p0pjDbAY ZVQDEP5ZtyykovV3HhFTNEkuFbtavl3LNws9RG76leARMwjMfk5W+z1h7xSPwsT01QAO I/XQ== 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=9Ro5rHts4hw/ry0Xv8bJ+87ugrvD9L7WktYG6axhJ2w=; b=Nv6bsbTJQXhzHTC5FuOIuY81dZAWxure1Tyqau0K/E6GSXZvqRPBsq5cJQlJ4ZWU/Z BgM0EQNDTVGmfuJ7EybvA3/QUEAkvif2J+0JrKTyrIWgNX/InWn2bl94DhvecfYrTeji jPDE6L/L6T7cLUyZRVW0DIgIolcI04rajbiroS8RM5UIx5kZBl6UMCqpXRrq6gDFa24W 7Ui4WYjHPW+JgNCR5fTmxN5cYcTEWx9TCg8a0Hnrq7veQvdX3Tn6HlEnkZTjdFGBwKjq mW7PUjWBB/O90+VyVGRSU4bdUF1WiVAzUxxK8oOdR1ITR7mkBKPZIMsESb4Mzdvz6t9v KZpA== X-Gm-Message-State: AOAM533v3UEi8nMc0xy7Y3z4j8fdB1+eeeMRk0PmPwxshD1cZ5XJbcBT PEwlocQNG9vxSj9xiUwVxGIO8Rf7UtFoWg== X-Google-Smtp-Source: ABdhPJz/SxTRdAznFSSYmtmiGA5ADv4DAVkhlsP0z1zhGvc7s+Mxhx/OeHZn2kjBo2uKTJ6cOUKADw== X-Received: by 2002:a7b:c005:: with SMTP id c5mr15839744wmb.113.1623666848689; Mon, 14 Jun 2021 03:34:08 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34: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 v2 28/30] receive-pack: convert receive hooks to hook.h Date: Mon, 14 Jun 2021 12:33:17 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 b2ccdb66daa..ec90e10477a 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 Mon Jun 14 10:33:18 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: 12318543 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 AFECFC4743C for ; Mon, 14 Jun 2021 10:38:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9DDA461245 for ; Mon, 14 Jun 2021 10:38:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233718AbhFNKk1 (ORCPT ); Mon, 14 Jun 2021 06:40:27 -0400 Received: from mail-wr1-f44.google.com ([209.85.221.44]:41896 "EHLO mail-wr1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233535AbhFNKhN (ORCPT ); Mon, 14 Jun 2021 06:37:13 -0400 Received: by mail-wr1-f44.google.com with SMTP id o3so13978532wri.8 for ; Mon, 14 Jun 2021 03:35: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=8beOkfWpcE16od71qmf64VjLMSAc1uewpLPaCHbK6XM=; b=KY/wSR9BKAlOUSEm1byKg2rttqjUYJBANASizLBnt0a7FHpF+Azn2Z285nyzB+Au2h /EmyoY1sFxsWq5wHKcvGmFWiyRGA+4vIPS+m10/45HQvXQYV8vmQ1KBp+PXlT55FdyMg 3nqD2NozKFEv/h2yoMxvXBTteMMYSh0euyQuE/bXAGP/PRhR6pawHeXgpBqBFrdHmQ62 GKwv3yJ0x20Jvbox5IqfxGx+1IiejkrsjPgdFQuw1+aKVLh1wS1JOF9lY2Q/ONJwIyTm HhD4RYNySjGGG2f52kAx6bTBoqmVoilSUTyj+l4KBlf00BOqFk4uXMjY1hGtwNobi/iY kVHA== 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=8beOkfWpcE16od71qmf64VjLMSAc1uewpLPaCHbK6XM=; b=n4qla0k91HDF/oH7XuMIPQEVXYXnvDbPAJDlTxIxciJ1LCgEKLKKUZRTT4/r9IRRyf wgEFRxoNphmpkXLjfT5jKUwu6Qr8KhvseYK3rKv1mMV/oJDj5jP7yK5TVDWc54kgdmzD 0YJ/D8b5SJhVuCaorbsVemM7V+AhheO/fmH59hJM3ZoQHZ6JfyoZ6ulr6PdCJlp3wUnt 2f/wVSRIRzSQXsXuaqb7pHQxD8nIBvroCHR/ZZMeAimvcuLTu5CZUABItuRFBKdf5k2I yPsaWGsSA+5c7ZMqiDm3eRV3Hq2GjEpsX3fgYD4y3L1Y1e4DmiJMPypopYXq4ruWhjKC 3pgw== X-Gm-Message-State: AOAM532e38QTa7yZmda/qG2HEUMcLYhyRalHRUK2mYkzQZd8qN7Z1TtS kozhBM09X7dBYSTbMO/w2HXy5yLiynsm5g== X-Google-Smtp-Source: ABdhPJyEntn+F+kD6IlojsTEphN9xo/S/SHb9xFGhaUExG2YX7jTSOh/zIW9bOp1t6pGrYBOztc0Pg== X-Received: by 2002:a05:6000:151:: with SMTP id r17mr17631386wrx.151.1623666849647; Mon, 14 Jun 2021 03:34:09 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34: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 v2 29/30] hooks: fix a TOCTOU in "did we run a hook?" heuristic Date: Mon, 14 Jun 2021 12:33:18 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 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 dad4e565443..a66727a612a 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 6128b60942f..0425c9bf2b5 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 ec90e10477a..cd658f41d58 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 e8147a88fc6..cf62ebceae5 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 df42eb434f3..b5a542993c6 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 17ae65eca31..3cf51460279 100644 --- a/hook.c +++ b/hook.c @@ -138,6 +138,9 @@ static int notify_hook_finished(int result, /* |= rc in cb */ hook_cb->rc |= result; + if (hook_cb->invoked_hook) + *hook_cb->invoked_hook = 1; + return 1; } @@ -152,6 +155,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 5f895032341..9d163e6f992 100644 --- a/hook.h +++ b/hook.h @@ -57,6 +57,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 { \ @@ -81,6 +90,7 @@ struct hook_cb_data { const char *hook_name; struct hook *run_me; struct run_hooks_opt *options; + int *invoked_hook; }; /* diff --git a/sequencer.c b/sequencer.c index ec2761e47d9..2440b9dccd8 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); From patchwork Mon Jun 14 10:33:19 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: 12318559 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.7 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,URIBL_BLOCKED,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 7D83EC49EA3 for ; Mon, 14 Jun 2021 10:39:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 761876141E for ; Mon, 14 Jun 2021 10:39:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233428AbhFNKlE (ORCPT ); Mon, 14 Jun 2021 06:41:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233836AbhFNKiZ (ORCPT ); Mon, 14 Jun 2021 06:38:25 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 577A5C0611C0 for ; Mon, 14 Jun 2021 03:34:12 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id n7so7814210wri.3 for ; Mon, 14 Jun 2021 03:34: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=50F9Y5seLSwaPstRpyvEtgeCricW1QUgDtiwclRPDjI=; b=HgIuMddttzyYaAlqRWlTb7b3tt7ImPj/eqYaCoqaRCMfLQGdWuU5Mm3Bal2UlDbGPq /9xSrMIA3yMYXfk9nhrPKaH7yIwe6dC4UVhNRtLYah3DbFAU0BmwGfQJfxdNgWe/3MdU TxqBThO1kC3Luftqn5oB6L368NifUVRzRtRXWG2OUEc4wWjSD8a4mhw8ApE/GfuegtFE ME0BDK0hx1WkbL4GyMVt2C4aK3UpEivg7JAu1ge0cV8ROVDDtLxgbwQrmLVlSBIQKLGn YbeTJVZ0gspt/KTcBmwMbVioW4KTPcN7IJFd9ibOBMNMaTOVopIm04ZrOWZxgaWlAF7u r30g== 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=50F9Y5seLSwaPstRpyvEtgeCricW1QUgDtiwclRPDjI=; b=Bp8Q7cKowWhCbYH27GffYrJZeeZ/Yxjla9gx8mvfusoHBtaEWIR/1xhMUfNiyt+SuC MSROejTb4IkBMJm7+k6DzSKuPozAo4F8EED+oDgRCPlAIrb+E4KhSi/W7nv7QqCyAUD3 HE5iKJeqCnRz6ly+XtJ4MLCVq5b/HOtJLkRbpEXMsMZkfKLBPvJTp6HkYrpNF3N5yuuT dAUh227SWR37x3aMZAOp+ztStQNDDB8X1yyk9I1OReIv7pNn6x7Z81ll7E/ZNxNE9phi V6iX2WbJSRyzEv+7LPGKjAskrM11yi4RfYP1hOsooDtPkLHrBRCpiURipO5VIVxxPt93 o8zg== X-Gm-Message-State: AOAM532yLFtt6z9Bs7vo8G7id4BsS9wJgXWsZbY1we4WLyeL3OAdaOgu olA+eGdA5HtcwfMN3j/AT0YDZbc2Q2uPWA== X-Google-Smtp-Source: ABdhPJwg9tlHlk2kjj7HEPzlmauEc8SBEj9+h42pwosqzINPxmpeRMlh6195ZlceP9sozmF844iUqg== X-Received: by 2002:a5d:694b:: with SMTP id r11mr18168238wrw.168.1623666850691; Mon, 14 Jun 2021 03:34:10 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id w18sm15911843wrt.55.2021.06.14.03.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:34: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 v2 30/30] hook-list.h: add a generated list of hooks, like config-list.h Date: Mon, 14 Jun 2021 12:33:19 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc3.434.gd8aed1f08a7 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Resolve a long-standing TODO item in bugreport.c of there being no centralized listing of hooks, this fixes a bug with the bugreport listing only knowing about 1/4 of the p4 hooks. It didn't know about the "reference-transaction" hook either. We can now make sure this is kept up-to-date, as the hook.c library will die if asked to find a hook we don't know about yet. The only (undocumented) exception is the artificial "test-hook" used in our own test suite. Move some of the tests away from the "does-not-exist" pseudo-hook, and test for the new behavior. Signed-off-by: Ævar Arnfjörð Bjarmason --- .gitignore | 1 + Makefile | 14 +++++++++++--- builtin/bugreport.c | 44 ++++++++------------------------------------ generate-hooklist.sh | 24 ++++++++++++++++++++++++ hook.c | 22 ++++++++++++++++++++++ t/t1800-hook.sh | 14 +++++++++++--- 6 files changed, 77 insertions(+), 42 deletions(-) create mode 100755 generate-hooklist.sh diff --git a/.gitignore b/.gitignore index de39dc9961b..66189ca3cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -191,6 +191,7 @@ /gitweb/static/gitweb.min.* /config-list.h /command-list.h +/hook-list.h *.tar.gz *.dsc *.deb diff --git a/Makefile b/Makefile index a6b71a0fbed..d0532f3c744 100644 --- a/Makefile +++ b/Makefile @@ -817,6 +817,7 @@ XDIFF_LIB = xdiff/lib.a GENERATED_H += command-list.h GENERATED_H += config-list.h +GENERATED_H += hook-list.h LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \ $(FIND) . \ @@ -2207,7 +2208,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS) help.sp help.s help.o: command-list.h -builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX +hook.sp hook.s hook.o: hook-list.h + +builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \ '-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \ '-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \ @@ -2240,6 +2243,11 @@ command-list.h: $(wildcard Documentation/git*.txt) $(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \ command-list.txt >$@+ && mv $@+ $@ +hook-list.h: generate-hooklist.sh +hook-list.h: Documentation/githooks.txt + $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh \ + >$@+ && mv $@+ $@ + SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\ $(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\ $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\ @@ -2890,7 +2898,7 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE .PHONY: sparse $(SP_OBJ) sparse: $(SP_OBJ) -EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/% +EXCEPT_HDRS := command-list.h config-list.h hook-list.h unicode-width.h compat/% xdiff/% ifndef GCRYPT_SHA256 EXCEPT_HDRS += sha256/gcrypt.h endif @@ -2912,7 +2920,7 @@ hdr-check: $(HCO) style: git clang-format --style file --diff --extensions c,h -check: config-list.h command-list.h +check: config-list.h command-list.h hook-list.h @if sparse; \ then \ echo >&2 "Use 'make sparse' instead"; \ diff --git a/builtin/bugreport.c b/builtin/bugreport.c index 941c8d5e270..a7a1fcb8a7a 100644 --- a/builtin/bugreport.c +++ b/builtin/bugreport.c @@ -4,6 +4,7 @@ #include "help.h" #include "compat/compiler.h" #include "hook.h" +#include "hook-list.h" static void get_system_info(struct strbuf *sys_info) @@ -41,39 +42,7 @@ static void get_system_info(struct strbuf *sys_info) static void get_populated_hooks(struct strbuf *hook_info, int nongit) { - /* - * NEEDSWORK: Doesn't look like there is a list of all possible hooks; - * so below is a transcription of `git help hooks`. Later, this should - * be replaced with some programmatically generated list (generated from - * doc or else taken from some library which tells us about all the - * hooks) - */ - static const char *hook[] = { - "applypatch-msg", - "pre-applypatch", - "post-applypatch", - "pre-commit", - "pre-merge-commit", - "prepare-commit-msg", - "commit-msg", - "post-commit", - "pre-rebase", - "post-checkout", - "post-merge", - "pre-push", - "pre-receive", - "update", - "post-receive", - "post-update", - "push-to-checkout", - "pre-auto-gc", - "post-rewrite", - "sendemail-validate", - "fsmonitor-watchman", - "p4-pre-submit", - "post-index-change", - }; - int i; + const char **p; if (nongit) { strbuf_addstr(hook_info, @@ -81,9 +50,12 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit) return; } - for (i = 0; i < ARRAY_SIZE(hook); i++) - if (hook_exists(hook[i])) - strbuf_addf(hook_info, "%s\n", hook[i]); + for (p = hook_name_list; *p; p++) { + const char *hook = *p; + + if (hook_exists(hook)) + strbuf_addf(hook_info, "%s\n", hook); + } } static const char * const bugreport_usage[] = { diff --git a/generate-hooklist.sh b/generate-hooklist.sh new file mode 100755 index 00000000000..5a3f7f849c8 --- /dev/null +++ b/generate-hooklist.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +echo "/* Automatically generated by generate-hooklist.sh */" + +print_hook_list () { + cat < $l[$. - 1] if /^~~~+$/s; + END { + print qq[\t"$_",\n] for sort @h; + } + ' stderr.expect <<-\EOF && - error: cannot find a hook named does-not-exist + error: cannot find a hook named test-hook EOF - test_expect_code 1 git hook run does-not-exist 2>stderr.actual && + test_expect_code 1 git hook run test-hook 2>stderr.actual && test_cmp stderr.expect stderr.actual ' test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' ' - git hook run --ignore-missing does-not-exist 2>stderr.actual && + git hook run --ignore-missing test-hook 2>stderr.actual && test_must_be_empty stderr.actual ' +test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' ' + cat >stderr.expect <<-\EOF && + fatal: the hook '"'"'unknown-hook'"'"' is not known to git, should be in hook-list.h via githooks(5) + EOF + test_expect_code 128 git hook run unknown-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