mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-11 14:30:24 +02:00
ignore: fix filtering searching subdir or .ignore in parent dir
The previous code deleted too many parts of the path when constructing the absolute path, resulting in a shortened final path. This patch creates the correct absolute path by only removing the necessary parts. Fixes #2836
This commit is contained in:
@ -461,21 +461,31 @@ impl Ignore {
|
|||||||
// off of `path`. Overall, this seems a little ham-fisted, but
|
// off of `path`. Overall, this seems a little ham-fisted, but
|
||||||
// it does fix a nasty bug. It should do fine until we overhaul
|
// it does fix a nasty bug. It should do fine until we overhaul
|
||||||
// this crate.
|
// this crate.
|
||||||
let dirpath = self.0.dir.as_path();
|
|
||||||
let path_prefix = match strip_prefix("./", dirpath) {
|
fn strip_if_is_prefix<'a, P: AsRef<Path> + ?Sized>(
|
||||||
None => dirpath,
|
prefix: &'a P,
|
||||||
Some(stripped_dot_slash) => stripped_dot_slash,
|
path: &'a Path,
|
||||||
};
|
) -> &'a Path {
|
||||||
let path = match strip_prefix(path_prefix, path) {
|
strip_prefix(prefix, path).map_or(path, |p| p)
|
||||||
None => abs_parent_path.join(path),
|
|
||||||
Some(p) => {
|
|
||||||
let p = match strip_prefix("/", p) {
|
|
||||||
None => p,
|
|
||||||
Some(p) => p,
|
|
||||||
};
|
|
||||||
abs_parent_path.join(p)
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
let path = abs_parent_path.join(
|
||||||
|
self.parents()
|
||||||
|
.take_while(|ig| !ig.0.is_absolute_parent)
|
||||||
|
.last()
|
||||||
|
.map_or(path, |ig| {
|
||||||
|
strip_if_is_prefix(
|
||||||
|
"/",
|
||||||
|
strip_if_is_prefix(
|
||||||
|
strip_if_is_prefix(
|
||||||
|
"./",
|
||||||
|
ig.0.dir.as_path(),
|
||||||
|
),
|
||||||
|
path,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
for ig in
|
for ig in
|
||||||
self.parents().skip_while(|ig| !ig.0.is_absolute_parent)
|
self.parents().skip_while(|ig| !ig.0.is_absolute_parent)
|
||||||
|
@ -965,6 +965,15 @@ rgtest!(f1757, |dir: Dir, _: TestCommand| {
|
|||||||
eqnice!("rust/source.rs\n", dir.command().args(args).stdout());
|
eqnice!("rust/source.rs\n", dir.command().args(args).stdout());
|
||||||
let args = &["--files-with-matches", "needle", "./rust"];
|
let args = &["--files-with-matches", "needle", "./rust"];
|
||||||
eqnice!("./rust/source.rs\n", dir.command().args(args).stdout());
|
eqnice!("./rust/source.rs\n", dir.command().args(args).stdout());
|
||||||
|
|
||||||
|
dir.create_dir("rust1/target/onemore");
|
||||||
|
dir.create(".ignore", "rust1/target/onemore");
|
||||||
|
dir.create("rust1/source.rs", "needle");
|
||||||
|
dir.create("rust1/target/onemore/rustdoc-output.html", "needle");
|
||||||
|
let args = &["--files-with-matches", "needle", "rust1"];
|
||||||
|
eqnice!("rust1/source.rs\n", dir.command().args(args).stdout());
|
||||||
|
let args = &["--files-with-matches", "needle", "./rust1"];
|
||||||
|
eqnice!("./rust1/source.rs\n", dir.command().args(args).stdout());
|
||||||
});
|
});
|
||||||
|
|
||||||
// See: https://github.com/BurntSushi/ripgrep/issues/1765
|
// See: https://github.com/BurntSushi/ripgrep/issues/1765
|
||||||
@ -1217,3 +1226,13 @@ rgtest!(r2658_null_data_line_regexp, |dir: Dir, mut cmd: TestCommand| {
|
|||||||
let got = cmd.args(&["--null-data", "--line-regexp", r"bar"]).stdout();
|
let got = cmd.args(&["--null-data", "--line-regexp", r"bar"]).stdout();
|
||||||
eqnice!("haystack:bar\0", got);
|
eqnice!("haystack:bar\0", got);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
rgtest!(f2836, |dir: Dir, mut cmd: TestCommand| {
|
||||||
|
dir.create_dir("testdir/sub/sub2");
|
||||||
|
dir.create(".ignore", "/testdir/sub/sub2/");
|
||||||
|
dir.create("testdir/sub/sub2/testfile", "needle");
|
||||||
|
|
||||||
|
let args = &["--files-with-matches", "needle"];
|
||||||
|
cmd.current_dir(dir.path().join("testdir"));
|
||||||
|
cmd.args(args).assert_err();
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user