Skip to content

Commit d9a9357

Browse files
arachsysgitster
authored andcommitted
Allow edit of empty message with commit --amend
"git commit --amend" used on a commit with an empty message fails unless -m is given, whether or not --allow-empty-message is specified. Allow it to proceed to the editor with an empty commit message. Unless --allow-empty-message is in force, it will still abort later if an empty message is saved from the editor (this check was already necessary to prevent a non-empty commit message being edited to an empty one). Add a test for --amend --edit of an empty commit message which fails without this fix, as it's a rare case that won't get frequently tested otherwise. Signed-off-by: Chris Webb <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0f1ea6 commit d9a9357

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
690690
hook_arg1 = "message";
691691
} else if (use_message) {
692692
buffer = strstr(use_message_buffer, "\n\n");
693-
if (!buffer || buffer[2] == '\0')
693+
if (!use_editor && (!buffer || buffer[2] == '\0'))
694694
die(_("commit has empty message"));
695695
strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
696696
hook_arg1 = "commit";

t/t7501-commit.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ test_expect_success '--amend --edit' '
124124
test_cmp expect msg
125125
'
126126

127+
test_expect_success '--amend --edit of empty message' '
128+
cat >replace <<-\EOF &&
129+
#!/bin/sh
130+
echo "amended" >"$1"
131+
EOF
132+
chmod 755 replace &&
133+
git commit --allow-empty --allow-empty-message -m "" &&
134+
echo more bongo >file &&
135+
git add file &&
136+
EDITOR=./replace git commit --edit --amend &&
137+
git diff-tree -s --format=%s HEAD >msg &&
138+
./replace expect &&
139+
test_cmp expect msg
140+
'
141+
127142
test_expect_success '-m --edit' '
128143
echo amended >expect &&
129144
git commit --allow-empty -m buffer &&

0 commit comments

Comments
 (0)