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
use std::fmt;
use bitflags::bitflags;
use bstr::{BStr, ByteSlice};
use crate::{pattern, wildmatch, Pattern};
bitflags! {
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Mode: u32 {
const NO_SUB_DIR = 1 << 0;
const ENDS_WITH = 1 << 1;
const MUST_BE_DIR = 1 << 2;
const NEGATIVE = 1 << 3;
const ABSOLUTE = 1 << 4;
}
}
#[derive(Debug, PartialOrd, PartialEq, Copy, Clone, Hash, Ord, Eq)]
pub enum Case {
Sensitive,
Fold,
}
impl Default for Case {
fn default() -> Self {
Case::Sensitive
}
}
impl Pattern {
pub fn from_bytes(text: &[u8]) -> Option<Self> {
crate::parse::pattern(text).map(|(text, mode, first_wildcard_pos)| Pattern {
text,
mode,
first_wildcard_pos,
})
}
pub fn is_negative(&self) -> bool {
self.mode.contains(Mode::NEGATIVE)
}
pub fn matches_repo_relative_path<'a>(
&self,
path: impl Into<&'a BStr>,
basename_start_pos: Option<usize>,
is_dir: Option<bool>,
case: Case,
) -> bool {
let is_dir = is_dir.unwrap_or(false);
if !is_dir && self.mode.contains(pattern::Mode::MUST_BE_DIR) {
return false;
}
let flags = wildmatch::Mode::NO_MATCH_SLASH_LITERAL
| match case {
Case::Fold => wildmatch::Mode::IGNORE_CASE,
Case::Sensitive => wildmatch::Mode::empty(),
};
let path = path.into();
debug_assert_eq!(
basename_start_pos,
path.rfind_byte(b'/').map(|p| p + 1),
"BUG: invalid cached basename_start_pos provided"
);
debug_assert!(!path.starts_with(b"/"), "input path must be relative");
if self.mode.contains(pattern::Mode::NO_SUB_DIR) && !self.mode.contains(pattern::Mode::ABSOLUTE) {
let basename = &path[basename_start_pos.unwrap_or_default()..];
self.matches(basename, flags)
} else {
self.matches(path, flags)
}
}
fn matches<'a>(&self, value: impl Into<&'a BStr>, mode: wildmatch::Mode) -> bool {
let value = value.into();
match self.first_wildcard_pos {
Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) && !value.contains(&b'/') => {
let text = &self.text[pos + 1..];
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
value
.len()
.checked_sub(text.len())
.map(|start| text.eq_ignore_ascii_case(&value[start..]))
.unwrap_or(false)
} else {
value.ends_with(text.as_ref())
}
}
Some(pos) => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
if !value
.get(..pos)
.map_or(false, |value| value.eq_ignore_ascii_case(&self.text[..pos]))
{
return false;
}
} else if !value.starts_with(&self.text[..pos]) {
return false;
}
crate::wildmatch(self.text.as_bstr(), value, mode)
}
None => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
self.text.eq_ignore_ascii_case(value)
} else {
self.text == value
}
}
}
}
}
impl fmt::Display for Pattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.mode.contains(Mode::NEGATIVE) {
"!".fmt(f)?;
}
if self.mode.contains(Mode::ABSOLUTE) {
"/".fmt(f)?;
}
self.text.fmt(f)?;
if self.mode.contains(Mode::MUST_BE_DIR) {
"/".fmt(f)?;
}
Ok(())
}
}