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 <gitster@pobox.com>

Junio C Hamano committed Mar 30, 2012 at 12:14 UTC b2eda9bdfbacdd3bb52da2ebcb468415cf153940
2 files changed +47 -19
builtin/commit.c
+41 -19
@@ -899,27 +899,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
899 return 1;
900 }
901
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)
903 {
908 - struct strbuf tmpl = STRBUF_INIT;
904 + int i, eol;
905 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);
906
907 /* Check if the rest is just whitespace and Signed-of-by's. */
908 for (i = start; i < sb->len; i++) {
@@ -942,6 +925,40 @@ static int message_is_empty(struct strbuf *sb)
925 return 1;
926 }
927
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 +
962 static const char *find_author_by_nickname(const char *name)
963 {
964 struct rev_info revs;
@@ -1490,6 +1507,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1507
1508 if (cleanup_mode != CLEANUP_NONE)
1509 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 + }
1515 if (message_is_empty(&sb) && !allow_empty_message) {
1516 rollback_index_files();
1517 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
t/t7501-commit.sh
+6
@@ -86,6 +86,12 @@ test_expect_success 'template "emptyness" check does not kick in with -F' '
86 git commit -t file -F file
87 '
88
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 +
95 test_expect_success 'setup: commit message from file' '
96 git checkout HEAD file && echo >>file && git add file &&
97 echo this is the commit message, coming from a file >msg &&