Skip to content

Commit 091df17

Browse files
committed
Merge branch 'jc/commit-unedited-template'
When "git commit --template F" errors out because the user did not touch the message, it claimed that it aborts due to "empty message", which was utterly wrong. By Junio C Hamano (4) and Adam Monsen (1) * jc/commit-unedited-template: Documentation/git-commit: rephrase the "initial-ness" of templates git-commit.txt: clarify -t requires editing message commit: rephrase the error when user did not touch templated log message commit: do not trigger bogus "has templated message edited" check t7501: test the right kind of breakage
2 parents fa0ba72 + 1f08c2c commit 091df17

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

Documentation/git-commit.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,14 @@ OPTIONS
132132

133133
-t <file>::
134134
--template=<file>::
135-
Use the contents of the given file as the initial version
136-
of the commit message. The editor is invoked and you can
137-
make subsequent changes. If a message is specified using
138-
the `-m` or `-F` options, this option has no effect. This
139-
overrides the `commit.template` configuration variable.
135+
When editing the commit message, start the editor with the
136+
contents in the given file. The `commit.template` configuration
137+
variable is often used to give this option implicitly to the
138+
command. This mechanism can be used by projects that want to
139+
guide participants with some hints on what to write in the message
140+
in what order. If the user exits the editor without editing the
141+
message, the commit is aborted. This has no effect when a message
142+
is given by other means, e.g. with the `-m` or `-F` options.
140143

141144
-s::
142145
--signoff::

builtin/commit.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -921,27 +921,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
921921
return 1;
922922
}
923923

924-
/*
925-
* Find out if the message in the strbuf contains only whitespace and
926-
* Signed-off-by lines.
927-
*/
928-
static int message_is_empty(struct strbuf *sb)
924+
static int rest_is_empty(struct strbuf *sb, int start)
929925
{
930-
struct strbuf tmpl = STRBUF_INIT;
926+
int i, eol;
931927
const char *nl;
932-
int eol, i, start = 0;
933-
934-
if (cleanup_mode == CLEANUP_NONE && sb->len)
935-
return 0;
936-
937-
/* See if the template is just a prefix of the message. */
938-
if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
939-
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
940-
if (start + tmpl.len <= sb->len &&
941-
memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
942-
start += tmpl.len;
943-
}
944-
strbuf_release(&tmpl);
945928

946929
/* Check if the rest is just whitespace and Signed-of-by's. */
947930
for (i = start; i < sb->len; i++) {
@@ -964,6 +947,40 @@ static int message_is_empty(struct strbuf *sb)
964947
return 1;
965948
}
966949

950+
/*
951+
* Find out if the message in the strbuf contains only whitespace and
952+
* Signed-off-by lines.
953+
*/
954+
static int message_is_empty(struct strbuf *sb)
955+
{
956+
if (cleanup_mode == CLEANUP_NONE && sb->len)
957+
return 0;
958+
return rest_is_empty(sb, 0);
959+
}
960+
961+
/*
962+
* See if the user edited the message in the editor or left what
963+
* was in the template intact
964+
*/
965+
static int template_untouched(struct strbuf *sb)
966+
{
967+
struct strbuf tmpl = STRBUF_INIT;
968+
char *start;
969+
970+
if (cleanup_mode == CLEANUP_NONE && sb->len)
971+
return 0;
972+
973+
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
974+
return 0;
975+
976+
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
977+
start = (char *)skip_prefix(sb->buf, tmpl.buf);
978+
if (!start)
979+
start = sb->buf;
980+
strbuf_release(&tmpl);
981+
return rest_is_empty(sb, start - sb->buf);
982+
}
983+
967984
static const char *find_author_by_nickname(const char *name)
968985
{
969986
struct rev_info revs;
@@ -1071,6 +1088,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
10711088
die(_("Only one of -c/-C/-F/--fixup can be used."));
10721089
if (message.len && f > 0)
10731090
die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1091+
if (f || message.len)
1092+
template_file = NULL;
10741093
if (edit_message)
10751094
use_message = edit_message;
10761095
if (amend && !use_message && !fixup_message)
@@ -1510,6 +1529,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15101529

15111530
if (cleanup_mode != CLEANUP_NONE)
15121531
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1532+
if (template_untouched(&sb) && !allow_empty_message) {
1533+
rollback_index_files();
1534+
fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1535+
exit(1);
1536+
}
15131537
if (message_is_empty(&sb) && !allow_empty_message) {
15141538
rollback_index_files();
15151539
fprintf(stderr, _("Aborting commit due to empty commit message.\n"));

t/t7501-commit.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ test_expect_success 'setup: initial commit' '
3030
'
3131

3232
test_expect_success '-m and -F do not mix' '
33+
git checkout HEAD file && echo >>file && git add file &&
3334
test_must_fail git commit -m foo -m bar -F file
3435
'
3536

3637
test_expect_success '-m and -C do not mix' '
38+
git checkout HEAD file && echo >>file && git add file &&
3739
test_must_fail git commit -C HEAD -m illegal
3840
'
3941

@@ -79,7 +81,19 @@ test_expect_success 'empty commit message' '
7981
test_must_fail git commit -F msg -a
8082
'
8183

84+
test_expect_success 'template "emptyness" check does not kick in with -F' '
85+
git checkout HEAD file && echo >>file && git add file &&
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+
8295
test_expect_success 'setup: commit message from file' '
96+
git checkout HEAD file && echo >>file && git add file &&
8397
echo this is the commit message, coming from a file >msg &&
8498
git commit -F msg -a
8599
'

0 commit comments

Comments
 (0)