Conversation
A `#` or `;` outside quotes starts a comment in git, with or without a space before it and whether or not the value is quoted. The parser only cut a `;` that was preceded by whitespace in an unquoted value, so `name = Alice # work` read back with the comment attached, and `k = "quoted" # after` was mistaken for an unterminated multi-line quote and returned `quoted" # after`. `strip_inline_comment` cuts the comment before the quote-structure branches, using the same quote- and escape-aware scan that `is_line_continuation` already uses, so all three branches see comment-free text. Escape handling is untouched. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
rawsun007
force-pushed
the
inline-comment-stripping
branch
from
September 17, 2026 09:46
71af3aa to
452878f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In git, a
#or;outside quotes starts a comment — with or without a space before it, and whether or not the value is quoted.GitConfigParsercut only a;that was preceded by whitespace in a value that did not start with":Measured against
git config -f <file> --get a.kon git 2.50.1:[a]\n\tk = …value # commentvaluevalue # commentvaluevalue ; commentvaluevaluevaluevalue#nospacevaluevalue#nospacevaluevalue;nospacevaluevalue;nospacevaluea # b ; caa # ba"quoted" # afterquotedquoted" # afterquotedThe last row is the worst of them: because the value starts with
"and does not end with one, the comment made it look like an unterminated quote, so it was read as the start of a multi-line value and the following lines were swallowed into it.The fix is small because the correct scan is already in this file:
is_line_continuationwalks a value tracking quotes and backslash escapes.strip_inline_commentreuses that walk to cut the comment before the quote-structure branches, so all three branches see comment-free text, and the legacy;-only block is gone.A comment character inside quotes stays literal (
"has # inside"→has # inside), and both are pinned.Escape handling is deliberately untouched. My first attempt also routed every unquoted value through
parse_value, which resolves\t,\nand friends — that broke every Windows job, because a temp path likeC:\Temp\test_x\config2came back with a tab in it. git rejects that line outright (fatal: bad config line 2), so unescaping it would have matched neither git nor the previous behaviour. Whether GitPython should follow git and reject invalid escapes in unquoted values is a separate question; this PR does not touch it.Verification:
test/test_config.pyis 46 passed, 2 skipped, 14 subtests. Reverting onlygit/config.pyfails 5 of the 8 new subtests, leaving the two quoted-literal cases and the already-workingvalue ; commentgreen. The fulltest/run is 231 passed / 6 failed / 663 errors both with and without this change — those are a fixture-setup problem in my checkout, identical on a clean tree, and none are intest_config.py.Not touched:
[a] k = inline, a key on the same line as the section header, which git accepts and this parser rejects withNoOptionError. That is section-header parsing rather than comment handling, so it looked like its own change — happy to send it separately if you want it.