Skip to content

Commit

Permalink
ignore: strip BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrg committed Apr 12, 2022
1 parent ced5b92 commit 18da36a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ignore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ regex = "1.1"
same-file = "1.0.4"
thread_local = "1"
walkdir = "2.2.7"
encoding_rs_io = "0.1.6"

[target.'cfg(windows)'.dependencies.winapi-util]
version = "0.1.2"
Expand Down
8 changes: 7 additions & 1 deletion crates/ignore/src/gitignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::{Path, PathBuf};
use std::str;
use std::sync::Arc;

use encoding_rs_io::DecodeReaderBytesBuilder;
use globset::{Candidate, GlobBuilder, GlobSet, GlobSetBuilder};
use regex::bytes::Regex;
use thread_local::ThreadLocal;
Expand Down Expand Up @@ -389,7 +390,12 @@ impl GitignoreBuilder {
Err(err) => return Some(Error::Io(err).with_path(path)),
Ok(file) => file,
};
let rdr = io::BufReader::new(file);
let rdr = io::BufReader::new(
DecodeReaderBytesBuilder::new()
.bom_sniffing(false)
.strip_bom(true)
.build(file),
);
let mut errs = PartialErrorBuilder::default();
for (i, line) in rdr.lines().enumerate() {
let lineno = (i + 1) as u64;
Expand Down
1 change: 1 addition & 0 deletions crates/ignore/tests/bom_test.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignoreme
23 changes: 23 additions & 0 deletions crates/ignore/tests/bom_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::path::Path;

use ignore::gitignore::{Gitignore, GitignoreBuilder};

const IGNORE_FILE: &'static str = "tests/bom_test.gitignore";

fn get_gitignore() -> Gitignore {
let mut builder = GitignoreBuilder::new("ROOT");
let error = builder.add(IGNORE_FILE);
assert!(error.is_none(), "failed to open gitignore file");
builder.build().unwrap()
}

// First entry should be ignored even when the ignore file starts with a BOM. See
// https://github.com/BurntSushi/ripgrep/issues/2177
#[test]
fn test_match_first_entry() {
let gitignore = get_gitignore();
let path = "ignoreme";
assert!(gitignore
.matched_path_or_any_parents(Path::new(path), true)
.is_ignore());
}

0 comments on commit 18da36a

Please sign in to comment.