[PATCH] merge: Honor prepare-commit-msg return code

Subsystems: the rest

DORMANTno replies

2 messages, 1 author, 2016-06-15 · open the first message on its own page

[PATCH] merge: Honor prepare-commit-msg return code

From: Antoine Pelisse <hidden>
Date: 2016-06-15 22:55:38

prepare-commit-msg hook is run when committing to prepare the log
message. If the exit-status is non-zero, the commit should be aborted.

While the script is run before committing a successful merge, the
exit-status is ignored and a non-zero exit doesn't abort the commit.

Abort the commit if prepare-commit-msg returns with a non-zero status
when committing a successful merge.

Signed-off-by: Antoine Pelisse <redacted>
---
 builtin/merge.c                    |  5 +++--
 t/t7505-prepare-commit-msg-hook.sh | 13 +++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index a96e8ea..3a31c4b 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -800,8 +800,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	if (0 < option_edit)
 		strbuf_add_lines(&msg, "# ", comment, strlen(comment));
 	write_merge_msg(&msg);
-	run_hook(get_index_file(), "prepare-commit-msg",
-		 git_path("MERGE_MSG"), "merge", NULL, NULL);
+	if (run_hook(get_index_file(), "prepare-commit-msg",
+		     git_path("MERGE_MSG"), "merge", NULL, NULL))
+		abort_commit(remoteheads, NULL);
 	if (0 < option_edit) {
 		if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
 			abort_commit(remoteheads, NULL);
diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh
index 5b4b694..bc497bc 100755
--- a/t/t7505-prepare-commit-msg-hook.sh
+++ b/t/t7505-prepare-commit-msg-hook.sh
@@ -167,5 +167,18 @@ test_expect_success 'with failing hook (--no-verify)' '
 
 '
 
+test_expect_success 'with failing hook (merge)' '
+
+	git checkout -B other HEAD@{1} &&
+	echo "more" >> file &&
+	git add file &&
+	chmod -x $HOOK &&
+	git commit -m other &&
+	chmod +x $HOOK &&
+	git checkout - &&
+	head=`git rev-parse HEAD` &&
+	test_must_fail git merge other
+
+'
 
 test_done
-- 
1.8.1.rc3.27.g3b73c7d.dirty

Re: [PATCH] merge: Honor prepare-commit-msg return code

From: Antoine Pelisse <hidden>
Date: 2016-06-15 22:55:38

On Wed, Jan 2, 2013 at 7:42 PM, Antoine Pelisse [off-list ref] wrote:
quoted hunk
prepare-commit-msg hook is run when committing to prepare the log
message. If the exit-status is non-zero, the commit should be aborted.

While the script is run before committing a successful merge, the
exit-status is ignored and a non-zero exit doesn't abort the commit.

Abort the commit if prepare-commit-msg returns with a non-zero status
when committing a successful merge.

Signed-off-by: Antoine Pelisse <redacted>
---
 builtin/merge.c                    |  5 +++--
 t/t7505-prepare-commit-msg-hook.sh | 13 +++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index a96e8ea..3a31c4b 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -800,8 +800,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
        if (0 < option_edit)
                strbuf_add_lines(&msg, "# ", comment, strlen(comment));
        write_merge_msg(&msg);
-       run_hook(get_index_file(), "prepare-commit-msg",
-                git_path("MERGE_MSG"), "merge", NULL, NULL);
+       if (run_hook(get_index_file(), "prepare-commit-msg",
+                    git_path("MERGE_MSG"), "merge", NULL, NULL))
+               abort_commit(remoteheads, NULL);
        if (0 < option_edit) {
                if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
                        abort_commit(remoteheads, NULL);
diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh
index 5b4b694..bc497bc 100755
--- a/t/t7505-prepare-commit-msg-hook.sh
+++ b/t/t7505-prepare-commit-msg-hook.sh
@@ -167,5 +167,18 @@ test_expect_success 'with failing hook (--no-verify)' '

 '

+test_expect_success 'with failing hook (merge)' '
+
+       git checkout -B other HEAD@{1} &&
+       echo "more" >> file &&
+       git add file &&
+       chmod -x $HOOK &&
+       git commit -m other &&
+       chmod +x $HOOK &&
+       git checkout - &&
+       head=`git rev-parse HEAD` &&
The line above is useless ... Sorry about the noise.
+       test_must_fail git merge other
+
+'

 test_done
--
1.8.1.rc3.27.g3b73c7d.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help