Skip to content

Commit b2eda9b

Browse files
committed
commit: rephrase the error when user did not touch templated log message
When the user exited editor without editing the commit log template given by "git commit -t <template>", the commit was aborted (correct) with an error message that said "due to empty commit message" (incorrect). This was because the original template support was done by piggybacking on the check to detect an empty log message. Split the codepaths into two independent checks to clarify the error. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 010c7db commit b2eda9b

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

builtin/commit.c

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -899,27 +899,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
899899
return 1;
900900
}
901901

902-
/*
903-
* Find out if the message in the strbuf contains only whitespace and
904-
* Signed-off-by lines.
905-
*/
906-
static int message_is_empty(struct strbuf *sb)
902+
static int rest_is_empty(struct strbuf *sb, int start)
907903
{
908-
struct strbuf tmpl = STRBUF_INIT;
904+
int i, eol;
909905
const char *nl;
910-
int eol, i, start = 0;
911-
912-
if (cleanup_mode == CLEANUP_NONE && sb->len)
913-
return 0;
914-
915-
/* See if the template is just a prefix of the message. */
916-
if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
917-
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
918-
if (start + tmpl.len <= sb->len &&
919-
memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
920-
start += tmpl.len;
921-
}
922-
strbuf_release(&tmpl);
923906

924907
/* Check if the rest is just whitespace and Signed-of-by's. */
925908
for (i = start; i < sb->len; i++) {
@@ -942,6 +925,40 @@ static int message_is_empty(struct strbuf *sb)
942925
return 1;
943926
}
944927

928+
/*
929+
* Find out if the message in the strbuf contains only whitespace and
930+
* Signed-off-by lines.
931+
*/
932+
static int message_is_empty(struct strbuf *sb)
933+
{
934+
if (cleanup_mode == CLEANUP_NONE && sb->len)
935+
return 0;
936+
return rest_is_empty(sb, 0);
937+
}
938+
939+
/*
940+
* See if the user edited the message in the editor or left what
941+
* was in the template intact
942+
*/
943+
static int template_untouched(struct strbuf *sb)
944+
{
945+
struct strbuf tmpl = STRBUF_INIT;
946+
char *start;
947+
948+
if (cleanup_mode == CLEANUP_NONE && sb->len)
949+
return 0;
950+
951+
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
952+
return 0;
953+
954+
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
955+
start = (char *)skip_prefix(sb->buf, tmpl.buf);
956+
if (!start)
957+
start = sb->buf;
958+
strbuf_release(&tmpl);
959+
return rest_is_empty(sb, start - sb->buf);
960+
}
961+
945962
static const char *find_author_by_nickname(const char *name)
946963
{
947964
struct rev_info revs;
@@ -1490,6 +1507,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14901507

14911508
if (cleanup_mode != CLEANUP_NONE)
14921509
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1510+
if (template_untouched(&sb) && !allow_empty_message) {
1511+
rollback_index_files();
1512+
fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1513+
exit(1);
1514+
}
14931515
if (message_is_empty(&sb) && !allow_empty_message) {
14941516
rollback_index_files();
14951517
fprintf(stderr, _("Aborting commit due to empty commit message.\n"));

t/t7501-commit.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ test_expect_success 'template "emptyness" check does not kick in with -F' '
8686
git commit -t file -F file
8787
'
8888

89+
test_expect_success 'template "emptyness" check' '
90+
git checkout HEAD file && echo >>file && git add file &&
91+
test_must_fail git commit -t file 2>err &&
92+
test_i18ngrep "did not edit" err
93+
'
94+
8995
test_expect_success 'setup: commit message from file' '
9096
git checkout HEAD file && echo >>file && git add file &&
9197
echo this is the commit message, coming from a file >msg &&

0 commit comments

Comments
 (0)