Skip to content

Commit

Permalink
ignore/gitignore: Skip BOM at start of ignore file
Browse files Browse the repository at this point in the history
Match Git's behavior.
Fixes #2177.
  • Loading branch information
starthal committed Apr 17, 2024
1 parent d922b7a commit 4ab99ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/ignore/src/gitignore.rs
Expand Up @@ -401,6 +401,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));
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ignore/tests/gitignore_skip_bom.gitignore
@@ -0,0 +1,2 @@
ignore/this/path
# This file begins with a BOM (U+FEFF)
14 changes: 14 additions & 0 deletions crates/ignore/tests/gitignore_skip_bom.rs
@@ -0,0 +1,14 @@
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.
#[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());
}

0 comments on commit 4ab99ab

Please sign in to comment.