Skip to content

Commit

Permalink
Fix ignores when searching subdirectories
Browse files Browse the repository at this point in the history
When searching subdirectories the path was not correctly build and
included duplicate parts. This fix will remove the duplicate part if
possible. It also contains a small optimization that will skip the
second for loop if there is already a match.

Fixes BurntSushi#1757
  • Loading branch information
sengaya committed Aug 29, 2022
1 parent bdf10ab commit d6f8e3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crates/ignore/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,25 @@ impl Ignore {
}
saw_git = saw_git || ig.0.has_git;
}
if self.0.opts.parents {
if self.0.opts.parents
&& m_custom_ignore.is_none()
&& m_ignore.is_none()
&& m_gi.is_none()
&& m_gi_exclude.is_none()
{
if let Some(abs_parent_path) = self.absolute_base() {
let path = abs_parent_path.join(path);
let path_prefix = match self.0.dir.as_path().strip_prefix("./")
{
Ok(stripped_dot_slash) => stripped_dot_slash,
Err(_) => self.0.dir.as_path(),
};
let path = match path.strip_prefix(path_prefix) {
Ok(stripped_path_prefix) => {
abs_parent_path.join(stripped_path_prefix)
}
Err(_) => abs_parent_path.join(path),
};

for ig in
self.parents().skip_while(|ig| !ig.0.is_absolute_parent)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,3 +1126,16 @@ rgtest!(r2236, |dir: Dir, mut cmd: TestCommand| {
dir.create("foo/bar", "test\n");
cmd.args(&["test"]).assert_err();
});

// See: https://github.com/BurntSushi/ripgrep/issues/1757
rgtest!(f1757, |dir: Dir, _: TestCommand| {
dir.create_dir("rust/target");
dir.create(".ignore", "rust/target");
dir.create("rust/source.rs", "needle");
dir.create("rust/target/rustdoc-output.html", "needle");

let args = &["--files-with-matches", "needle", "rust"];
eqnice!("rust/source.rs\n", dir.command().args(args).stdout());
let args = &["--files-with-matches", "needle", "./rust"];
eqnice!("./rust/source.rs\n", dir.command().args(args).stdout());
});

0 comments on commit d6f8e3d

Please sign in to comment.