Skip to content

Commit a3ec9ea

Browse files
newrengitster
authored andcommitted
sequencer: fix --allow-empty-message behavior, make it smarter
In commit b00bf1c ("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 b00bf1c (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b00bf1c commit a3ec9ea

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

sequencer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
858858
if ((flags & ALLOW_EMPTY))
859859
argv_array_push(&cmd.args, "--allow-empty");
860860

861-
if (opts->allow_empty_message)
861+
if (!(flags & EDIT_MSG))
862862
argv_array_push(&cmd.args, "--allow-empty-message");
863863

864864
if (cmd.err == -1) {
@@ -1272,7 +1272,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
12721272

12731273
if (cleanup != COMMIT_MSG_CLEANUP_NONE)
12741274
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)) {
12761276
res = 1; /* run 'git commit' to display error message */
12771277
goto out;
12781278
}

t/t3404-rebase-interactive.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,15 @@ test_expect_success '--continue tries to commit, even for "edit"' '
553553
'
554554

555555
test_expect_success 'aborted --continue does not squash commits after "edit"' '
556-
test_when_finished "git rebase --abort" &&
557556
old=$(git rev-parse HEAD) &&
558557
test_tick &&
559558
set_fake_editor &&
560559
FAKE_LINES="edit 1" git rebase -i HEAD^ &&
561560
echo "edited again" > file7 &&
562561
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
566565
'
567566

568567
test_expect_success 'auto-amend only edited commits after "edit"' '

t/t3405-rebase-malformed.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test_expect_success 'rebase -m commit with empty message' '
8383
test_expect_success 'rebase -i commit with empty message' '
8484
git checkout diff-in-message &&
8585
set_fake_editor &&
86-
env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
86+
test_must_fail env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
8787
git rebase -i HEAD^
8888
'
8989

t/t3505-cherry-pick-empty.sh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,32 @@ test_expect_success setup '
1111
test_tick &&
1212
git commit -m "first" &&
1313
14-
git checkout -b empty-branch &&
15-
test_tick &&
16-
git commit --allow-empty -m "empty" &&
17-
14+
git checkout -b empty-message-branch &&
1815
echo third >> file1 &&
1916
git add file1 &&
2017
test_tick &&
2118
git commit --allow-empty-message -m "" &&
2219
2320
git checkout master &&
24-
git checkout -b empty-branch2 &&
21+
git checkout -b empty-change-branch &&
2522
test_tick &&
2623
git commit --allow-empty -m "empty"
2724
2825
'
2926

3027
test_expect_success 'cherry-pick an empty commit' '
3128
git checkout master &&
32-
test_expect_code 1 git cherry-pick empty-branch^
29+
test_expect_code 1 git cherry-pick empty-change-branch
3330
'
3431

3532
test_expect_success 'index lockfile was removed' '
3633
test ! -f .git/index.lock
3734
'
3835

3936
test_expect_success 'cherry-pick a commit with an empty message' '
37+
test_when_finished "git reset --hard empty-message-branch~1" &&
4038
git checkout master &&
41-
test_expect_code 1 git cherry-pick empty-branch
39+
git cherry-pick empty-message-branch
4240
'
4341

4442
test_expect_success 'index lockfile was removed' '
@@ -47,20 +45,20 @@ test_expect_success 'index lockfile was removed' '
4745

4846
test_expect_success 'cherry-pick a commit with an empty message with --allow-empty-message' '
4947
git checkout -f master &&
50-
git cherry-pick --allow-empty-message empty-branch
48+
git cherry-pick --allow-empty-message empty-message-branch
5149
'
5250

5351
test_expect_success 'cherry pick an empty non-ff commit without --allow-empty' '
5452
git checkout master &&
5553
echo fourth >>file2 &&
5654
git add file2 &&
5755
git commit -m "fourth" &&
58-
test_must_fail git cherry-pick empty-branch2
56+
test_must_fail git cherry-pick empty-change-branch
5957
'
6058

6159
test_expect_success 'cherry pick an empty non-ff commit with --allow-empty' '
6260
git checkout master &&
63-
git cherry-pick --allow-empty empty-branch2
61+
git cherry-pick --allow-empty empty-change-branch
6462
'
6563

6664
test_expect_success 'cherry pick with --keep-redundant-commits' '

0 commit comments

Comments
 (0)