@ 261027.5 · fetching

250919.8 · 483628469a2580e4eab0edfbe7a7f3f8b1951848

ignore/gitignore: skip BOM at start of ignore file

This matches Git's behavior.

Fixes #2177, Closes #2782

reading declarations

CHANGELOG.md

@@ -11,6 +11,8 @@ Bug fixes:   [BUG #2836](https://github.com/BurntSushi/ripgrep/issues/2836),   [BUG #2933](https://github.com/BurntSushi/ripgrep/pull/2933):   Fix bug related to gitignores from parent directories.+* [BUG #2177](https://github.com/BurntSushi/ripgrep/issues/2177):+  Ignore a UTF-8 BOM marker at the start of `.gitignore` (and similar files).  Feature enhancements: 

crates/ignore/src/gitignore.rs

@@ -402,6 +402,12 @@ impl GitignoreBuilder {                     break;                 }             };++            // Match Git's handling of .gitignore files that begin with the Unicode BOM+            const UTF8_BOM: &str = "\u{feff}";+            let line =+                if i == 0 { line.trim_start_matches(UTF8_BOM) } else { &line };+             if let Err(err) = self.add_line(Some(path.to_path_buf()), &line) {                 errs.push(err.tagged(path, lineno));             }

crates/ignore/tests/gitignore_skip_bom.gitignore

new file mode 100644@@ -0,0 +1,2 @@+ignore/this/path+# This file begins with a BOM (U+FEFF)

crates/ignore/tests/gitignore_skip_bom.rs

new file mode 100644@@ -0,0 +1,17 @@+use ignore::gitignore::GitignoreBuilder;++const IGNORE_FILE: &'static str = "tests/gitignore_skip_bom.gitignore";++/// Skip a Byte-Order Mark (BOM) at the beginning of the file, matching Git's+/// behavior.+///+/// Ref: <https://github.com/BurntSushi/ripgrep/issues/2177>+#[test]+fn gitignore_skip_bom() {+    let mut builder = GitignoreBuilder::new("ROOT");+    let error = builder.add(IGNORE_FILE);+    assert!(error.is_none(), "failed to open gitignore file");+    let g = builder.build().unwrap();++    assert!(g.matched("ignore/this/path", false).is_ignore());+}