From d6f8e3d548306221cd295df561237623cbf928e1 Mon Sep 17 00:00:00 2001 From: Thilo Uttendorfer Date: Mon, 29 Aug 2022 22:32:29 +0200 Subject: [PATCH] Fix ignores when searching subdirectories 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 #1757 --- crates/ignore/src/dir.rs | 20 ++++++++++++++++++-- tests/regression.rs | 13 +++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/ignore/src/dir.rs b/crates/ignore/src/dir.rs index 2577665d5c..41c8e7a7a3 100644 --- a/crates/ignore/src/dir.rs +++ b/crates/ignore/src/dir.rs @@ -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) { diff --git a/tests/regression.rs b/tests/regression.rs index e2c56968a7..cb2d370a2c 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -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()); +});