Skip to content

Commit 2140b14

Browse files
jrngitster
authored andcommitted
commit: error out for missing commit message template
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 protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da656f1 commit 2140b14

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
602602
if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
603603
die_errno("could not read SQUASH_MSG");
604604
hook_arg1 = "squash";
605-
} else if (template_file && !stat(template_file, &statbuf)) {
605+
} else if (template_file) {
606606
if (strbuf_read_file(&sb, template_file, 0) < 0)
607607
die_errno("could not read '%s'", template_file);
608608
hook_arg1 = "template";

t/t7500-commit.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ test_expect_success 'a basic commit in an empty tree should succeed' '
2323
test_expect_success 'nonexistent template file should return error' '
2424
echo changes >> foo &&
2525
git add foo &&
26-
test_must_fail git commit --template "$PWD"/notexist
26+
(
27+
GIT_EDITOR="echo hello >\"\$1\"" &&
28+
export GIT_EDITOR &&
29+
test_must_fail git commit --template "$PWD"/notexist
30+
)
2731
'
2832

2933
test_expect_success 'nonexistent template file in config should return error' '
3034
git config commit.template "$PWD"/notexist &&
31-
test_must_fail git commit &&
32-
git config --unset commit.template
35+
test_when_finished "git config --unset commit.template" &&
36+
(
37+
GIT_EDITOR="echo hello >\"\$1\"" &&
38+
export GIT_EDITOR &&
39+
test_must_fail git commit
40+
)
3341
'
3442

3543
# From now on we'll use a template file that exists.

0 commit comments

Comments
 (0)