Skip to content

Ignore a leading UTF-8 BOM in gitignore lines - #169

Closed
ice-pring wants to merge 1 commit into
cpburnz:masterfrom
ice-pring:fix/ignore-leading-utf8-bom
Closed

ice-pring wants to merge 1 commit into
cpburnz:masterfrom
ice-pring:fix/ignore-leading-utf8-bom

Conversation

@ice-pring

Copy link
Copy Markdown

Reading a UTF-8 BOM-prefixed .gitignore with encoding="utf-8" leaves the BOM in the first pattern. For example:

GitIgnoreSpec.from_lines(["\ufeff*.log\n"]).match_file("test.log")
# False before this change; Git ignores test.log.

Git skips one UTF-8 BOM at the start of an ignore file before parsing its lines (implementation in Git 2.54.0). Strip it at the GitIgnoreSpec.from_lines() boundary, where the first line can be distinguished from later patterns. Comments, negations and anchored directory rules then retain their meaning. A second BOM or a BOM on a later line remains literal. Both text and byte lines are supported, including single-pass iterators and the reversed argument form.

Added regression and control tests, a docstring note and a changelog entry.

Validation on Windows:

  • Focused tests fail before the fix and pass afterward.
  • Tox py312 and py314: the full base suite succeeds on CPython 3.12.14 and 3.14.5, with optional backend/platform skips.
  • With both optional backends installed, python -m unittest discover -t . -s tests/ on CPython 3.14.5 runs all 300 tests successfully, without skips.
  • mypy pathspec tests/check_usage.py passes for all 32 source files.
  • 90 results across 10 representative ignore files agree with git check-ignore --no-index --verbose --non-matching -z using Git 2.54.0.windows.1.
  • git diff --check passes.

@cpburnz

cpburnz commented Oct 23, 2026

Copy link
Copy Markdown
Owner

The appropriate way to handle a UTF-8 BOM is to open the file with encoding="utf-8-sig". That will remove the BOM.

@ice-pring

Copy link
Copy Markdown
Author

Thanks for clarifying the intended behavior. Using encoding="utf-8-sig" when opening the file makes sense, so I'll close this PR and leave BOM handling to the caller. I appreciate your review.

@ice-pring ice-pring closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants