Re: [PATCH v2] git-commit.txt: clarify -t requires editing message

6 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH v2] git-commit.txt: clarify -t requires editing message

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:26

Adam Monsen [off-list ref] writes:
Junio C Hamano wrote:
quoted
I think that is something that needs fixing the broken code to behave
less confusingly, not documenting its wrong behaviour.
Excellent! I concur.

I wish I wanted to do this enough to make time to work on it. Ivan, how
are your C chops? :)
Don't worry.  While looking around the vicinity of the codepath, I noticed
a few more bugs there, so I'll post something today.

[PATCH 0/3] "commit --template" fixes

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:26

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).  The
goal of this series is to fix this, which is the third patch.

While looking at this, I found another bug that the contents of the
template file is still used for error checking even when it is ignored
when the editor is populated for the user to edit.  The second patch
addresses this.

Junio C Hamano (3):
  t7501: test the right kind of breakage
  commit: do not trigger bogus "has templated message edited" check
  commit: rephrase the error when user did not touch templated log message

 builtin/commit.c  |   62 +++++++++++++++++++++++++++++++++++++----------------
 t/t7501-commit.sh |   14 ++++++++++++
 2 files changed, 57 insertions(+), 19 deletions(-)

-- 
1.7.10.rc3.55.g06e99

[PATCH 3/3] commit: rephrase the error when user did not touch templated log message

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:26

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 <redacted>
---
 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

[PATCH 1/3] t7501: test the right kind of breakage

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:26

These tests try to run "git commit" with various "forbidden" combinations
of options and expect the command to fail, but they do so without having
any change added to the index.  We wouldn't be able to catch breakages
that would allow these combinations by mistake with them because the
command will fail with "nothing to commit" anyway.

Make sure we have something added to the index before running the command.

Signed-off-by: Junio C Hamano <redacted>
---
 t/t7501-commit.sh |    2 ++
 1 file changed, 2 insertions(+)
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 8bb3833..45446b1 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -30,10 +30,12 @@ test_expect_success 'setup: initial commit' '
 '
 
 test_expect_success '-m and -F do not mix' '
+	git checkout HEAD file && echo >>file && git add file &&
 	test_must_fail git commit -m foo -m bar -F file
 '
 
 test_expect_success '-m and -C do not mix' '
+	git checkout HEAD file && echo >>file && git add file &&
 	test_must_fail git commit -C HEAD -m illegal
 '
 
-- 
1.7.10.rc3.55.g06e99

[PATCH 2/3] commit: do not trigger bogus "has templated message edited" check

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:26

When "-t template" and "-F msg" options are both given (or worse yet,
there is "commit.template" configuration but a message is given in some
other way), the documentation says that template is ignored.  However,
the "has the user edited the message?" check still used the contents of
the template file as the basis of the emptyness check.

Signed-off-by: Junio C Hamano <redacted>
---
 builtin/commit.c  |    2 ++
 t/t7501-commit.sh |    6 ++++++
 2 files changed, 8 insertions(+)
diff --git a/builtin/commit.c b/builtin/commit.c
index eba1377..7141766 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1049,6 +1049,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
 		die(_("Only one of -c/-C/-F/--fixup can be used."));
 	if (message.len && f > 0)
 		die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
+	if (f || message.len)
+		template_file = NULL;
 	if (edit_message)
 		use_message = edit_message;
 	if (amend && !use_message && !fixup_message)
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 45446b1..e59cc4e 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -81,7 +81,13 @@ test_expect_success 'empty commit message' '
 	test_must_fail git commit -F msg -a
 '
 
+test_expect_success 'template "emptyness" check does not kick in with -F' '
+	git checkout HEAD file && echo >>file && git add file &&
+	git commit -t file -F file
+'
+
 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 &&
 	git commit -F msg -a
 '
-- 
1.7.10.rc3.55.g06e99

Re: [PATCH 0/3] "commit --template" fixes

From: Adam Monsen <hidden>
Date: 2016-06-15 22:53:26

On 03/30/2012 12:45 PM, Junio C Hamano wrote:
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).  The goal of this series is to fix this, which is the
third patch.
This is awesome. thanks!

I really like the new error message specific to the situation when
the user does not edit the template (as we discussed).

Your patches apply cleanly to maint b8939b2b3abaa.

I tested the patches and they work as expected. When I use
`git commit --template FILE` but do not edit the message in my editor,
I get

	Aborting commit; you did not edit the message.

Nice.

Only thing I'd add is a change to the git-commit(1) manpage.

* I prefer pragmatically explaining what will happen when the user
  uses --template but does not edit the message because it is more
  direct and terse (than "filling in a form").
* The below applies cleanly to maint as of today.
* I don't know the kosher procedure to add this commit to your patch
  series for further review, so hopefully this works.
* I'm not sure if the "Helped-by:" lines are kosher, I'm happy to
  remove them if not.

From 91a62baa1fe89032e7a3598e5d39241f3eb8f84b Mon Sep 17 00:00:00 2001
From: Adam Monsen <redacted>
Date: Sat, 31 Mar 2012 12:09:29 -0700
Subject: [PATCH] git-commit.txt: clarify -t requires editing message

Make it clear that, when using commit --template, the message *must* be
changed or the commit will be aborted.

Helped-by: Junio C Hamano [off-list ref]
Helped-by: Ivan Heffner [off-list ref]
Signed-off-by: Adam Monsen <redacted>
---
I wrapped the text at 77 characters because that was the longest
line in the file (according to wc -L).

I used ":set noet nosta ts=8 sw=8 tw=77" in Vim.

 Documentation/git-commit.txt |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-commit.txt b/Documentation/git-commit.txt
index 5cc84a1..f584a62 100644
--- a/Documentation/git-commit.txt
+++ b/Documentation/git-commit.txt
@@ -132,11 +132,11 @@ OPTIONS
 
 -t <file>::
 --template=<file>::
-	Use the contents of the given file as the initial version
-	of the commit message. The editor is invoked and you can
-	make subsequent changes. If a message is specified using
-	the `-m` or `-F` options, this option has no effect. This
-	overrides the `commit.template` configuration variable.
+	Use the contents of the given file as the initial version of the
+	commit message. The editor is invoked so you can make subsequent
+	changes. If you make no changes, the commit is aborted. If a message
+	is specified using the `-m` or `-F` options, this option has no
+	effect. This overrides the `commit.template` configuration variable.
 
 -s::
 --signoff::
-- 
1.7.5.4
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help