This commit is contained in:
Cole Miller 2025-07-29 10:43:00 -04:00
parent 890ff160d7
commit 37336c6211
2 changed files with 54 additions and 31 deletions

View file

@ -1,42 +1,60 @@
use ignore::gitignore::Gitignore;
use std::{ffi::OsStr, path::Path, sync::Arc};
#[derive(Clone, Debug)]
pub struct IgnoreStack {
pub repo_root: Option<Arc<Path>>,
pub top: Arc<IgnoreStackEntry>,
}
#[derive(Debug)]
pub enum IgnoreStack {
pub enum IgnoreStackEntry {
None,
Global {
repo_root: Option<Arc<Path>>,
ignore: Arc<Gitignore>,
},
Some {
abs_base_path: Arc<Path>,
ignore: Arc<Gitignore>,
parent: Arc<IgnoreStack>,
parent: Arc<IgnoreStackEntry>,
},
All,
}
impl IgnoreStack {
pub fn none() -> Arc<Self> {
Arc::new(Self::None)
pub fn none() -> Self {
Self {
repo_root: None,
top: Arc::new(IgnoreStackEntry::None),
}
}
pub fn all() -> Arc<Self> {
Arc::new(Self::All)
pub fn all() -> Self {
Self {
repo_root: None,
top: Arc::new(IgnoreStackEntry::All),
}
}
pub fn global(repo_root: Option<Arc<Path>>, ignore: Arc<Gitignore>) -> Arc<Self> {
Arc::new(Self::Global { repo_root, ignore })
pub fn global(ignore: Arc<Gitignore>) -> Self {
Self {
repo_root: None,
top: Arc::new(IgnoreStackEntry::Global { ignore }),
}
}
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 {
pub fn append(self, abs_base_path: Arc<Path>, ignore: Arc<Gitignore>) -> Self {
let top = match self.top.as_ref() {
IgnoreStackEntry::All => self.top.clone(),
_ => Arc::new(IgnoreStackEntry::Some {
abs_base_path,
ignore,
parent: self,
parent: self.top.clone(),
}),
};
Self {
repo_root: self.repo_root,
top,
}
}
@ -45,12 +63,12 @@ impl IgnoreStack {
return true;
}
match self {
Self::None => false,
Self::All => true,
Self::Global { repo_root, ignore } => {
match self.top.as_ref() {
IgnoreStackEntry::None => false,
IgnoreStackEntry::All => true,
IgnoreStackEntry::Global { ignore } => {
let combined_path;
let abs_path = if let Some(repo_root) = repo_root {
let abs_path = if let Some(repo_root) = self.repo_root.as_ref() {
combined_path = ignore.path().join(
abs_path
.strip_prefix(repo_root)
@ -66,12 +84,16 @@ impl IgnoreStack {
ignore::Match::Whitelist(_) => false,
}
}
Self::Some {
IgnoreStackEntry::Some {
abs_base_path,
ignore,
parent: prev,
} => 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::None => IgnoreStack {
repo_root: self.repo_root.clone(),
top: prev.clone(),
}
.is_abs_path_ignored(abs_path, is_dir),
ignore::Match::Ignore(_) => true,
ignore::Match::Whitelist(_) => false,
},

View file

@ -2777,12 +2777,7 @@ impl LocalSnapshot {
inodes
}
fn ignore_stack_for_abs_path(
&self,
abs_path: &Path,
is_dir: bool,
fs: &dyn Fs,
) -> Arc<IgnoreStack> {
fn ignore_stack_for_abs_path(&self, abs_path: &Path, is_dir: bool, fs: &dyn Fs) -> IgnoreStack {
let mut new_ignores = Vec::new();
let mut repo_root = None;
for (index, ancestor) in abs_path.ancestors().enumerate() {
@ -2803,10 +2798,11 @@ impl LocalSnapshot {
}
let mut ignore_stack = if let Some(global_gitignore) = self.global_gitignore.clone() {
IgnoreStack::global(repo_root, global_gitignore)
IgnoreStack::global(global_gitignore)
} else {
IgnoreStack::none()
};
ignore_stack.repo_root = repo_root;
for (parent_abs_path, ignore) in new_ignores.into_iter().rev() {
if ignore_stack.is_abs_path_ignored(parent_abs_path, true) {
ignore_stack = IgnoreStack::all();
@ -4369,12 +4365,14 @@ impl BackgroundScanner {
swap_to_front(&mut child_paths, *GITIGNORE);
swap_to_front(&mut child_paths, *DOT_GIT);
let mut repo_root = None;
for child_abs_path in child_paths {
let child_abs_path: Arc<Path> = child_abs_path.into();
let child_name = child_abs_path.file_name().unwrap();
let child_path: Arc<Path> = job.path.join(child_name).into();
if child_name == *DOT_GIT {
repo_root = Some(child_abs_path.clone());
let mut state = self.state.lock();
state.insert_git_repository(
child_path.clone(),
@ -4386,6 +4384,9 @@ impl BackgroundScanner {
Ok(ignore) => {
let ignore = Arc::new(ignore);
ignore_stack = ignore_stack.append(job.abs_path.clone(), ignore.clone());
if let Some(repo_root) = repo_root.clone() {
ignore_stack.repo_root = Some(repo_root);
}
new_ignore = Some(ignore);
}
Err(error) => {
@ -4688,7 +4689,7 @@ impl BackgroundScanner {
&self,
scan_job_tx: Sender<ScanJob>,
prev_snapshot: LocalSnapshot,
mut ignores_to_update: impl Iterator<Item = (Arc<Path>, Arc<IgnoreStack>)>,
mut ignores_to_update: impl Iterator<Item = (Arc<Path>, IgnoreStack)>,
) {
let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
{
@ -5147,7 +5148,7 @@ fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
struct ScanJob {
abs_path: Arc<Path>,
path: Arc<Path>,
ignore_stack: Arc<IgnoreStack>,
ignore_stack: IgnoreStack,
scan_queue: Sender<ScanJob>,
ancestor_inodes: TreeSet<u64>,
is_external: bool,
@ -5155,7 +5156,7 @@ struct ScanJob {
struct UpdateIgnoreStatusJob {
abs_path: Arc<Path>,
ignore_stack: Arc<IgnoreStack>,
ignore_stack: IgnoreStack,
ignore_queue: Sender<UpdateIgnoreStatusJob>,
scan_queue: Sender<ScanJob>,
}