Use absolute paths to compute ignored status

This lays the groundwork for harvesting gitignores up above the
worktree.
This commit is contained in:
Antonio Scandurra 2022-07-08 10:56:58 +02:00
parent ca225d0765
commit 9328ab121a
2 changed files with 87 additions and 54 deletions

View file

@ -4,7 +4,7 @@ use std::{ffi::OsStr, path::Path, sync::Arc};
pub enum IgnoreStack {
None,
Some {
base: Arc<Path>,
abs_base_path: Arc<Path>,
ignore: Arc<Gitignore>,
parent: Arc<IgnoreStack>,
},
@ -24,19 +24,19 @@ impl IgnoreStack {
matches!(self, IgnoreStack::All)
}
pub fn append(self: Arc<Self>, base: Arc<Path>, ignore: Arc<Gitignore>) -> Arc<Self> {
pub fn append(self: Arc<Self>, abs_base_path: Arc<Path>, ignore: Arc<Gitignore>) -> Arc<Self> {
match self.as_ref() {
IgnoreStack::All => self,
_ => Arc::new(Self::Some {
base,
abs_base_path,
ignore,
parent: self,
}),
}
}
pub fn is_path_ignored(&self, path: &Path, is_dir: bool) -> bool {
if is_dir && path.file_name() == Some(OsStr::new(".git")) {
pub fn is_abs_path_ignored(&self, abs_path: &Path, is_dir: bool) -> bool {
if is_dir && abs_path.file_name() == Some(OsStr::new(".git")) {
return true;
}
@ -44,11 +44,11 @@ impl IgnoreStack {
Self::None => false,
Self::All => true,
Self::Some {
base,
abs_base_path,
ignore,
parent: prev,
} => match ignore.matched(path.strip_prefix(base).unwrap(), is_dir) {
ignore::Match::None => prev.is_path_ignored(path, is_dir),
} => match ignore.matched(abs_path.strip_prefix(abs_base_path).unwrap(), is_dir) {
ignore::Match::None => prev.is_abs_path_ignored(abs_path, is_dir),
ignore::Match::Ignore(_) => true,
ignore::Match::Whitelist(_) => false,
},