Skip to content

Commit 84c9dc2

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 50b54fd commit 84c9dc2

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

Documentation/config.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ core.commentchar::
544544
messages consider a line that begins with this character
545545
commented, and removes them after the editor returns
546546
(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.
547550

548551
sequence.editor::
549552
Text editor used by `git rebase -i` for editing the rebase instruction file.

builtin/commit.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,36 @@ static char *cut_ident_timestamp_part(char *string)
594594
return ket;
595595
}
596596

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+
597627
static int prepare_to_commit(const char *index_file, const char *prefix,
598628
struct commit *current_head,
599629
struct wt_status *s,
@@ -748,6 +778,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
748778
if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
749779
die_errno(_("could not write commit template"));
750780

781+
if (auto_comment_line_char)
782+
adjust_comment_line_char(&sb);
751783
strbuf_release(&sb);
752784

753785
/* This checks if committer ident is explicitly given */

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ extern int precomposed_unicode;
602602
* that is subject to stripspace.
603603
*/
604604
extern char comment_line_char;
605+
extern int auto_comment_line_char;
605606

606607
enum branch_track {
607608
BRANCH_TRACK_UNSPECIFIED = -1,

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,11 @@ static int git_default_core_config(const char *var, const char *value)
828828
int ret = git_config_string(&comment, var, value);
829829
if (ret)
830830
return ret;
831+
else if (!strcasecmp(comment, "auto"))
832+
auto_comment_line_char = 1;
831833
else if (comment[0] && !comment[1]) {
832834
comment_line_char = comment[0];
835+
auto_comment_line_char = 0;
833836
} else
834837
return error("core.commentChar should only be one character");
835838
return 0;

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ unsigned long pack_size_limit_cfg;
6969
* that is subject to stripspace.
7070
*/
7171
char comment_line_char = '#';
72+
int auto_comment_line_char;
7273

7374
/* Parallel index stat data preload? */
7475
int core_preload_index = 0;

t/t7502-commit.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,30 @@ test_expect_success 'commit --status with custom comment character' '
563563
test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
564564
'
565565

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+
566592
test_done

0 commit comments

Comments
 (0)