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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 27, 2024 at 04:19 UTC 9ccf3e9b22b6843892319b189fd7aed37c451420
3 files changed +27 -8
Documentation/config/core.txt
+16 -3
@@ -520,15 +520,28 @@ core.editor::
520 `GIT_EDITOR` is not set. See linkgit:git-var[1].
521
522 core.commentChar::
523 +core.commentString::
524 Commands such as `commit` and `tag` that let you edit
525 messages consider a line that begins with this character
526 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 '#').
528 +
529 If set to "auto", `git-commit` would select a character that is not
530 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 = "//"
545
546 core.filesRefLockTimeout::
547 The length of time, in milliseconds, to retry when trying to
config.c
+4 -3
@@ -1560,18 +1560,19 @@ static int git_default_core_config(const char *var, const char *value,
1560 if (!strcmp(var, "core.editor"))
1561 return git_config_string(&editor_program, var, value);
1562
1563 - if (!strcmp(var, "core.commentchar")) {
1563 + if (!strcmp(var, "core.commentchar") ||
1564 + !strcmp(var, "core.commentstring")) {
1565 if (!value)
1566 return config_error_nonbool(var);
1567 else if (!strcasecmp(value, "auto"))
1568 auto_comment_line_char = 1;
1569 else if (value[0]) {
1570 if (strchr(value, '\n'))
1570 - return error(_("core.commentChar cannot contain newline"));
1571 + return error(_("%s cannot contain newline"), var);
1572 comment_line_str = xstrdup(value);
1573 auto_comment_line_char = 0;
1574 } else
1574 - return error(_("core.commentChar must have at least one character"));
1575 + return error(_("%s must have at least one character"), var);
1576 return 0;
1577 }
1578
t/t0030-stripspace.sh
+7 -2
@@ -401,14 +401,19 @@ test_expect_success 'strip comments with changed comment char' '
401 test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
402 '
403
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 +
409 test_expect_success 'newline as commentchar is forbidden' '
410 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
412 '
413
414 test_expect_success 'empty commentchar is forbidden' '
415 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
417 '
418
419 test_expect_success '-c with single line' '