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>
Nguyễn Thái Ngọc Duy committed
May 17, 2014 at 08:52 UTC
84c9dc2c5a2d34351a06554af32501d4f99990e9
6 files changed
+66
Documentation/config.txt
+3
@@ -544,6 +544,9 @@ core.commentchar::
544
messages consider a line that begins with this character
545
commented, and removes them after the editor returns
546
(default '#').
547
++
548
+If set to "auto", `git-commit` would select a character that is not
549
+the beginning character of any line in existing commit messages.
550
551
sequence.editor::
552
Text editor used by `git rebase -i` for editing the rebase instruction file.
builtin/commit.c
+32
@@ -594,6 +594,36 @@ static char *cut_ident_timestamp_part(char *string)
594
return ket;
595
}
596
597
+static void adjust_comment_line_char(const struct strbuf *sb)
598
+{
599
+ char candidates[] = "#;@!$%^&|:";
600
+ char *candidate;
601
+ const char *p;
602
+
603
+ comment_line_char = candidates[0];
604
+ if (!memchr(sb->buf, comment_line_char, sb->len))
605
+ return;
606
+
607
+ p = sb->buf;
608
+ candidate = strchr(candidates, *p);
609
+ if (candidate)
610
+ *candidate = ' ';
611
+ for (p = sb->buf; *p; p++) {
612
+ if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
613
+ candidate = strchr(candidates, p[1]);
614
+ if (candidate)
615
+ *candidate = ' ';
616
+ }
617
+ }
618
+
619
+ for (p = candidates; *p == ' '; p++)
620
+ ;
621
+ if (!*p)
622
+ die(_("unable to select a comment character that is not used\n"
623
+ "in the current commit message"));
624
+ comment_line_char = *p;
625
+}
626
+
627
static int prepare_to_commit(const char *index_file, const char *prefix,
628
struct commit *current_head,
629
struct wt_status *s,
@@ -748,6 +778,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
778
if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
779
die_errno(_("could not write commit template"));
780
781
+ if (auto_comment_line_char)
782
+ adjust_comment_line_char(&sb);
783
strbuf_release(&sb);
784
785
/* This checks if committer ident is explicitly given */
cache.h
+1
@@ -602,6 +602,7 @@ extern int precomposed_unicode;
602
* that is subject to stripspace.
603
*/
604
extern char comment_line_char;
605
+extern int auto_comment_line_char;
606
607
enum branch_track {
608
BRANCH_TRACK_UNSPECIFIED = -1,
config.c
+3
@@ -828,8 +828,11 @@ static int git_default_core_config(const char *var, const char *value)
828
int ret = git_config_string(&comment, var, value);
829
if (ret)
830
return ret;
831
+ else if (!strcasecmp(comment, "auto"))
832
+ auto_comment_line_char = 1;
833
else if (comment[0] && !comment[1]) {
834
comment_line_char = comment[0];
835
+ auto_comment_line_char = 0;
836
} else
837
return error("core.commentChar should only be one character");
838
return 0;
environment.c
+1
@@ -69,6 +69,7 @@ unsigned long pack_size_limit_cfg;
69
* that is subject to stripspace.
70
*/
71
char comment_line_char = '#';
72
+int auto_comment_line_char;
73
74
/* Parallel index stat data preload? */
75
int core_preload_index = 0;
t/t7502-commit.sh
+26
@@ -563,4 +563,30 @@ test_expect_success 'commit --status with custom comment character' '
563
test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
564
'
565
566
+test_expect_success 'switch core.commentchar' '
567
+ test_commit "#foo" foo &&
568
+ GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
569
+ test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
570
+'
571
+
572
+test_expect_success 'switch core.commentchar but out of options' '
573
+ cat >text <<\EOF &&
574
+# 1
575
+; 2
576
+@ 3
577
+! 4
578
+$ 5
579
+% 6
580
+^ 7
581
+& 8
582
+| 9
583
+: 10
584
+EOF
585
+ git commit --amend -F text &&
586
+ (
587
+ test_set_editor .git/FAKE_EDITOR &&
588
+ test_must_fail git -c core.commentChar=auto commit --amend
589
+ )
590
+'
591
+
592
test_done