Show More
@@ -1,380 +1,534 b'' | |||
|
1 | 1 | // filepatterns.rs |
|
2 | 2 | // |
|
3 | 3 | // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> |
|
4 | 4 | // |
|
5 | 5 | // This software may be used and distributed according to the terms of the |
|
6 | 6 | // GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | //! Handling of Mercurial-specific patterns. |
|
9 | 9 | |
|
10 | use crate::{ | |
|
11 | utils::SliceExt, FastHashMap, LineNumber, PatternError, PatternFileError, | |
|
12 | }; | |
|
10 | use crate::{utils::SliceExt, FastHashMap, PatternError}; | |
|
13 | 11 | use lazy_static::lazy_static; |
|
14 | 12 | use regex::bytes::{NoExpand, Regex}; |
|
15 | 13 | use std::fs::File; |
|
16 | 14 | use std::io::Read; |
|
17 | 15 | use std::path::{Path, PathBuf}; |
|
18 | 16 | use std::vec::Vec; |
|
19 | 17 | |
|
20 | 18 | lazy_static! { |
|
21 | 19 | static ref RE_ESCAPE: Vec<Vec<u8>> = { |
|
22 | 20 | let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
|
23 | 21 | let to_escape = b"()[]{}?*+-|^$\\.&~# \t\n\r\x0b\x0c"; |
|
24 | 22 | for byte in to_escape { |
|
25 | 23 | v[*byte as usize].insert(0, b'\\'); |
|
26 | 24 | } |
|
27 | 25 | v |
|
28 | 26 | }; |
|
29 | 27 | } |
|
30 | 28 | |
|
31 | 29 | /// These are matched in order |
|
32 | 30 | const GLOB_REPLACEMENTS: &[(&[u8], &[u8])] = |
|
33 | 31 | &[(b"*/", b"(?:.*/)?"), (b"*", b".*"), (b"", b"[^/]*")]; |
|
34 | 32 | |
|
33 | /// Appended to the regexp of globs | |
|
34 | const GLOB_SUFFIX: &[u8; 7] = b"(?:/|$)"; | |
|
35 | ||
|
35 | 36 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
|
36 | 37 | pub enum PatternSyntax { |
|
38 | /// A regular expression | |
|
37 | 39 | Regexp, |
|
38 | 40 | /// Glob that matches at the front of the path |
|
39 | 41 | RootGlob, |
|
40 | 42 | /// Glob that matches at any suffix of the path (still anchored at |
|
41 | 43 | /// slashes) |
|
42 | 44 | Glob, |
|
45 | /// a path relative to repository root, which is matched recursively | |
|
43 | 46 | Path, |
|
47 | /// A path relative to cwd | |
|
44 | 48 | RelPath, |
|
49 | /// an unrooted glob (*.rs matches Rust files in all dirs) | |
|
45 | 50 | RelGlob, |
|
51 | /// A regexp that needn't match the start of a name | |
|
46 | 52 | RelRegexp, |
|
53 | /// A path relative to repository root, which is matched non-recursively | |
|
54 | /// (will not match subdirectories) | |
|
47 | 55 | RootFiles, |
|
48 | 56 | } |
|
49 | 57 | |
|
50 | 58 | /// Transforms a glob pattern into a regex |
|
51 | 59 | fn glob_to_re(pat: &[u8]) -> Vec<u8> { |
|
52 | 60 | let mut input = pat; |
|
53 | 61 | let mut res: Vec<u8> = vec![]; |
|
54 | 62 | let mut group_depth = 0; |
|
55 | 63 | |
|
56 | 64 | while let Some((c, rest)) = input.split_first() { |
|
57 | 65 | input = rest; |
|
58 | 66 | |
|
59 | 67 | match c { |
|
60 | 68 | b'*' => { |
|
61 | 69 | for (source, repl) in GLOB_REPLACEMENTS { |
|
62 | 70 | if let Some(rest) = input.drop_prefix(source) { |
|
63 | 71 | input = rest; |
|
64 | 72 | res.extend(*repl); |
|
65 | 73 | break; |
|
66 | 74 | } |
|
67 | 75 | } |
|
68 | 76 | } |
|
69 | 77 | b'?' => res.extend(b"."), |
|
70 | 78 | b'[' => { |
|
71 | 79 | match input.iter().skip(1).position(|b| *b == b']') { |
|
72 | 80 | None => res.extend(b"\\["), |
|
73 | 81 | Some(end) => { |
|
74 | 82 | // Account for the one we skipped |
|
75 | 83 | let end = end + 1; |
|
76 | 84 | |
|
77 | 85 | res.extend(b"["); |
|
78 | 86 | |
|
79 | 87 | for (i, b) in input[..end].iter().enumerate() { |
|
80 | 88 | if *b == b'!' && i == 0 { |
|
81 | 89 | res.extend(b"^") |
|
82 | 90 | } else if *b == b'^' && i == 0 { |
|
83 | 91 | res.extend(b"\\^") |
|
84 | 92 | } else if *b == b'\\' { |
|
85 | 93 | res.extend(b"\\\\") |
|
86 | 94 | } else { |
|
87 | 95 | res.push(*b) |
|
88 | 96 | } |
|
89 | 97 | } |
|
90 | 98 | res.extend(b"]"); |
|
91 | 99 | input = &input[end + 1..]; |
|
92 | 100 | } |
|
93 | 101 | } |
|
94 | 102 | } |
|
95 | 103 | b'{' => { |
|
96 | 104 | group_depth += 1; |
|
97 | 105 | res.extend(b"(?:") |
|
98 | 106 | } |
|
99 | 107 | b'}' if group_depth > 0 => { |
|
100 | 108 | group_depth -= 1; |
|
101 | 109 | res.extend(b")"); |
|
102 | 110 | } |
|
103 | 111 | b',' if group_depth > 0 => res.extend(b"|"), |
|
104 | 112 | b'\\' => { |
|
105 | 113 | let c = { |
|
106 | 114 | if let Some((c, rest)) = input.split_first() { |
|
107 | 115 | input = rest; |
|
108 | 116 | c |
|
109 | 117 | } else { |
|
110 | 118 | c |
|
111 | 119 | } |
|
112 | 120 | }; |
|
113 | 121 | res.extend(&RE_ESCAPE[*c as usize]) |
|
114 | 122 | } |
|
115 | 123 | _ => res.extend(&RE_ESCAPE[*c as usize]), |
|
116 | 124 | } |
|
117 | 125 | } |
|
118 | 126 | res |
|
119 | 127 | } |
|
120 | 128 | |
|
121 | 129 | fn escape_pattern(pattern: &[u8]) -> Vec<u8> { |
|
122 | 130 | pattern |
|
123 | 131 | .iter() |
|
124 | 132 | .flat_map(|c| RE_ESCAPE[*c as usize].clone()) |
|
125 | 133 | .collect() |
|
126 | 134 | } |
|
127 | 135 | |
|
128 | fn parse_pattern_syntax(kind: &[u8]) -> Result<PatternSyntax, PatternError> { | |
|
136 | pub fn parse_pattern_syntax( | |
|
137 | kind: &[u8], | |
|
138 | ) -> Result<PatternSyntax, PatternError> { | |
|
129 | 139 | match kind { |
|
130 | b"re" => Ok(PatternSyntax::Regexp), | |
|
131 | b"path" => Ok(PatternSyntax::Path), | |
|
132 | b"relpath" => Ok(PatternSyntax::RelPath), | |
|
133 | b"rootfilesin" => Ok(PatternSyntax::RootFiles), | |
|
134 | b"relglob" => Ok(PatternSyntax::RelGlob), | |
|
135 | b"relre" => Ok(PatternSyntax::RelRegexp), | |
|
136 | b"glob" => Ok(PatternSyntax::Glob), | |
|
137 | b"rootglob" => Ok(PatternSyntax::RootGlob), | |
|
140 | b"re:" => Ok(PatternSyntax::Regexp), | |
|
141 | b"path:" => Ok(PatternSyntax::Path), | |
|
142 | b"relpath:" => Ok(PatternSyntax::RelPath), | |
|
143 | b"rootfilesin:" => Ok(PatternSyntax::RootFiles), | |
|
144 | b"relglob:" => Ok(PatternSyntax::RelGlob), | |
|
145 | b"relre:" => Ok(PatternSyntax::RelRegexp), | |
|
146 | b"glob:" => Ok(PatternSyntax::Glob), | |
|
147 | b"rootglob:" => Ok(PatternSyntax::RootGlob), | |
|
138 | 148 | _ => Err(PatternError::UnsupportedSyntax( |
|
139 | 149 | String::from_utf8_lossy(kind).to_string(), |
|
140 | 150 | )), |
|
141 | 151 | } |
|
142 | 152 | } |
|
143 | 153 | |
|
144 | 154 | /// Builds the regex that corresponds to the given pattern. |
|
145 | 155 | /// If within a `syntax: regexp` context, returns the pattern, |
|
146 | 156 | /// otherwise, returns the corresponding regex. |
|
147 | fn _build_single_regex( | |
|
148 | syntax: PatternSyntax, | |
|
149 | pattern: &[u8], | |
|
150 | globsuffix: &[u8], | |
|
151 | ) -> Vec<u8> { | |
|
157 | fn _build_single_regex(entry: &IgnorePattern) -> Vec<u8> { | |
|
158 | let IgnorePattern { | |
|
159 | syntax, pattern, .. | |
|
160 | } = entry; | |
|
152 | 161 | if pattern.is_empty() { |
|
153 | 162 | return vec![]; |
|
154 | 163 | } |
|
155 | 164 | match syntax { |
|
156 | 165 | PatternSyntax::Regexp => pattern.to_owned(), |
|
157 | 166 | PatternSyntax::RelRegexp => { |
|
158 | 167 | if pattern[0] == b'^' { |
|
159 | 168 | return pattern.to_owned(); |
|
160 | 169 | } |
|
161 | [b".*", pattern].concat() | |
|
170 | [&b".*"[..], pattern].concat() | |
|
162 | 171 | } |
|
163 | 172 | PatternSyntax::Path | PatternSyntax::RelPath => { |
|
164 | 173 | if pattern == b"." { |
|
165 | 174 | return vec![]; |
|
166 | 175 | } |
|
167 | 176 | [escape_pattern(pattern).as_slice(), b"(?:/|$)"].concat() |
|
168 | 177 | } |
|
169 | 178 | PatternSyntax::RootFiles => { |
|
170 | 179 | let mut res = if pattern == b"." { |
|
171 | 180 | vec![] |
|
172 | 181 | } else { |
|
173 | 182 | // Pattern is a directory name. |
|
174 | 183 | [escape_pattern(pattern).as_slice(), b"/"].concat() |
|
175 | 184 | }; |
|
176 | 185 | |
|
177 | 186 | // Anything after the pattern must be a non-directory. |
|
178 | 187 | res.extend(b"[^/]+$"); |
|
179 | 188 | res |
|
180 | 189 | } |
|
181 | 190 | PatternSyntax::RelGlob => { |
|
182 | 191 | let glob_re = glob_to_re(pattern); |
|
183 | 192 | if let Some(rest) = glob_re.drop_prefix(b"[^/]*") { |
|
184 |
[b".*", rest, |
|
|
193 | [b".*", rest, GLOB_SUFFIX].concat() | |
|
185 | 194 | } else { |
|
186 |
[b"(?:|.*/)", glob_re.as_slice(), |
|
|
195 | [b"(?:|.*/)", glob_re.as_slice(), GLOB_SUFFIX].concat() | |
|
187 | 196 | } |
|
188 | 197 | } |
|
189 | 198 | PatternSyntax::Glob | PatternSyntax::RootGlob => { |
|
190 |
[glob_to_re(pattern).as_slice(), |
|
|
199 | [glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat() | |
|
191 | 200 | } |
|
192 | 201 | } |
|
193 | 202 | } |
|
194 | 203 | |
|
195 | 204 | const GLOB_SPECIAL_CHARACTERS: [u8; 7] = |
|
196 | 205 | [b'*', b'?', b'[', b']', b'{', b'}', b'\\']; |
|
197 | 206 | |
|
207 | /// TODO support other platforms | |
|
208 | #[cfg(unix)] | |
|
209 | pub fn normalize_path_bytes(bytes: &[u8]) -> Vec<u8> { | |
|
210 | if bytes.is_empty() { | |
|
211 | return b".".to_vec(); | |
|
212 | } | |
|
213 | let sep = b'/'; | |
|
214 | ||
|
215 | let mut initial_slashes = bytes.iter().take_while(|b| **b == sep).count(); | |
|
216 | if initial_slashes > 2 { | |
|
217 | // POSIX allows one or two initial slashes, but treats three or more | |
|
218 | // as single slash. | |
|
219 | initial_slashes = 1; | |
|
220 | } | |
|
221 | let components = bytes | |
|
222 | .split(|b| *b == sep) | |
|
223 | .filter(|c| !(c.is_empty() || c == b".")) | |
|
224 | .fold(vec![], |mut acc, component| { | |
|
225 | if component != b".." | |
|
226 | || (initial_slashes == 0 && acc.is_empty()) | |
|
227 | || (!acc.is_empty() && acc[acc.len() - 1] == b"..") | |
|
228 | { | |
|
229 | acc.push(component) | |
|
230 | } else if !acc.is_empty() { | |
|
231 | acc.pop(); | |
|
232 | } | |
|
233 | acc | |
|
234 | }); | |
|
235 | let mut new_bytes = components.join(&sep); | |
|
236 | ||
|
237 | if initial_slashes > 0 { | |
|
238 | let mut buf: Vec<_> = (0..initial_slashes).map(|_| sep).collect(); | |
|
239 | buf.extend(new_bytes); | |
|
240 | new_bytes = buf; | |
|
241 | } | |
|
242 | if new_bytes.is_empty() { | |
|
243 | b".".to_vec() | |
|
244 | } else { | |
|
245 | new_bytes | |
|
246 | } | |
|
247 | } | |
|
248 | ||
|
198 | 249 | /// Wrapper function to `_build_single_regex` that short-circuits 'exact' globs |
|
199 | 250 | /// that don't need to be transformed into a regex. |
|
200 | 251 | pub fn build_single_regex( |
|
201 | kind: &[u8], | |
|
202 | pat: &[u8], | |
|
203 | globsuffix: &[u8], | |
|
252 | entry: &IgnorePattern, | |
|
204 | 253 | ) -> Result<Vec<u8>, PatternError> { |
|
205 | let enum_kind = parse_pattern_syntax(kind)?; | |
|
206 | if enum_kind == PatternSyntax::RootGlob | |
|
207 | && !pat.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b)) | |
|
254 | let IgnorePattern { | |
|
255 | pattern, syntax, .. | |
|
256 | } = entry; | |
|
257 | let pattern = match syntax { | |
|
258 | PatternSyntax::RootGlob | |
|
259 | | PatternSyntax::Path | |
|
260 | | PatternSyntax::RelGlob | |
|
261 | | PatternSyntax::RootFiles => normalize_path_bytes(&pattern), | |
|
262 | _ => pattern.to_owned(), | |
|
263 | }; | |
|
264 | if *syntax == PatternSyntax::RootGlob | |
|
265 | && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b)) | |
|
208 | 266 | { |
|
209 | let mut escaped = escape_pattern(pat); | |
|
210 |
escaped.extend( |
|
|
267 | let mut escaped = escape_pattern(&pattern); | |
|
268 | escaped.extend(GLOB_SUFFIX); | |
|
211 | 269 | Ok(escaped) |
|
212 | 270 | } else { |
|
213 | Ok(_build_single_regex(enum_kind, pat, globsuffix)) | |
|
271 | let mut entry = entry.clone(); | |
|
272 | entry.pattern = pattern; | |
|
273 | Ok(_build_single_regex(&entry)) | |
|
214 | 274 | } |
|
215 | 275 | } |
|
216 | 276 | |
|
217 | 277 | lazy_static! { |
|
218 | 278 | static ref SYNTAXES: FastHashMap<&'static [u8], &'static [u8]> = { |
|
219 | 279 | let mut m = FastHashMap::default(); |
|
220 | 280 | |
|
221 | 281 | m.insert(b"re".as_ref(), b"relre:".as_ref()); |
|
222 | 282 | m.insert(b"regexp".as_ref(), b"relre:".as_ref()); |
|
223 | 283 | m.insert(b"glob".as_ref(), b"relglob:".as_ref()); |
|
224 | 284 | m.insert(b"rootglob".as_ref(), b"rootglob:".as_ref()); |
|
225 | m.insert(b"include".as_ref(), b"include".as_ref()); | |
|
226 | m.insert(b"subinclude".as_ref(), b"subinclude".as_ref()); | |
|
285 | m.insert(b"include".as_ref(), b"include:".as_ref()); | |
|
286 | m.insert(b"subinclude".as_ref(), b"subinclude:".as_ref()); | |
|
227 | 287 | m |
|
228 | 288 | }; |
|
229 | 289 | } |
|
230 | 290 | |
|
231 | pub type PatternTuple = (Vec<u8>, LineNumber, Vec<u8>); | |
|
232 | type WarningTuple = (PathBuf, Vec<u8>); | |
|
291 | #[derive(Debug)] | |
|
292 | pub enum PatternFileWarning { | |
|
293 | /// (file path, syntax bytes) | |
|
294 | InvalidSyntax(PathBuf, Vec<u8>), | |
|
295 | /// File path | |
|
296 | NoSuchFile(PathBuf), | |
|
297 | } | |
|
233 | 298 | |
|
234 | 299 | pub fn parse_pattern_file_contents<P: AsRef<Path>>( |
|
235 | 300 | lines: &[u8], |
|
236 | 301 | file_path: P, |
|
237 | 302 | warn: bool, |
|
238 | ) -> (Vec<PatternTuple>, Vec<WarningTuple>) { | |
|
303 | ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { | |
|
239 | 304 | let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
|
240 | 305 | let comment_escape_regex = Regex::new(r"\\#").unwrap(); |
|
241 |
let mut inputs: Vec<Pattern |
|
|
242 |
let mut warnings: Vec<Warning |
|
|
306 | let mut inputs: Vec<IgnorePattern> = vec![]; | |
|
307 | let mut warnings: Vec<PatternFileWarning> = vec![]; | |
|
243 | 308 | |
|
244 | 309 | let mut current_syntax = b"relre:".as_ref(); |
|
245 | 310 | |
|
246 | 311 | for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { |
|
247 | 312 | let line_number = line_number + 1; |
|
248 | 313 | |
|
249 | 314 | let line_buf; |
|
250 | 315 | if line.contains(&b'#') { |
|
251 | 316 | if let Some(cap) = comment_regex.captures(line) { |
|
252 | 317 | line = &line[..cap.get(1).unwrap().end()] |
|
253 | 318 | } |
|
254 | 319 | line_buf = comment_escape_regex.replace_all(line, NoExpand(b"#")); |
|
255 | 320 | line = &line_buf; |
|
256 | 321 | } |
|
257 | 322 | |
|
258 | 323 | let mut line = line.trim_end(); |
|
259 | 324 | |
|
260 | 325 | if line.is_empty() { |
|
261 | 326 | continue; |
|
262 | 327 | } |
|
263 | 328 | |
|
264 | 329 | if let Some(syntax) = line.drop_prefix(b"syntax:") { |
|
265 | 330 | let syntax = syntax.trim(); |
|
266 | 331 | |
|
267 | 332 | if let Some(rel_syntax) = SYNTAXES.get(syntax) { |
|
268 | 333 | current_syntax = rel_syntax; |
|
269 | 334 | } else if warn { |
|
270 | warnings | |
|
271 |
|
|
|
335 | warnings.push(PatternFileWarning::InvalidSyntax( | |
|
336 | file_path.as_ref().to_owned(), | |
|
337 | syntax.to_owned(), | |
|
338 | )); | |
|
272 | 339 | } |
|
273 | 340 | continue; |
|
274 | 341 | } |
|
275 | 342 | |
|
276 | 343 | let mut line_syntax: &[u8] = ¤t_syntax; |
|
277 | 344 | |
|
278 | 345 | for (s, rels) in SYNTAXES.iter() { |
|
279 | 346 | if let Some(rest) = line.drop_prefix(rels) { |
|
280 | 347 | line_syntax = rels; |
|
281 | 348 | line = rest; |
|
282 | 349 | break; |
|
283 | 350 | } |
|
284 | 351 | if let Some(rest) = line.drop_prefix(&[s, &b":"[..]].concat()) { |
|
285 | 352 | line_syntax = rels; |
|
286 | 353 | line = rest; |
|
287 | 354 | break; |
|
288 | 355 | } |
|
289 | 356 | } |
|
290 | 357 | |
|
291 | inputs.push(( | |
|
292 | [line_syntax, line].concat(), | |
|
358 | inputs.push(IgnorePattern::new( | |
|
359 | parse_pattern_syntax(&line_syntax).map_err(|e| match e { | |
|
360 | PatternError::UnsupportedSyntax(syntax) => { | |
|
361 | PatternError::UnsupportedSyntaxInFile( | |
|
362 | syntax, | |
|
363 | file_path.as_ref().to_string_lossy().into(), | |
|
293 | 364 | line_number, |
|
294 | line.to_owned(), | |
|
365 | ) | |
|
366 | } | |
|
367 | _ => e, | |
|
368 | })?, | |
|
369 | &line, | |
|
370 | &file_path, | |
|
295 | 371 | )); |
|
296 | 372 | } |
|
297 | (inputs, warnings) | |
|
373 | Ok((inputs, warnings)) | |
|
298 | 374 | } |
|
299 | 375 | |
|
300 | 376 | pub fn read_pattern_file<P: AsRef<Path>>( |
|
301 | 377 | file_path: P, |
|
302 | 378 | warn: bool, |
|
303 |
) -> Result<(Vec<Pattern |
|
|
304 |
let mut f = File::open(file_path.as_ref()) |
|
|
379 | ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { | |
|
380 | let mut f = match File::open(file_path.as_ref()) { | |
|
381 | Ok(f) => Ok(f), | |
|
382 | Err(e) => match e.kind() { | |
|
383 | std::io::ErrorKind::NotFound => { | |
|
384 | return Ok(( | |
|
385 | vec![], | |
|
386 | vec![PatternFileWarning::NoSuchFile( | |
|
387 | file_path.as_ref().to_owned(), | |
|
388 | )], | |
|
389 | )) | |
|
390 | } | |
|
391 | _ => Err(e), | |
|
392 | }, | |
|
393 | }?; | |
|
305 | 394 | let mut contents = Vec::new(); |
|
306 | 395 | |
|
307 | 396 | f.read_to_end(&mut contents)?; |
|
308 | 397 | |
|
309 | Ok(parse_pattern_file_contents(&contents, file_path, warn)) | |
|
398 | Ok(parse_pattern_file_contents(&contents, file_path, warn)?) | |
|
399 | } | |
|
400 | ||
|
401 | /// Represents an entry in an "ignore" file. | |
|
402 | #[derive(Debug, Eq, PartialEq, Clone)] | |
|
403 | pub struct IgnorePattern { | |
|
404 | pub syntax: PatternSyntax, | |
|
405 | pub pattern: Vec<u8>, | |
|
406 | pub source: PathBuf, | |
|
310 | 407 | } |
|
311 | 408 | |
|
409 | impl IgnorePattern { | |
|
410 | pub fn new( | |
|
411 | syntax: PatternSyntax, | |
|
412 | pattern: &[u8], | |
|
413 | source: impl AsRef<Path>, | |
|
414 | ) -> Self { | |
|
415 | Self { | |
|
416 | syntax, | |
|
417 | pattern: pattern.to_owned(), | |
|
418 | source: source.as_ref().to_owned(), | |
|
419 | } | |
|
420 | } | |
|
421 | } | |
|
422 | ||
|
423 | pub type PatternResult<T> = Result<T, PatternError>; | |
|
424 | ||
|
312 | 425 | #[cfg(test)] |
|
313 | 426 | mod tests { |
|
314 | 427 | use super::*; |
|
428 | use pretty_assertions::assert_eq; | |
|
315 | 429 | |
|
316 | 430 | #[test] |
|
317 | 431 | fn escape_pattern_test() { |
|
318 | let untouched = br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#; | |
|
432 | let untouched = | |
|
433 | br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#; | |
|
319 | 434 | assert_eq!(escape_pattern(untouched), untouched.to_vec()); |
|
320 | 435 | // All escape codes |
|
321 | 436 | assert_eq!( |
|
322 | 437 | escape_pattern(br#"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"#), |
|
323 | 438 | br#"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\ \\t\\n\\r\\v\\f"# |
|
324 | 439 | .to_vec() |
|
325 | 440 | ); |
|
326 | 441 | } |
|
327 | 442 | |
|
328 | 443 | #[test] |
|
329 | 444 | fn glob_test() { |
|
330 | 445 | assert_eq!(glob_to_re(br#"?"#), br#"."#); |
|
331 | 446 | assert_eq!(glob_to_re(br#"*"#), br#"[^/]*"#); |
|
332 | 447 | assert_eq!(glob_to_re(br#"**"#), br#".*"#); |
|
333 | 448 | assert_eq!(glob_to_re(br#"**/a"#), br#"(?:.*/)?a"#); |
|
334 | 449 | assert_eq!(glob_to_re(br#"a/**/b"#), br#"a/(?:.*/)?b"#); |
|
335 | 450 | assert_eq!(glob_to_re(br#"[a*?!^][^b][!c]"#), br#"[a*?!^][\^b][^c]"#); |
|
336 | 451 | assert_eq!(glob_to_re(br#"{a,b}"#), br#"(?:a|b)"#); |
|
337 | 452 | assert_eq!(glob_to_re(br#".\*\?"#), br#"\.\*\?"#); |
|
338 | 453 | } |
|
339 | 454 | |
|
340 | 455 | #[test] |
|
341 | 456 | fn test_parse_pattern_file_contents() { |
|
342 | 457 | let lines = b"syntax: glob\n*.elc"; |
|
343 | 458 | |
|
344 | 459 | assert_eq!( |
|
345 | vec![(b"relglob:*.elc".to_vec(), 2, b"*.elc".to_vec())], | |
|
346 | 460 | parse_pattern_file_contents(lines, Path::new("file_path"), false) |
|
461 | .unwrap() | |
|
347 | 462 | .0, |
|
463 | vec![IgnorePattern::new( | |
|
464 | PatternSyntax::RelGlob, | |
|
465 | b"*.elc", | |
|
466 | Path::new("file_path") | |
|
467 | )], | |
|
348 | 468 | ); |
|
349 | 469 | |
|
350 | 470 | let lines = b"syntax: include\nsyntax: glob"; |
|
351 | 471 | |
|
352 | 472 | assert_eq!( |
|
353 | 473 | parse_pattern_file_contents(lines, Path::new("file_path"), false) |
|
474 | .unwrap() | |
|
354 | 475 | .0, |
|
355 | 476 | vec![] |
|
356 | 477 | ); |
|
357 | 478 | let lines = b"glob:**.o"; |
|
358 | 479 | assert_eq!( |
|
359 | 480 | parse_pattern_file_contents(lines, Path::new("file_path"), false) |
|
481 | .unwrap() | |
|
360 | 482 | .0, |
|
361 | vec![(b"relglob:**.o".to_vec(), 1, b"**.o".to_vec())] | |
|
483 | vec![IgnorePattern::new( | |
|
484 | PatternSyntax::RelGlob, | |
|
485 | b"**.o", | |
|
486 | Path::new("file_path") | |
|
487 | )] | |
|
488 | ); | |
|
489 | } | |
|
490 | ||
|
491 | #[test] | |
|
492 | fn test_build_single_regex() { | |
|
493 | assert_eq!( | |
|
494 | build_single_regex(&IgnorePattern::new( | |
|
495 | PatternSyntax::RelGlob, | |
|
496 | b"rust/target/", | |
|
497 | Path::new("") | |
|
498 | )) | |
|
499 | .unwrap(), | |
|
500 | br"(?:|.*/)rust/target(?:/|$)".to_vec(), | |
|
362 | 501 | ); |
|
363 | 502 | } |
|
364 | 503 | |
|
365 | 504 | #[test] |
|
366 | 505 | fn test_build_single_regex_shortcut() { |
|
367 | 506 | assert_eq!( |
|
368 | br"(?:/|$)".to_vec(), | |
|
369 | build_single_regex(b"rootglob", b"", b"").unwrap() | |
|
507 | build_single_regex(&IgnorePattern::new( | |
|
508 | PatternSyntax::RootGlob, | |
|
509 | b"", | |
|
510 | Path::new("") | |
|
511 | )) | |
|
512 | .unwrap(), | |
|
513 | br"\.(?:/|$)".to_vec(), | |
|
370 | 514 | ); |
|
371 | 515 | assert_eq!( |
|
516 | build_single_regex(&IgnorePattern::new( | |
|
517 | PatternSyntax::RootGlob, | |
|
518 | b"whatever", | |
|
519 | Path::new("") | |
|
520 | )) | |
|
521 | .unwrap(), | |
|
372 | 522 | br"whatever(?:/|$)".to_vec(), |
|
373 | build_single_regex(b"rootglob", b"whatever", b"").unwrap() | |
|
374 | 523 | ); |
|
375 | 524 | assert_eq!( |
|
376 | br"[^/]*\.o".to_vec(), | |
|
377 | build_single_regex(b"rootglob", b"*.o", b"").unwrap() | |
|
525 | build_single_regex(&IgnorePattern::new( | |
|
526 | PatternSyntax::RootGlob, | |
|
527 | b"*.o", | |
|
528 | Path::new("") | |
|
529 | )) | |
|
530 | .unwrap(), | |
|
531 | br"[^/]*\.o(?:/|$)".to_vec(), | |
|
378 | 532 | ); |
|
379 | 533 | } |
|
380 | 534 | } |
@@ -1,143 +1,169 b'' | |||
|
1 | 1 | // Copyright 2018-2020 Georges Racinet <georges.racinet@octobus.net> |
|
2 | 2 | // and Mercurial contributors |
|
3 | 3 | // |
|
4 | 4 | // This software may be used and distributed according to the terms of the |
|
5 | 5 | // GNU General Public License version 2 or any later version. |
|
6 | 6 | mod ancestors; |
|
7 | 7 | pub mod dagops; |
|
8 | 8 | pub use ancestors::{AncestorsIterator, LazyAncestors, MissingAncestors}; |
|
9 | 9 | mod dirstate; |
|
10 | 10 | pub mod discovery; |
|
11 | 11 | pub mod testing; // unconditionally built, for use from integration tests |
|
12 | 12 | pub use dirstate::{ |
|
13 | 13 | dirs_multiset::{DirsMultiset, DirsMultisetIter}, |
|
14 | 14 | dirstate_map::DirstateMap, |
|
15 | 15 | parsers::{pack_dirstate, parse_dirstate, PARENT_SIZE}, |
|
16 | 16 | status::{status, StatusResult}, |
|
17 | 17 | CopyMap, CopyMapIter, DirstateEntry, DirstateParents, EntryState, |
|
18 | 18 | StateMap, StateMapIter, |
|
19 | 19 | }; |
|
20 | 20 | mod filepatterns; |
|
21 | 21 | pub mod matchers; |
|
22 | 22 | pub mod revlog; |
|
23 | 23 | pub use revlog::*; |
|
24 | 24 | pub mod utils; |
|
25 | 25 | |
|
26 | 26 | use crate::utils::hg_path::{HgPathBuf, HgPathError}; |
|
27 | 27 | pub use filepatterns::{ |
|
28 | build_single_regex, read_pattern_file, PatternSyntax, PatternTuple, | |
|
28 | parse_pattern_syntax, read_pattern_file, IgnorePattern, | |
|
29 | PatternFileWarning, PatternSyntax, | |
|
29 | 30 | }; |
|
30 | 31 | use std::collections::HashMap; |
|
31 | 32 | use twox_hash::RandomXxHashBuilder64; |
|
32 | 33 | |
|
33 | 34 | pub type LineNumber = usize; |
|
34 | 35 | |
|
35 | 36 | /// Rust's default hasher is too slow because it tries to prevent collision |
|
36 | 37 | /// attacks. We are not concerned about those: if an ill-minded person has |
|
37 | 38 | /// write access to your repository, you have other issues. |
|
38 | 39 | pub type FastHashMap<K, V> = HashMap<K, V, RandomXxHashBuilder64>; |
|
39 | 40 | |
|
40 | 41 | #[derive(Clone, Debug, PartialEq)] |
|
41 | 42 | pub enum DirstateParseError { |
|
42 | 43 | TooLittleData, |
|
43 | 44 | Overflow, |
|
44 | 45 | CorruptedEntry(String), |
|
45 | 46 | Damaged, |
|
46 | 47 | } |
|
47 | 48 | |
|
48 | 49 | impl From<std::io::Error> for DirstateParseError { |
|
49 | 50 | fn from(e: std::io::Error) -> Self { |
|
50 | 51 | DirstateParseError::CorruptedEntry(e.to_string()) |
|
51 | 52 | } |
|
52 | 53 | } |
|
53 | 54 | |
|
54 | 55 | impl ToString for DirstateParseError { |
|
55 | 56 | fn to_string(&self) -> String { |
|
56 | 57 | use crate::DirstateParseError::*; |
|
57 | 58 | match self { |
|
58 | 59 | TooLittleData => "Too little data for dirstate.".to_string(), |
|
59 | 60 | Overflow => "Overflow in dirstate.".to_string(), |
|
60 | 61 | CorruptedEntry(e) => format!("Corrupted entry: {:?}.", e), |
|
61 | 62 | Damaged => "Dirstate appears to be damaged.".to_string(), |
|
62 | 63 | } |
|
63 | 64 | } |
|
64 | 65 | } |
|
65 | 66 | |
|
66 | 67 | #[derive(Debug, PartialEq)] |
|
67 | 68 | pub enum DirstatePackError { |
|
68 | 69 | CorruptedEntry(String), |
|
69 | 70 | CorruptedParent, |
|
70 | 71 | BadSize(usize, usize), |
|
71 | 72 | } |
|
72 | 73 | |
|
73 | 74 | impl From<std::io::Error> for DirstatePackError { |
|
74 | 75 | fn from(e: std::io::Error) -> Self { |
|
75 | 76 | DirstatePackError::CorruptedEntry(e.to_string()) |
|
76 | 77 | } |
|
77 | 78 | } |
|
78 | 79 | #[derive(Debug, PartialEq)] |
|
79 | 80 | pub enum DirstateMapError { |
|
80 | 81 | PathNotFound(HgPathBuf), |
|
81 | 82 | EmptyPath, |
|
82 | 83 | InvalidPath(HgPathError), |
|
83 | 84 | } |
|
84 | 85 | |
|
85 | 86 | impl ToString for DirstateMapError { |
|
86 | 87 | fn to_string(&self) -> String { |
|
87 | 88 | match self { |
|
88 | 89 | DirstateMapError::PathNotFound(_) => { |
|
89 | 90 | "expected a value, found none".to_string() |
|
90 | 91 | } |
|
91 | 92 | DirstateMapError::EmptyPath => "Overflow in dirstate.".to_string(), |
|
92 | 93 | DirstateMapError::InvalidPath(e) => e.to_string(), |
|
93 | 94 | } |
|
94 | 95 | } |
|
95 | 96 | } |
|
96 | 97 | |
|
97 | 98 | pub enum DirstateError { |
|
98 | 99 | Parse(DirstateParseError), |
|
99 | 100 | Pack(DirstatePackError), |
|
100 | 101 | Map(DirstateMapError), |
|
101 | 102 | IO(std::io::Error), |
|
102 | 103 | } |
|
103 | 104 | |
|
104 | 105 | impl From<DirstateParseError> for DirstateError { |
|
105 | 106 | fn from(e: DirstateParseError) -> Self { |
|
106 | 107 | DirstateError::Parse(e) |
|
107 | 108 | } |
|
108 | 109 | } |
|
109 | 110 | |
|
110 | 111 | impl From<DirstatePackError> for DirstateError { |
|
111 | 112 | fn from(e: DirstatePackError) -> Self { |
|
112 | 113 | DirstateError::Pack(e) |
|
113 | 114 | } |
|
114 | 115 | } |
|
115 | 116 | |
|
116 | 117 | #[derive(Debug)] |
|
117 | 118 | pub enum PatternError { |
|
119 | Path(HgPathError), | |
|
118 | 120 | UnsupportedSyntax(String), |
|
121 | UnsupportedSyntaxInFile(String, String, usize), | |
|
122 | TooLong(usize), | |
|
123 | IO(std::io::Error), | |
|
119 | 124 | } |
|
120 | 125 | |
|
121 | #[derive(Debug)] | |
|
122 | pub enum PatternFileError { | |
|
123 | IO(std::io::Error), | |
|
124 | Pattern(PatternError, LineNumber), | |
|
126 | impl ToString for PatternError { | |
|
127 | fn to_string(&self) -> String { | |
|
128 | match self { | |
|
129 | PatternError::UnsupportedSyntax(syntax) => { | |
|
130 | format!("Unsupported syntax {}", syntax) | |
|
125 | 131 | } |
|
126 | ||
|
127 | impl From<std::io::Error> for PatternFileError { | |
|
128 | fn from(e: std::io::Error) -> Self { | |
|
129 | PatternFileError::IO(e) | |
|
132 | PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => { | |
|
133 | format!( | |
|
134 | "{}:{}: unsupported syntax {}", | |
|
135 | file_path, line, syntax | |
|
136 | ) | |
|
137 | } | |
|
138 | PatternError::TooLong(size) => { | |
|
139 | format!("matcher pattern is too long ({} bytes)", size) | |
|
140 | } | |
|
141 | PatternError::IO(e) => e.to_string(), | |
|
142 | PatternError::Path(e) => e.to_string(), | |
|
143 | } | |
|
130 | 144 | } |
|
131 | 145 | } |
|
132 | 146 | |
|
133 | 147 | impl From<DirstateMapError> for DirstateError { |
|
134 | 148 | fn from(e: DirstateMapError) -> Self { |
|
135 | 149 | DirstateError::Map(e) |
|
136 | 150 | } |
|
137 | 151 | } |
|
138 | 152 | |
|
139 | 153 | impl From<std::io::Error> for DirstateError { |
|
140 | 154 | fn from(e: std::io::Error) -> Self { |
|
141 | 155 | DirstateError::IO(e) |
|
142 | 156 | } |
|
143 | 157 | } |
|
158 | ||
|
159 | impl From<std::io::Error> for PatternError { | |
|
160 | fn from(e: std::io::Error) -> Self { | |
|
161 | PatternError::IO(e) | |
|
162 | } | |
|
163 | } | |
|
164 | ||
|
165 | impl From<HgPathError> for PatternError { | |
|
166 | fn from(e: HgPathError) -> Self { | |
|
167 | PatternError::Path(e) | |
|
168 | } | |
|
169 | } |
General Comments 0
You need to be logged in to leave comments.
Login now