Skip to content

Commit 076cbd6

Browse files
peffgitster
authored andcommitted
commit: do not complain of empty messages from -C
When we pick another commit's message, we die() immediately if we find that it's empty and we are not going to run an editor (i.e., when running "-C" instead of "-c"). However, this check is redundant and harmful. It's redundant because we will already notice the empty message later, after we would have run the editor, and die there (just as we would for a regular, not "-C" case, where the user provided an empty message in the editor). It's harmful for a few reasons: 1. It does not respect --allow-empty-message. As a result, a "git rebase -i" cannot "pick" such a commit. So you cannot even go back in time to fix it with a "reword" or "edit" instruction. 2. It does not take into account other ways besides the editor to modify the message. For example, "git commit -C empty-commit -m foo" could take the author information from empty-commit, but add a message to it. There's more to do to make that work correctly (and right now we explicitly forbid "-C with -m"), but this removes one roadblock. 3. The existing check is not enough to prevent segfaults. We try to find the "\n\n" header/body boundary in the commit. If it is at the end of the string (i.e., no body), _or_ if we cannot find it at all (i.e., a truncated commit object), we consider the message empty. With "-C", that's OK; we die in either case. But with "-c", we continue on, and in the case of a truncated commit may end up dereferencing NULL+2. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bbc4e8 commit 076cbd6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

builtin/commit.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
650650
hook_arg1 = "message";
651651
} else if (use_message) {
652652
buffer = strstr(use_message_buffer, "\n\n");
653-
if (!use_editor && (!buffer || buffer[2] == '\0'))
654-
die(_("commit has empty message"));
655-
strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
653+
if (buffer)
654+
strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
656655
hook_arg1 = "commit";
657656
hook_arg2 = use_message;
658657
} else if (fixup_message) {

t/t7500-commit.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ test_expect_success 'Commit without message is allowed with --allow-empty-messag
223223
git add foo &&
224224
>empty &&
225225
git commit --allow-empty-message <empty &&
226-
commit_msg_is ""
226+
commit_msg_is "" &&
227+
git tag empty-message-commit
227228
'
228229

229230
test_expect_success 'Commit without message is no-no without --allow-empty-message' '
@@ -240,6 +241,14 @@ test_expect_success 'Commit a message with --allow-empty-message' '
240241
commit_msg_is "hello there"
241242
'
242243

244+
test_expect_success 'commit -C empty respects --allow-empty-message' '
245+
echo more >>foo &&
246+
git add foo &&
247+
test_must_fail git commit -C empty-message-commit &&
248+
git commit -C empty-message-commit --allow-empty-message &&
249+
commit_msg_is ""
250+
'
251+
243252
commit_for_rebase_autosquash_setup () {
244253
echo "first content line" >>foo &&
245254
git add foo &&

0 commit comments

Comments
 (0)