diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2014-05-17 08:52:23 +0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-05-19 13:37:25 -0700 |
commit | 84c9dc2c5a2d34351a06554af32501d4f99990e9 (patch) | |
tree | 737dde6bdaab102e46f8694558c3645aaeecb1fa /builtin | |
parent | config: be strict on core.commentChar (diff) | |
download | tgif-84c9dc2c5a2d34351a06554af32501d4f99990e9.tar.xz |
commit: allow core.commentChar=auto for character auto selection
When core.commentChar is "auto", the comment char starts with '#' as
in default but if it's already in the prepared message, find another
char in a small subset. This should stop surprises because git strips
some lines unexpectedly.
Note that git is not smart enough to recognize '#' as the comment char
in custom templates and convert it if the final comment char is
different. It thinks '#' lines in custom templates as part of the
commit message. So don't use this with custom templates.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/commit.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/builtin/commit.c b/builtin/commit.c index 9cfef6c6cc..515c4c4c05 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -594,6 +594,36 @@ static char *cut_ident_timestamp_part(char *string) return ket; } +static void adjust_comment_line_char(const struct strbuf *sb) +{ + char candidates[] = "#;@!$%^&|:"; + char *candidate; + const char *p; + + comment_line_char = candidates[0]; + if (!memchr(sb->buf, comment_line_char, sb->len)) + return; + + p = sb->buf; + candidate = strchr(candidates, *p); + if (candidate) + *candidate = ' '; + for (p = sb->buf; *p; p++) { + if ((p[0] == '\n' || p[0] == '\r') && p[1]) { + candidate = strchr(candidates, p[1]); + if (candidate) + *candidate = ' '; + } + } + + for (p = candidates; *p == ' '; p++) + ; + if (!*p) + die(_("unable to select a comment character that is not used\n" + "in the current commit message")); + comment_line_char = *p; +} + static int prepare_to_commit(const char *index_file, const char *prefix, struct commit *current_head, struct wt_status *s, @@ -748,6 +778,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix, if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) die_errno(_("could not write commit template")); + if (auto_comment_line_char) + adjust_comment_line_char(&sb); strbuf_release(&sb); /* This checks if committer ident is explicitly given */ |