Skip to content

Commit 3e4141d

Browse files
apelissegitster
authored andcommitted
merge: Honor prepare-commit-msg return code
65969d4 (merge: honor prepare-commit-msg hook, 2011-02-14) tried to make "git commit" and "git merge" consistent, because a merge that required user assistance has to be concluded with "git commit", but back then only "git commit" triggered prepare-commit-msg hook. When it added a call to run the prepare-commit-msg hook, however, it forgot to check the exit code from the hook like "git commit" does, and ended up replacing one inconsistency with another. When prepare-commit-msg hook that is run from "git merge" exits with a non-zero status, abort the commit. Signed-off-by: Antoine Pelisse <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1599999 commit 3e4141d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

builtin/merge.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
903903
if (0 < option_edit)
904904
strbuf_add_lines(&msg, "# ", comment, strlen(comment));
905905
write_merge_msg(&msg);
906-
run_hook(get_index_file(), "prepare-commit-msg",
907-
git_path("MERGE_MSG"), "merge", NULL, NULL);
906+
if (run_hook(get_index_file(), "prepare-commit-msg",
907+
git_path("MERGE_MSG"), "merge", NULL, NULL))
908+
abort_commit(remoteheads, NULL);
908909
if (0 < option_edit) {
909910
if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
910911
abort_commit(remoteheads, NULL);

t/t7505-prepare-commit-msg-hook.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,19 @@ test_expect_success 'with failing hook (--no-verify)' '
167167
168168
'
169169

170+
test_expect_success 'with failing hook (merge)' '
171+
172+
git checkout -B other HEAD@{1} &&
173+
echo "more" >> file &&
174+
git add file &&
175+
rm -f "$HOOK" &&
176+
git commit -m other &&
177+
write_script "$HOOK" <<-EOF
178+
exit 1
179+
EOF
180+
git checkout - &&
181+
test_must_fail git merge other
182+
183+
'
170184

171185
test_done

0 commit comments

Comments
 (0)