Skip to content

Commit 1e2600d

Browse files
committed
Merge branch 'nd/status-auto-comment-char'
* nd/status-auto-comment-char: commit: allow core.commentChar=auto for character auto selection config: be strict on core.commentChar
2 parents 0756529 + 84c9dc2 commit 1e2600d

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
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
@@ -620,6 +620,36 @@ static int author_date_is_interesting(void)
620620
return author_message || force_date;
621621
}
622622

623+
static void adjust_comment_line_char(const struct strbuf *sb)
624+
{
625+
char candidates[] = "#;@!$%^&|:";
626+
char *candidate;
627+
const char *p;
628+
629+
comment_line_char = candidates[0];
630+
if (!memchr(sb->buf, comment_line_char, sb->len))
631+
return;
632+
633+
p = sb->buf;
634+
candidate = strchr(candidates, *p);
635+
if (candidate)
636+
*candidate = ' ';
637+
for (p = sb->buf; *p; p++) {
638+
if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
639+
candidate = strchr(candidates, p[1]);
640+
if (candidate)
641+
*candidate = ' ';
642+
}
643+
}
644+
645+
for (p = candidates; *p == ' '; p++)
646+
;
647+
if (!*p)
648+
die(_("unable to select a comment character that is not used\n"
649+
"in the current commit message"));
650+
comment_line_char = *p;
651+
}
652+
623653
static int prepare_to_commit(const char *index_file, const char *prefix,
624654
struct commit *current_head,
625655
struct wt_status *s,
@@ -773,6 +803,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
773803
if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
774804
die_errno(_("could not write commit template"));
775805

806+
if (auto_comment_line_char)
807+
adjust_comment_line_char(&sb);
776808
strbuf_release(&sb);
777809

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

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ extern int precomposed_unicode;
618618
* that is subject to stripspace.
619619
*/
620620
extern char comment_line_char;
621+
extern int auto_comment_line_char;
621622

622623
enum branch_track {
623624
BRANCH_TRACK_UNSPECIFIED = -1,

config.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,16 @@ static int git_default_core_config(const char *var, const char *value)
826826
if (!strcmp(var, "core.commentchar")) {
827827
const char *comment;
828828
int ret = git_config_string(&comment, var, value);
829-
if (!ret)
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]) {
830834
comment_line_char = comment[0];
831-
return ret;
835+
auto_comment_line_char = 0;
836+
} else
837+
return error("core.commentChar should only be one character");
838+
return 0;
832839
}
833840

834841
if (!strcmp(var, "core.askpass"))

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
@@ -570,4 +570,30 @@ test_expect_success 'commit --status with custom comment character' '
570570
test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
571571
'
572572

573+
test_expect_success 'switch core.commentchar' '
574+
test_commit "#foo" foo &&
575+
GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
576+
test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
577+
'
578+
579+
test_expect_success 'switch core.commentchar but out of options' '
580+
cat >text <<\EOF &&
581+
# 1
582+
; 2
583+
@ 3
584+
! 4
585+
$ 5
586+
% 6
587+
^ 7
588+
& 8
589+
| 9
590+
: 10
591+
EOF
592+
git commit --amend -F text &&
593+
(
594+
test_set_editor .git/FAKE_EDITOR &&
595+
test_must_fail git -c core.commentChar=auto commit --amend
596+
)
597+
'
598+
573599
test_done

t/t7508-status.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,7 @@ test_expect_success "status (core.commentchar with submodule summary)" '
13501350

13511351
test_expect_success "status (core.commentchar with two chars with submodule summary)" '
13521352
test_config core.commentchar ";;" &&
1353-
git -c status.displayCommentPrefix=true status >output &&
1354-
test_i18ncmp expect output
1353+
test_must_fail git -c status.displayCommentPrefix=true status
13551354
'
13561355

13571356
test_expect_success "--ignore-submodules=all suppresses submodule summary" '

0 commit comments

Comments
 (0)