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
39 match self {
40 Self::None => false,
41 Self::All => true,
42 Self::Some {
43 abs_base_path,
44 ignore,
45 parent: prev,
46 } => match ignore.matched(abs_path.strip_prefix(abs_base_path).unwrap(), is_dir) {
47 ignore::Match::None => prev.is_abs_path_ignored(abs_path, is_dir),
48 ignore::Match::Ignore(_) => true,
49 ignore::Match::Whitelist(_) => false,
50 },
51 }
52 }
53}