aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2024-03-12 05:17:06 -0400
committerJunio C Hamano <gitster@pobox.com>2024-03-12 13:28:09 -0700
commit727565ef15f5281fd61bd8b3c5ef2b08bc5096e6 (patch)
tree38e2129d454ca189666cb5b153bbe7a027f02d6e
parent43072b4ca132437f21975ac6acc6b72dc22fd398 (diff)
downloadgit-727565ef15f5281fd61bd8b3c5ef2b08bc5096e6.tar.gz
config: forbid newline as core.commentChar
Since we usually look for a comment char while parsing line-oriented files, setting core.commentChar to a single newline can confuse our code quite a bit. For example, using it with "git commit" causes us to fail to recognize any of the template as comments, including it in the config message. Which kind of makes sense, since the template content is on its own line (so no line can "start" with a newline). In other spots I would not be surprised if you can create more mischief (e.g., violating loop assumptions) but I didn't dig into it. Since comment characters are a local preference, to some degree this is a case of "if it hurts, don't do it". But given that this would be a silly and pointless thing to do, and that it makes it harder to reason about code parsing comment lines, let's just forbid it. There are other cases that are perhaps questionable (e.g., setting the comment char to a single space), but they seem to behave reasonably (at least a simple "git commit" will correctly identify and strip the template lines). So I haven't worried about going on a hunt for every stupid thing a user might do to themselves, and just focused on the most confusing case. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--config.c2
-rwxr-xr-xt/t0030-stripspace.sh5
2 files changed, 7 insertions, 0 deletions
diff --git a/config.c b/config.c
index 3cfeb3d8bd..f561631374 100644
--- a/config.c
+++ b/config.c
@@ -1566,6 +1566,8 @@ static int git_default_core_config(const char *var, const char *value,
else if (!strcasecmp(value, "auto"))
auto_comment_line_char = 1;
else if (value[0] && !value[1]) {
+ if (value[0] == '\n')
+ return error(_("core.commentChar cannot be newline"));
comment_line_char = value[0];
auto_comment_line_char = 0;
} else
diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
index d1b3be8725..e399dd9189 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 'newline as commentchar is forbidden' '
+ test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err &&
+ grep "core.commentChar cannot be newline" err
+'
+
test_expect_success '-c with single line' '
printf "# foo\n" >expect &&
printf "foo" | git stripspace -c >actual &&