1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::{ffi::OsStr, path::Path};
use crate::{entry::Entry, ignore::IgnoreDir, options::TreeOptions};
pub struct FilteredEntry {
pub filter_state: FilterState,
pub entry: Entry,
}
impl AsRef<Entry> for FilteredEntry {
fn as_ref(&self) -> &Entry {
&self.entry
}
}
#[derive(Default, Clone)]
pub struct FilterState {
/// This counter is used to reproduce some very unintuitive behaviour in the
/// reference implementation. It is not used unless the --compat option is
/// set.
///
/// In compat mode, if --matchdirs is passed, then include matchers are
/// ignored when the parent directory was matched by an include matcher.
/// However, this is not applied recursively, so if an ancestor directory
/// was already matched, then no more directories can be matched.
///
/// There are three possible states:
///
/// 0: No ancestor directory was matched
/// - If the entry is a file, and it matches the include patterns, then
/// it will be displayed
/// - If the entry is a directory, it will be displayed
/// - If the entry is a directory and matches the include pattern, the
/// value is incremented by 1 for it's children
///
/// 1: The parent directory was matched
/// - If the entry is a file, it will be displayed (include matchers are
/// not checked)
/// - If the entry is a directory, it will be displayed
/// - If the entry is a directory, and it matches the include pattern,
/// the value is incremented by 1 for all children
///
/// 2: An ancestor directory was matched
/// - If the entry is a file, and it matches the include patterns, then
/// it will be displayed
/// - If the entry is a directory, it will be displayed
matched_dir_depth: u8,
}
pub struct TreeFilter<'filter> {
state: FilterState,
ignore_dir: Option<IgnoreDir<'filter>>,
}
impl<'filter> TreeFilter<'filter> {
pub(crate) fn new(dir: &Path, options: &TreeOptions) -> anyhow::Result<Self> {
Ok(Self {
state: FilterState::default(),
ignore_dir: if options.respect_gitignore {
Some(IgnoreDir::new(dir)?)
} else {
None
},
})
}
pub(crate) fn enter_dir(
&'filter self,
dir: &Entry,
_options: &TreeOptions,
state: FilterState,
) -> anyhow::Result<Self> {
if let Some(ignore_dir) = &self.ignore_dir {
Ok(Self {
state,
ignore_dir: Some(ignore_dir.enter_dir(dir.path())?),
})
} else {
Ok(Self {
state,
ignore_dir: None,
})
}
}
#[inline]
fn file_name_included_by_pattern(&self, file_name: &OsStr, options: &TreeOptions) -> bool {
if let Some(file_include_globset) = options.file_include_globset.as_ref() {
if !file_include_globset.is_match(file_name) {
return false;
}
}
true
}
#[inline]
fn file_name_excluded_by_pattern(&self, file_name: &OsStr, options: &TreeOptions) -> bool {
if let Some(file_exclude_globset) = options.file_exclude_globset.as_ref() {
if file_exclude_globset.is_match(file_name) {
return true;
}
}
false
}
pub(crate) fn filter(&self, entry: Entry, options: &TreeOptions) -> Option<FilteredEntry> {
if !options.show_hidden_files && entry.is_hidden() {
return None;
}
let mut filter_state = self.state.clone();
if entry.file_type().is_file() {
if options.list_directories_only {
return None;
}
if self.state.matched_dir_depth != 1
&& !self.file_name_included_by_pattern(entry.file_name(), options)
{
return None;
}
if self.file_name_excluded_by_pattern(entry.file_name(), options) {
return None;
}
} else if entry.file_type().is_dir() {
if options.compat {
let mut matched_dir = false;
if (self.state.matched_dir_depth == 0)
&& options.match_dirs
&& self.file_name_included_by_pattern(entry.file_name(), options)
{
matched_dir = true;
}
if filter_state.matched_dir_depth == 1 || matched_dir {
filter_state.matched_dir_depth += 1;
}
} else if !self.file_name_included_by_pattern(entry.file_name(), options) {
return None;
}
if self.file_name_excluded_by_pattern(entry.file_name(), options) {
return None;
}
}
if !self
.ignore_dir
.as_ref()
.map(|ignore_dir| ignore_dir.include(entry.path(), entry.file_type().is_dir()))
.unwrap_or(true)
{
return None;
}
Some(FilteredEntry {
filter_state,
entry,
})
}
}