From patchwork Fri May 28 12:11: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: 12286857 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,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 29BE4C4708D for ; Fri, 28 May 2021 12:11:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EFCB7611BD for ; Fri, 28 May 2021 12:11:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236050AbhE1MNV (ORCPT ); Fri, 28 May 2021 08:13:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230256AbhE1MNT (ORCPT ); Fri, 28 May 2021 08:13:19 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4326FC061574 for ; Fri, 28 May 2021 05:11:43 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id r10so3045910wrj.11 for ; Fri, 28 May 2021 05:11:43 -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=y76LIDEogVgjWLyqVhKSZu/O8Gex/gwCHP7JWyhmFoY=; b=HEC6AOKB4Oa8njgeeXhIu6Q8SHnjdA1YDNs56dyr92YqORmpWNOMH3ZN9we7u3qq0l L+ke7uk1JKHuyrJhAypQVQ19kbmUgBnUpxjIBk7DpjCG2pcJPCsbY0XzvidnV6M3MPZS 0xddC1GXgP+LDwHkZ8gWZ0ykroVgn96er9yFBGQl1zCvS2eqycrQC34MjGEfJ/HFCPdQ GbJNfO+aIEmZIClMmzjsb/yz0Dr4xP+F6v+ulvv+Hubj0AiAMjfGPFx8XXO1LkAS5EG2 fi+ReBaKZQVizXThycSB79GNdvYJ3mQDB/fgaQTMZQ6lSUoQdeNiP74b1GXgGNE2wC3D 3zkA== 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=y76LIDEogVgjWLyqVhKSZu/O8Gex/gwCHP7JWyhmFoY=; b=FQV6Xnu7IRLTsze7tqb47YipQudTEhmbyQWKnUK6RrDCkk9WDCHLRjCDXr4jx71JWf pDqzpnChEqPu48hN+cmllQXfiCq9vaMX0taFHD6LC11RhCMOzOF3h2y4D9IfbVKwULh2 iGs0yRkVRo7Mnk/gTWd8hm4UQE8FptS4DSq2NphiyYzUpCQoUbyUirbESbXpA2ltLnkj DWxJAdzfgTtRbLwdM7iDbwcYY7cbNkPj0CGPByOnRojDArMPdgktIrhYE52jnH+EscDU u7hdheYJKXD7Cn6SdCwXPEDVtQrc5HIAozFbQ8kGrIaptwm9kTc82g0ULL0NETCnsLd3 2OWg== X-Gm-Message-State: AOAM530zRIQRJKbXgkmDuCwSJExxgMqx0wC+BDWvesDdp7pLKM7VtmGZ cXjZQFlcZY5zDBZbsAEUMWGwucU+QLNbOg== X-Google-Smtp-Source: ABdhPJytOP9dscC2d6EFSDR6opss4T0qQIjM73EL4lN9fFl7yDZeRViA2D6iNEdZF/h505oB82Fb7Q== X-Received: by 2002:a5d:508f:: with SMTP id a15mr8630178wrt.242.1622203901590; Fri, 28 May 2021 05:11:41 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11:41 -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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 01/31] hooks tests: don't leave "actual" nonexisting on failure Date: Fri, 28 May 2021 14:11:03 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Start by creating an "actual" file in a core.hooksPath test that has the hook echoing to the "actual" file. We later test_cmp that file to see what hooks were run. Having that list of files be empty if we fail instead of throwing an error about it being nonexistent makes for friendlier output, we'll see what hooks we should have run. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1350-config-hooks-path.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/t1350-config-hooks-path.sh b/t/t1350-config-hooks-path.sh index f1f9aee9f5d..fa9647a7c0b 100755 --- a/t/t1350-config-hooks-path.sh +++ b/t/t1350-config-hooks-path.sh @@ -5,6 +5,7 @@ test_description='Test the core.hooksPath configuration variable' . ./test-lib.sh 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 From patchwork Fri May 28 12:11: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: 12286859 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,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 64149C47087 for ; Fri, 28 May 2021 12:11:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 43AD1611C9 for ; Fri, 28 May 2021 12:11:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236297AbhE1MNW (ORCPT ); Fri, 28 May 2021 08:13:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235255AbhE1MNU (ORCPT ); Fri, 28 May 2021 08:13:20 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30CE8C06174A for ; Fri, 28 May 2021 05:11:44 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id n4so3102883wrw.3 for ; Fri, 28 May 2021 05:11:44 -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=MlicDSbKY3+IsStW6iHd8sEA8jZjwjf/HhdAOmz7pVM=; b=Sz+DuW7e0xK3AcCcl2/Ziq0JUQ6IJCtR4TWwYVb4UWCoNo9qldxuJ4JVSV5bwu0yne ro3sC+75AvM1J0P8Ve9I3tKNvFpm3m+Rw7xCdNmJ5/yDFaSeGzRhrEuZ5NidRhkLJ7Yv cI3f6d3QpBzMjFrPiX4ApOFL24UjZ7c1pb1HJQZa+AThIPvbDXjrn7XafGJL/OdY9hI7 atG1C0A4lK8G1X8DNpXACADfio2mrg9awLxHKhwNuXtSfHmM0iExIu14u2fu4UVRoZIv fofTilb5x3Uu8DI8eq14TS+VqOvJ84Ql60rKE1svdgDe+D4Q7nH6IrP85DFZ1hAZNGJ3 b4XQ== 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=MlicDSbKY3+IsStW6iHd8sEA8jZjwjf/HhdAOmz7pVM=; b=JYoy7Y16x64YnE80Eny2KK2lLJGDYf3e33d2HxboBRMw1g2Iad/JY1Qb+9HyXbkUCF 3R2I9LuXzqNhErEhQ+26APIN59D12wR6gdZJpzDsU0RAkuKvvP/EEGcK0Z6WxZu/2iHR oGdCE/61C2Atdtza/DdmH0FZlRIG3/XLb6si9mefeQ0nYX39UJNJsPMg441WLv/AB3Ot +ArUINHQ9eywM/vd/6t8AJ+TYB9WGk2g5w3JtplBBvXWHZ6y1FnviFrLaJLBobS6b87N JDfHS4kBVBvcRogCbYW+ZZ6R2huNK1y0jxPMTNfvr9HA+LqZj9ta7+FQLe49hHWHAkSA eGUg== X-Gm-Message-State: AOAM533c0atqhnSkrqWqr7dyVJkqoTGD7kEaTbihg/06/on9rbJTBodn 5ETRICbncIKCO1ILvfVvWCLAe7E9DcSLcw== X-Google-Smtp-Source: ABdhPJzUDf5ORsojsK31YQP5FfygWzu8wVQFTm4xZvQ3kuOCdCBmr4r895agmwQGVJiNnSVzrrvvsQ== X-Received: by 2002:adf:d081:: with SMTP id y1mr8258756wrh.179.1622203902492; Fri, 28 May 2021 05:11:42 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11:42 -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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 02/31] gc tests: add a test for the "pre-auto-gc" hook Date: Fri, 28 May 2021 14:11:04 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a missing test for the behavior of the pre-auto-gc hook added in 0b85d92661e (Documentation/hooks: add pre-auto-gc hook, 2008-04-02). Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t6500-gc.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh index 60d961b5260..10c7ae7f09c 100755 --- a/t/t6500-gc.sh +++ b/t/t6500-gc.sh @@ -95,6 +95,52 @@ test_expect_success 'gc --keep-largest-pack' ' ) ' +test_expect_success 'pre-auto-gc hook can stop auto gc' ' + cat >err.expect <<-\EOF && + no gc for you + EOF + + git init pre-auto-gc-hook && + ( + cd pre-auto-gc-hook && + write_script ".git/hooks/pre-auto-gc" <<-\EOF && + echo >&2 no gc for you && + exit 1 + EOF + + git config gc.auto 3 && + git config gc.autoDetach false && + + # We need to create two object whose sha1s start with 17 + # since this is what git gc counts. As it happens, these + # two blobs will do so. + test_commit "$(test_oid obj1)" && + test_commit "$(test_oid obj2)" && + + git gc --auto >../out.actual 2>../err.actual + ) && + test_must_be_empty out.actual && + test_cmp err.expect err.actual && + + cat >err.expect <<-\EOF && + will gc for you + Auto packing the repository for optimum performance. + See "git help gc" for manual housekeeping. + EOF + + ( + cd pre-auto-gc-hook && + write_script ".git/hooks/pre-auto-gc" <<-\EOF && + echo >&2 will gc for you && + exit 0 + EOF + git gc --auto >../out.actual 2>../err.actual + ) && + + test_must_be_empty out.actual && + test_cmp err.expect err.actual +' + test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' ' test_config gc.auto 3 && test_config gc.autodetach false && From patchwork Fri May 28 12:11: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: 12286861 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,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 98176C4708D for ; Fri, 28 May 2021 12:11:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6F8D2611BD for ; Fri, 28 May 2021 12:11:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236309AbhE1MNY (ORCPT ); Fri, 28 May 2021 08:13:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236171AbhE1MNV (ORCPT ); Fri, 28 May 2021 08:13:21 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8495DC061760 for ; Fri, 28 May 2021 05:11:45 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id y184-20020a1ce1c10000b02901769b409001so2264050wmg.3 for ; Fri, 28 May 2021 05:11:45 -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=xAfRQmovfhGyIEaQgLXCiATnBkYFhViC1WnSV7AWrQ0=; b=QHYcuc144FK90Kp5OKqEG+8632stdxRyvBqDwhhgAKXhxIp3SZVvavSpUp0Qtg4s8E WVkbf3NxB5kc2WXaIV5Ze13Ic/8Sox05UQxcZekjymivrpQoTwFefm0fC146I4YLJA+3 eP6Pe2sywHn0kwwhP3DvylKUQBWQ2qp6dXQ/2GgQbgjdHOjcKurt+wAmCE7hz9sztAS9 4psWmpqSs75vscqWwYepMmC31j9TZQRbuZ8+my+Roa/9ry4QyKdV89l+JaPLEtSLEH2g Ux8asDp32Y9LifXKg6Lx8eeVfO0ELW0RuYxtGgGq0nPQxduMJ0w4WaZ8obQW+fvStfQn NmHw== 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=xAfRQmovfhGyIEaQgLXCiATnBkYFhViC1WnSV7AWrQ0=; b=DckDQr1SUWBhU5ZaPzdA8bgtN/0R5vmsiZlpYbkkvqz3GB5yxhiX+LFk3E9EQ4VoxV tqs3rOWDdCwX/8WL5aZj/bE4NEkymsLoz32KQJdFkyRNRrGNl/u1IQHrpYFvwQRQN4qw gIunF5WP4sgwi5v3kdWvUpks7wegnGQbi+KCseBpBueVz3tyumXS//HED3YmFZvQzzUO xkABNtcMKBhjnWrQYGJV9mvl53+ek9Sv/lyRvIS3J8e6Gs17+1uNvxVvEW8eZY3PavyG nIRgZ8QfLXyunMNhInVdyJ+90uwAHq3Xobzy6pv2+KuGFsXoYLeOeYFYzA9T2lix6HK2 NWJg== X-Gm-Message-State: AOAM533JjJGN97es2ZzkUa6XfZ8w7MZ44KMJaOLIM+eXPIqEwSh5lInk EbZAkXr8eGi1vPMyGJUkBSR6c0W+liffAA== X-Google-Smtp-Source: ABdhPJwaB4ZvYCL7qRl4sg5MJ0Yp3MosXFcO/HF9gRuuMwZPtJcbH8XHrRFW0H8W3iZzL37aDZQGpA== X-Received: by 2002:a1c:2cc3:: with SMTP id s186mr13505008wms.150.1622203903399; Fri, 28 May 2021 05:11:43 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11:42 -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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 03/31] hook: add 'run' subcommand Date: Fri, 28 May 2021 14:11:05 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 | 135 +++++++++++++++++++++++++++++++++++++ 11 files changed, 414 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..7cacd27c748 --- /dev/null +++ b/t/t1800-hook.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +test_description='git-hook command' + +. ./test-lib.sh + +test_expect_success 'setup .git/hooks' ' + mkdir .git/hooks +' + +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 Fri May 28 12:11: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: 12286863 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,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 07C24C4708C for ; Fri, 28 May 2021 12:11:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D9C99611BD for ; Fri, 28 May 2021 12:11:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236328AbhE1MN0 (ORCPT ); Fri, 28 May 2021 08:13:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230256AbhE1MNV (ORCPT ); Fri, 28 May 2021 08:13:21 -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 0C611C061574 for ; Fri, 28 May 2021 05:11:46 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id j14so3086225wrq.5 for ; Fri, 28 May 2021 05:11:45 -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=ZG0nX+Dz+mc27CdfokQHJOZDnyQU+6jsvdsYO11XhGo=; b=RoWSAM42MrxyjSXuHGEVkxRAr++FRGxmvi2fF+s1rW2k6iivc5XVdzoliO7p2wPHPv j5ordgZGb975ON8KntqBtiUExDpucBRGeDBcX6ag4W/yYPT0rgVpkDeEZm+JdkERwUWx aS0v/lYODcE2fsHungakCt2whDK4YnTZ/t08hcdmgUkrbIHDJ0vUBBoA4lnvzDIIt0Ix AEwivGAC7smLk7ohrwBCXa5mayJpY/9G/cYPZGwqihduUiQo+Relt/XV7aOaTT8+w2z1 3xyr3UBpD22PM4X7dLkPUHIAaRYfDv+wcfmkJ/9lMd+rklRPd2N8sCbfNUaeuGKcRj3l pyPw== 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=ZG0nX+Dz+mc27CdfokQHJOZDnyQU+6jsvdsYO11XhGo=; b=BPe0zQeQtFjDWtJUNit40pMiKxRUZSQla0Y4g4rUQTH+8gEnSWjvke8XwhEmtjl3kq 5eAh6C7S/HIiEAeHlttPTPDw3Hh4YisoLUhn6m46Q9Tkau0Weq7bUMvtpHRSdVd/UKDu My/oV7wUwNm6jFzIvDjYdkmCjwhs46Eo8d/g9UMbsOKZvpQ7rjyvO5kNHJWli8L5omu8 i8Ve6NEJVxH1mqlsdWMK84AMZ7abPm15XjTClJjmpsBkkHSbSmT554tXzC+XSLMvOSrF WamknyE9T8ZpBOmNspFcd6OM1IHBzBEtbjwz71TezrhRv9ZcadsFS03Qmx2eghuGu8WB z0Yw== X-Gm-Message-State: AOAM533eOHG29GKiRH1jX97tPSh4CS2sEP1+LkcGo5XklnmkBf3BC7At sQ9mL6vVvi5Y77rZqWVhLFp++IxaA0j1Qg== X-Google-Smtp-Source: ABdhPJwjrcZ9nz7UkQPQMd2Bc6hZkqfPEU5bfC1Tn8MN5gT9QO1MNXcjzC2y8sEBO5AaIqiY+OsN2A== X-Received: by 2002:a5d:6109:: with SMTP id v9mr8567383wrt.0.1622203904147; Fri, 28 May 2021 05:11:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11:43 -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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 04/31] run-command.h: move find_hook() to hook.h Date: Fri, 28 May 2021 14:11:06 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 eddb8ae70d6..5a10f6e3c96 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 6cf3da19ebd..e6c46adf60c 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 Fri May 28 12:11: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: 12286865 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,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 01B8AC4708E for ; Fri, 28 May 2021 12:11:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D3858611BD for ; Fri, 28 May 2021 12:11:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236367AbhE1MN2 (ORCPT ); Fri, 28 May 2021 08:13:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236290AbhE1MNV (ORCPT ); Fri, 28 May 2021 08:13:21 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADDBCC06174A for ; Fri, 28 May 2021 05:11:46 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id x7so3047525wrt.12 for ; Fri, 28 May 2021 05:11: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=Qh1W8CHM+5quh8R+f13/jE7JfhopytkR4b/WfPwdz7g=; b=odVmvP/64er/FdFtlZDDB91St65WFU8pPdorAWrVGLbq2IxuuD16MM6rsksVH+Va5i Ex9rW5FdBM3KPt5J++eFvNygBZS4QNW24TevuJErDq2ROFszTlCuJfIcMRC8oLDVou3x cGZt+MlLuoD5tHjWum1hznBmXG8rwy5nnERwXu6jzoD6H8SkTWIcQTjX1L4EUWl9GRoc vUlKmv/fL+bYJMl3nExu/PwlRxV2W9G1xk19UkTlgoz5EsZkHiyg5/i3X43+eQ13CGoq MtuitHnrx+TUgvC1VZoqW5FkuIjHdRbgOnvGmuX0gLW4gSY5PnLBK9bzcNPVeT908fyk 355Q== 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=Qh1W8CHM+5quh8R+f13/jE7JfhopytkR4b/WfPwdz7g=; b=SHXdVyXcSwVVPx+nwQRpTqOUFH0hY4eQ2+Mu+vdylGDfk6adn7KnxnXfdHmR9CDTw6 PdKbvIbQ3AeV+rHwhMfBKkNY30MRlFYlEclBBekhP6LK1CpxWSba/8KIUmLabzJP8uS2 eCEtGQnr4NbRHPl3LI4GQO08QmI/ON1jItZyX19h5GbvXACCHfaVbUXvNvtalExWG+tg n2qU+1hXfQOod426znkEirZAQ6EFBK5YkKVbuoxQHENIiliKiIGylPBD45l/Kx2/zSGs 8ibxPa3eIU5CuSzDjXFz3GPwyLHzmxjQH7l558l7sjk0U4s9qnErwv5FOSiCejiBxkVp 98eA== X-Gm-Message-State: AOAM532cFdcNLR2RzSCzluX1+9pJ6AV19OPixvsRKYfiaD5HouN8Zr/J 2sR7zaU17ORrbRGQT/zSoyf9frA7xGXLlQ== X-Google-Smtp-Source: ABdhPJyEkWHn1F94NIRZFKpvMJ/FOwFqUCXmLX6E2kQZ7o+5zUutq2xT9lLjqButNCTCJ0ngfmU3KQ== X-Received: by 2002:a5d:438c:: with SMTP id i12mr8171587wrq.44.1622203905046; Fri, 28 May 2021 05:11:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 05/31] hook.c: add a hook_exists() wrapper and use it in bugreport.c Date: Fri, 28 May 2021 14:11:07 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286867 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,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 2097CC47087 for ; Fri, 28 May 2021 12:11:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 06673611C9 for ; Fri, 28 May 2021 12:11:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236437AbhE1MNa (ORCPT ); Fri, 28 May 2021 08:13:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236302AbhE1MNX (ORCPT ); Fri, 28 May 2021 08:13:23 -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 8BFCBC061760 for ; Fri, 28 May 2021 05:11:47 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id r10so3046106wrj.11 for ; Fri, 28 May 2021 05:11: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=fMxzbAjD2oqFrcGDtoYjkHgDGiS+DZ0Z8CPr+qlbeUA=; b=U/irqKXN/UVDRgcdZscjQjo2kBey53NsOhYF7uXzufyYmvct1NpPD78otDpH9SdZN1 Buh4oAniJr3jTh71ZLOVxuOxJvdQ0qDBgq7lK1aB4o3yJ1kJEzRrVzt6yEs2p6Lug8in vGXUN79GA2MnvKREiDoiO0Mgjg6lWYW7fzij4V8CfO5YRrVLasd91M333l4sImEt59tz CYi1ri78+EoaCTD19arLd56tI3tFZ6g5t6QRDSeclEXFgo7bhTx72TgFC9S7VKnNJBAu wcguFpe7xMtTz2bFfpYCjzW8NNdcB5A3n9HcqEQcR/ll7MmTeY7oEY8K6NygAjoGWzjo zuIg== 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=fMxzbAjD2oqFrcGDtoYjkHgDGiS+DZ0Z8CPr+qlbeUA=; b=rUal3sXpF8XxDLhUywMdoBpjULtoUHOpvAIPT2az77sYTdU/QILwtpoZVwchpYPxaX 8p4eCLCyihjl4m0OjGJGIktkvPh22ShOq9/mnHd9LU0CywMbloTLjEqUdkjVkis71Mtf Ia3RRXblNqc1kPNRtiCBbCg8rhXvAZ2kN80Fmw+TNqRWkl3r0cim0dnT76jbgz6dZ74J 4VDhKOu4OuJxkL976FO1XBXO/sRSc92N49EgzHsiZkpo9ySlHpJfpp5etWvpIW55LSlD Uef8x4IiQxZ1FamFLnR62S5s0OZ7qQX0ysxUCSCoierTYzB92lf+ztGW36b0My7UlUj1 TRcQ== X-Gm-Message-State: AOAM530dMyu8LfmBkl8elnB4uvnBipa/2DU3eJNpyWTf92/SJP7VQ3b3 0ROTS+A/N4HDvPFV3r9kmwPskKmHaiZIYQ== X-Google-Smtp-Source: ABdhPJx5+A6zhtg+QtXNqtKojNJQKKj+0UH2FQ9kHcjI/V0bQDmkSNWGrmYwt2C7WckhALQMIT7EXg== X-Received: by 2002:a5d:66cc:: with SMTP id k12mr2805026wrw.185.1622203905937; Fri, 28 May 2021 05:11:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 06/31] gc: use hook library for pre-auto-gc hook Date: Fri, 28 May 2021 14:11:08 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286881 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,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 D8B58C47087 for ; Fri, 28 May 2021 12:12:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B2F79601FD for ; Fri, 28 May 2021 12:12:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236527AbhE1MNi (ORCPT ); Fri, 28 May 2021 08:13:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236305AbhE1MNY (ORCPT ); Fri, 28 May 2021 08:13:24 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74DDDC061574 for ; Fri, 28 May 2021 05:11:48 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id o127so1990020wmo.4 for ; Fri, 28 May 2021 05:11: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=pFyNt4mCuC/3gswFUmq2Sq7Y3wIuCybTNd41AG0A2DM=; b=kaZIOT4uHifskW4yVxe7BUpPph87FxKw/2fFwTWDQbVn5MFyzWKxyLzhllp/iccuaF 4+ASIs+gjJomRShSHxaO3TUbOFMc3BnGS+kF8jDJHp6iQURfgkEAq5V0gVmhFR4Br49f gcc02dcxVt3R/qzKLu7xeTWkSp9QYykw5e6fxpdB4x7P56hpTQ0iPZ2Jun/hs7XEpMta i1+j1EfVLzbjRV7oZ1X0Um9qTkjXgTALFf7QlxegLm4u9OhYRjeNIGKag9jL6DS2h+cO KdzGDOXHFAY5kA2e/37VASgGy20mYrrh28AMRBVM6NQ4UNd1Gxh7VCEQd+NodAfL6Eu3 9VSg== 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=pFyNt4mCuC/3gswFUmq2Sq7Y3wIuCybTNd41AG0A2DM=; b=Vjw1B2HzhtbEt9CtBosYtw008K+Am+N80KYgskTE2wHh7oTqt/qm2BFItuzcCOMhkf igiM2XuGuA1uw95c4PNYa+0A/6itJ+E+niuZve5LxtluZGSE71bXL57LaNxQmEh6tHLe XOXVpx4h6zpPe/cE3jAfZCMBEDFWbrlep6N4p0ZVBJu7nHUVSxze9NKllwGAmfEjjf/L OdVzGAfTdkjlF0ZHkKzMTA0QBAVvueq5ab6kb3vsRn9jGZWGnWNIs7pe3ArEdaTuLf2M H6JmwfUOJ36DuN4zGSTLHy9L4/Kv4oq+5E08d0B9pHLNUwzZzO1V8ZvUXk5NthRtrTrN Z8Og== X-Gm-Message-State: AOAM5303ttjqYf/w9TQHOEfr9u1ReJi4jHYd+Z0s1Gd5cGCiai0iElOw nQvoTgIEgdeetg2HWv5P8PhReKT2xL76Yw== X-Google-Smtp-Source: ABdhPJwoaGTrwRvhuV3Kis9y0rX0AuOixq2nZ9CQM5RhRNPOUY9ggkKbYkWJ9WQXQPAai8BNgaVoEw== X-Received: by 2002:a7b:ca58:: with SMTP id m24mr13235396wml.151.1622203906769; Fri, 28 May 2021 05:11:46 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 07/31] rebase: teach pre-rebase to use hook.h Date: Fri, 28 May 2021 14:11:09 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286879 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=-12.9 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,UNWANTED_LANGUAGE_BODY, 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 6DA59C4708D for ; Fri, 28 May 2021 12:12:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 51A7C601FD for ; Fri, 28 May 2021 12:12:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236481AbhE1MNg (ORCPT ); Fri, 28 May 2021 08:13:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236304AbhE1MNY (ORCPT ); Fri, 28 May 2021 08:13:24 -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 6A039C06174A for ; Fri, 28 May 2021 05:11:49 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id h3so1989927wmq.3 for ; Fri, 28 May 2021 05:11: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=W/yKjUWk//XPaqhS8tyea275td1NnG52b4khH87gT0I=; b=ANN1NXpw9aupDbAHLJ/vApdZmaRDsQMnziHHTpUtkU3MrJ0HHscKR8coSzNIAh8Q/H SODR7S2jqskk7txnXpthbcpQW/GYUjh9wB2QrjoKkxck4kWQfAtakC0z0AYbLkKJdGcI 5Seqboo+wBfEIGhpBn9xfdmeBah0kJZKtfWbIbfGHbYxGdNAfWitpadlXMN9QQaGKA6z qZ5q3awTP1IF7NKrty4naJBDcHwQoIJ/8s1AYvwYnBSY7VVITcWgSS2ixb25z1ai5W0N sMhqw9VD33ibmgnlhzZBKH/ZeORto3wpiH/x7KlCEG5J+D/c/WuwwvmGURxMsLEHSg8r 2qqg== 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=W/yKjUWk//XPaqhS8tyea275td1NnG52b4khH87gT0I=; b=iJbhBBxP5qEekN851NCJgrWAFefmoWvoB75jxyjEN4nWv9RGZ/T61XQYK4fHVu1Ckz Zo+LCO9LdvbQrDA7ZhEID1uSpGxwwtpltMtoQts8PF8b34+SYIR9RZQoqypm+1aXpqb6 5ODbtEV33NBAo6EORr2X278E7t1yqPq+VQcQ5Skex4BjBLzvsTkOs7c/m5ZfB7yhLoEZ ChjGaGh7Onj8xgDI2qyBDiwqB6ngxx7deuV6gXGJGBeq91NquSplgtrcOv17uMZpHvVP SEEYxnaiTqksJu2ynafZusxl6pB+PKI1t6RvShXgb/IRke2UtPxINI5VdvVpJks6cERp EQyg== X-Gm-Message-State: AOAM530EY0PpovKbWnI2x6qnxNNcT160y4Pgwu5NaWpU/d/Lb4osMJoF VScsxjs2jxA12+Bv7BxJXYyTHiyPP3rn2w== X-Google-Smtp-Source: ABdhPJwmjGMayhEjde+/rhHwk2cu/+eJs0P5kZH+JpdbfhE2yrgIn+J9YR1lWgHj4VjLyK8D5voajg== X-Received: by 2002:a05:600c:3043:: with SMTP id n3mr8257094wmh.98.1622203907831; Fri, 28 May 2021 05:11:47 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 08/31] am: convert applypatch hooks to use config Date: Fri, 28 May 2021 14:11:10 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286883 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,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 EC042C47087 for ; Fri, 28 May 2021 12:12:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CDD0D601FD for ; Fri, 28 May 2021 12:12:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236557AbhE1MNk (ORCPT ); Fri, 28 May 2021 08:13:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236314AbhE1MN0 (ORCPT ); Fri, 28 May 2021 08:13:26 -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 781D7C061763 for ; Fri, 28 May 2021 05:11:50 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id x7so3047764wrt.12 for ; Fri, 28 May 2021 05:11: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=wt33zWu6xIqqe9lXqz3Kx2wqMdRaDVo/yidJ8NYZxHU=; b=sD5vB2YbE9eQj6fUpvBtxckXeEIE5QSJmcevDA7W06dbgDHbj4kCNVrscq7zJ4TwrS JVNM4tj6CUmTyeeBAYU5MmEk7dPIVH03ebFOFr4OwXPyTzlHgSrwC9rjEAguu1BwDHlY 2G4DGrpx7jBbKhkvBskZuFHjhx79cfNvi9h3r9Bo/nJf/uXSBnQv06t6NuJwQoZNwRCs Z3pSOFV1fllWvnX9V3JKWx3isD5iIzYWvp8Bf2yhWZ8t1cBMAMwqPkcFRQsFHfppyi7Z fhzUdztdJQTWUc26pXthCszn3GVE1NpkEvSMGplWwa2HzGqJzmydCGxdrOvxms/oX+ty zpUw== 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=wt33zWu6xIqqe9lXqz3Kx2wqMdRaDVo/yidJ8NYZxHU=; b=mwAOCfas9JPxJXpqOpQ2GqCRxoAZjYKzPke26w4zxLoz6HXXDKf00kTx3R12qcaP0P 5cRtabAqaRORlu8q15zxJW7Xzu2powSy6aAi+HKXu7DnDN1tP9sPvndd5hym0nwQBCWw JNnxPHJwmmaVGWtzEIndgxCCQgnTaIJCSh1mAsYwmEsNw6rT5Vu/xxXU8Uby0ET8Yv3D 7baCQN6pa0/KT+NdqWtRLfySTYj1vitXtDPgZA8nAk/8mJOv9FJa7di2qR+fGzcNkqvt XABQuRPAroUJMllgp4zmWsXly7l3ogmoe3rqUzZfud6bWLMrEbAI3fpoc+tBhQfo4w1F TnCQ== X-Gm-Message-State: AOAM530+sM1sD5RM6Jkhv/Qx7oFixrBMWlqchjHu4Pv7owdGCmiWpxeP AV5vFPFH9gFMRtO6jJxhIPkTZOUdlGrhUA== X-Google-Smtp-Source: ABdhPJxxUhxHmmEDsZKh5sbJueDytKWgxJx1xiqLI2oII/enUYEZ6rV6S3DOW6SzHNTmQQNG44Xk5A== X-Received: by 2002:a5d:6da9:: with SMTP id u9mr8410354wrs.264.1622203908748; Fri, 28 May 2021 05:11:48 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 09/31] hooks: convert 'post-checkout' hook to hook library Date: Fri, 28 May 2021 14:11:11 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 eeb74c0217c..6687025bea5 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 1b3c2eb408b..775e970402c 100644 --- a/read-cache.c +++ b/read-cache.c @@ -26,6 +26,7 @@ #include "thread-utils.h" #include "progress.h" #include "sparse-index.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 Fri May 28 12:11: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: 12286885 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,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 8B677C4708C for ; Fri, 28 May 2021 12:12:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6F39D613E6 for ; Fri, 28 May 2021 12:12:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236571AbhE1MNo (ORCPT ); Fri, 28 May 2021 08:13:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236347AbhE1MN1 (ORCPT ); Fri, 28 May 2021 08:13:27 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 678F5C061761 for ; Fri, 28 May 2021 05:11:51 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id f6-20020a1c1f060000b0290175ca89f698so4424949wmf.5 for ; Fri, 28 May 2021 05:11: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=Xj8icIvejGwsanHl7uf5Vf1isdc38/vb+8pGosmEsUE=; b=HiXVbvt4aJ3G8Pc5ehaQ5/UTmAJWDFsk9Mg9v0l/nnDiB/iuBMXyyRF8E6CWlRT1HH 4VNVWvTb9BB6XIoGVEKMtnLn/UmKw6AOViexU6t/YRhuqGpySSgzGdeUaCrw3MhdpHVK n0zAo72H1cYiJHtpg8jOBr5LshhNyQKC0yT84kPyC8zkrNBgGgUuV9TADzLy5qVyxQFQ Bs70idMM09A7kSjMvLQgjr25Q9mIkLJ2Zm77ZZFe7zIACJFZUa6ZPjLGIzVecBx17AzM lsNxrlTrTlCws09V4aA9J5VQZ8w/tnpT3u3OcnxeZkNtcYoMqGQQlzCTndFTPc9geaI8 IkmQ== 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=Xj8icIvejGwsanHl7uf5Vf1isdc38/vb+8pGosmEsUE=; b=SYr51oiMuzlU0qK+W1qWQHc865dLA6FzXghehr284UpWXVYkinDF9LtHq2S6hy7V2P 9PLgoam1ezhnv41A8dTqZqhK7QtRNwZcqXjhjVcEBG1B1ShU6RZKchXDDSboyI60DTc8 P4GX+8P2T13COLqEWKQhF/hU+NAhtImIeo3pBoZjBaozliJJbpvv1TreW9SLfIq2Z1Ic vJSq7Yhlw9MuH/DfWuvWcauxKiKWWeLXdRwdGs9g4sAPq4LbRAmuJMyH0Px0fFnt6iiC GhiGVCHP3a0cGAG19KhynJvCZzU21GKc+7nRWRyQcJmjGvYoQiXnylpphUxe437KzoCL Xvfw== X-Gm-Message-State: AOAM530mQA5gqZAXyTplsuyLoRT9WUiFYDGLwFSuWlPBQMSP6dy4qYCl j4KdBr3GQhHpHJ8POTy2lU+PPwYhAMbwrA== X-Google-Smtp-Source: ABdhPJwTLETYPPSA0rFpI0nzfavKHfDdOGmUgZiqYykv+iIil0CZik0Rb5NwHqkKexlu/TN1NjCtLg== X-Received: by 2002:a7b:cc84:: with SMTP id p4mr8020319wma.171.1622203909580; Fri, 28 May 2021 05:11:49 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 10/31] merge: use config-based hooks for post-merge hook Date: Fri, 28 May 2021 14:11:12 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 5a10f6e3c96..a9363b94065 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 Fri May 28 12:11: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: 12286887 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,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 2FEA3C4708E for ; Fri, 28 May 2021 12:12:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 139C6601FD for ; Fri, 28 May 2021 12:12:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236590AbhE1MNp (ORCPT ); Fri, 28 May 2021 08:13:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236364AbhE1MN1 (ORCPT ); Fri, 28 May 2021 08:13:27 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 318EBC061760 for ; Fri, 28 May 2021 05:11:52 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso4428855wmh.4 for ; Fri, 28 May 2021 05:11: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=aapaffphkhfPAMBsSWwwx81dCQgErvJMkMMkU2qcqCM=; b=auGeOdCQ1YkWxmuFcIeAdNzo7Vtugk8TlnrdLHWCScd7Za8BMwo2b+c9ARinp7cErq pug/AIzW/2/1PD55cFFqgsV23LYO9d3a6X3PIFSQEcVV52rr+ISHiJA+MBWDpApKHdJE uNiwPcDrIVQT1ObbbIdtXxS7pPgA2HWpwjbANaoniBOqz4cLm8CVkcitpM6wTZ4qI5rH wXFKV/SD0IzSMoh64AyW3yGS4JAi13paoMhO9ZWNEFWc5ebpwKoJEKZoNRs6xWPrtwcb o35Ac4Vg80KwCnulHRDkxvDRN9Yz+sN7rq86IMZUDlCCVDMHc5uUDQtlSe908TNdB24Z RzDg== 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=aapaffphkhfPAMBsSWwwx81dCQgErvJMkMMkU2qcqCM=; b=dz+LTYB4POXTSeOyUrMJ/4BEQsKtnkwgJ9YoCIkmDBes/BEsxAYkkU3xdWOjmMWocH NwDEPrZlYZ079wltWfMIWqwrzP/kJC53h15ghSG2DwAHPa/aPqC4pbKf6DnWfXPXxdj7 gjQVZM9ZJgbjBd+iTDOSoXr9Pn90fKaNSXS2mxpbOSkwEiagQLWqFNYWOeCF1SL++UYK iUXT+ASRfVLhVRN65EOpCy0In3DeltQp8ri1+J5KNye5c+owsb+6b81gebclEUVVfynd +tHQd5TSaeQwrTAi8GyJY0Q3BEu8c6/DEl2Lvr+Bia/Pn/+4xYkN6GuZLe83iH+NpX5g 9UuA== X-Gm-Message-State: AOAM530fyuQ+RS97UjsiXdQt4PaElmCXOTKxFbe7+AM/Up3ZgoJ9NZsv hwAS1/tX6cY1tHhVHs97TGobIfSvDw33mA== X-Google-Smtp-Source: ABdhPJz6NitH9dK2G8pqh+1TZ20TkPrkYTBVIK4UkBIrguPPil9e164QfWmcUgOo/npiSZJR08UJxg== X-Received: by 2002:a1c:2985:: with SMTP id p127mr8306958wmp.165.1622203910543; Fri, 28 May 2021 05:11:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 11/31] git hook run: add an --ignore-missing flag Date: Fri, 28 May 2021 14:11:13 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 7cacd27c748..9077afa1ed9 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -16,6 +16,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 Fri May 28 12:11: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: 12286889 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,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 C9603C47087 for ; Fri, 28 May 2021 12:12:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC6B4613E6 for ; Fri, 28 May 2021 12:12:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236598AbhE1MNq (ORCPT ); Fri, 28 May 2021 08:13:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236372AbhE1MN3 (ORCPT ); Fri, 28 May 2021 08:13:29 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21C28C0613ED for ; Fri, 28 May 2021 05:11:53 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id x8so3068500wrq.9 for ; Fri, 28 May 2021 05:11: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=BDpqV0yBXKPaX/BTacQ2XgGOr5OICnJ0IhKZ/EXa8Jg=; b=CpEGfcaJDy4tU7TZ06tFa43SuaLTaVugRrSX77QLlRmhjmUqSGJCjaR5VLAr/acy0I 1g1PhMENciC8TpCPgpLn6De7/vPN/zeM3ArGKEgwNAfej+LXhe32+gDDH9PT+0CY0JzG wn5z3li7a/WwEOVTB8Wsfn9CC9q2JSdT4aIwCLw5YdCPUr92z9UYAhu1IjHnZXL5goke PKeQlDb7k94z2jgdfZwUDi6ZTUK9+i5V6ElG92yJrQohVUqlKDX1s25DNPbmqLJ3asPj e3kWKO5g9HiHG+v8APi2pzQ60JFTrKbii0Nw96I4NfBwmZVhaX6qu4nJhdMeD8+Y9PzA HLVQ== 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=BDpqV0yBXKPaX/BTacQ2XgGOr5OICnJ0IhKZ/EXa8Jg=; b=TVp+edRCk3ZwPI98RLRQG/glweOMpYwHXoceVISO1g3wOyRD7FKtaql2sq0NssuwIO 549ZqP4druaGl+cYRPQogy90oFELzcA7RFuyQFsz/uKkNxB18pmWaexapnUH3L1XrpcM YJKvzLrKLxJcVKnxR+IJkgtBMF8Fm/E7HxemSfwN14nm/vtT5i/7QttobKYnUBGRX+tD C7DoGittHzbJY/KVXJ5WibRgCMlVrcMxoXF/+R7R6Gv/pn93lsxdOnmLdioDdql14nwv 2olih1uHDxzJQO9tt6P0ChKlgYgtcZ90CfK45yND9imFvHFl1Z7ThvrvK4d7ZFhtbWPo 3NpQ== X-Gm-Message-State: AOAM5333+NQ1nSsRVaPWxIYt33MPIOHsvyM2yBwKV3f3mYEZrJxSYzl1 Dfq0JEJ0OxBS59zKLWpii4rhVbm6+Y4Pbg== X-Google-Smtp-Source: ABdhPJyXL2jHWuNh3M7Nsh3ds2fydAb7EJTXTE2H0lLFUOSca/26Wne4E6jQMROSZKiTViOmDJgRxA== X-Received: by 2002:a05:6000:104a:: with SMTP id c10mr8475079wrx.45.1622203911413; Fri, 28 May 2021 05:11:51 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 12/31] send-email: use 'git hook run' for 'sendemail-validate' Date: Fri, 28 May 2021 14:11:14 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 25be2ebd2af..2ab8dfdbded 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -213,13 +213,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 @@ -1959,9 +1959,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); @@ -1970,13 +1970,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 3b7540050ca..35b513c015f 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 Fri May 28 12:11: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: 12286891 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,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 7E73FC4708E for ; Fri, 28 May 2021 12:12:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 646D2611BD for ; Fri, 28 May 2021 12:12:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236613AbhE1MNr (ORCPT ); Fri, 28 May 2021 08:13:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236429AbhE1MNa (ORCPT ); Fri, 28 May 2021 08:13:30 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFF7BC06138B for ; Fri, 28 May 2021 05:11:53 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id n5-20020a1c72050000b0290192e1f9a7e1so2273732wmc.2 for ; Fri, 28 May 2021 05:11: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=T7r9ULGv9apYUhZVJDTU9OoHivQOcGw4trxulZmX+NM=; b=r0ThF77Wp+e/BxBJwZKaGpiHnVnzvMKYAOjaUIafVZSSCYu7y4NVJBUuyhG5EH/Wvl DRfTwHAJqrJcA8unSwFM1qFpdw/hfA/0y743kuKJj7WumE3c60GRm3fQ0//KO4/5fHK1 p3Q1u9tidbgaRntdvF6S1nYzOyhYBwAsutQwvi/4K1QTZ4VXVWS7fq4sQni/GtfJTkUX 42qp+TACGkOPwJm0cxYm9mPHGuSI+iNnF/XR/qW2bM6Scqmjja1C//sJj86Iq1BgXyL9 U2qFc5OP79bXhzq4QS5KVZ20b3oyYWTpHxwYcfqj6vPS6OXOq7bVuhr9pOC9Gq3ZiwPh z4fw== 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=T7r9ULGv9apYUhZVJDTU9OoHivQOcGw4trxulZmX+NM=; b=QB8YoJFtxXjIBK9gmiGyQIaO4Vlaatkl+XaQfo334EjR+osdAciDWH+UKMQOmv6/Ov VQBPU1wsdW+QQiOn10ui4iCCN0LweVKynzkd2z0TdiODPrBixPL8vdctzhyE6Evs2Jw/ 8LOUKpZWJj1e+PF56YzApoY+HzbsMxtsAHU0W576XtRljbxMLRRf2BZuWSBWu9nPh3be gI7Lj5UVtVo/bVqKZ5ivsjuYAnax7raPe3smfVYB/2cIue6taxjBqfUcpT+Zsx0lAgfI KQWFotOJXZTe+PsfidkJ6VqSOwHskXn4CFbhuhi4dEXqO2BkjwWqwggDM8Uu1nqUBXHg zy1g== X-Gm-Message-State: AOAM531XN+HGnsyNTXmVbKza+iLQzGmWSy6J1kgRDGQCXREQQRrrxQ5V AeCyl8RaVkombWqrElUI7EeWqtN4aqg4sQ== X-Google-Smtp-Source: ABdhPJxAGWxH+mgigrkJLf8cwsz0+cYP7106fy7Uounwb6WpdPZ2cOix4Ip0JEHY1VOugsuFApuoBw== X-Received: by 2002:a1c:a50e:: with SMTP id o14mr12196711wme.78.1622203912269; Fri, 28 May 2021 05:11:52 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 13/31] git-p4: use 'git hook' to run hooks Date: Fri, 28 May 2021 14:11:15 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286893 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,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 2A1D3C47087 for ; Fri, 28 May 2021 12:12:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0EE11601FD for ; Fri, 28 May 2021 12:12:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236490AbhE1MOC (ORCPT ); Fri, 28 May 2021 08:14:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236475AbhE1MNg (ORCPT ); Fri, 28 May 2021 08:13:36 -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 F2E07C06138D for ; Fri, 28 May 2021 05:11:54 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id f75-20020a1c1f4e0000b0290171001e7329so2275020wmf.1 for ; Fri, 28 May 2021 05:11: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=Gu2/EP8+WZtdqS+LfCUB7epHyQjdD+jLlCHAe/9Yvvs=; b=N6va65qAeuikLyMxWL+aIC1BXKEOTq3aRHYRLzmDdtDARhi1+OrhxhwpHPZ499YlE4 y/CAlRkH72ENBIP+3Tc7lGCbtlp/N/bKK1rZEbHfdVd7j/Xd7jjsJLbognyWNw+JT2tT iYa4raUvpJgOPezg7tTKdmKsXEK/LNEIwx67t0MIPjBE9kcQ1NF9Vqnss5pOIfAeipJN I7e9EhXVE7JNk4TfbTNoqCRpisXh7TES5z2yRoAcox3QGCpurYscLRIg9tABmtpn09oe 5QlR/rOd3EoWngdNB4zra5aP97myEo1aW+nsrcZzLGgx+3ilPgX3WYG73AWuP+HbFpQY Uiug== 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=Gu2/EP8+WZtdqS+LfCUB7epHyQjdD+jLlCHAe/9Yvvs=; b=P5AzEyIKxauXuLO5j9aVTyHqVbYZ6WXywwKeRbQ6uKshduEinvja2gYEpGwQmDyIi3 9tiOfSw2CdJTMA8gei7Uku9rgBcHFKBre5KzwUsnunT15/AsuiS4KzQTtLFd8hz10mIu Mu/80qeh9CWJFcB5DguqyQ1/mkxzOzcv/JfTleNsTPR5iTvzapTLQNvpOnTqyXlykcA5 HyqOiaUgVJMxp0twnl+YUBrC714O17zSGg7zZGDk11IwMCncq7/xBZW7svfY/Qdv84Wi 5gcP5t8WAOBOKmOY0bj7urFOuLPnJjTz3fhWIsQiFORf4o6LUb2FM7DN651vEsHJYcQ6 314w== X-Gm-Message-State: AOAM533muXdoiB8jRvUzvMzvNeG5S69Rwq5avEcNLWPtLWPzUR1W681m m0oKoCu5d60gybUIJ3EnjRgGucA/TojRIw== X-Google-Smtp-Source: ABdhPJzirDWKQ4fVGyB6ous0qsrcwwZ2fDPiYeqfm1YtOXFVHkuTEQC1IzaHaVD0ZCTLFzmQE2Bb2Q== X-Received: by 2002:a05:600c:2056:: with SMTP id p22mr13488872wmg.146.1622203913316; Fri, 28 May 2021 05:11:53 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 14/31] commit: use hook.h to execute hooks Date: Fri, 28 May 2021 14:11:16 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11: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: 12286895 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,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 BA0C6C4708C for ; Fri, 28 May 2021 12:12:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FBE4611C9 for ; Fri, 28 May 2021 12:12:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236635AbhE1MOF (ORCPT ); Fri, 28 May 2021 08:14:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236476AbhE1MNg (ORCPT ); Fri, 28 May 2021 08:13:36 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1E49C061342 for ; Fri, 28 May 2021 05:11:55 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso4428969wmh.4 for ; Fri, 28 May 2021 05:11:55 -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=p99YD4HVnPDTIi0NUGecUgtJQt+1orFFGAjsrdLOZ2A=; b=RtLjtAQp9BVLaTqgVy8lXXgo6qVR456wTRzKWPEqD8FvA+lk/BEpbOBzviSG1vNmfc LTqx8V5blbYOU6kUi2Jc78w07IjQ5pDaUJN0K7HHsS4jbqIYd4ax9lFelLEXB1kP9Utw /PExTsErmT69X0PnA1zVVbHbiWFbj4n1+cMp0Dx/y/388v/pwOzIHRYCqHn/FamZjNVM wjwfJVZzu1UEv+3utqXmoly/NLG9xEC6PdtK9LKoylTkilYyOv4S7143v+suXcm9euhb NZShoE+Ht5vtudylZmYlUrjSl/jhDt76u23IRlF5qN9V/GyWF0UFuCZ/QoNGd+sXt+89 Rhfg== 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=p99YD4HVnPDTIi0NUGecUgtJQt+1orFFGAjsrdLOZ2A=; b=qKA6EoShh9DipTO01NBuDQ8YoxJe6sFc1CNiuK/ouf5qd/X0+UKovXi4CeKznt9RT3 +t98QxPphwERjQBHSBljZyZlSFIEo/Q3tGJ6uMS6FIheEO/iOOSRoD7VyqdVqNsiTACe NFrRTQR0vdMMi/M+oqiaYpycGES+Fml1M+nnYSvJFPLHVD90MVcqxIAl4y9A6a19RAq5 eWRl3slsUimKUjjbjU2O8GANsREIVxkW1p6KLg8nLH1nv9DGthrWG8OQhCmp2cyl+h0H sn69XKDS95Sgd6hmd2KxXf5MhaG13r6o14Igl9vh8hWnHp9QtcqbdP6sh7kE8WfviaW3 roFw== X-Gm-Message-State: AOAM5300zz4sFh+QbWbB4xqCHm4S+UyoJswH0LlLfSaHJD+MnzC4FEVV U0Ns9F/oOSjDvVjWLRNYpUlOnf8yci6u7Q== X-Google-Smtp-Source: ABdhPJyJ/tnDf/W8MZLFGkYNR2FHUaWaEB1Neg44gCYmDMHuGYkLgOFwNsWOn1wRsg4rP+I2fJNV6Q== X-Received: by 2002:a7b:cc10:: with SMTP id f16mr8429644wmh.24.1622203914208; Fri, 28 May 2021 05:11:54 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 15/31] read-cache: convert post-index-change hook to use config Date: Fri, 28 May 2021 14:11:17 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 775e970402c..a17bc30f870 100644 --- a/read-cache.c +++ b/read-cache.c @@ -3132,6 +3132,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); @@ -3160,9 +3161,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 Fri May 28 12:11: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: 12286897 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,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 D01FDC47087 for ; Fri, 28 May 2021 12:12:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B8F60601FD for ; Fri, 28 May 2021 12:12:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236660AbhE1MOO (ORCPT ); Fri, 28 May 2021 08:14:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236304AbhE1MNg (ORCPT ); Fri, 28 May 2021 08:13:36 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBC85C061344 for ; Fri, 28 May 2021 05:11:56 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id n5-20020a1c72050000b0290192e1f9a7e1so2273820wmc.2 for ; Fri, 28 May 2021 05:11: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=7a3CoEvFbxF7c8JD6MpEyFbe4NX8fRQq+CO16EgHBa4=; b=XQjY/YUTyGERBH5in3Y6Dcilgp1M4ijHRMoyYw4ON6OEbXRK5QB7Y3JVXeIZxUfd78 FyZJy2czkZoeHdjrkskJaZ9Qyvf+RwTm6OvZc2TGocLtErLhSvSYI4LwmdOg7WD1AOH1 27LNorsocPBaRI3/KvSJ+aTT9K9qOVNllGkCxsLEEP1par5UyJdE9AGkX2/y/gMIJaVx kzksADYIfbG5FakUDk5lr2RcFN8Abj1gRYAkIXAx/+Lu32u1YhBmEa8/S3woECEQaqqz hjWy6PY927aYdIEZMJXjiLJU95UVqYDyB1LSwT2bbF/oiT+2o7zfo24JCGEnPHwyWEeI RL1Q== 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=7a3CoEvFbxF7c8JD6MpEyFbe4NX8fRQq+CO16EgHBa4=; b=UJ2EYJEW4E/SXQ1KXl5EG0Z5JiYkybWC27NfVed77mFw8xEDjfo4Yux67jZrsxPsHH nBa/T9wZZUPe+ykXWw64H6Ef7jMJ8aLxSEcLX+YBAFfgv9epQveeSXQeuxyWhCVsKxnY IIqJ1f++RJBkwpRx/Kt+Y/6c+OMRVWtfZvaYihhNbHBkTq1B6KLg6tw61Ym8apydpkFe WUqiw3Zr18AvL0y1Gs4+2K+6Yk9A+B13mTUtdPw2+iNZVJCSmCrvw2XSsyFDCjXtwySN WmCECyIlWz8yJqZ3+h7jChisY6F0k/PEbjONbCUiRQPx3vX3gpP0Tv2gvvy84O4xqQ20 64GQ== X-Gm-Message-State: AOAM533gzeCuX4NgcRAcKqsQhoWiTHrqG3Ux7T/II4n0G2Ep21AEV/mV nFLt7IberJeqUnDav+l664PRFCJneGsp9g== X-Google-Smtp-Source: ABdhPJxz0mwxz5pYQYNKqjALxm96+f9K3DqtWHdedsB0P49wXPjOzoMCrRAOmttWge5HRSHMET+REg== X-Received: by 2002:a05:600c:8a9:: with SMTP id l41mr13134734wmp.73.1622203915235; Fri, 28 May 2021 05:11:55 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 16/31] receive-pack: convert push-to-checkout hook to hook.h Date: Fri, 28 May 2021 14:11:18 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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. This is the last user of the run_hook_le() API, so let's remove it while we're at it, since run_hook_le() itself is the last user of run_hook_ve() we can remove that too. The last direct user of run_hook_le() was removed in the commit preceding this one. Signed-off-by: Emily Shaffer Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 14 ++++++++++---- run-command.c | 32 -------------------------------- run-command.h | 16 ---------------- 3 files changed, 10 insertions(+), 52 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); 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 Fri May 28 12:11: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: 12286901 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,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 9FB1EC4708C for ; Fri, 28 May 2021 12:12:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8353E611BD for ; Fri, 28 May 2021 12:12:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236509AbhE1MO2 (ORCPT ); Fri, 28 May 2021 08:14:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236515AbhE1MNh (ORCPT ); Fri, 28 May 2021 08:13:37 -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 C0658C061346 for ; Fri, 28 May 2021 05:11:57 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id f6-20020a1c1f060000b0290175ca89f698so4425139wmf.5 for ; Fri, 28 May 2021 05:11: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=C48NOrtXqZ8LJ2Cu1DC9QJu+9kYGnkR+sIoDge4AOPQ=; b=elXKzD/69G8hiOHZT5n0eGVUwXyUeAkR7qhGxgGFtySaxff+kljvOusWAhAw6z5CCr Eo478THmrORzkJS3R2ubUrv1rPUHKwdk58sdCXRRkCnNLDXYMzdUb/3D4C9HqO65F/1F vVx/97XOQCIk5r3ekY62TBbxTqRY3rDwuB9JL5pQSUGgdk8L1mLsoqTiBnv/o96EFsy0 skkJI8onalahiybtX5oyzIbgW0+phrC1ZVG1szMa/81LKZyu8ztPg75PUxhbMvNJ/zYA lPykK+HzGLzAEs0cu8Wcz5gpeyvRh2EgXugbUZzH/BVyexfPEVCOq7L9c7xh9hg5sXgY 5JTA== 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=C48NOrtXqZ8LJ2Cu1DC9QJu+9kYGnkR+sIoDge4AOPQ=; b=cNx1dw7IQQJ9C/+kp6xFRTL/lFEBD23O1/WrnVFT+AIYh3wVX85+pyPUi1FQXpesZV QRpFW7MOgkrm8lMEYBuDzrvFY5CzQPZFNH+/AZWmmpwqsdS10bCqrOudPmICBKI9k6uz yDK3tiAmmA179xXiSWsD1eOape7C6iRpt6G6boxRb/y//hGYLQC7UuRkyXY5i+XE/iny jzf4nYkQSUHSZQNvuyQ68amPpr310Sydv74El+NFRm/m4ZXo0ot2s1ysjQ8b9IiY5RP8 lpbsP0lYsRoroifhUSmrNeguqPzJM5WlFYXY0ArTMvMp0bh3jfsLebcA3TUsjlhh9yP7 dpmA== X-Gm-Message-State: AOAM532N5aoUBeVQxOjhLl1ybjhvLTqpjOmIBQxUqqeaf/y2VVXV/h6s orJ5uqivAICFordQtwbhdJcmw9kUDz3JAA== X-Google-Smtp-Source: ABdhPJxZkjeV7Lb5zGjcDyVJJi27g722/dqKGTJldgNjWgmxFmSv9pI0t/2HvaGuHL3kq/43+YhyRg== X-Received: by 2002:a1c:f717:: with SMTP id v23mr13050815wmh.32.1622203916148; Fri, 28 May 2021 05:11:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 17/31] run-command: allow stdin for run_processes_parallel Date: Fri, 28 May 2021 14:11:19 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11:20 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: 12286903 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,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 EE10AC4708D for ; Fri, 28 May 2021 12:12:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFABF601FD for ; Fri, 28 May 2021 12:12:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236547AbhE1MOb (ORCPT ); Fri, 28 May 2021 08:14:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236525AbhE1MNh (ORCPT ); Fri, 28 May 2021 08:13:37 -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 B385FC061348 for ; Fri, 28 May 2021 05:11:58 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id z137-20020a1c7e8f0000b02901774f2a7dc4so6684474wmc.0 for ; Fri, 28 May 2021 05:11: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=uWJhMm5EPfBadxamxZZKdhxIcSwYZaw7Ps6FrbMmv6g=; b=gFNCZx/P0JIXNqMetDNVq7yeha8JFxNlxmfxMyxINWeKH2YdV2jm6sUodNgNVXAJOV yMbszkD5S+/7Bl0yimnIHk4zm72XvRHdVJOEU41epZYMzy6q5lp2kYuFb4lqdPKohIfE sLUgxepiOxkM2BJrSDQYnaUZ4C4UXcnEvtCgOYqhO06O8wf+Ef5UtTIS7Mzfu5DO+Tnf GwqSTWbG3Qrek7ARa2QXrUHqnlHC2C2n34goHKtqO76h7GhtjcoPFKCOHDMGKu+lCbtW W+YXdsk36eoLLj7+KtBsAQ8dOHIk9eVE2KdWkWKCgb0cYqK9h+2roVjUhCLilfcPtFOI 1B2w== 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=uWJhMm5EPfBadxamxZZKdhxIcSwYZaw7Ps6FrbMmv6g=; b=My1/e2XnfgnddFzzsCfPQvtzjISPM0bbme84kIERBD+k3KcHFVHKVnp0Hl6r7fyZrg UeCe6fGb023U1qfZxNRJ6XC4sjvFlf5gGM7xZ5dtL2W7KQkoDc6Nt9eHHjo71RpSoqNo swSnl5Dl81vcJWOKM7r3Rnc4ddNlW5l84knQIZUCL2cfKFosOmd+YbNrPoSvOTiK2vm5 qZSbO4fYu21wAgwOyMq4V2EuCnPe62nwz8oXkWx42DkqkgE/LpSrPckbJQGfzpBajgEj LwbsHlxaibYPsnnaBNsIFad56Or3ShuJAKB6JcZYFLAypBnOqipcRcSN/OOLzMdeMiYX QoqQ== X-Gm-Message-State: AOAM5320yyAj7wc4eycaWusL99Vw6fdMWZh85S+3uXREyC+xVWO1lo9X fjT8UDbTWPTuPL6GCV77X9nSyKwSzziw7w== X-Google-Smtp-Source: ABdhPJxpKXLCvVlED1wDjHBY8PTjxQV8+WNYwGSbz3aoYW9cVwguxAvM1t55bxr3jegtIa6nBkwCgw== X-Received: by 2002:a1c:2cc3:: with SMTP id s186mr13506073wms.150.1622203917007; Fri, 28 May 2021 05:11:57 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 18/31] hook: support passing stdin to hooks Date: Fri, 28 May 2021 14:11:20 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 9077afa1ed9..9e2dd64275c 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -137,4 +137,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 Fri May 28 12:11:21 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: 12286899 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,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 C66DAC4708C for ; Fri, 28 May 2021 12:12:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A35E7611C9 for ; Fri, 28 May 2021 12:12:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236324AbhE1MO0 (ORCPT ); Fri, 28 May 2021 08:14:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236509AbhE1MNh (ORCPT ); Fri, 28 May 2021 08:13:37 -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 7D657C06134A for ; Fri, 28 May 2021 05:11:59 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id h3so1990185wmq.3 for ; Fri, 28 May 2021 05:11: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=qerW7OjwcsQDxH4kajgV+Dx56Zalu/ZtqD0N2463G60=; b=s2K8gQWdWqfR2ZoSmTCWGFv2enNNRW9c6TIdiLExTDK+qO13OtgJ7CZNsNVFlfptYO GZuaadpD1gMooieD/FtXRG/KzMI3O4FT63okNQfKt2D4B+TPbqCfXr2UK9LG+AsGF/oa Du0I0AI8lhO+29uNYoC/gkbJk7K3AiKTthFvAu/D8wQ5Bfa+BA6SvD75jhCcbCRvpKi3 u9L+fKAV4P6xDHeVhjlZ3zfkAZbSXxoMFcepVm5NJcIJw4K3yFzlISylNJC6mQRv6BFb bzslM1i7hADP2AvHhtTmUh1eQ22G+rGJmWjaquUxwFHc+v9rq+S7xdzwWNe1cneP4ZQn ddvQ== 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=qerW7OjwcsQDxH4kajgV+Dx56Zalu/ZtqD0N2463G60=; b=ExwAhJPrLl1HK1lRQEP/iaZOCP78HdR4ZHjeKPNu1Ln0ND+zP5CAXbHNEaKZL1dtaU uRQZecUkoDWhJ+DmqdlCaJrKyD8Z2CKC4O7jU7ZqWvCnJaAMtDepwrpQw7nqontvqNe7 kIrVAmpgKu+4TsVgauUxBRt+OZnKyjq0FeESVptMQiDdODVpne6O8s6jKn9tBw4UMFYW dl5mvzGHx7iVcyWHPclMUb/grL+rr1c1LEVNLyU3uhzxYIUvsrsllnRM3kgsEBYvad0+ juSu5pE31Ebtl/cH6utp4GSqnoh++flQ05Xj2hv527d/ku5hGz92UPQCnLS3pJutXtXj u9Jw== X-Gm-Message-State: AOAM532K8FV45Mm8g7p1tlwS3piKh0g80gHuFI0vzWzN/g8680rIhuMu bXfU5YBnCXm7Q/ELl9vGbRz6wA3ARftg+g== X-Google-Smtp-Source: ABdhPJxQng2yf/uUwluIU6GinguMRjQ9+fxkYgSV9dwGYwEl8ZgayKtVd0lN7tnC6gdcrqSfNbSkpg== X-Received: by 2002:a7b:c152:: with SMTP id z18mr8232853wmi.136.1622203917832; Fri, 28 May 2021 05:11:57 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 19/31] am: convert 'post-rewrite' hook to hook.h Date: Fri, 28 May 2021 14:11:21 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11:22 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: 12286905 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,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 C36F6C47087 for ; Fri, 28 May 2021 12:13:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 99B4D601FD for ; Fri, 28 May 2021 12:13:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236681AbhE1MOp (ORCPT ); Fri, 28 May 2021 08:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236593AbhE1MNp (ORCPT ); Fri, 28 May 2021 08:13:45 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1FA8C06134D for ; Fri, 28 May 2021 05:12:00 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id m18so2028101wmq.0 for ; Fri, 28 May 2021 05:12: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=S9mjDfk++LIIVr2VKDRNRxOCBcEi8IjzmMWEsYog3GM=; b=PziZD6k54H5vjW+DJ7CZUVy55HFbjGJq0d/dVoW5mBTF9gMnvQP+uAFhX14hIrJ1Sw YN9+QsQU9inXU0Z1HA3AGmTQlxDMcO2vImfG6PZSzsQyIUMjpVzZ2ZFCz5bDkogC3SVj 34zR8av4SI8EQEptSwogTdDG1qODz7rRkAJVAW3mXITaH2yC4+UFSf+IDVR8eV0HHkOY PBTOWmEAcEVUV/QVBcORg2sMQmsxHdN28e1QFNwD6OLSdBvCuVHJVLlnQj6BB8WV228s 0E8tJqP5wpmAI6K4AWcnHUGt/j//Hna15VYuIyrNE7UmUpDLUGIFNxvAEtFDIKKBf1C+ sRHQ== 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=S9mjDfk++LIIVr2VKDRNRxOCBcEi8IjzmMWEsYog3GM=; b=MAsMvjMDmdKqqDKFfBxO8e4oFW/GsEbhDsxQziaeIVgT77ob068eyoU9zGtjh34Wht iIlqTxfvp7CXS3Qtf7r1gFpoX2JN7EGpG3Z86cgiywh7JVNv0jDY+pC39IUll6/BDkXR I2Ti57IPBVIvxNqoEeaYfzbsIdGpxt5ilmz/aWF8X6f6vDF4xBeEnu1Ke5/3PNqMr2J7 CiaU4unC/zQI9yOMybS0qPauIOWAPWng5t0ay9PKLx93gj4qCoPWMR2Mj9IjBViC4jgx ywPTWMWlXqVTA1tOGY7YSVs1pkaRs85k+OPRp7QXfujv6dCZLrxxfPqRvOtW7bwdOV6p vcZw== X-Gm-Message-State: AOAM533iAVPBso7sR/nBA6GLAOx+saK+NvuVh7sG2+MWHZEboolNTLBh cRsUltILkfO7MkTn8eUhp0bVXXGPSOsivA== X-Google-Smtp-Source: ABdhPJzvbTlAgS96yn2tEVa504oL6vgrn+FaSQ2GqaMdVxn55cY7qQNddgY+rHfsHh1k5VMQRILQxw== X-Received: by 2002:a05:600c:3043:: with SMTP id n3mr8257964wmh.98.1622203918860; Fri, 28 May 2021 05:11:58 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 20/31] run-command: add stdin callback for parallelization Date: Fri, 28 May 2021 14:11:22 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 dfde96a4354..a07816b6504 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 d55f6262e9c..8d1e7310737 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 Fri May 28 12:11:23 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: 12286907 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,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 E9C02C4708D for ; Fri, 28 May 2021 12:13:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE7A5613E6 for ; Fri, 28 May 2021 12:13:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236615AbhE1MOr (ORCPT ); Fri, 28 May 2021 08:14:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236595AbhE1MNq (ORCPT ); Fri, 28 May 2021 08:13:46 -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 4F91AC061574 for ; Fri, 28 May 2021 05:12:01 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id z137-20020a1c7e8f0000b02901774f2a7dc4so6684540wmc.0 for ; Fri, 28 May 2021 05:12:01 -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=GQQjhSTHoH4IiTKM5fGNPEQJLpMyCBbHyQthfVCwWaA=; b=RoII6yVx4gSV/dsVuSszgQErZ73j/H2jw5VUnn7p0hZ73YjP2pcN6kKlL2o8cdD7yA +dvj/gbXN5vlZe0pUU1WR/urfhu8M5nOkNAfXjPnb8S/kGcVTHpK1h9o8tICf37+QT8/ 35oItRCPAQ/Z+4rPqk9o4BfwJNznLaZfBXGe8lr7VuwdC+/TuadOqmhm4LdJ8wrz+4Gq gmlUrRwr0g0Y8BWy8UqVC/nL947PGO8MtPR6+S+xRVUj1W/3Nxbuyo2jxJYvPqUFPSwC bFulzinqU1TnQvTyBrTlbcilM95GfTGh2dYvmtPWm1M7Xv6eE3HoZlkoH3i3gPRovSlW 6iuA== 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=GQQjhSTHoH4IiTKM5fGNPEQJLpMyCBbHyQthfVCwWaA=; b=CB92SO+1iaNri3QYxTf7T7haTkN0BpgG5D0W9Sea6wdvM/SH5YtIYRL/CySHJgToU8 2fGcw94UlxLcXbU+S5z7IXmSm57GvoIgtCadrIRJ8zo/odAp4JKl618OLo6QG6wdMifF 31AtZxHCRiFB3C4BG30xa6INOJADMLg6sJwMzZt0TFa0Y1/1QszXrmFHq6neAh0o3OUp IXq1Rb2WNDXnbb7dcWxPpxhBC5Mp1KYEv5GjWrn1zjucffb/7VfbGwf3m/ub/6iX6Kqz xL+6OYXKJnNN2o0K6tWd7WC6AT+hxDFTaknHLlNNc3JBsF00pBty2wUY9s55lGZZ9Lo9 Zkdg== X-Gm-Message-State: AOAM533b4rZYH56QdOi5bw+mhla28jBMeGLZ9zxlOZUJjTJqoJmrKs6t 7sJw293vpXOS+IuMDtnCBJreIAv32Gik3Q== X-Google-Smtp-Source: ABdhPJxGZxbcGZXRKZw8vaADo4EDFPs/7mtGLF1VyeoL0UrrnZX7Hdphvv/Bdie+m9HgrhdXa00YCA== X-Received: by 2002:a7b:c0cf:: with SMTP id s15mr13518986wmh.134.1622203919583; Fri, 28 May 2021 05:11:59 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 21/31] hook: provide stdin by string_list or callback Date: Fri, 28 May 2021 14:11:23 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 provide a callback the users can populate line after line with instead. 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 Fri May 28 12:11:24 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: 12286909 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,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 EFCE8C47087 for ; Fri, 28 May 2021 12:13:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D592E611C9 for ; Fri, 28 May 2021 12:13:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236691AbhE1MOt (ORCPT ); Fri, 28 May 2021 08:14:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236372AbhE1MNq (ORCPT ); Fri, 28 May 2021 08:13:46 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F2C7C06174A for ; Fri, 28 May 2021 05:12:02 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 16so1985583wmj.5 for ; Fri, 28 May 2021 05:12: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=AxaKIcqgkwmJb+giZN2LbhUNw8StGAGqmg9mLvKY2e0=; b=bwW0yLAHZXnxYWMYTRJHPbnZ+X7LyhPNG4PNWLQInXFrdLbphuhQZtY/vEcrsEQPYy DRJJqsctc+jLUHwSfA36Cbgiq9Yh/Na1j1VaCAewk/q//cOTw+gyzeyutF0659i8wz0H xyJDhZclx4VPMkfth63L/DH7pu+pd8lxy3C7SYriGSQx0ayxjGLMBmQxwMvFuLdfuVsh 0uRb7540dkq3BHFat9dq4cMCi17/uPnELIN2utAa8YPrUeeOwSnunDDlaKWB4fwP0mm7 OVw17mb3YKBeg/UvEfE+klkamfoTrBxkqQsZoMMuHM6YU1LTZotyrPGtSp4dv1douJzC NyYw== 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=AxaKIcqgkwmJb+giZN2LbhUNw8StGAGqmg9mLvKY2e0=; b=Bkbljp1+Os2+Kj3HBV3evMnaQWj1s6DWdRmlpUEPbGUfZ6WpWGMpAvGal+uG4fI2MG VgHKpZLrDQEPlQBuYusWIn1H559bOyWQATrfLtjdgbBQcgIDgppKqXW/VubiAWsmCW9J 6wIpWaKy1B9YEALpsEgHMw7hhapl9L61hlTMwSM7p0Aj+Obsi1Wx92gYug1ifICHwlYx tysZJQcMMGf3stgZ4MeXvfi/qPrfM7DCsZ+Zvj5CfPkbXA4pTCE9PV1FsjEdAhIHwEJV Df5mvjeRtrzF7puECt6zucCOEr2Imkx2HGcJYozEmdKUmmVEVvYZJ7Bu4LmJlxDgMspH jPfQ== X-Gm-Message-State: AOAM5327OI1s3ZxoLsPe9fhiNKxlYzeKSYVtmu8CPMWi1SGUCw3IpqyI V+OGIb+E4zgGw02bpoj/jR7SWXp+Yla37Q== X-Google-Smtp-Source: ABdhPJz58iv7AB8F0yClatNJvw/kPi7kEHuCDHtFAne8QJlXfYMXvlczdm+MYG6OVqi27NtqZ/STOQ== X-Received: by 2002:a1c:7402:: with SMTP id p2mr13046707wmc.88.1622203920412; Fri, 28 May 2021 05:12:00 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:11: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 22/31] hook: convert 'post-rewrite' hook in sequencer.c to hook.h Date: Fri, 28 May 2021 14:11:24 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11:25 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: 12286911 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,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 BD052C4708C for ; Fri, 28 May 2021 12:13:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A19B9601FD for ; Fri, 28 May 2021 12:13:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236698AbhE1MOv (ORCPT ); Fri, 28 May 2021 08:14:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236612AbhE1MNr (ORCPT ); Fri, 28 May 2021 08:13:47 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 308B3C061760 for ; Fri, 28 May 2021 05:12:03 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id f17so568134wmf.2 for ; Fri, 28 May 2021 05:12: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=GGImfH037wcNCUcNE1zzMJcQegHcwPgdiVclWcwVQ7M=; b=GG60UOdUL9bFafABlHHFjZxr2DVlS7myFS7Ce7igheAeDol/M3NqGe0WInPo/tm53o xsOOLw2DfSCaQnBjc0qHgqnwwGMMEh2Xn6v2t5M4Inu8P4MqRRwPRF0xyI9PbaQxheRf p5PelT9bzkk0SHwNHj58oJAPBPd/QeL1c4uQuR3lk7Tyr6zu47A4uGQJYR3QdXXrxZkx RD5WCiet/17iOGxtIbA67FL9hTplpWC2ifpp0Xk1Cdl/2A/vW9TDrfOh+ukRW56y1zjn JIShuRYXSQHJCvCzvRLOtQ3jhpBloF/H77QXjElBysRMwbSoL7Rbu/NuEAUJF5krRv+8 8uNg== 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=GGImfH037wcNCUcNE1zzMJcQegHcwPgdiVclWcwVQ7M=; b=pRZAdbpNTcnXsrq2z8Z+pb5oJR/YQvzBinYVcuNui6+5lNniWiVWyHxrrK6AXH8fZ5 dbh0lPwOzla2/ptCZ2HsdaPw5bosXMDVCBZa6KPCq5VAv3zST8VJ1Bz/qGZNZkkbG420 tZbGDiVVLMUBa/t8tv0QEmU4Vqs/t2CGxMUoDcUySBb3Iqpy8wgoQo1tICeAD6zgwzfB +WtEomZ0+frz5Vf1hGZhtmPH399ASlGx/tTiwbPQBHzD4okM7v6eW0jMitMXpRAudzzM J9KWMqru/FSHTDWFd7zYIagcjIv4HazN3eEaTfvbtMl6YHWEjf9g0zsRx6MX5s4NGPAd 40ag== X-Gm-Message-State: AOAM530XSfvgtQ32nDjQQat/hzH7aguLH2rsiVX50clDPz/6PKtxQ5zV 6ViYNcl87sBbHTUJDtofOH4pvVhzA3NVgw== X-Google-Smtp-Source: ABdhPJx5GmxWtqjdv/0XKEOSKkOcmkDuKBGgevTcXLCQvxOdI1MgusAdh7Ma+CoxpT8uijj61l9Nww== X-Received: by 2002:a1c:c382:: with SMTP id t124mr4971320wmf.39.1622203921442; Fri, 28 May 2021 05:12:01 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 23/31] transport: convert pre-push hook to use config Date: Fri, 28 May 2021 14:11:25 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 e6c46adf60c..1146ed3143c 100644 --- a/transport.c +++ b/transport.c @@ -1197,31 +1197,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; @@ -1230,30 +1213,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 Fri May 28 12:11:26 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: 12286913 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,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 B67FAC47087 for ; Fri, 28 May 2021 12:13:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98055611C9 for ; Fri, 28 May 2021 12:13:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236459AbhE1MO6 (ORCPT ); Fri, 28 May 2021 08:14:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236321AbhE1MNw (ORCPT ); Fri, 28 May 2021 08:13:52 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED234C06134E for ; Fri, 28 May 2021 05:12:03 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id n4so3103852wrw.3 for ; Fri, 28 May 2021 05:12: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=dGy8NXKxgkRrLnBEksuxOFf3L7dDb8rvfm8xBzrPtYw=; b=e0s68Wp/S4uB7FkhVp0dAtB7i2exxm5+euBDEu+//cbdGVYEv+/YMeES8KTu4w7Zba RfQ2P0cBKKr0bhcVsOjXXQkfeIgNJDbBUIfbvdn4tROz++VGvjur08VOHNwv8wAuNrE2 XWMBVf0NrQWSKgDCkNPN2XlWzYP3g1WlOd6Ppfxo4p3l+af5bebXuRsXaeowCVk1H+wN x3Qr/r+uEn2IiyAtTXUHhRq76/YwQL/bzjpeGgow65R2zMzsl7XON160T/C1bhLHEP4v fHkv1xcQkGwuayAriWeJufT2MK7eySIzxQM9PG0W72VtI+Q4WcCIytODa12e7JrSP3b3 dz8w== 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=dGy8NXKxgkRrLnBEksuxOFf3L7dDb8rvfm8xBzrPtYw=; b=Nyi5VXw5WNomQzYCvkcsDfnzNRBqi67sTH+3QGcmqTexorzPBx0lL+juS2kGCnXg2x gjtdcp4MAa1yt2a+BfMD0IrT6X2de/S3LR5yLF8vl28mM461PlLefjX/inCVR4VqdjS1 iRiLi8aBXNMyROW/b804WfVTWpvAyN4DJEVbwzH+1UNAg+lOMV9yyYMg+e4RahFEto21 bLbKWF+g6hXkU9y/1P07kYWUhkjyxh2IcpTG6Rb4Bc91GVzKH6cha38rn/v2BYdeQjjf 9a22iwqshoWbHDQ41Xdm9RgIJ2YkZvswrtrCOn9Ae8yg14xvJxhPPZyb+pMMvaucmos4 Mapg== X-Gm-Message-State: AOAM532zTcJ7TLNBJXCrdpVMhCjuka6MaqY5MU2SBcA7e3f/aaCDTwlm Ank9AF39/rjTx0gzqYYYhO4ovKjTAiSu+g== X-Google-Smtp-Source: ABdhPJx8jXq67Dly2KVrC5INcjujp9jkNvYFA3ooPOddQFCa31WoEeAkqVtZvmniqRP2diOyLUN76w== X-Received: by 2002:a5d:570c:: with SMTP id a12mr8223786wrv.354.1622203922291; Fri, 28 May 2021 05:12:02 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 24/31] reference-transaction: use hook.h to run hooks Date: Fri, 28 May 2021 14:11:26 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using the hook.h library, we get closer to removing the hook code in run-command.c. 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 Fri May 28 12:11:27 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: 12286915 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,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 1DD44C4708C for ; Fri, 28 May 2021 12:13:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2331611BD for ; Fri, 28 May 2021 12:13:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236703AbhE1MPB (ORCPT ); Fri, 28 May 2021 08:15:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236624AbhE1MOB (ORCPT ); Fri, 28 May 2021 08:14:01 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CF1DC061351 for ; Fri, 28 May 2021 05:12:05 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id m18so3106099wrv.2 for ; Fri, 28 May 2021 05:12:05 -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=clCx5ygP1OBzbraL9x8+j9URBdkOg06KV7KkKiioWwo=; b=gv7i05da8J8pgUo7ouQwaDYdSjJg7h+2qhmT7QGRNTtDspoaVA4uWv+mKQU5nWRpa6 1FT0gaSYSTS4pgxn4d1tmBaYkPz4qN8z4bJNrI/DhnQnjG1uUO7FIG7ixCSD+lO8Z46s REUhIB53Ky41Qzv6evS5GF8J7LoPkTa9c8vg0lyrpClJhK/vzl81CQctUAOFTHuQq2/9 7EAeqwKz2yJKOFEHRgUhTBkkq8IdfNQ21Li6XFZbScIsL8v2mDreZ8keQ172UrbS6vav 1NorRqTUEG92oeyXpas1qIuTLRWYuHnTSO+OZN4FZn1xUc1a0ni4/3OOFzdsuFZ4qqJn HfVA== 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=clCx5ygP1OBzbraL9x8+j9URBdkOg06KV7KkKiioWwo=; b=VPtV29vKoty8MxqJqZv2MyWJmTzqN2eu3DOCirSvIml49GZp5EICnn/PRYpqHQlqFg KM9jCb6v2V7e4Ut93DQOA1a8gOYVhqpsbs/oqbL1J5UOJG/V5Bxexd5F2Dxq/aNwjHM2 wDRLxdot0LJ5tGI31dUMUnPJHJm2/6LL/BTznHHN0YZXuyJQRZ6uXwNpOj+rJ1uAMepd 20pPbeUXYGFd24SRlx3ar/M5NrClitSaZjXz1U4BL0v8R/2Y9VL+5ayWRKpb6MQ+ikaa afQnILSl9JB9jOZz1GIdtp88TNc4cc4c8pnVe3nP2n1so2M8YHWWvZ68HiXqDhW312eE AA/A== X-Gm-Message-State: AOAM533SEIn7a4Aty1wLhT/KhkpLyo5/3fdNefTZ4fF4X0u9bfjQiCA8 Su//lu+6wM0XM7DJ6NyToHbRmPxq1lRnJQ== X-Google-Smtp-Source: ABdhPJyOWkDcs1MtfRCmxciERFkuxzANhx5F6jiBePVAa6DIbbZsOHvf+nXiGsm4VPJQerSkbtsAGw== X-Received: by 2002:a5d:508f:: with SMTP id a15mr8632017wrt.242.1622203923383; Fri, 28 May 2021 05:12:03 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 25/31] run-command: allow capturing of collated output Date: Fri, 28 May 2021 14:11:27 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 a07816b6504..769af53ca45 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 8d1e7310737..fef8392e1da 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 Fri May 28 12:11:28 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: 12286917 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,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 2F739C47087 for ; Fri, 28 May 2021 12:13:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1510F611BD for ; Fri, 28 May 2021 12:13:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236708AbhE1MPE (ORCPT ); Fri, 28 May 2021 08:15:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236633AbhE1MOD (ORCPT ); Fri, 28 May 2021 08:14:03 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EDE4C061354 for ; Fri, 28 May 2021 05:12:06 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id q5so3095137wrs.4 for ; Fri, 28 May 2021 05:12: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=jOgdpNLc8PGAK/W5GNH1UaftrJhOB3vvgq3B9Qv58Qc=; b=RFZe3ois1xi3uP13ruyLs55LYGTPbSYfkMBb3Sw7n6AmuRp9/WyVoQxEj50+KwIKFT 3+Xq0foTss416RvpZs4ETpzavQD+9OGT3Oh4v0z0TxOtDMsszttnt2FjboeqhWaCAVlC ggjx/EY7LewU53G5fc1i7hFR0g2JkXHA7v4lN2Muh8UhCRa2PGvSPlDAFCm2GCpclqze c4z00dHnPWYauJ1dcGxHMVbvpL1LUq+jErPIu8QHEzJMa8XKeEiOFtjsGs8QwKt7p1+r Ll1r3HSYQoAs/LFE1tYVO3KQnVZGZqvrbEUQL91Lz3qd4/W2maE2R1qcS7JrOsN883fx 9v/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jOgdpNLc8PGAK/W5GNH1UaftrJhOB3vvgq3B9Qv58Qc=; b=Pfjp/bDbF/Gv2wZyEJ6hVu/0XKO/sB0pQx2BGr5OSvkrk6PuMFgrQFQuw7haY1HXMP XiBwMCoZ7Cv7t3paE9UGZPkGgzFJu5KwDgiJl2pLjD6wLHHJpi+ymo1KFW6BQNqFzfHj KLhhxPznOpeaulsa5Ep6kflhk/G43ov9kNIwhQR8DXofNnqTNPEpdBaGW88BpCz4lYop GwiDd/eUrx4jhuOSxmPlDhu0ouyQ0NGVwQ7shDwvbbytv+5oPuWoOdgDIOVzyAXQI+9e LY7lIiS3ffRoorCGFesAeR6bxSxvUZiL7Lii8MbxZOLX4jZ3EA2RxYZFoqAFfR6rWS2o eY9g== X-Gm-Message-State: AOAM533k3rmLW4KNS4nu1gWObxFR4en+4gG9BJS/9ecsLt+ppVBJrDDW jFsnATGoNRNBaoPoLKDg9ScrgYPT/nNe2Q== X-Google-Smtp-Source: ABdhPJxqowNgSkzxlI/8eyS358nPmGcB7szKNXOAzzSagMRDwSIfRhe/YZSfWq3pjHEzNvKzH++4dQ== X-Received: by 2002:adf:d081:: with SMTP id y1mr8260560wrh.179.1622203924461; Fri, 28 May 2021 05:12:04 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 26/31] hooks: allow callers to capture output Date: Fri, 28 May 2021 14:11:28 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 Fri May 28 12:11:29 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: 12286921 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,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 820D3C4708C for ; Fri, 28 May 2021 12:13:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 610E9611C9 for ; Fri, 28 May 2021 12:13:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236716AbhE1MPG (ORCPT ); Fri, 28 May 2021 08:15:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236339AbhE1MOE (ORCPT ); Fri, 28 May 2021 08:14:04 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D6BAC061355 for ; Fri, 28 May 2021 05:12:07 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id x7so3048606wrt.12 for ; Fri, 28 May 2021 05:12: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=eGU5/c5lmdTaH+JpCf05jhjFfxBSuPGEgD/+rQmcssc=; b=Q9CaDNnUfg8a+F1SdddtaT2D1n5wGcFUIgv/FpXxQ7Ln9sKGKqoCKPWc8/soa8KuML UlTW2V8zZtIjbJenuZ9LjKnC3uYpwZ/Dmaxk8cryrBETlCBB37G5PL3KwRCh2QEfn+JS 8NvbXq9ZgP5K1SWzU9tZSxdEOCquE6o+3xahkMCQZrD0jrc4DNJBLi0P4dwdoJLIJmH0 5UgdQE4EXZ4s6rU9Ekgw8gD3EepwdI+kX3owbE+kfC9LaBBcrE/CiGW+H/YeiODBZuML /2YrplKeqcmHJDEi7Oz/KkuvjGktcBe14b5x4G8bVE1f2nvNXntzRoE+kiR7Mvp8CeOa uvBA== 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=eGU5/c5lmdTaH+JpCf05jhjFfxBSuPGEgD/+rQmcssc=; b=q8vAPa+Ihbi7GADHt3vxFZOSe/+5S70Ilj7q4TP1iz+CBU+Ck6cF1H/kaPuwtorcml NWZuj37MN+LDXPRPbApVGPMKtmyj+8Ou+4aSA9eUMH4jub6vEg6To3MIH92kvVkOBSs2 D9zSikbP7xZFPcZwKXzf3I1YpJlVeeZduSn7wmBiEZ70/mcBFRAFKde7AEifiQSz0347 MXYD+d0//4EDJeZFhL69tp31YjdTwD3X8kNNNUUlY9AYNDn13B1M6PKAcumLPDvAwjM7 B2PGfyCdS8gZ7VHKMHzbhAPLFI3otI6cRhXoT43ftLZLEYbdxnvlPMuzIHElC81W5/K3 MSUg== X-Gm-Message-State: AOAM533dlzkdP5sBBTQWzbN1k1Wbx2d+2czSJFKUhtOPiOFzeg7KGMVx JXOs5JhPkne/8/3ceYxhlUjOdaQ1lj94YA== X-Google-Smtp-Source: ABdhPJyruA+8ofRDgpnvxQE13QZEew9oovm4Y//Qfkg2RRADZfH+igIB3JDhSF0odDBQB8TOR00N5g== X-Received: by 2002:a5d:6488:: with SMTP id o8mr6018638wri.113.1622203925560; Fri, 28 May 2021 05:12:05 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 27/31] receive-pack: convert 'update' hook to hook.h Date: Fri, 28 May 2021 14:11:29 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using hook.h to invoke the 'update' hook we closer to removing the hooks code in run-command.c. 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 Fri May 28 12:11:30 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: 12286919 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,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 2D221C4708D for ; Fri, 28 May 2021 12:13:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 14C1A611BD for ; Fri, 28 May 2021 12:13:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236709AbhE1MPF (ORCPT ); Fri, 28 May 2021 08:15:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236475AbhE1MOE (ORCPT ); Fri, 28 May 2021 08:14:04 -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 166A8C061357 for ; Fri, 28 May 2021 05:12:08 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id r10so3047049wrj.11 for ; Fri, 28 May 2021 05:12:08 -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=DlAKzSA3B2Kp+0xcCtyzX097Eqwt1MMmLewOTJgUuPA=; b=UZOtuW/0dzUniH6qUzE8aqVsCgBmnRnCE+4F+aDw6cpdNcrVFOOlufx+oDhlZzy/2N 4Y7Lov0VfGTmtutisTLhK5cVENd7kmrHHERBGCfrgTRMRhVmAkIRld31schQuzX+wmj1 y+9DSW/F+pEr4HP+c+Qb2nh/Hm5+WeY3wLbD+wLuWiBmp2EgMUNMfOmCiXG6GK9VvCbB qDYs91VHKtJESMDNz6NqMm613w/LbaPT/bD7+sQkGXERZ6YuG3/esspmBTpZixbSXTS5 91J+cJ3A9UtsMoKxPozsl7To1GHSuqYjKA2K3YdSE+ImWJH9A2zBmsykM2EaYsbl/sAS xJXw== 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=DlAKzSA3B2Kp+0xcCtyzX097Eqwt1MMmLewOTJgUuPA=; b=przbxfkiye28KzYePt36UbjWa5IBxVNIP8V/2PtOmdiHzk8gze6qQtl1c2hcrYWMVF bmyrGdv4RCAPll6Y8aBw5GxGvSJxzoFMTRSw0aEOkPVed4lZuxjfzaGEsSciA6t8NhyR //Dk7EsDkLn5ooTAgQJ6XN+5kf7Z/cIwvoqAYMJqMsCx3VzywNZrqnwRVQR16W+aLe4p w0lJyQ6A0xUvKnKpiaUB+wtZVOqqXnuNYD5u+h/V6OMpzkHlUvxUojUpbkPJvo6zIw0V 1Qd3RiX7z8POx6y9dCH/2716FNP6KiMAPBWKS/QG6CQdNPeurXxM+3bNGhiSUe62mhfW vgvA== X-Gm-Message-State: AOAM531+d+Zr5zksBF2ZyA6Um9hlEYzNfHZyw26C1wo5vpKdHT4zpm+M 4kpRiWTciACWRgKL46nP4hASshMOOwtPMQ== X-Google-Smtp-Source: ABdhPJxfifGkZkgIfteRPgkbTPi1ldzYKCvhTngqG1eKGfRoduGswmjxxxog4VNJRqGNGGz87HJheA== X-Received: by 2002:a5d:66cc:: with SMTP id k12mr2806812wrw.185.1622203926463; Fri, 28 May 2021 05:12:06 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 28/31] post-update: use hook.h library Date: Fri, 28 May 2021 14:11:30 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using run_hooks() instead of run_hook_le(), 'post-update' hooks can be specified in the config as well as the hookdir. 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 Fri May 28 12:11:31 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: 12286923 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,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 B485CC4708C for ; Fri, 28 May 2021 12:13:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 93E95611BD for ; Fri, 28 May 2021 12:13:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236636AbhE1MPY (ORCPT ); Fri, 28 May 2021 08:15:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235986AbhE1MOg (ORCPT ); Fri, 28 May 2021 08:14:36 -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 A3570C06135A for ; Fri, 28 May 2021 05:12:09 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id f17so568301wmf.2 for ; Fri, 28 May 2021 05:12: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=+FYWIIiU0pWapRN/M6RsTE3dosqtcEQvLWm9P4hcTVQ=; b=dOarbZzIpRCwo0r8AP2MWm6Zl7Um3bebAPH2VaENZvkofVXKetwCmf0hSick9+KFG8 u78g1OReSLjz4dLaSZg4hFMD1vadrhKDTXLhUQdQfOfabCewHfOcEM2Y4D+d6fs8Mm59 kem3YODruvwKESmCA8yF0htaCcb9uaMFKOXEfGmzIuYY2U1qKDTpECYi8mPkp6MAZDx3 8Dex/E6G0AmfnGi4Hf/j8psBwxmJvKc0hpkXuPqBrFlguJx4MqxudzSOx//KQvjn/U8X 33Lho4ozufKGgEElgLHjElGAvQbiX7qmT+evW1Uyaccfz/pA6q27i4NESac+FqKO/yHn UvnA== 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=+FYWIIiU0pWapRN/M6RsTE3dosqtcEQvLWm9P4hcTVQ=; b=BV0DU7JgqUxQ0e7fnLqsssRXiMFN9hUYaMG6zv437F+qLwbL8EPJX2i+UVbJATC2GC TRQjMInhQYIQ7Ose+nMI9AxEx/Hv7CM4RjXqyL95oO7VgF3fhdZnuTCRaWb61znjENz3 jWeWjgFW1vp4F66zQaEOtpMPqxyJnC2VQV5Xy8RMlhd/s+XyznXAzayURbd+N/sT86UD I+kSJKD3Soo86IAfgwVORNDZOlvhEbmxpHVm3WGEw4s913Wgc6D2GlMeMsKkjMvzP/w3 vsijiFPFn6mYGUxUzgD3B9yDXtg8YifLMyUpe81EU+BRMBUoqZU/Jutj657/gJ3K2mjT IFXw== X-Gm-Message-State: AOAM5323n5jiPMp23pr91A7G9MpP5UOLqr+aSkCc3kB53ZsDi+h9lVFw +6jt5rXkctgEjpKq0UEe06ATEQvbQ0DpzA== X-Google-Smtp-Source: ABdhPJyI0PcrLxWEb17zluYpsZM9xIK8x/HPsdi25jEmeuk/dT/tBoOEZcIEpX0KUDww6per/i5yTg== X-Received: by 2002:a7b:cc10:: with SMTP id f16mr8430776wmh.24.1622203927600; Fri, 28 May 2021 05:12:07 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 29/31] receive-pack: convert receive hooks to hook.h Date: Fri, 28 May 2021 14:11:31 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Emily Shaffer By using the hook.h library to run receive hooks we get closer to deleting the hook functions in run-command.c 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 Fri May 28 12:11:32 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: 12286925 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,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 7603CC4708C for ; Fri, 28 May 2021 12:13:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5CAA9611BD for ; Fri, 28 May 2021 12:13:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236435AbhE1MP2 (ORCPT ); Fri, 28 May 2021 08:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236574AbhE1MOg (ORCPT ); Fri, 28 May 2021 08:14:36 -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 7AAD3C06135B for ; Fri, 28 May 2021 05:12:10 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id n4so3104167wrw.3 for ; Fri, 28 May 2021 05:12: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=9qIKEQmLNbOOLJEB4OKelsMD4Km+J6zbnIabi6bK3kY=; b=Lqn5naDmjwnErZ5c1Gnlp2IED7c4eJ2t1OJ8Xyifatgk9ObZenzeSow9b0R/CTRca2 zc9752k+X2QzKbpgxsGE5lWuWn9m+mSG8rL/Ybtqtj4Kn2/oNpQfVeLrFFlw5UGq6E3o UnuvM06weP+/pf+GpHmK+iAUVcvE+fENVmAoVHGm6GsQM0CYMEKkih1YPptdlikfP6Po gV8lzwK0JzIWSmL+Ory1YBv1mDux6E18f8qmZtO3Fpr5fp4pT0OsjFms4gPrUk8mOC8T JkrYf/utQ49X0OjkDftOGrUFmf1A0crYcmy23Fn2u3rqnELL1HZV5SuBTRx0MZyiQVZG w3Cw== 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=9qIKEQmLNbOOLJEB4OKelsMD4Km+J6zbnIabi6bK3kY=; b=e5HR9d0rJBZHSzgOuv9lJpp6ekEwqZ6VweJe77mSyyb98apkwCSXtCsPZRiw6yyccI L4G+tF8dvNTuoFoa/B8evyOcrGgkz4ekAP3py4gnoOXh9kV1wXOFBOgCqH6lUficyW69 nxmqCmGj6QFqlVTIc27oS5QS+sQo3mSkiQHYcBmu6R+OJn0kEZ+4hPGpgYOWfHqy2Pc5 u+3AAp7xv3z3nq77ieRPJW6M7+loDesc/5afnPlbs4r5wBYn6BCzgL6I2v+HfI/cNfSY eH1FmvL2e1QgcJ9bdedoEESJVOOgZHCbSdCMTGhy9S1uzZgX8L3IUEoYdZ5I1jKzV6kL HtUw== X-Gm-Message-State: AOAM531j66zV25fIfeYir6Bf3D5NFug8XoO2UZDM9dDI+kurtsTPW2ti r288k13WOyjyBZTqcNBX+K/LLzLl38pAtg== X-Google-Smtp-Source: ABdhPJwFsRBvaAo1iXS102YuBa2qsTMH1vzT8xkSUF8BwEyfz1Cb+a9hDUua3cu8WxMKQ0y1WXd49g== X-Received: by 2002:a5d:488b:: with SMTP id g11mr8369333wrq.317.1622203928474; Fri, 28 May 2021 05:12:08 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 30/31] hooks: fix a TOCTOU in "did we run a hook?" heuristic Date: Fri, 28 May 2021 14:11:32 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a race in code added in 680ee550d72 (commit: skip discarding the index if there is no pre-commit hook, 2017-08-14) by changing the hook.c API to optionally indicate whether or not the requested hook ran or not. This was suggested in the discussion around 680ee550d72[1]. Let's also change this for the pre-merge-commit hook, see 6098817fd7f (git-merge: honor pre-merge-commit hook, 2019-08-07) for the introduction of the 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 there purely for optimization purposes. 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 a9363b94065..2bbdfd6080d 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 Fri May 28 12:11:33 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: 12286927 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,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 6A9B8C47087 for ; Fri, 28 May 2021 12:13:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4C471611C9 for ; Fri, 28 May 2021 12:13:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236595AbhE1MPa (ORCPT ); Fri, 28 May 2021 08:15:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236203AbhE1MOj (ORCPT ); Fri, 28 May 2021 08:14:39 -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 6F13DC0613ED for ; Fri, 28 May 2021 05:12:11 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id m18so2028395wmq.0 for ; Fri, 28 May 2021 05:12:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SUjcSmLH6B5QoUWTi0RKrPJSBmEUM3ptY4RXdyddrmc=; b=J/UN0k50z0oSyTuUJEfsoZIleaWhHPfjQQCiS2KeI1iRjEKH3HlrmzdYb/kOLj7b9y vNN0wRdA4OXkiDu1RIZOCWsPtUbOBh4VrDyHlwPj56vWCbq5NCr36yinC6CRwN2bHLCP gAXXX+iwXRUa3v0C/5Fmqh80D8tIb9dr1oOuHDK+7shPiOtIcjqT+QJA/vi+gJsba5Ep gGTarzjhg/OWZkOZB4h3q0ftras9aL+rdr903aPYkYY44UJYRBzMAZUIQArLYoUfcRdE 04cfU1sZGPU3xTKBtV7LwQLuf/r8Pf1Y4vvGZyg8mfVg93aL0EiWEFKDyCbIwPRPLGar gVTg== 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=SUjcSmLH6B5QoUWTi0RKrPJSBmEUM3ptY4RXdyddrmc=; b=FszB2thh8sT3rxNMiJv+4D9vlZ2GMRlPhSbmzee5731LGYIV2IHJH+lhB8kNipr9Te 31QHuOYMut4hrOYRofQSfhDXplCOIea/HN+tCt/77SbPSh82eWCMAgBkDwHKYAIAWppN KViTllsDwthTzmQTM7TxATnvpdq/ZqhZJxLKB10hrW309hxYlCp6toeJXVXnF9ROkSJG svR5KZOXbDl7Sj35FxwfVGalcSw/Q+EEGXfsApBScODaRcZEtUffN7ID8fGN40HLTh5j ihq+xDzsQ6VzGZASOjZg/lHYghFYptv3Ogalt/BwkcVqcTDVIfDATYvYVKPA2SZW9/Wb 1jLg== X-Gm-Message-State: AOAM530IlY1kkyvQMPAeHx60CpOX0XzRePI2fFFufoe/X88s3iPGj5Ha BILs6aMTWDBOk4ZN0C+sdrYGOEME1yJvXw== X-Google-Smtp-Source: ABdhPJxHrdpZ3BwPlO0Rx5ZKP63qMKTYDN/LJDhRdsXK1226J3qFrlT8XOf3qTThNwEINGBdJfk5iQ== X-Received: by 2002:a1c:f016:: with SMTP id a22mr8502743wmb.65.1622203929729; Fri, 28 May 2021 05:12:09 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c15sm7372269wro.21.2021.05.28.05.12.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 May 2021 05:12: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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 31/31] hook-list.h: add a generated list of hooks, like config-list.h Date: Fri, 28 May 2021 14:11:33 +0200 Message-Id: X-Mailer: git-send-email 2.32.0.rc1.460.g26a014da44c In-Reply-To: References: <87lf80l1m6.fsf@evledraar.gmail.com> 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 | 20 ++++++++++++++++++++ hook.c | 22 ++++++++++++++++++++++ t/t1800-hook.sh | 14 +++++++++++--- 6 files changed, 73 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..25a7207b276 --- /dev/null +++ b/generate-hooklist.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +echo "/* Automatically generated by generate-hooklist.sh */" + +print_hook_list () { + cat <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