From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Adam Monsen <haircut@gmail.com>
Subject: [PATCH 3/3] commit: rephrase the error when user did not touch templated log message
Date: Fri, 30 Mar 2012 12:45:19 -0700 [thread overview]
Message-ID: <1333136719-12657-4-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1333136719-12657-1-git-send-email-gitster@pobox.com>
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>
---
builtin/commit.c | 60 ++++++++++++++++++++++++++++++++++++-----------------
t/t7501-commit.sh | 6 ++++++
2 files changed, 47 insertions(+), 19 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 7141766..847d363 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -899,27 +899,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
return 1;
}
-/*
- * Find out if the message in the strbuf contains only whitespace and
- * Signed-off-by lines.
- */
-static int message_is_empty(struct strbuf *sb)
+static int rest_is_empty(struct strbuf *sb, int start)
{
- struct strbuf tmpl = STRBUF_INIT;
+ int i, eol;
const char *nl;
- int eol, i, start = 0;
-
- if (cleanup_mode == CLEANUP_NONE && sb->len)
- return 0;
-
- /* See if the template is just a prefix of the message. */
- if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
- stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
- if (start + tmpl.len <= sb->len &&
- memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
- start += tmpl.len;
- }
- strbuf_release(&tmpl);
/* Check if the rest is just whitespace and Signed-of-by's. */
for (i = start; i < sb->len; i++) {
@@ -942,6 +925,40 @@ static int message_is_empty(struct strbuf *sb)
return 1;
}
+/*
+ * Find out if the message in the strbuf contains only whitespace and
+ * Signed-off-by lines.
+ */
+static int message_is_empty(struct strbuf *sb)
+{
+ if (cleanup_mode == CLEANUP_NONE && sb->len)
+ return 0;
+ return rest_is_empty(sb, 0);
+}
+
+/*
+ * See if the user edited the message in the editor or left what
+ * was in the template intact
+ */
+static int template_untouched(struct strbuf *sb)
+{
+ struct strbuf tmpl = STRBUF_INIT;
+ char *start;
+
+ if (cleanup_mode == CLEANUP_NONE && sb->len)
+ return 0;
+
+ if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
+ return 0;
+
+ stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
+ start = (char *)skip_prefix(sb->buf, tmpl.buf);
+ if (!start)
+ start = sb->buf;
+ strbuf_release(&tmpl);
+ return rest_is_empty(sb, start - sb->buf);
+}
+
static const char *find_author_by_nickname(const char *name)
{
struct rev_info revs;
@@ -1490,6 +1507,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (cleanup_mode != CLEANUP_NONE)
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
+ if (template_untouched(&sb) && !allow_empty_message) {
+ rollback_index_files();
+ fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
+ exit(1);
+ }
if (message_is_empty(&sb) && !allow_empty_message) {
rollback_index_files();
fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index e59cc4e..b20ca0e 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -86,6 +86,12 @@ test_expect_success 'template "emptyness" check does not kick in with -F' '
git commit -t file -F file
'
+test_expect_success 'template "emptyness" check' '
+ git checkout HEAD file && echo >>file && git add file &&
+ test_must_fail git commit -t file 2>err &&
+ test_i18ngrep "did not edit" err
+'
+
test_expect_success 'setup: commit message from file' '
git checkout HEAD file && echo >>file && git add file &&
echo this is the commit message, coming from a file >msg &&
--
1.7.10.rc3.55.g06e99
next prev parent reply other threads:[~2012-03-30 19:45 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-29 17:57 [PATCH] git-commit doc: say -t requires editing commit message Adam Monsen
2012-03-29 18:09 ` Ivan Heffner
2012-03-29 23:04 ` [PATCH v2] git-commit.txt: clarify -t requires editing message Adam Monsen
2012-03-30 2:05 ` Junio C Hamano
2012-03-30 3:07 ` Adam Monsen
2012-03-30 3:52 ` Junio C Hamano
2012-03-30 4:53 ` Adam Monsen
2012-03-30 5:08 ` Junio C Hamano
2012-03-30 5:43 ` Adam Monsen
2012-03-30 18:17 ` Junio C Hamano
2012-03-30 19:45 ` [PATCH 0/3] "commit --template" fixes Junio C Hamano
2012-03-30 19:45 ` [PATCH 1/3] t7501: test the right kind of breakage Junio C Hamano
2012-03-30 19:45 ` [PATCH 2/3] commit: do not trigger bogus "has templated message edited" check Junio C Hamano
2012-03-30 19:45 ` Junio C Hamano [this message]
2012-03-31 19:28 ` [PATCH 0/3] "commit --template" fixes Adam Monsen
2012-04-01 22:28 ` Junio C Hamano
2012-04-03 17:11 ` Adam Monsen
2012-04-03 21:55 ` Junio C Hamano
2012-04-05 14:29 ` Adam Monsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1333136719-12657-4-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=haircut@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).