Skip to content

Skip a UTF-8 BOM at the start of an ignore file - #37

Merged
andrew merged 1 commit into
git-pkgs:mainfrom
AbuYahya94:skip-utf8-bom
Nov 24, 2026
Merged

andrew merged 1 commit into
git-pkgs:mainfrom
AbuYahya94:skip-utf8-bom

Conversation

@AbuYahya94

Copy link
Copy Markdown
Contributor

A .gitignore saved with a UTF-8 byte order mark kept the mark as part of its first line, so *.log turned into a pattern that starts with the mark and never matched.

Reproduce

.gitignore = <BOM>*.log: x.log is not ignored by this package; git check-ignore x.log says it is.

Why

Git skips the mark before splitting lines: skip_utf8_bom, called from add_patterns_from_buffer in dir.c.

Change

Trim the mark in addPatterns, which covers files and AddPatterns alike. A mark anywhere else is still part of its pattern, as in git.

Testing

  • New unit test, plus a conformance case checked against git check-ignore.
  • go test -race ./...: 220 of 220 pass.
  • golangci-lint: 0 issues.

Found by a differential run of ignore implementations against Git 2.56.1. Prepared with AI assistance (Claude) and reviewed by me.

🤖 Generated with Claude Code

A .gitignore saved with a byte order mark kept the mark as part of its
first line, so "*.log" turned into a pattern that starts with the mark
and never matched. git skips the mark before splitting lines
(skip_utf8_bom, called from add_patterns_from_buffer in dir.c), so the
first pattern works there.

Trim it in addPatterns, which covers files and AddPatterns alike. A mark
anywhere else is still part of its pattern, as in git.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 55ad92b4-8192-4dda-b3f7-42aa863d91a9
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@andrew

andrew commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, the fix matches what I see from git 2.56.0 here too.

I'm curious about the differential run you mentioned. Which implementations did you compare, and how were the patterns and paths generated (hand-written cases, fuzzing, real-world .gitignore files)? Is the harness public anywhere?

Did it turn up any other places where this library disagrees with git check-ignore? Even a raw list of failing pattern/path pairs would be useful, they can go straight into testdata/conformance.

@andrew
andrew merged commit 71c24a6 into git-pkgs:main Nov 24, 2026
8 of 9 checks passed
@AbuYahya94

Copy link
Copy Markdown
Contributor Author

Thanks for merging, Andrew. Answers to your questions:

Harness. It's public now: https://github.com/AbuYahya94/gitignore-differential. It was built and run by AI coding agents (Claude) that I directed, so please treat the results as leads to confirm. Each named case was re-run with a separately written checker (verify/).

What was compared. git 2.56.1 as the reference, against 19 projects: Apple git 2.50.1, ripgrep, fd, the ignore crate, gitoxide, libgit2, JGit, go-git, this library, sabhiram/go-gitignore, dulwich, pathspec, gitignore_parser, node-ignore, globby, plus ignore-walk and the two dockerignore matchers as separate dialects.

How cases were generated. 20,000 seeded cases from a small grammar biased toward the subtle features, plus 298 corpus cases (170 from t3070-wildmatch.sh, 9 from t0008-ignores.sh, 17 from gitignore(5), 102 hand-written). No real-world .gitignore files; KaizenShogun/gitignore-conformance already covers that side. Every disagreement is reduced to the smallest pattern set and path that still disagrees.

This library. v1.3.0 differed from git on 348 of 20,298 cases (1.7%), which reduce to 95 minimal vectors. 75 of those were the BOM case this PR fixed. The other 20 were re-checked against main (71c24a6) and real git (2.56.1 and Apple 2.50.1 agree on all of them) and come down to four causes:

  1. Slash inside a bracket expression: [a/] vs a. git ignores it, the library doesn't. buildSegments splits on / before brackets are parsed, so the pattern is rejected as an unclosed bracket.
  2. Reversed range: [c-a] vs c. git ignores it, the library doesn't.
  3. Backslash at the end of a pattern or segment: a\ vs a file named a\. git doesn't ignore it, the library does. Related: a\/b vs a/b is ignored by git but not by the library.
  4. Trailing space after an escaped backslash: a\\ (with the trailing space) vs a file named a\. git ignores it, the library doesn't.

I have these as six cases in the testdata/conformance format on a branch, with skip files following your convention, so go test ./... still passes. I can open a PR if you'd like them. One caveat: three of the cases need backslashes in file names, which I expect won't work on the Windows CI job. I haven't tested on Windows or Linux.

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