1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-08-04 21:52:54 +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 #829, Fixes #2731, Fixes #2747, Fixes #2778, Fixes #2836, Fixes #2933
Closes #2933
This commit is contained in:
ChristopherYoung
2024-11-15 18:40:27 +08:00
committed by Andrew Gallant
parent 78803979c5
commit c2f1653ddd
3 changed files with 178 additions and 15 deletions

View File

@ -461,21 +461,23 @@ impl Ignore {
// off of `path`. Overall, this seems a little ham-fisted, but
// it does fix a nasty bug. It should do fine until we overhaul
// this crate.
let dirpath = self.0.dir.as_path();
let path_prefix = match strip_prefix("./", dirpath) {
None => dirpath,
Some(stripped_dot_slash) => stripped_dot_slash,
};
let path = match strip_prefix(path_prefix, path) {
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
self.parents().skip_while(|ig| !ig.0.is_absolute_parent)
@ -874,6 +876,15 @@ fn resolve_git_commondir(
Ok(commondir_abs)
}
/// Strips `prefix` from `path` if it's a prefix, otherwise returns `path`
/// unchanged.
fn strip_if_is_prefix<'a, P: AsRef<Path> + ?Sized>(
prefix: &'a P,
path: &'a Path,
) -> &'a Path {
strip_prefix(prefix, path).map_or(path, |p| p)
}
#[cfg(test)]
mod tests {
use std::{io::Write, path::Path};