Skip to content

Commit 727565e

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43072b4 commit 727565e

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,8 @@ static int git_default_core_config(const char *var, const char *value,
15661566
else if (!strcasecmp(value, "auto"))
15671567
auto_comment_line_char = 1;
15681568
else if (value[0] && !value[1]) {
1569+
if (value[0] == '\n')
1570+
return error(_("core.commentChar cannot be newline"));
15691571
comment_line_char = value[0];
15701572
auto_comment_line_char = 0;
15711573
} else

t/t0030-stripspace.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ 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 'newline as commentchar is forbidden' '
405+
test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err &&
406+
grep "core.commentChar cannot be newline" err
407+
'
408+
404409
test_expect_success '-c with single line' '
405410
printf "# foo\n" >expect &&
406411
printf "foo" | git stripspace -c >actual &&

0 commit comments

Comments
 (0)