Skip to content

Commit

Permalink
commit: rephrase the error when user did not touch templated log message
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Mar 30, 2012
1 parent 010c7db commit b2eda9b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
60 changes: 41 additions & 19 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down
6 changes: 6 additions & 0 deletions t/t7501-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit b2eda9b

Please sign in to comment.