fix: skip a UTF-8 BOM when reading config files - #2249
Merged
Merged
Conversation
git ignores a UTF-8 BOM at the start of a config file, so a file written by a Windows editor still parses. GitConfigParser raised MissingSectionHeaderError instead, because the BOM was decoded into the first line, which then no longer matched a section header. Evidence on git 2.47.3: `git config -f bom.cfg --list` prints core.bare=true for a file starting with the three BOM bytes, while GitPython raised MissingSectionHeaderError. With this change both read the same values, also when the BOM file is pulled in through include.path. The new test fails without the change.
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.
Summary
gitskips a UTF-8 BOM at the start of a config file.GitConfigParserraisedMissingSectionHeaderErrorinstead, because the decoded BOM became part of the first line, and that line then no longer matched a section header.Why this is a bug and not malformed input
git reads such a file fine, so GitPython disagreeing with git here is the bug. The error is also hard to act on: the file clearly starts with
[core], yet the message says it contains no section headers.Validation
git 2.47.3, file starting with the three BOM bytes
EF BB BF:GitPython before the change:
After the change
get_value("core", "bare")isTrue, matching git. A BOM file reached throughinclude.pathis read the same as git as well (user.name=bom-inc).Test runs:
pytest test/test_config.py -o addopts= -q->48 passed, 2 skipped, 20 subtests passedpytest test/ -o addopts= -q->988 passed, 82 skipped, 1 xfailedruff check/ruff format --checkon both files -> cleanAI agent disclosure
Per CONTRIBUTING ("Prevent agent impersonation"): this PR was prepared by an AI
agent (Battle Phoenix) driving the
geek-alphaaccount. The account owner reviewedand approved the change, the test and the evidence above.