git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 15/15] config: allow multi-byte core.commentChar

From
Jeff King <peff@peff.net>
Date
Mar 7, 2024, 09:34 UTC
Message-ID
<20240307093431.GO2080210@coredump.intra.peff.net>
In-Reply-To
<20240307091407.GA2072522@coredump.intra.peff.net>

Now that all of the code handles multi-byte comment characters, it's safe to allow users to set them.

There is one special case I kept: we still will not allow an empty string for the commentChar. While it might make sense in some contexts (e.g., output where you don't want any comment prefix), there are plenty where it will behave badly (e.g., all of our starts_with() checks will indicate that every line is a comment!). It might be reasonable to assign some meaningful semantics, but it would probably involve checking how each site behaves. In the interim let's forbid it and we can loosen things later.

Since comment_line_str is used in many parts of the code, it's hard to cover all possibilities with tests. We can convert the existing double-semicolon prefix test to show that "git status" works. And we'll give it a more challenging case in t7507, where we confirm that git-commit strips out the commit template along with any --verbose text when reading the edited commit message back in. That covers the basics, though it's possible there could be issues in more exotic spots (e.g., the sequencer todo list uses its own code).

Signed-off-by: Jeff King <peff@peff.net>
---
Obviously everything works using the "str" variant with a single
character, and many tests are already covering that. You can swap out
the default to "foo>" or something and run the test suite, but there are
many spots that hard-code "#" in their expectations.

I do think it's an acceptable risk, though; for the most part you'd only find new bugs if you set a multi-byte core.commentChar, which was simply not allowed before. So we're more likely to see bugs in the new feature than regression of existing cases.

 Documentation/config/core.txt |  4 +++-
 config.c                      |  6 +++---
 t/t0030-stripspace.sh         |  5 +++++
 t/t7507-commit-verbose.sh     | 10 ++++++++++
 t/t7508-status.sh             |  4 +++-
 5 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/Documentation/config/core.txt b/Documentation/config/core.txt
index 0e8c2832bf..c86b8c8408 100644
--- a/Documentation/config/core.txt
+++ b/Documentation/config/core.txt
@@ -523,7 +523,9 @@ core.commentChar::
 	Commands such as `commit` and `tag` that let you edit
 	messages consider a line that begins with this character
 	commented, and removes them after the editor returns
-	(default '#').
+	(default '#'). Note that this option can take values larger than
+	a byte (whether a single multi-byte character, or you
+	could even go wild with a multi-character sequence).
 +
 If set to "auto", `git-commit` would select a character that is not
 the beginning character of any line in existing commit messages.
diff --git a/config.c b/config.c
index e12ea68f24..4dea34936c 100644
--- a/config.c
+++ b/config.c
@@ -1565,11 +1565,11 @@ static int git_default_core_config(const char *var, const char *value,
 			return config_error_nonbool(var);
 		else if (!strcasecmp(value, "auto"))
 			auto_comment_line_char = 1;
-		else if (value[0] && !value[1]) {
-			comment_line_str = xstrfmt("%c", value[0]);
+		else if (value[0]) {
+			comment_line_str = xstrdup(value);
 			auto_comment_line_char = 0;
 		} else
-			return error(_("core.commentChar should only be one ASCII character"));
+			return error(_("core.commentChar must have at least one character"));
 		return 0;
 	}
 
diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
index d1b3be8725..9cdf2bddbd 100755
--- a/t/t0030-stripspace.sh
+++ b/t/t0030-stripspace.sh
@@ -401,6 +401,11 @@ test_expect_success 'strip comments with changed comment char' '
 	test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
 '
 
+test_expect_success 'empty commentchar is forbidden' '
+	test_must_fail git -c core.commentchar= stripspace -s 2>err &&
+	grep "core.commentChar must have at least one character" err
+'
+
 test_expect_success '-c with single line' '
 	printf "# foo\n" >expect &&
 	printf "foo" | git stripspace -c >actual &&
diff --git a/t/t7507-commit-verbose.sh b/t/t7507-commit-verbose.sh
index c3281b192e..4c7db19ce7 100755
--- a/t/t7507-commit-verbose.sh
+++ b/t/t7507-commit-verbose.sh
@@ -101,6 +101,16 @@ test_expect_success 'verbose diff is stripped out with set core.commentChar' '
 	test_grep "Aborting commit due to empty commit message." err
 '
 
+test_expect_success 'verbose diff is stripped with multi-byte comment char' '
+	(
+		GIT_EDITOR=cat &&
+		export GIT_EDITOR &&
+		test_must_fail git -c core.commentchar="foo>" commit -a -v >out 2>err
+	) &&
+	grep "^foo> " out &&
+	test_grep "Aborting commit due to empty commit message." err
+'
+
 test_expect_success 'status does not verbose without --verbose' '
 	git status >actual &&
 	! grep "^diff --git" actual
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index a3c18a4fc2..10ed8b32bc 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -1403,7 +1403,9 @@ test_expect_success "status (core.commentchar with submodule summary)" '
 
 test_expect_success "status (core.commentchar with two chars with submodule summary)" '
 	test_config core.commentchar ";;" &&
-	test_must_fail git -c status.displayCommentPrefix=true status
+	sed "s/^/;/" <expect >expect.double &&
+	git -c status.displayCommentPrefix=true status >output &&
+	test_cmp expect.double output
 '
 
 test_expect_success "--ignore-submodules=all suppresses submodule summary" '
-- 
2.44.0.463.g71abcb3a9f
Previous: Jeff KingNext: Phillip Wood
Message 42 of 82 in “Clarify the meaning of "character" in the documentation”
  1. Manlio PerilloMar 5, 2024
  2. Kristoffer HaugsbakkMar 5, 2024
  3. Junio C HamanoMar 5, 2024
  4. Dragan SimicMar 5, 2024
  5. Kristoffer HaugsbakkMar 5, 2024
  6. Dragan SimicMar 5, 2024
  7. Kristoffer HaugsbakkMar 5, 2024
  8. Dragan SimicMar 5, 2024
  9. Junio C HamanoMar 5, 2024
  10. Dragan SimicMar 5, 2024
  11. multi-byte core.commentCharJeff King, Mar 6, 2024
  12. 0/15 allow multi-byte core.commentCharJeff King, Mar 7, 2024
  13. 01/15 strbuf: simplify comment-handling in add_lines() helperJeff King, Mar 7, 2024
  14. 02/15 strbuf: avoid static variables in strbuf_add_commented_lines()Jeff King, Mar 7, 2024
  15. 03/15 commit: refactor base-case of adjust_comment_line_char()Jeff King, Mar 7, 2024
  16. 04/15 strbuf: avoid shadowing global comment_line_char nameJeff King, Mar 7, 2024
  17. 05/15 environment: store comment_line_char as a stringJeff King, Mar 7, 2024
  18. 06/15 strbuf: accept a comment string for strbuf_stripspace()Jeff King, Mar 7, 2024
  19. Jeff KingMar 7, 2024
  20. 07/15 strbuf: accept a comment string for strbuf_commented_addf()Jeff King, Mar 7, 2024
  21. 08/15 strbuf: accept a comment string for strbuf_add_commented_lines()Jeff King, Mar 7, 2024
  22. 09/15 prefer comment_line_str to comment_line_char for printingJeff King, Mar 7, 2024
  23. 10/15 find multi-byte comment chars in NUL-terminated stringsJeff King, Mar 7, 2024
  24. 11/15 find multi-byte comment chars in unterminated buffersJeff King, Mar 7, 2024
  25. Jeff KingMar 7, 2024
  26. René ScharfeMar 7, 2024
  27. René ScharfeMar 7, 2024
  28. René ScharfeMar 7, 2024
  29. Phillip WoodMar 8, 2024
  30. Junio C HamanoMar 8, 2024
  31. Phillip WoodMar 8, 2024
  32. Jeff KingMar 12, 2024
  33. phillip.wood123@gmail.comMar 12, 2024
  34. Jeff KingMar 13, 2024
  35. Jeff KingMar 12, 2024
  36. René ScharfeMar 14, 2024
  37. 12/15 sequencer: handle multi-byte comment characters when writing todo listJeff King, Mar 7, 2024
  38. Phillip WoodMar 8, 2024
  39. Jeff KingMar 12, 2024
  40. 13/15 wt-status: drop custom comment-char stringificationJeff King, Mar 7, 2024
  41. 14/15 environment: drop comment_line_char compatibility macroJeff King, Mar 7, 2024
  42. 15/15 config: allow multi-byte core.commentCharJeff King, Mar 7, 2024
  43. Phillip WoodMar 8, 2024
  44. 0/16 allow multi-byte core.commentCharJeff King, Mar 12, 2024
  45. 01/16 config: forbid newline as core.commentCharJeff King, Mar 12, 2024
  46. 02/16 strbuf: simplify comment-handling in add_lines() helperJeff King, Mar 12, 2024
  47. 03/16 strbuf: avoid static variables in strbuf_add_commented_lines()Jeff King, Mar 12, 2024
  48. 04/16 commit: refactor base-case of adjust_comment_line_char()Jeff King, Mar 12, 2024
  49. 05/16 strbuf: avoid shadowing global comment_line_char nameJeff King, Mar 12, 2024
  50. 06/16 environment: store comment_line_char as a stringJeff King, Mar 12, 2024
  51. 07/16 strbuf: accept a comment string for strbuf_stripspace()Jeff King, Mar 12, 2024
  52. 08/16 strbuf: accept a comment string for strbuf_commented_addf()Jeff King, Mar 12, 2024
  53. 09/16 strbuf: accept a comment string for strbuf_add_commented_lines()Jeff King, Mar 12, 2024
  54. 10/16 prefer comment_line_str to comment_line_char for printingJeff King, Mar 12, 2024
  55. 11/16 find multi-byte comment chars in NUL-terminated stringsJeff King, Mar 12, 2024
  56. 12/16 find multi-byte comment chars in unterminated buffersJeff King, Mar 12, 2024
  57. 13/16 sequencer: handle multi-byte comment characters when writing todo listJeff King, Mar 12, 2024
  58. 14/16 wt-status: drop custom comment-char stringificationJeff King, Mar 12, 2024
  59. 15/16 environment: drop comment_line_char compatibility macroJeff King, Mar 12, 2024
  60. 16/16 config: allow multi-byte core.commentCharJeff King, Mar 12, 2024
  61. Kristoffer HaugsbakkMar 13, 2024
  62. Junio C HamanoMar 13, 2024
  63. Jeff KingMar 15, 2024
  64. Kristoffer HaugsbakkMar 15, 2024
  65. Jeff KingMar 15, 2024
  66. Kristoffer HaugsbakkMar 15, 2024
  67. Junio C HamanoMar 15, 2024
  68. Jeff KingMar 16, 2024
  69. Junio C HamanoMar 26, 2024
  70. Kristoffer HaugsbakkMar 26, 2024
  71. Jeff KingMar 27, 2024
  72. 17/16 config: add core.commentStringJeff King, Mar 27, 2024
  73. Chris TorekMar 27, 2024
  74. Junio C HamanoMar 27, 2024
  75. Jeff KingMar 28, 2024
  76. Junio C HamanoMar 27, 2024
  77. phillip.wood123@gmail.comMar 12, 2024
  78. Junio C HamanoMar 12, 2024
  79. Kristoffer HaugsbakkMar 5, 2024
  80. Junio C HamanoMar 5, 2024
  81. Kristoffer HaugsbakkMar 5, 2024
  82. brian m. carlsonMar 5, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.