Subject: [PATCH] commit: error out for missing commit message template

From: Jonathan Nieder <[email-masked]>
Date: Fri, 25 Feb 2011 03:07:57 -0600

When "git commit" was rewritten in C (v1.5.4-rc0~78^2~30,
2007-11-08), a subtle bug in --template was introduced.  If the
file named by a --template parameter is missing, previously git
would error out with a message:

	Commit template file does not exist.

but in the C version the --template parameter gets ignored and
the default template is used.

t7500 has two tests for this case which would have caught it, except
that with the default $EDITOR, the commit message template is left
unmodified, causing 'git commit' to error out and the test to
succeed.

Signed-off-by: Jonathan Nieder <[email-masked]>
---
Ævar Arnfjörð Bjarmason wrote:

> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -725,15 +725,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
[...]
> -				"%s"
> -				"# Author:    %s\n",
> +				_("%s"
> +				"# Author:    %s\n"),

The above change causes the commit message template to be nonempty,
excluding comments, so bare "git commit" succeeds, exposing this
longstanding bug.

 builtin/commit.c  |    2 +-
 t/t7500-commit.sh |   14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index d7f55e3..1a1fb73 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -634,7 +634,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
 			die_errno("could not read SQUASH_MSG");
 		hook_arg1 = "squash";
-	} else if (template_file && !stat(template_file, &statbuf)) {
+	} else if (template_file) {
 		if (strbuf_read_file(&sb, template_file, 0) < 0)
 			die_errno("could not read '%s'", template_file);
 		hook_arg1 = "template";
diff --git a/t/t7500-commit.sh b/t/t7500-commit.sh
index d551b77..5976f59 100755
--- a/t/t7500-commit.sh
+++ b/t/t7500-commit.sh
@@ -28,13 +28,21 @@ test_expect_success 'a basic commit in an empty tree should succeed' '
 test_expect_success 'nonexistent template file should return error' '
 	echo changes >> foo &&
 	git add foo &&
-	test_must_fail git commit --template "$PWD"/notexist
+	(
+		GIT_EDITOR="echo hello >\"\$1\"" &&
+		export GIT_EDITOR &&
+		test_must_fail git commit --template "$PWD"/notexist
+	)
 '
 
 test_expect_success 'nonexistent template file in config should return error' '
 	git config commit.template "$PWD"/notexist &&
-	test_must_fail git commit &&
-	git config --unset commit.template
+	test_when_finished "git config --unset commit.template" &&
+	(
+		GIT_EDITOR="echo hello >\"\$1\"" &&
+		export GIT_EDITOR &&
+		test_must_fail git commit
+	)
 '
 
 # From now on we'll use a template file that exists.
-- 
1.7.4.1