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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 25, 2014 at 19:11 UTC 076cbd6341182d296671cb101c75145eb3bfda0a
2 files changed +12 -4
builtin/commit.c
+2 -3
@@ -650,9 +650,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
650 hook_arg1 = "message";
651 } else if (use_message) {
652 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));
655 hook_arg1 = "commit";
656 hook_arg2 = use_message;
657 } else if (fixup_message) {
t/t7500-commit.sh
+10 -1
@@ -223,7 +223,8 @@ test_expect_success 'Commit without message is allowed with --allow-empty-messag
223 git add foo &&
224 >empty &&
225 git commit --allow-empty-message <empty &&
226 - commit_msg_is ""
226 + commit_msg_is "" &&
227 + git tag empty-message-commit
228 '
229
230 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' '
241 commit_msg_is "hello there"
242 '
243
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 +
252 commit_for_rebase_autosquash_setup () {
253 echo "first content line" >>foo &&
254 git add foo &&