From patchwork Thu Aug 1 22:20:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 11071409 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3CA5D13AC for ; Thu, 1 Aug 2019 22:20:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2BB0E28563 for ; Thu, 1 Aug 2019 22:20:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EA03285C6; Thu, 1 Aug 2019 22:20:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73D4B28563 for ; Thu, 1 Aug 2019 22:20:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732189AbfHAWUx (ORCPT ); Thu, 1 Aug 2019 18:20:53 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:50009 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729750AbfHAWUx (ORCPT ); Thu, 1 Aug 2019 18:20:53 -0400 Received: by mail-qk1-f201.google.com with SMTP id l14so62567838qke.16 for ; Thu, 01 Aug 2019 15:20:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=O4AvoCjuwA+WjTqL1DaSIUatCjugC4tSL0G9vqNzyYY=; b=FtFjmz8fCw/TFnMd03kVkuxjEqhZlPa8ZQSFeB+1sal5wFzVRHmTq+6cYmPBVkPxvS oieOzKfLVyhZKaCBXafEpUO1Mxvb4ZIMWUP0meTJoSbB7URKD16ek+LtePEUjqGHobtv vRyaWxWTw8BnIVIE0CYNNoVLiY9g3Uv9RPA+q7O4IOtkuY9mXuzHtaYLeW2tySyTtM3h p2Lp+TALEtV1lNk+/CYQe+ybjPEK8AdHbSd9vVhpwPaGWQFVNRoW+vu069mMg3/slj2Y 1gr+a6dE4o34es47LqRxEkSHtiSimuYai967BrKPaD2L9OtW20IclDr4FBfDquXJA6LJ +ong== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=O4AvoCjuwA+WjTqL1DaSIUatCjugC4tSL0G9vqNzyYY=; b=iBdE1v4Wl3xNWVmU43HwJ4TU5RkUMGrFIgQNWvoAZ/LMy0JuZ/eIHB0Cfkog0oZ/Nj 8nrSWvxLBXSxHeHrOvC4iz5WweKsFNQ7G4RbS0+ay61jU0saSVIUMQwG6FniF18gYWmW kbukzsmO/9aTpbkxnHc+lmgwOc0FhLsDal3+JtqLW7meVpV+pCNVhiuJDTt8H7f9AVkf jKR7brAA7YeRkEAIA6vfh05EXyc8jiFMHu8+Jip7MPmHXKQJxIz++OuYm4nLn8wHsLwM Kdx3mtGVsnR5xpTPOEhfW4z6G3rXAAc5n3l/OpnT27r1VNpwIZknJ49GdFrb3lxNUiAj QKVQ== X-Gm-Message-State: APjAAAW3bx4Y1nSMhi1NzRBrf84NnE5R5IKElwm8IYe0dh343rkYgORU EdLQJC+1zJ7M775dwhWWTE6Dbg/Z/ZS/2Y7MpsoAePfYfnF7wfxAC5gOT1iDRGtdgJ3nlKOpeAF nOdxCisPUD6UgsbLW86wN6llNPguP/u+2g99y5M9tjWpbM4wl6JM+OoGq8DJpO3g= X-Google-Smtp-Source: APXvYqxrRfJzP73OsiAmg9OIP0MXlHjATGSX7eSgW/CPR9KyEf7QSRNZixV6O5pGYPvu3JaKvk4ek4ObYF2VUg== X-Received: by 2002:ac8:929:: with SMTP id t38mr93398705qth.287.1564698051773; Thu, 01 Aug 2019 15:20:51 -0700 (PDT) Date: Thu, 1 Aug 2019 15:20:43 -0700 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH v3 1/4] t7503: verify proper hook execution From: Josh Steadmon To: git@vger.kernel.org Cc: gitster@pobox.com, git@grubix.eu, martin.agren@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP t7503 did not verify that the expected hooks actually ran during testing. Fix that by making the hook scripts write their $0 into a file so that we can compare actual execution vs. expected execution. While we're at it, do some test style cleanups, such as using write_script() and doing setup inside a test_expect_success block. Signed-off-by: Josh Steadmon --- t/t7503-pre-commit-hook.sh | 162 +++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 68 deletions(-) diff --git a/t/t7503-pre-commit-hook.sh b/t/t7503-pre-commit-hook.sh index 984889b39d..500bdd97c2 100755 --- a/t/t7503-pre-commit-hook.sh +++ b/t/t7503-pre-commit-hook.sh @@ -4,124 +4,149 @@ test_description='pre-commit hook' . ./test-lib.sh -test_expect_success 'with no hook' ' +HOOKDIR="$(git rev-parse --git-dir)/hooks" +PRECOMMIT="$HOOKDIR/pre-commit" + +# Prepare sample scripts that write their $0 to actual_hooks +test_expect_success 'sample script setup' ' + mkdir -p "$HOOKDIR" && + write_script "$HOOKDIR/success.sample" <<-\EOF && + echo $0 >>actual_hooks + exit 0 + EOF + write_script "$HOOKDIR/fail.sample" <<-\EOF && + echo $0 >>actual_hooks + exit 1 + EOF + write_script "$HOOKDIR/require-prefix.sample" <<-\EOF && + echo $0 >>actual_hooks + test $GIT_PREFIX = "success/" + EOF + write_script "$HOOKDIR/check-author.sample" <<-\EOF + echo $0 >>actual_hooks + test "$GIT_AUTHOR_NAME" = "New Author" && + test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com" + EOF +' - echo "foo" > file && +test_expect_success 'with no hook' ' + test_when_finished "rm -f expected_hooks actual_hooks" && + touch expected_hooks actual_hooks && + echo "foo" >file && git add file && - git commit -m "first" - + git commit -m "first" && + test_cmp expected_hooks actual_hooks ' test_expect_success '--no-verify with no hook' ' - - echo "bar" > file && + test_when_finished "rm -f expected_hooks actual_hooks" && + touch expected_hooks actual_hooks && + echo "bar" >file && git add file && - git commit --no-verify -m "bar" - + git commit --no-verify -m "bar" && + test_cmp expected_hooks actual_hooks ' -# now install hook that always succeeds -HOOKDIR="$(git rev-parse --git-dir)/hooks" -HOOK="$HOOKDIR/pre-commit" -mkdir -p "$HOOKDIR" -cat > "$HOOK" <> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && + ln -s "success.sample" "$PRECOMMIT" && + touch actual_hooks && + echo "$PRECOMMIT" >expected_hooks && + echo "more" >>file && git add file && - git commit -m "more" - + git commit -m "more" && + test_cmp expected_hooks actual_hooks ' test_expect_success '--no-verify with succeeding hook' ' - - echo "even more" >> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && + ln -s "success.sample" "$PRECOMMIT" && + touch expected_hooks actual_hooks && + echo "even more" >>file && git add file && - git commit --no-verify -m "even more" - + git commit --no-verify -m "even more" && + test_cmp expected_hooks actual_hooks ' -# now a hook that fails -cat > "$HOOK" <> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && + ln -s "fail.sample" "$PRECOMMIT" && + touch actual_hooks && + echo "$PRECOMMIT" >expected_hooks && + echo "another" >>file && git add file && - test_must_fail git commit -m "another" - + test_must_fail git commit -m "another" && + test_cmp expected_hooks actual_hooks ' test_expect_success '--no-verify with failing hook' ' - - echo "stuff" >> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && + ln -s "fail.sample" "$PRECOMMIT" && + touch expected_hooks actual_hooks && + echo "stuff" >>file && git add file && - git commit --no-verify -m "stuff" - + git commit --no-verify -m "stuff" && + test_cmp expected_hooks actual_hooks ' -chmod -x "$HOOK" test_expect_success POSIXPERM 'with non-executable hook' ' - - echo "content" >> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks; chmod +x \"$HOOKDIR/fail.sample\"" && + ln -s "fail.sample" "$PRECOMMIT" && + chmod -x "$HOOKDIR/fail.sample" && + touch expected_hooks actual_hooks && + echo "content" >>file && git add file && - git commit -m "content" - + git commit -m "content" && + test_cmp expected_hooks actual_hooks ' test_expect_success POSIXPERM '--no-verify with non-executable hook' ' - - echo "more content" >> file && + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks; chmod +x \"$HOOKDIR/fail.sample\"" && + ln -s "fail.sample" "$PRECOMMIT" && + chmod -x "$HOOKDIR/fail.sample" && + touch expected_hooks actual_hooks && + echo "more content" >>file && git add file && - git commit --no-verify -m "more content" - + git commit --no-verify -m "more content" && + test_cmp expected_hooks actual_hooks ' -chmod +x "$HOOK" - -# a hook that checks $GIT_PREFIX and succeeds inside the -# success/ subdirectory only -cat > "$HOOK" <> file && + test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" && + ln -s "require-prefix.sample" "$PRECOMMIT" && + echo "$PRECOMMIT" >expected_hooks && + echo "more content" >>file && git add file && mkdir success && ( cd success && git commit -m "hook requires GIT_PREFIX = success/" ) && - rmdir success + test_cmp expected_hooks actual_hooks ' test_expect_success 'with failing hook requiring GIT_PREFIX' ' - - echo "more content" >> file && + test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" && + ln -s "require-prefix.sample" "$PRECOMMIT" && + echo "$PRECOMMIT" >expected_hooks && + echo "more content" >>file && git add file && mkdir fail && ( cd fail && test_must_fail git commit -m "hook must fail" ) && - rmdir fail && - git checkout -- file + git checkout -- file && + test_cmp expected_hooks actual_hooks ' test_expect_success 'check the author in hook' ' - write_script "$HOOK" <<-\EOF && - test "$GIT_AUTHOR_NAME" = "New Author" && - test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com" + test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && + ln -s "check-author.sample" "$PRECOMMIT" && + cat >expected_hooks <<-EOF && + $PRECOMMIT + $PRECOMMIT + $PRECOMMIT EOF test_must_fail git commit --allow-empty -m "by a.u.thor" && ( @@ -133,7 +158,8 @@ test_expect_success 'check the author in hook' ' ) && git commit --author="New Author " \ --allow-empty -m "by new.author via command line" && - git show -s + git show -s && + test_cmp expected_hooks actual_hooks ' test_done From patchwork Thu Aug 1 22:20:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 11071411 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 608B113AC for ; Thu, 1 Aug 2019 22:20:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5193228563 for ; Thu, 1 Aug 2019 22:20:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 45FCF285C6; Thu, 1 Aug 2019 22:20:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D27D028563 for ; Thu, 1 Aug 2019 22:20:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389437AbfHAWUz (ORCPT ); Thu, 1 Aug 2019 18:20:55 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:40784 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729750AbfHAWUz (ORCPT ); Thu, 1 Aug 2019 18:20:55 -0400 Received: by mail-pf1-f201.google.com with SMTP id z1so46656599pfb.7 for ; Thu, 01 Aug 2019 15:20:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lGUSbt+34syfPccHw0l4DrGv0PoqDbOxTp+Fh1W7Cnk=; b=HkBzfvYO+5zKy/ZDraLT4HPpc47dFFysgGVGl2ED2hXac9UTWJoUQvYP8GyoEVaYzU NJCIergcUyDL/R/K6YZl6l3+jLrqeDqO9wlTslA9NtpGQtPRPg5xHacjsUqlKYtIqBOJ n36q+KP3vAtmVR4wHr+6nec2zzmNxU1U01NQGsq+lBbumavXaPyJ/4CC9gGE4zcdINM0 WP682yhdlrVa4rGNJGyf5u4w8Ge8FftTHh6pMqnn+9iImTuKxP632SajE4enyWd70Imh SXtMr+PwCtItjTOPJQUbwxC4x0xFhJCbWpKP2N4RnguR61Lr9XWfsqOVYnIm4tYXTsbo Kajg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lGUSbt+34syfPccHw0l4DrGv0PoqDbOxTp+Fh1W7Cnk=; b=OomSpry3LXW5FNjkn/3eUeX5ClMVUm5lxuzMPglXYC5UrwJXl65yFrWsSwwzkuGguK 4FGuACamzgCL3UT/giIw0mBpMuuuFgFpC98e2S9cqOln3p7IjhIz1HC723fmz1uKgJBF GljOfHBu54nr9hNf7AJupUsZuaNb5vYtff8ejRDcU8E96o0Ny3ST+SYWISr/kP1DjRCi TxxSixfXAsOdaGCvJROZ30hLqcE3g9ZB7MRQDfNLkIrvlIMa8GPz3qMHrdc9rqUMGDcc ZpvoF30LrAZmA1j99y8aX6M66rcx1Z93KC/A7X+XY9HbybXvVNYeFrjzFoXCA2hB5x+b 69VA== X-Gm-Message-State: APjAAAXJw9Mk+p9K5wrH8W1rbUyuAFeI/PNOml5u4AXHsCW6J5/pJR1n MmcDGWbslGTdfnbO44BcCptPc3wLvugFmQRrFY8vPUZ52V+MpzI65NbaH+3dWcw5WgSL0/ZRBGC LrjfSBHnSp/P4ie75gzLnbsSKdx/4/tJwgS/lyEvPyihG0AI6NunuEfgAv0M0QGE= X-Google-Smtp-Source: APXvYqwsAn/woJg2TmZX2k8/YWs2/q3PLf72iHDNdB5gwg2zy246ASd/+Eyp/6JrKbPo6sBE86JDoucvcF98YQ== X-Received: by 2002:a63:c84d:: with SMTP id l13mr116395435pgi.154.1564698054229; Thu, 01 Aug 2019 15:20:54 -0700 (PDT) Date: Thu, 1 Aug 2019 15:20:44 -0700 In-Reply-To: Message-Id: <89ddbf410f71bbd0dc556297913561c8492a4084.1564695893.git.steadmon@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH v3 2/4] merge: do no-verify like commit From: Josh Steadmon To: git@vger.kernel.org Cc: gitster@pobox.com, git@grubix.eu, martin.agren@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP f8b863598c ("builtin/merge: honor commit-msg hook for merges", 2017-09-07) introduced the no-verify flag to merge for bypassing the commit-msg hook, though in a different way from the implementation in commit.c. Change the implementation in merge.c to be the same as in commit.c so that both do the same in the same way. This also changes the output of "git merge --help" to be more clear that the hook return code is respected by default. [js: * reworded commit message * squashed documentation changes from original series' patch 3/4 ] Signed-off-by: Michael J Gruber Signed-off-by: Josh Steadmon --- Documentation/git-merge.txt | 2 +- Documentation/merge-options.txt | 4 ++++ builtin/merge.c | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Documentation/git-merge.txt b/Documentation/git-merge.txt index 01fd52dc70..092529c619 100644 --- a/Documentation/git-merge.txt +++ b/Documentation/git-merge.txt @@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git merge' [-n] [--stat] [--no-commit] [--squash] [--[no-]edit] - [-s ] [-X ] [-S[]] + [--no-verify] [-s ] [-X ] [-S[]] [--[no-]allow-unrelated-histories] [--[no-]rerere-autoupdate] [-m ] [-F ] [...] 'git merge' (--continue | --abort | --quit) diff --git a/Documentation/merge-options.txt b/Documentation/merge-options.txt index 79a00d2a4a..d6a9f4b96f 100644 --- a/Documentation/merge-options.txt +++ b/Documentation/merge-options.txt @@ -105,6 +105,10 @@ option can be used to override --squash. + With --squash, --commit is not allowed, and will fail. +--no-verify:: + This option bypasses the pre-merge and commit-msg hooks. + See also linkgit:githooks[5]. + -s :: --strategy=:: Use the given merge strategy; can be supplied more than diff --git a/builtin/merge.c b/builtin/merge.c index e2ccbc44e2..4425a7a12e 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -81,7 +81,7 @@ static int show_progress = -1; static int default_to_upstream = 1; static int signoff; static const char *sign_commit; -static int verify_msg = 1; +static int no_verify; static struct strategy all_strategy[] = { { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL }, @@ -287,7 +287,7 @@ static struct option builtin_merge_options[] = { N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")), - OPT_BOOL(0, "verify", &verify_msg, N_("verify commit-msg hook")), + OPT_BOOL(0, "no-verify", &no_verify, N_("bypass commit-msg hook")), OPT_END() }; @@ -842,7 +842,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) abort_commit(remoteheads, NULL); } - if (verify_msg && run_commit_hook(0 < option_edit, get_index_file(), + if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(), "commit-msg", git_path_merge_msg(the_repository), NULL)) abort_commit(remoteheads, NULL); From patchwork Thu Aug 1 22:20:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 11071413 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F048013B1 for ; Thu, 1 Aug 2019 22:20:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E0A5B28563 for ; Thu, 1 Aug 2019 22:20:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D4F0C285C6; Thu, 1 Aug 2019 22:20:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D48328563 for ; Thu, 1 Aug 2019 22:20:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389439AbfHAWU6 (ORCPT ); Thu, 1 Aug 2019 18:20:58 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:49361 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729750AbfHAWU6 (ORCPT ); Thu, 1 Aug 2019 18:20:58 -0400 Received: by mail-qt1-f201.google.com with SMTP id l16so59343743qtq.16 for ; Thu, 01 Aug 2019 15:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=HZBcqrPynoQhiVNi5IwOba8mqaeypL2a5fyqZP55ZLQ=; b=gUO1cQ/Rj11GyJqjm+0Ajn1gy5GHqT9LI0C3PzjBVZS3HADPoZMjHkpUFRCuIEDgGG zE7HBCi1me2WKWHaHXXgXbOB/QBxVp8wZfTRfn8ibM8YRZlGLTC2ahOZ7BJfUIGsRQ34 D9jeDWbAD4dHSCUQvrPE3MF8pwX1wMFbC57wJR5hRbh1tlsKNRCv92eiKaiTRkjJ2Wyv 1wcjIih+phlGXuVX6bmOjQTf2uvLq1S0IDkzr8MEN8wmGvjSVxFP6pt4hNXOXjZNkHad /xNSAVrAtzKoJP2BTgW08wyKGm6Mj0VzefPl/eUNFCZIEPBQ9fymCMVwmYNjqRNKs6wc 9Crw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HZBcqrPynoQhiVNi5IwOba8mqaeypL2a5fyqZP55ZLQ=; b=ko8QP8XYlc2+kzFEIK2txMNHe0B0OmzAlB+90UiUILjxSrn91Qavt3vZ1MsTTi8pb/ yW3nYw69OTJycJz2sCQZu7b0BsU4ARDbuHHd5rdO+sf9/VONv8kvvMSSm9Qcminua2xt TxDCe6wQ6hM6s3o2ffCBBZwrun9GiRZCK6DnAEWjS7hPbyLpadcexd3itl7kzRIed5Lb MbP+HWykPoSkl00d8Wh0uWJjbTr6KfdyIAMqbxQrMlgdICrOblVXTbX8LB8GOYYEGQY3 eTR5Y86Pa03nV8zH4B8XqR2Cxi8v1alv4IF4Jq3Ofj7O3w0vocx2Qx5SY6UE8CuUwHaz b58A== X-Gm-Message-State: APjAAAUIQcELtNUIIy7FWSWZtk0GhCGRIj4+nafUImZZkDyMqw71y2zJ suJwW1St2tPsiAWiwWsOMg2dCD3xhL5RuLCkIN41J+W8aI1CcDLmUt6Z/Vjuk/vLmY9tvn7Fzt9 it/WKh8I9ps4wlWRxXCabejv0xo0OqPohDjjIC4Fsras7bRy2iuA25mtNdDQw9uw= X-Google-Smtp-Source: APXvYqzlpeoOY8oI0spBXG3XF8CaFGn+EmpKfgtlHCUJq8m4NrFH41gbWClv4azMzZxRDRH+0u+B2wQgYI5X3Q== X-Received: by 2002:a0c:b036:: with SMTP id k51mr96622360qvc.103.1564698056999; Thu, 01 Aug 2019 15:20:56 -0700 (PDT) Date: Thu, 1 Aug 2019 15:20:45 -0700 In-Reply-To: Message-Id: <61b989ff16eadfd0508e10f71c9b318eb15ce2a7.1564695893.git.steadmon@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH v3 3/4] git-merge: honor pre-merge-commit hook From: Josh Steadmon To: git@vger.kernel.org Cc: gitster@pobox.com, git@grubix.eu, martin.agren@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP git-merge does not honor the pre-commit hook when doing automatic merge commits, and for compatibility reasons this is going to stay. Introduce a pre-merge-commit hook which is called for an automatic merge commit just like pre-commit is called for a non-automatic merge commit (or any other commit). [js: * renamed hook from "pre-merge" to "pre-merge-commit" * only discard the index if the hook is actually present * clarified that hook should write messages to stderr * squashed test changes from the original series' patch 4/4 * modified tests to follow new pattern from this series' patch 1/4 * reworded commit message ] Signed-off-by: Michael J Gruber Signed-off-by: Josh Steadmon --- Documentation/githooks.txt | 7 +++ builtin/merge.c | 12 +++++ ...-pre-commit-and-pre-merge-commit-hooks.sh} | 45 ++++++++++++++++++- templates/hooks--pre-merge-commit.sample | 13 ++++++ 4 files changed, 76 insertions(+), 1 deletion(-) rename t/{t7503-pre-commit-hook.sh => t7503-pre-commit-and-pre-merge-commit-hooks.sh} (78%) create mode 100755 templates/hooks--pre-merge-commit.sample diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 82cd573776..7c4c994858 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -103,6 +103,13 @@ The default 'pre-commit' hook, when enabled--and with the `hooks.allownonascii` config option unset or set to false--prevents the use of non-ASCII filenames. +pre-merge-commit +~~~~~~~~~~~~~~~~ + +This hook is invoked by 'git merge' when doing an automatic merge +commit; it is equivalent to 'pre-commit' for a non-automatic commit +for a merge. + prepare-commit-msg ~~~~~~~~~~~~~~~~~~ diff --git a/builtin/merge.c b/builtin/merge.c index 4425a7a12e..bf0ae68c40 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -816,6 +816,18 @@ static void write_merge_heads(struct commit_list *); static void prepare_to_commit(struct commit_list *remoteheads) { struct strbuf msg = STRBUF_INIT; + const char *index_file = get_index_file(); + + if (run_commit_hook(0 < option_edit, index_file, "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 + * the editor and after we invoke run_status above. + */ + if (find_hook("pre-merge-commit")) + discard_cache(); + read_cache_from(index_file); strbuf_addbuf(&msg, &merge_msg); if (squash) BUG("the control must not reach here under --squash"); diff --git a/t/t7503-pre-commit-hook.sh b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh similarity index 78% rename from t/t7503-pre-commit-hook.sh rename to t/t7503-pre-commit-and-pre-merge-commit-hooks.sh index 500bdd97c2..040dfa0175 100755 --- a/t/t7503-pre-commit-hook.sh +++ b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh @@ -1,11 +1,12 @@ #!/bin/sh -test_description='pre-commit hook' +test_description='pre-commit and pre-merge-commit hooks' . ./test-lib.sh HOOKDIR="$(git rev-parse --git-dir)/hooks" PRECOMMIT="$HOOKDIR/pre-commit" +PREMERGE="$HOOKDIR/pre-merge-commit" # Prepare sample scripts that write their $0 to actual_hooks test_expect_success 'sample script setup' ' @@ -29,6 +30,17 @@ test_expect_success 'sample script setup' ' EOF ' +test_expect_success 'root commit' ' + echo "root" > file && + git add file && + git commit -m "zeroth" && + git checkout -b side && + echo "foo" > foo && + git add foo && + git commit -m "make it non-ff" && + git checkout master +' + test_expect_success 'with no hook' ' test_when_finished "rm -f expected_hooks actual_hooks" && touch expected_hooks actual_hooks && @@ -38,6 +50,15 @@ test_expect_success 'with no hook' ' test_cmp expected_hooks actual_hooks ' +test_expect_success 'with no hook (merge)' ' + test_when_finished "rm -f expected_hooks actual_hooks" && + touch expected_hooks actual_hooks && + git checkout side && + git merge -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success '--no-verify with no hook' ' test_when_finished "rm -f expected_hooks actual_hooks" && touch expected_hooks actual_hooks && @@ -58,6 +79,17 @@ test_expect_success 'with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +test_expect_success 'with succeeding hook (merge)' ' + test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && + ln -s "success.sample" "$PREMERGE" && + touch actual_hooks && + echo "$PREMERGE" >expected_hooks && + git checkout side && + git merge -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success '--no-verify with succeeding hook' ' test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && ln -s "success.sample" "$PRECOMMIT" && @@ -89,6 +121,17 @@ test_expect_success '--no-verify with failing hook' ' test_cmp expected_hooks actual_hooks ' +test_expect_success 'with failing hook (merge)' ' + test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && + ln -s "fail.sample" "$PREMERGE" && + touch actual_hooks && + echo "$PREMERGE" >expected_hooks && + git checkout side && + test_must_fail git merge -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success POSIXPERM 'with non-executable hook' ' test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks; chmod +x \"$HOOKDIR/fail.sample\"" && ln -s "fail.sample" "$PRECOMMIT" && diff --git a/templates/hooks--pre-merge-commit.sample b/templates/hooks--pre-merge-commit.sample new file mode 100755 index 0000000000..399eab1924 --- /dev/null +++ b/templates/hooks--pre-merge-commit.sample @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +: From patchwork Thu Aug 1 22:20:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 11071415 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C7AE513B1 for ; Thu, 1 Aug 2019 22:21:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6E7528563 for ; Thu, 1 Aug 2019 22:21:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AB6AA285C6; Thu, 1 Aug 2019 22:21:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35DFF28563 for ; Thu, 1 Aug 2019 22:21:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389442AbfHAWVB (ORCPT ); Thu, 1 Aug 2019 18:21:01 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:34162 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728326AbfHAWVB (ORCPT ); Thu, 1 Aug 2019 18:21:01 -0400 Received: by mail-pg1-f201.google.com with SMTP id x19so46079301pgx.1 for ; Thu, 01 Aug 2019 15:21:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=x9lWRsKnrkZqQug0KWm6apMHUJA7iiieKmve2fqIzc8=; b=h37MskhJAQpBRUrBAjR6oQuYzxVrDp4y24Dqr9zmho2Sa+bJyX2kVY1ryPozXYvEjw OeMiSgAOg751GkbtfOXn18plOKz3h74TazNhqXROcSuvVDtgyNh8MjIzQpdGNEVWqsII sLDyrRS7uHhmqU2BwqqKoGnvxxhD/Kv+ALjGJNBmkcwRop0BpJd68rWBoUwlDaP3RcM3 vgNygYfscMdj2HjrM4vKo0+wUkofELAMkY93MxrsDL6p7zlG997iHitBlbqjzzO2I4p0 cGuhM6CxbcRNjRiRB3PE+jO7rF763Fy4vyPvBc6D1VA70b4v9ji9Cx5lr2cwqNhx2OJ4 WkPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=x9lWRsKnrkZqQug0KWm6apMHUJA7iiieKmve2fqIzc8=; b=s9n6sTBNiIXIcN5ruVgS10p2FvZNHZ3wXwYYbUF5XThoum9zsGrLpTC3Mptkj09R6g sEeGx+W7ONDM8r/NOgrKLi4ZatNeDSXFrLBvwEyDRHs0L6Q7x4t87K3SgwU7hUiXNIoD +csw868tDERmhTJ42NolzIIh3Gv2ReQTZZ7vGdYqICnW42uSWEc1Nr9QApAeEm8XWJYD Gsz8b/p5Xsec4PfJdzncaARLeUNmesDAsDHm12pXK6m5PBYow7vnEBnYBZLJyMVO/cWx MA8PPax2lCp+sIiQCcAzcgrVDUh4A4/BuGWYcrHvNsCrrurd+mQfqdxWcFxCLViMOV5Q VPgw== X-Gm-Message-State: APjAAAVlo2QRmWmRB1xxKurm0brNa9vzy7//y3vDKea3WI/54KzJatsZ RgPb3gQ6ha4epCiBqXqM9sgdm+etjMEo1bM/0YIOyCiL2hdiFdLen6mviR99iwhher//6vEn4+f 752L+htzkhtup/U87Tyzz1+7ffsrSKQIp7JcLY4AjhnkTmJaVx4kB2+zqI8kZcaw= X-Google-Smtp-Source: APXvYqw0fMouZ4MkBuQj2WSAFShozCrMM8M52m2RsRzKtXA8WZshD9gBPxjZRLQvjFri2+p6S9+PuV9+fR3COw== X-Received: by 2002:a63:9e43:: with SMTP id r3mr97435461pgo.148.1564698059441; Thu, 01 Aug 2019 15:20:59 -0700 (PDT) Date: Thu, 1 Aug 2019 15:20:46 -0700 In-Reply-To: Message-Id: <45828c56fcd672193096ad767eab24730a0e47ec.1564695893.git.steadmon@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH v3 4/4] merge: --no-verify to bypass pre-merge-commit hook From: Josh Steadmon To: git@vger.kernel.org Cc: gitster@pobox.com, git@grubix.eu, martin.agren@gmail.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Analogous to commit, introduce a '--no-verify' option which bypasses the pre-merge-commit hook. The shorthand '-n' is taken by '--no-stat' already. [js: * reworded commit message to reflect current state of --no-stat flag and new hook name * fixed flag documentation to reflect new hook name * cleaned up trailing whitespace * squashed test changes from the original series' patch 4/4 * modified tests to follow pattern from this series' patch 1/4 ] Signed-off-by: Michael J Gruber Signed-off-by: Josh Steadmon --- Documentation/githooks.txt | 2 +- builtin/merge.c | 4 +-- ...3-pre-commit-and-pre-merge-commit-hooks.sh | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index 7c4c994858..a934553509 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -108,7 +108,7 @@ pre-merge-commit This hook is invoked by 'git merge' when doing an automatic merge commit; it is equivalent to 'pre-commit' for a non-automatic commit -for a merge. +for a merge, and can be bypassed with the `--no-verify` option. prepare-commit-msg ~~~~~~~~~~~~~~~~~~ diff --git a/builtin/merge.c b/builtin/merge.c index bf0ae68c40..c9746e37b8 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -287,7 +287,7 @@ static struct option builtin_merge_options[] = { N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")), - OPT_BOOL(0, "no-verify", &no_verify, N_("bypass commit-msg hook")), + OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")), OPT_END() }; @@ -818,7 +818,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) struct strbuf msg = STRBUF_INIT; const char *index_file = get_index_file(); - if (run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL)) + if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL)) abort_commit(remoteheads, NULL); /* * Re-read the index as pre-merge-commit hook could have updated it, diff --git a/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh index 040dfa0175..86a375ab3e 100755 --- a/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +++ b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh @@ -68,6 +68,15 @@ test_expect_success '--no-verify with no hook' ' test_cmp expected_hooks actual_hooks ' +test_expect_success '--no-verify with no hook (merge)' ' + test_when_finished "rm -f expected_hooks actual_hooks" && + touch expected_hooks actual_hooks && + git checkout side && + git merge --no-verify -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with succeeding hook' ' test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && ln -s "success.sample" "$PRECOMMIT" && @@ -100,6 +109,16 @@ test_expect_success '--no-verify with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +test_expect_success '--no-verify with succeeding hook (merge)' ' + test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && + ln -s "success.sample" "$PREMERGE" && + touch expected_hooks actual_hooks && + git checkout side && + git merge --no-verify -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with failing hook' ' test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && ln -s "fail.sample" "$PRECOMMIT" && @@ -132,6 +151,16 @@ test_expect_success 'with failing hook (merge)' ' test_cmp expected_hooks actual_hooks ' +test_expect_success '--no-verify with failing hook (merge)' ' + test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && + ln -s "fail.sample" "$PREMERGE" && + touch expected_hooks actual_hooks && + git checkout side && + git merge --no-verify -m "merge master" master && + git checkout master && + test_cmp expected_hooks actual_hooks +' + test_expect_success POSIXPERM 'with non-executable hook' ' test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks; chmod +x \"$HOOKDIR/fail.sample\"" && ln -s "fail.sample" "$PRECOMMIT" &&