Because --amend (-c foo) internally load the message from HEAD (foo, resp.) using the same code paths as -C, they erroneously refuse to work at all when the message of HEAD (foo) is empty. Remove the corresponding check under --amend and -c. None of this behavior was ever tested (not even for -C empty_message), so we add a whole batch of new tests. Signed-off-by: Thomas Rast <trast@xxxxxxxxxxxxxxx> --- Noticed while helping "starlays" on IRC. It's possible to work around the bug by first giving HEAD a non-empty commit message from the command line, as in git commit --amend -mfoo git commit --amend I haven't really checked, but from an irresponsibly quick glance it looks like this bug has always been there in the C version (that is, since f5bbc322). builtin/commit.c | 2 +- t/t7501-commit.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/builtin/commit.c b/builtin/commit.c index 3714582..45a57af 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -690,7 +690,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, hook_arg1 = "message"; } else if (use_message) { buffer = strstr(use_message_buffer, "\n\n"); - if (!buffer || buffer[2] == '\0') + if (!amend && !edit_message && (!buffer || buffer[2] == '\0')) die(_("commit has empty message")); strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); hook_arg1 = "commit"; diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh index 8bb3833..6ab7712 100755 --- a/t/t7501-commit.sh +++ b/t/t7501-commit.sh @@ -473,4 +473,70 @@ test_expect_success 'amend can copy notes' ' ' +test_expect_success 'amend on empty commit message' ' + + echo bar > bar && + git add bar && + test_tick && + git commit --allow-empty-message -m "" && + git tag empty_message && + git commit --amend -mnonempty && + git cat-file commit HEAD | grep nonempty + +' + +test_expect_success 'amend with editor on empty commit message' ' + + git reset --hard empty_message && + cat >editor <<-\EOF && + #!/bin/sh + echo nonempty_one >"$1" + EOF + chmod 755 editor && + EDITOR=./editor git commit --amend && + git cat-file commit HEAD | grep nonempty_one + +' + +test_expect_success '--amend -C empty_message fails' ' + + test_commit nonempty && + test_must_fail git commit --amend -C empty_message + +' + +test_expect_success '-C empty_message fails' ' + + echo 1 > bar && + git add bar && + test_must_fail git commit --amend -C empty_message + +' + +test_expect_success '--amend -c empty_message works' ' + + cat >editor <<-\EOF && + #!/bin/sh + echo nonempty_two >"$1" + EOF + chmod 755 editor && + EDITOR=./editor git commit --amend -c empty_message && + git cat-file commit HEAD | grep nonempty_two + +' + +test_expect_success '-c empty_message works' ' + + echo 2 > bar && + git add bar && + cat >editor <<-\EOF && + #!/bin/sh + echo nonempty_three >"$1" + EOF + chmod 755 editor && + EDITOR=./editor git commit -c empty_message && + git cat-file commit HEAD | grep nonempty_three + +' + test_done -- 1.7.9.2.467.g7fee4 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html