Skip to content

Commit c3954b7

Browse files
committed
gitignore: strip UTF-8 BOM as git would
GitIgnoreBuilder::add() would do the same thing, but we use add_line() to control file reading behavior. Fixes #9105
1 parent 1a28043 commit c3954b7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
3131

3232
### Fixed bugs
3333

34+
* `.gitignore` with UTF-8 BOM can now be parsed correctly.
35+
3436
## [0.39.0] - 2026-03-04
3537

3638
### Release highlights

lib/src/gitignore.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl GitIgnoreFile {
6868
input: &[u8],
6969
) -> Result<Arc<Self>, GitIgnoreError> {
7070
let mut builder = gitignore::GitignoreBuilder::new(prefix);
71-
for (i, input_line) in input.split(|b| *b == b'\n').enumerate() {
71+
for (i, input_line) in strip_bom(input).split(|b| *b == b'\n').enumerate() {
7272
if input_line.starts_with(b"#") {
7373
continue;
7474
}
@@ -153,6 +153,10 @@ impl GitIgnoreFile {
153153
}
154154
}
155155

156+
fn strip_bom(text: &[u8]) -> &[u8] {
157+
text.strip_prefix("\u{feff}".as_bytes()).unwrap_or(text)
158+
}
159+
156160
#[cfg(test)]
157161
mod tests {
158162

@@ -408,6 +412,12 @@ mod tests {
408412
assert!(matches(b"a/x**y/b\n", "a/xzzzy/b"));
409413
}
410414

415+
#[test]
416+
fn test_gitignore_with_utf8_bom() {
417+
assert!(matches(b"\xef\xbb\xbffoo\n", "foo"));
418+
assert!(!matches(b"\n\xef\xbb\xbffoo\n", "foo"));
419+
}
420+
411421
#[test]
412422
fn test_gitignore_line_ordering() {
413423
assert!(matches(b"foo\n!foo/bar\n", "foo"));

0 commit comments

Comments
 (0)