Skip to content
Myr

ignore: fix problem with searching whitelisted hidden files

Before adoption: this commit came in with the history the project adopted, so no landing records it.

... specifically, when the whitelist comes from a _parent_ gitignore file. Our handling of parent gitignores is pretty ham-fisted and has been a source of some unfortunate bugs. The problem is that we need to strip the parent path from the path we're searching in order to correctly apply the globs. But getting this stripping correct seems to be a subtle affair. Fixes #3173

authored by Andrew Gallant · committed by Andrew Gallant · parent bb88a1a · Browse files · 3 files+37 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+37 added−10 removed

  • tests:+21 added−0 removed
  • other:+16 added−10 removed

3 files+37 added−10 removed

modified:crates/ignore/src/dir.rs+14 added-10 removed
465 unmodified lines
466 self.parents()467 .take_while(|ig| !ig.0.is_absolute_parent)468 .last()469 .map_or(path, |ig| {470 strip_if_is_prefix(471 "/",472 strip_if_is_prefix(473 strip_if_is_prefix(474 "./",475 ig.0.dir.as_path(),476 ),477 path,478 ),479 )470 // This is a weird special case when ripgrep users471 // search with just a `.`, as some tools do472 // automatically (like consult). In this case, if473 // we don't bail out now, the code below will strip474 // a leading `.` from `path`, which might mangle475 // a hidden file name!476 if ig.0.dir.as_path() == Path::new(".") {477 return path;478 }479 let without_dot_slash =480 strip_if_is_prefix("./", ig.0.dir.as_path());481 let relative_base =482 strip_if_is_prefix(without_dot_slash, path);483 strip_if_is_prefix("/", relative_base)484 }),485 );486487 for ig in
764 unmodified lines
modified:tests/regression.rs+21 added
1584 unmodified lines
1585 .stdout();1586 eqnice!("test\n", got);1587 }1588);15891590// See: https://github.com/BurntSushi/ripgrep/issues/31731591rgtest!(r3173_hidden_whitelist_only_dot, |dir: Dir, _: TestCommand| {1592 dir.create_dir("subdir");1593 dir.create("subdir/.foo.txt", "text");1594 dir.create(".ignore", "!.foo.txt");15951596 let cmd = || dir.command();1597 eqnice!(cmd().args(&["--files"]).stdout(), "subdir/.foo.txt\n");1598 eqnice!(cmd().args(&["--files", "."]).stdout(), "./subdir/.foo.txt\n");1599 eqnice!(cmd().args(&["--files", "./"]).stdout(), "./subdir/.foo.txt\n");16001601 let cmd = || {1602 let mut cmd = dir.command();1603 cmd.current_dir(dir.path().join("subdir"));1604 cmd1605 };1606 eqnice!(cmd().args(&["--files"]).stdout(), ".foo.txt\n");1607 eqnice!(cmd().args(&["--files", "."]).stdout(), "./.foo.txt\n");1608 eqnice!(cmd().args(&["--files", "./"]).stdout(), "./.foo.txt\n");1609});
modified:CHANGELOG.md+2 added
35 unmodified lines
36* [BUG #3140](https://github.com/BurntSushi/ripgrep/issues/3140):37 Ensure hyphens in flag names are escaped in the roff text for the man page.38* [BUG #3155](https://github.com/BurntSushi/ripgrep/issues/3155):39 Statically compile PCRE2 into macOS release artifacts on `aarch64`.40* [BUG #3173](https://github.com/BurntSushi/ripgrep/issues/3173):41 Fix ancestor ignore filter bug when searching whitelisted hidden files.4243Feature enhancements:4445* Many enhancements to the default set of file types available for filtering.
1733 unmodified lines