Skip to content

Commit 9ccf3e9

Browse files
peffgitster
authored andcommitted
config: add core.commentString
The core.commentChar code recently learned to accept more than a single ASCII character. But using it is annoying with multiple versions of Git, since older ones will reject it outright: $ git.v2.44.0 -c core.commentchar=foo stripspace -s error: core.commentChar should only be one ASCII character fatal: unable to parse 'core.commentchar' from command-line config Let's add an alias core.commentString. That's arguably a better name anyway, since we now can handle strings, and it makes it possible to have a config that works reasonably with both old and new versions of Git (see the example in the documentation). This is strictly an alias, so there's not much point in adding duplicate tests; I added a single one to t0030 that exercises the alias code. Note also that the error messages for invalid values will now show the variable the config parser handed us, and thus will be normalized to lowercase (rather than camelcase). A few tests in t0030 are adjusted to match. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8b31147 commit 9ccf3e9

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

Documentation/config/core.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,28 @@ core.editor::
520520
`GIT_EDITOR` is not set. See linkgit:git-var[1].
521521

522522
core.commentChar::
523+
core.commentString::
523524
Commands such as `commit` and `tag` that let you edit
524525
messages consider a line that begins with this character
525526
commented, and removes them after the editor returns
526-
(default '#'). Note that this option can take values larger than
527-
a byte (whether a single multi-byte character, or you
528-
could even go wild with a multi-character sequence).
527+
(default '#').
529528
+
530529
If set to "auto", `git-commit` would select a character that is not
531530
the beginning character of any line in existing commit messages.
531+
+
532+
Note that these two variables are aliases of each other, and in modern
533+
versions of Git you are free to use a string (e.g., `//` or `⁑⁕⁑`) with
534+
`commentChar`. Versions of Git prior to v2.45.0 will ignore
535+
`commentString` but will reject a value of `commentChar` that consists
536+
of more than a single ASCII byte. If you plan to use your config with
537+
older and newer versions of Git, you may want to specify both:
538+
+
539+
[core]
540+
# single character for older versions
541+
commentChar = "#"
542+
# string for newer versions (which will override commentChar
543+
# because it comes later in the file)
544+
commentString = "//"
532545

533546
core.filesRefLockTimeout::
534547
The length of time, in milliseconds, to retry when trying to

config.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,18 +1560,19 @@ static int git_default_core_config(const char *var, const char *value,
15601560
if (!strcmp(var, "core.editor"))
15611561
return git_config_string(&editor_program, var, value);
15621562

1563-
if (!strcmp(var, "core.commentchar")) {
1563+
if (!strcmp(var, "core.commentchar") ||
1564+
!strcmp(var, "core.commentstring")) {
15641565
if (!value)
15651566
return config_error_nonbool(var);
15661567
else if (!strcasecmp(value, "auto"))
15671568
auto_comment_line_char = 1;
15681569
else if (value[0]) {
15691570
if (strchr(value, '\n'))
1570-
return error(_("core.commentChar cannot contain newline"));
1571+
return error(_("%s cannot contain newline"), var);
15711572
comment_line_str = xstrdup(value);
15721573
auto_comment_line_char = 0;
15731574
} else
1574-
return error(_("core.commentChar must have at least one character"));
1575+
return error(_("%s must have at least one character"), var);
15751576
return 0;
15761577
}
15771578

t/t0030-stripspace.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,19 @@ test_expect_success 'strip comments with changed comment char' '
401401
test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
402402
'
403403

404+
test_expect_success 'strip comments with changed comment string' '
405+
test ! -z "$(echo "// comment" | git -c core.commentchar=// stripspace)" &&
406+
test -z "$(echo "// comment" | git -c core.commentchar="//" stripspace -s)"
407+
'
408+
404409
test_expect_success 'newline as commentchar is forbidden' '
405410
test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err &&
406-
grep "core.commentChar cannot contain newline" err
411+
grep "core.commentchar cannot contain newline" err
407412
'
408413

409414
test_expect_success 'empty commentchar is forbidden' '
410415
test_must_fail git -c core.commentchar= stripspace -s 2>err &&
411-
grep "core.commentChar must have at least one character" err
416+
grep "core.commentchar must have at least one character" err
412417
'
413418

414419
test_expect_success '-c with single line' '

0 commit comments

Comments
 (0)