1use ignore::gitignore::Gitignore;
2use std::{ffi::OsStr, path::Path, sync::Arc};
3
4pub enum IgnoreStack {
5 None,
6 Some {
7 abs_base_path: Arc<Path>,
8 ignore: Arc<Gitignore>,
9 parent: Arc<IgnoreStack>,
10 },
11 All,
12}
13
14impl IgnoreStack {
15 pub fn none() -> Arc<Self> {
16 Arc::new(Self::None)
17 }
18
19 pub fn all() -> Arc<Self> {
20 Arc::new(Self::All)
21 }
22
23 pub fn append(self: Arc<Self>, abs_base_path: Arc<Path>, ignore: Arc<Gitignore>) -> Arc<Self> {
24 match self.as_ref() {
25 IgnoreStack::All => self,
26 _ => Arc::new(Self::Some {
27 abs_base_path,
28 ignore,
29 parent: self,
30 }),
31 }
32 }
33
34 pub fn is_abs_path_ignored(&self, abs_path: &Path, is_dir: bool) -> bool {
35 if is_dir && abs_path.file_name() == Some(OsStr::new(".git")) {
36 return true;
37 }
38 match self {
39 Self::None => false,
40 Self::All => true,
41 Self::Some {
42 abs_base_path,
43 ignore,
44 parent: prev,
45 } => match ignore.matched(abs_path.strip_prefix(abs_base_path).unwrap(), is_dir) {
46 ignore::Match::None => prev.is_abs_path_ignored(abs_path, is_dir),
47 ignore::Match::Ignore(_) => true,
48 ignore::Match::Whitelist(_) => false,
49 },
50 }
51 }
52}