sequencer: fix --allow-empty-message behavior, make it smarter

In commit b00bf1c9a8dd ("git-rebase: make --allow-empty-message the default", 2018-06-27), several arguments were given for transplanting empty commits without halting and asking the user for confirmation on each commit. These arguments were incomplete because the logic clearly assumed the only cases under consideration were transplanting of commits with empty messages (see the comment about "There are two sources for commits with empty messages). It didn't discuss or even consider rewords, squashes, etc. where the user is explicitly asked for a new commit message and provides an empty one. (My bad, I totally should have thought about that at the time, but just didn't.) Rewords and squashes are significantly different, though, as described by SZEDER: Let's suppose you start an interactive rebase, choose a commit to squash, save the instruction sheet, rebase fires up your editor, and then you notice that you mistakenly chose the wrong commit to squash. What do you do, how do you abort? Before [that commit] you could clear the commit message, exit the editor, and then rebase would say "Aborting commit due to empty commit message.", and you get to run 'git rebase --abort', and start over. But [since that commit, ...] saving the commit message as is would let rebase continue and create a bunch of unnecessary objects, and then you would have to use the reflog to return to the pre-rebase state. Also, he states: The instructions in the commit message template, which is shown for 'reword' and 'squash', too, still say... # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. These are sound arguments that when editing commit messages during a sequencer operation, that if the commit message is empty then the operation should halt and ask the user to correct. The arguments in commit b00bf1c9a8dd (referenced above) still apply when transplanting previously created commits with empty commit messages, so the sequencer should not halt for those. Furthermore, all rationale so far applies equally for cherry-pick as for rebase. Therefore, make the code default to --allow-empty-message when transplanting an existing commit, and to default to halting when the user is asked to edit a commit message and provides an empty one -- for both rebase and cherry-pick. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Sep 12, 2018 at 14:18 UTC a3ec9eaf3877fbe1048f5bda37063d6d4eaf1c54
4 files changed +14 -17
sequencer.c
+2 -2
@@ -858,7 +858,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
858 if ((flags & ALLOW_EMPTY))
859 argv_array_push(&cmd.args, "--allow-empty");
860
861 - if (opts->allow_empty_message)
861 + if (!(flags & EDIT_MSG))
862 argv_array_push(&cmd.args, "--allow-empty-message");
863
864 if (cmd.err == -1) {
@@ -1272,7 +1272,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
1272
1273 if (cleanup != COMMIT_MSG_CLEANUP_NONE)
1274 strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
1275 - if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) {
1275 + if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
1276 res = 1; /* run 'git commit' to display error message */
1277 goto out;
1278 }
t/t3404-rebase-interactive.sh
+3 -4
@@ -553,16 +553,15 @@ test_expect_success '--continue tries to commit, even for "edit"' '
553 '
554
555 test_expect_success 'aborted --continue does not squash commits after "edit"' '
556 - test_when_finished "git rebase --abort" &&
556 old=$(git rev-parse HEAD) &&
557 test_tick &&
558 set_fake_editor &&
559 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
560 echo "edited again" > file7 &&
561 git add file7 &&
563 - echo all the things >>conflict &&
564 - test_must_fail git rebase --continue &&
565 - test $old = $(git rev-parse HEAD)
562 + test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
563 + test $old = $(git rev-parse HEAD) &&
564 + git rebase --abort
565 '
566
567 test_expect_success 'auto-amend only edited commits after "edit"' '
t/t3405-rebase-malformed.sh
+1 -1
@@ -83,7 +83,7 @@ test_expect_success 'rebase -m commit with empty message' '
83 test_expect_success 'rebase -i commit with empty message' '
84 git checkout diff-in-message &&
85 set_fake_editor &&
86 - env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
86 + test_must_fail env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
87 git rebase -i HEAD^
88 '
89
t/t3505-cherry-pick-empty.sh
+8 -10
@@ -11,17 +11,14 @@ test_expect_success setup '
11 test_tick &&
12 git commit -m "first" &&
13
14 - git checkout -b empty-branch &&
15 - test_tick &&
16 - git commit --allow-empty -m "empty" &&
17 -
14 + git checkout -b empty-message-branch &&
15 echo third >> file1 &&
16 git add file1 &&
17 test_tick &&
18 git commit --allow-empty-message -m "" &&
19
20 git checkout master &&
24 - git checkout -b empty-branch2 &&
21 + git checkout -b empty-change-branch &&
22 test_tick &&
23 git commit --allow-empty -m "empty"
24
@@ -29,7 +26,7 @@ test_expect_success setup '
26
27 test_expect_success 'cherry-pick an empty commit' '
28 git checkout master &&
32 - test_expect_code 1 git cherry-pick empty-branch^
29 + test_expect_code 1 git cherry-pick empty-change-branch
30 '
31
32 test_expect_success 'index lockfile was removed' '
@@ -37,8 +34,9 @@ test_expect_success 'index lockfile was removed' '
34 '
35
36 test_expect_success 'cherry-pick a commit with an empty message' '
37 + test_when_finished "git reset --hard empty-message-branch~1" &&
38 git checkout master &&
41 - test_expect_code 1 git cherry-pick empty-branch
39 + git cherry-pick empty-message-branch
40 '
41
42 test_expect_success 'index lockfile was removed' '
@@ -47,7 +45,7 @@ test_expect_success 'index lockfile was removed' '
45
46 test_expect_success 'cherry-pick a commit with an empty message with --allow-empty-message' '
47 git checkout -f master &&
50 - git cherry-pick --allow-empty-message empty-branch
48 + git cherry-pick --allow-empty-message empty-message-branch
49 '
50
51 test_expect_success 'cherry pick an empty non-ff commit without --allow-empty' '
@@ -55,12 +53,12 @@ test_expect_success 'cherry pick an empty non-ff commit without --allow-empty' '
53 echo fourth >>file2 &&
54 git add file2 &&
55 git commit -m "fourth" &&
58 - test_must_fail git cherry-pick empty-branch2
56 + test_must_fail git cherry-pick empty-change-branch
57 '
58
59 test_expect_success 'cherry pick an empty non-ff commit with --allow-empty' '
60 git checkout master &&
63 - git cherry-pick --allow-empty empty-branch2
61 + git cherry-pick --allow-empty empty-change-branch
62 '
63
64 test_expect_success 'cherry pick with --keep-redundant-commits' '