gix_pathspec/search/
matching.rs1use bstr::{BStr, BString, ByteSlice};
2use gix_glob::pattern::Case;
3
4use crate::{
5 search::{Match, MatchKind, MatchKind::*, Spec},
6 MagicSignature, Pattern, Search, SearchMode,
7};
8
9impl Search {
10 pub fn pattern_matching_relative_path(
27 &mut self,
28 relative_path: &BStr,
29 is_dir: Option<bool>,
30 attributes: &mut dyn FnMut(&BStr, Case, bool, &mut gix_attributes::search::Outcome) -> bool,
31 ) -> Option<Match<'_>> {
32 static MATCH_ALL_STAND_IN: Pattern = Pattern {
33 path: BString::new(Vec::new()),
34 signature: MagicSignature::empty(),
35 search_mode: SearchMode::ShellGlob,
36 attributes: Vec::new(),
37 prefix_len: 0,
38 nil: true,
39 };
40 if relative_path.is_empty() {
41 return Some(Match {
42 pattern: &MATCH_ALL_STAND_IN,
43 sequence_number: 0,
44 kind: Always,
45 });
46 }
47 let basename_not_important = None;
48 if relative_path
49 .get(..self.common_prefix_len)
50 .map_or(true, |rela_path_prefix| rela_path_prefix != self.common_prefix())
51 {
52 return None;
53 }
54
55 let is_dir = is_dir.unwrap_or(false);
56 let patterns_len = self.patterns.len();
57 let res = self.patterns.iter_mut().find_map(|mapping| {
58 let ignore_case = mapping.value.pattern.signature.contains(MagicSignature::ICASE);
59 let prefix = mapping.value.pattern.prefix_directory();
60 if ignore_case && !prefix.is_empty() {
61 let pattern_requirement_is_met = relative_path.get(prefix.len()).map_or_else(|| is_dir, |b| *b == b'/');
62 if !pattern_requirement_is_met
63 || relative_path.get(..prefix.len()).map(ByteSlice::as_bstr) != Some(prefix)
64 {
65 return None;
66 }
67 }
68
69 let case = if ignore_case { Case::Fold } else { Case::Sensitive };
70 let mut is_match = mapping.value.pattern.always_matches();
71 let mut how = Always;
72 if !is_match {
73 is_match = if mapping.pattern.first_wildcard_pos.is_none() {
74 match_verbatim(mapping, relative_path, is_dir, case, &mut how)
75 } else {
76 let wildmatch_mode = match mapping.value.pattern.search_mode {
77 SearchMode::ShellGlob => Some(gix_glob::wildmatch::Mode::empty()),
78 SearchMode::Literal => None,
79 SearchMode::PathAwareGlob => Some(gix_glob::wildmatch::Mode::NO_MATCH_SLASH_LITERAL),
80 };
81 match wildmatch_mode {
82 Some(wildmatch_mode) => {
83 let is_match = mapping.pattern.matches_repo_relative_path(
84 relative_path,
85 basename_not_important,
86 Some(is_dir),
87 case,
88 wildmatch_mode,
89 );
90 if !is_match {
91 match_verbatim(mapping, relative_path, is_dir, case, &mut how)
92 } else {
93 how = mapping.pattern.first_wildcard_pos.map_or(Verbatim, |_| WildcardMatch);
94 true
95 }
96 }
97 None => match_verbatim(mapping, relative_path, is_dir, case, &mut how),
98 }
99 }
100 }
101
102 if let Some(attrs) = mapping.value.attrs_match.as_mut() {
103 if !attributes(relative_path, Case::Sensitive, is_dir, attrs) {
104 return None;
106 }
107 for (actual, expected) in attrs.iter_selected().zip(mapping.value.pattern.attributes.iter()) {
108 if actual.assignment != expected.as_ref() {
109 return None;
110 }
111 }
112 }
113
114 is_match.then_some(Match {
115 pattern: &mapping.value.pattern,
116 sequence_number: mapping.sequence_number,
117 kind: how,
118 })
119 });
120
121 if res.is_none() && self.all_patterns_are_excluded {
122 Some(Match {
123 pattern: &MATCH_ALL_STAND_IN,
124 sequence_number: patterns_len,
125 kind: Always,
126 })
127 } else {
128 res
129 }
130 }
131
132 pub fn can_match_relative_path(&self, relative_path: &BStr, is_dir: Option<bool>) -> bool {
141 if self.patterns.is_empty() || relative_path.is_empty() {
142 return true;
143 }
144 let common_prefix_len = self.common_prefix_len.min(relative_path.len());
145 if relative_path.get(..common_prefix_len).map_or(true, |rela_path_prefix| {
146 rela_path_prefix != self.common_prefix()[..common_prefix_len]
147 }) {
148 return false;
149 }
150 for mapping in &self.patterns {
151 let pattern = &mapping.value.pattern;
152 if mapping.pattern.first_wildcard_pos == Some(0) && !pattern.is_excluded() {
153 return true;
154 }
155 let max_usable_pattern_len = mapping.pattern.first_wildcard_pos.unwrap_or_else(|| pattern.path.len());
156 let common_len = max_usable_pattern_len.min(relative_path.len());
157
158 let ignore_case = pattern.signature.contains(MagicSignature::ICASE);
159 let mut is_match = pattern.always_matches();
160 if !is_match && common_len != 0 {
161 let pattern_path = pattern.path[..common_len].as_bstr();
162 let longest_possible_relative_path = &relative_path[..common_len];
163 is_match = if ignore_case {
164 pattern_path.eq_ignore_ascii_case(longest_possible_relative_path)
165 } else {
166 pattern_path == longest_possible_relative_path
167 };
168
169 if is_match {
170 is_match = if common_len < max_usable_pattern_len {
171 pattern.path.get(common_len) == Some(&b'/')
172 } else if relative_path.len() > max_usable_pattern_len
173 && mapping.pattern.first_wildcard_pos.is_none()
174 {
175 relative_path.get(common_len) == Some(&b'/')
176 } else {
177 is_match
178 };
179 if let Some(is_dir) = is_dir.filter(|_| pattern.signature.contains(MagicSignature::MUST_BE_DIR)) {
180 is_match = if is_dir {
181 matches!(pattern.path.get(common_len), None | Some(&b'/'))
182 } else {
183 relative_path.get(common_len) == Some(&b'/')
184 };
185 }
186 }
187 }
188 if is_match && (!pattern.is_excluded() || pattern.always_matches()) {
189 return !pattern.is_excluded();
190 }
191 }
192
193 self.all_patterns_are_excluded
194 }
195
196 pub fn directory_matches_prefix(&self, relative_path: &BStr, leading: bool) -> bool {
202 if self.patterns.is_empty() || relative_path.is_empty() {
203 return true;
204 }
205 let common_prefix_len = self.common_prefix_len.min(relative_path.len());
206 if relative_path.get(..common_prefix_len).map_or(true, |rela_path_prefix| {
207 rela_path_prefix != self.common_prefix()[..common_prefix_len]
208 }) {
209 return false;
210 }
211 for mapping in &self.patterns {
212 let pattern = &mapping.value.pattern;
213 if mapping.pattern.first_wildcard_pos.is_some() && pattern.is_excluded() {
214 return true;
215 }
216 let mut rightmost_idx = mapping.pattern.first_wildcard_pos.map_or_else(
217 || pattern.path.len(),
218 |idx| pattern.path[..idx].rfind_byte(b'/').unwrap_or(idx),
219 );
220 let ignore_case = pattern.signature.contains(MagicSignature::ICASE);
221 let mut is_match = pattern.always_matches();
222 if !is_match {
223 let plen = relative_path.len();
224 if leading && rightmost_idx > plen {
225 if let Some(idx) = pattern.path[..plen]
226 .rfind_byte(b'/')
227 .or_else(|| pattern.path[plen..].find_byte(b'/').map(|idx| idx + plen))
228 {
229 rightmost_idx = idx;
230 }
231 }
232 if let Some(relative_path) = relative_path.get(..rightmost_idx) {
233 let pattern_path = pattern.path[..rightmost_idx].as_bstr();
234 is_match = if ignore_case {
235 pattern_path.eq_ignore_ascii_case(relative_path)
236 } else {
237 pattern_path == relative_path
238 };
239 }
240 }
241 if is_match && (!pattern.is_excluded() || pattern.always_matches()) {
242 return !pattern.is_excluded();
243 }
244 }
245
246 self.all_patterns_are_excluded
247 }
248}
249
250fn match_verbatim(
251 mapping: &gix_glob::search::pattern::Mapping<Spec>,
252 relative_path: &BStr,
253 is_dir: bool,
254 case: Case,
255 how: &mut MatchKind,
256) -> bool {
257 let pattern_len = mapping.value.pattern.path.len();
258 let mut relative_path_ends_with_slash_at_pattern_len = false;
259 let (match_is_allowed, probably_how) = relative_path.get(pattern_len).map_or_else(
260 || (relative_path.len() == pattern_len, Verbatim),
261 |b| {
262 relative_path_ends_with_slash_at_pattern_len = *b == b'/';
263 (relative_path_ends_with_slash_at_pattern_len, Prefix)
264 },
265 );
266 *how = probably_how;
267 let pattern_requirement_is_met = !mapping.pattern.mode.contains(gix_glob::pattern::Mode::MUST_BE_DIR)
268 || (relative_path_ends_with_slash_at_pattern_len || is_dir);
269
270 if match_is_allowed && pattern_requirement_is_met {
271 let dir_or_file = &relative_path[..mapping.value.pattern.path.len()];
272 match case {
273 Case::Sensitive => mapping.value.pattern.path == dir_or_file,
274 Case::Fold => mapping.value.pattern.path.eq_ignore_ascii_case(dir_or_file),
275 }
276 } else {
277 false
278 }
279}