annotate rust/hg-core/src/filepatterns.rs @ 50857:796b5d6693a4

rust: simplify pattern file parsing Previously we kept the pattern syntax as a &[u8] until the last possible moment, which meant we had to handle potential errors when parsing that &[u8]. But such errors could never actually occur, given the structure of the code. Now we parse it eagerly (in two places) and pass it around as a PatternSyntax, so we can delete some error handling code. parse_one_pattern is in particular useful for parsing patterns passed on the command line, as we'll support later in this series.
author Spencer Baugh <sbaugh@janestreet.com>
date Wed, 09 Aug 2023 18:08:28 -0400
parents 1c31b343e514
children df6dfad5009a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42751
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
1 // filepatterns.rs
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
2 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
4 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
5 // This software may be used and distributed according to the terms of the
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
6 // GNU General Public License version 2 or any later version.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
7
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
8 //! Handling of Mercurial-specific patterns.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42636
diff changeset
9
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
10 use crate::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
11 utils::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
12 files::{canonical_path, get_bytes_from_path, get_path_from_bytes},
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
13 hg_path::{path_to_hg_path_buf, HgPathBuf, HgPathError},
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
14 SliceExt,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
15 },
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
16 FastHashMap, PatternError,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
17 };
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents: 42483
diff changeset
18 use lazy_static::lazy_static;
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
19 use regex::bytes::{NoExpand, Regex};
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
20 use std::ops::Deref;
42957
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents: 42865
diff changeset
21 use std::path::{Path, PathBuf};
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22 use std::vec::Vec;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
23
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
24 lazy_static! {
42483
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42438
diff changeset
25 static ref RE_ESCAPE: Vec<Vec<u8>> = {
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
26 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect();
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
27 let to_escape = b"()[]{}?*+-|^$\\.&~# \t\n\r\x0b\x0c";
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
28 for byte in to_escape {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
29 v[*byte as usize].insert(0, b'\\');
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
30 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
31 v
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
32 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
33 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
34
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
35 /// These are matched in order
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
36 const GLOB_REPLACEMENTS: &[(&[u8], &[u8])] =
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
37 &[(b"*/", b"(?:.*/)?"), (b"*", b".*"), (b"", b"[^/]*")];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
38
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
39 /// Appended to the regexp of globs
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
40 const GLOB_SUFFIX: &[u8; 7] = b"(?:/|$)";
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
41
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
42 #[derive(Debug, Clone, PartialEq, Eq)]
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
43 pub enum PatternSyntax {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
44 /// A regular expression
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
45 Regexp,
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
46 /// Glob that matches at the front of the path
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
47 RootGlob,
42841
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42751
diff changeset
48 /// Glob that matches at any suffix of the path (still anchored at
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42751
diff changeset
49 /// slashes)
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
50 Glob,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
51 /// a path relative to repository root, which is matched recursively
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
52 Path,
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
53 /// a single exact path relative to repository root
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
54 FilePath,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
55 /// A path relative to cwd
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
56 RelPath,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
57 /// an unrooted glob (*.rs matches Rust files in all dirs)
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
58 RelGlob,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
59 /// A regexp that needn't match the start of a name
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
60 RelRegexp,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
61 /// A path relative to repository root, which is matched non-recursively
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
62 /// (will not match subdirectories)
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
63 RootFiles,
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
64 /// A file of patterns to read and include
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
65 Include,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
66 /// A file of patterns to match against files under the same directory
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
67 SubInclude,
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
68 /// SubInclude with the result of parsing the included file
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
69 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
70 /// Note: there is no ExpandedInclude because that expansion can be done
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
71 /// in place by replacing the Include pattern by the included patterns.
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
72 /// SubInclude requires more handling.
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
73 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
74 /// Note: `Box` is used to minimize size impact on other enum variants
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
75 ExpandedSubInclude(Box<SubInclude>),
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
76 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
77
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
78 /// Transforms a glob pattern into a regex
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
79 fn glob_to_re(pat: &[u8]) -> Vec<u8> {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
80 let mut input = pat;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
81 let mut res: Vec<u8> = vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
82 let mut group_depth = 0;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
83
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
84 while let Some((c, rest)) = input.split_first() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
85 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
86
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
87 match c {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
88 b'*' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
89 for (source, repl) in GLOB_REPLACEMENTS {
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
90 if let Some(rest) = input.drop_prefix(source) {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
91 input = rest;
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
92 res.extend(*repl);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
93 break;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
94 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
95 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
96 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
97 b'?' => res.extend(b"."),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
98 b'[' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
99 match input.iter().skip(1).position(|b| *b == b']') {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
100 None => res.extend(b"\\["),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
101 Some(end) => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
102 // Account for the one we skipped
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
103 let end = end + 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
104
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
105 res.extend(b"[");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
106
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
107 for (i, b) in input[..end].iter().enumerate() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
108 if *b == b'!' && i == 0 {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
109 res.extend(b"^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
110 } else if *b == b'^' && i == 0 {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
111 res.extend(b"\\^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
112 } else if *b == b'\\' {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
113 res.extend(b"\\\\")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
114 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
115 res.push(*b)
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
116 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
117 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
118 res.extend(b"]");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
119 input = &input[end + 1..];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
120 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
121 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
122 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
123 b'{' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
124 group_depth += 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
125 res.extend(b"(?:")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
126 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
127 b'}' if group_depth > 0 => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
128 group_depth -= 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
129 res.extend(b")");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
130 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
131 b',' if group_depth > 0 => res.extend(b"|"),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
132 b'\\' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
133 let c = {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
134 if let Some((c, rest)) = input.split_first() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
135 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
136 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
137 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
138 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
139 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
140 };
42483
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42438
diff changeset
141 res.extend(&RE_ESCAPE[*c as usize])
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
142 }
42483
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42438
diff changeset
143 _ => res.extend(&RE_ESCAPE[*c as usize]),
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
144 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
145 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
146 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
147 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
148
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
149 fn escape_pattern(pattern: &[u8]) -> Vec<u8> {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
150 pattern
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
151 .iter()
42483
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42438
diff changeset
152 .flat_map(|c| RE_ESCAPE[*c as usize].clone())
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
153 .collect()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
154 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
155
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
156 pub fn parse_pattern_syntax(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
157 kind: &[u8],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
158 ) -> Result<PatternSyntax, PatternError> {
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
159 match kind {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
160 b"re:" => Ok(PatternSyntax::Regexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
161 b"path:" => Ok(PatternSyntax::Path),
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
162 b"filepath:" => Ok(PatternSyntax::FilePath),
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
163 b"relpath:" => Ok(PatternSyntax::RelPath),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
164 b"rootfilesin:" => Ok(PatternSyntax::RootFiles),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
165 b"relglob:" => Ok(PatternSyntax::RelGlob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
166 b"relre:" => Ok(PatternSyntax::RelRegexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
167 b"glob:" => Ok(PatternSyntax::Glob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
168 b"rootglob:" => Ok(PatternSyntax::RootGlob),
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
169 b"include:" => Ok(PatternSyntax::Include),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
170 b"subinclude:" => Ok(PatternSyntax::SubInclude),
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
171 _ => Err(PatternError::UnsupportedSyntax(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
172 String::from_utf8_lossy(kind).to_string(),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
173 )),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
174 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
175 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
176
49604
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
177 lazy_static! {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
178 static ref FLAG_RE: Regex = Regex::new(r"^\(\?[aiLmsux]+\)").unwrap();
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
179 }
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
180
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
181 /// Builds the regex that corresponds to the given pattern.
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
182 /// If within a `syntax: regexp` context, returns the pattern,
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
183 /// otherwise, returns the corresponding regex.
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
184 fn _build_single_regex(entry: &IgnorePattern) -> Vec<u8> {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
185 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
186 syntax, pattern, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
187 } = entry;
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
188 if pattern.is_empty() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
189 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
190 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
191 match syntax {
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
192 PatternSyntax::Regexp => pattern.to_owned(),
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
193 PatternSyntax::RelRegexp => {
44593
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44304
diff changeset
194 // The `regex` crate accepts `**` while `re2` and Python's `re`
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44304
diff changeset
195 // do not. Checking for `*` correctly triggers the same error all
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44304
diff changeset
196 // engines.
44833
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Raphaël Gomès <rgomes@octobus.net>
parents: 44832
diff changeset
197 if pattern[0] == b'^'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Raphaël Gomès <rgomes@octobus.net>
parents: 44832
diff changeset
198 || pattern[0] == b'*'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Raphaël Gomès <rgomes@octobus.net>
parents: 44832
diff changeset
199 || pattern.starts_with(b".*")
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Raphaël Gomès <rgomes@octobus.net>
parents: 44832
diff changeset
200 {
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
201 return pattern.to_owned();
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
202 }
49604
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
203 match FLAG_RE.find(pattern) {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
204 Some(mat) => {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
205 let s = mat.start();
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
206 let e = mat.end();
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
207 [
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
208 &b"(?"[..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
209 &pattern[s + 2..e - 1],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
210 &b":"[..],
49605
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
211 if pattern[e] == b'^'
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
212 || pattern[e] == b'*'
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
213 || pattern[e..].starts_with(b".*")
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
214 {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
215 &b""[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
216 } else {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
217 &b".*"[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
218 },
49604
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
219 &pattern[e..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
220 &b")"[..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
221 ]
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
222 .concat()
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
223 }
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
224 None => [&b".*"[..], pattern].concat(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
225 }
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
226 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
227 PatternSyntax::Path | PatternSyntax::RelPath => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
228 if pattern == b"." {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
229 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
230 }
42865
69195b6f8f97 rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42864
diff changeset
231 [escape_pattern(pattern).as_slice(), b"(?:/|$)"].concat()
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
232 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
233 PatternSyntax::RootFiles => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
234 let mut res = if pattern == b"." {
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
235 vec![]
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
236 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
237 // Pattern is a directory name.
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
238 [escape_pattern(pattern).as_slice(), b"/"].concat()
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
239 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
240
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
241 // Anything after the pattern must be a non-directory.
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
242 res.extend(b"[^/]+$");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
243 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
244 }
42864
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
245 PatternSyntax::RelGlob => {
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
246 let glob_re = glob_to_re(pattern);
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
247 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
248 [b".*", rest, GLOB_SUFFIX].concat()
42864
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
249 } else {
44593
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44304
diff changeset
250 [b"(?:.*/)?", glob_re.as_slice(), GLOB_SUFFIX].concat()
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
251 }
42864
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
252 }
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42863
diff changeset
253 PatternSyntax::Glob | PatternSyntax::RootGlob => {
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
254 [glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat()
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
255 }
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
256 PatternSyntax::Include
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
257 | PatternSyntax::SubInclude
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
258 | PatternSyntax::ExpandedSubInclude(_)
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
259 | PatternSyntax::FilePath => unreachable!(),
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
260 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
261 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
262
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
263 const GLOB_SPECIAL_CHARACTERS: [u8; 7] =
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
264 [b'*', b'?', b'[', b']', b'{', b'}', b'\\'];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
265
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
266 /// TODO support other platforms
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
267 #[cfg(unix)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
268 pub fn normalize_path_bytes(bytes: &[u8]) -> Vec<u8> {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
269 if bytes.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
270 return b".".to_vec();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
271 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
272 let sep = b'/';
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
273
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
274 let mut initial_slashes = bytes.iter().take_while(|b| **b == sep).count();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
275 if initial_slashes > 2 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
276 // POSIX allows one or two initial slashes, but treats three or more
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
277 // as single slash.
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
278 initial_slashes = 1;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
279 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
280 let components = bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
281 .split(|b| *b == sep)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
282 .filter(|c| !(c.is_empty() || c == b"."))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
283 .fold(vec![], |mut acc, component| {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
284 if component != b".."
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
285 || (initial_slashes == 0 && acc.is_empty())
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
286 || (!acc.is_empty() && acc[acc.len() - 1] == b"..")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
287 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
288 acc.push(component)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
289 } else if !acc.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
290 acc.pop();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
291 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
292 acc
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
293 });
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
294 let mut new_bytes = components.join(&sep);
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
295
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
296 if initial_slashes > 0 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
297 let mut buf: Vec<_> = (0..initial_slashes).map(|_| sep).collect();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
298 buf.extend(new_bytes);
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
299 new_bytes = buf;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
300 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
301 if new_bytes.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
302 b".".to_vec()
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
303 } else {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
304 new_bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
305 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
306 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
307
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
308 /// Wrapper function to `_build_single_regex` that short-circuits 'exact' globs
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
309 /// that don't need to be transformed into a regex.
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
310 pub fn build_single_regex(
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
311 entry: &IgnorePattern,
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
312 ) -> Result<Option<Vec<u8>>, PatternError> {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
313 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
314 pattern, syntax, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
315 } = entry;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
316 let pattern = match syntax {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
317 PatternSyntax::RootGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
318 | PatternSyntax::Path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
319 | PatternSyntax::RelGlob
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49928
diff changeset
320 | PatternSyntax::RootFiles => normalize_path_bytes(pattern),
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
321 PatternSyntax::Include | PatternSyntax::SubInclude => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
322 return Err(PatternError::NonRegexPattern(entry.clone()))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
323 }
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
324 _ => pattern.to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
325 };
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
326 let is_simple_rootglob = *syntax == PatternSyntax::RootGlob
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
327 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b));
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
328 if is_simple_rootglob || syntax == &PatternSyntax::FilePath {
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
329 Ok(None)
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
330 } else {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
331 let mut entry = entry.clone();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
332 entry.pattern = pattern;
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
333 Ok(Some(_build_single_regex(&entry)))
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
334 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
335 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
336
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
337 lazy_static! {
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
338 static ref SYNTAXES: FastHashMap<&'static [u8], PatternSyntax> = {
43826
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents: 42957
diff changeset
339 let mut m = FastHashMap::default();
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
340
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
341 m.insert(b"re:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
342 m.insert(b"regexp:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
343 m.insert(b"path:".as_ref(), PatternSyntax::Path);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
344 m.insert(b"filepath:".as_ref(), PatternSyntax::FilePath);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
345 m.insert(b"relpath:".as_ref(), PatternSyntax::RelPath);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
346 m.insert(b"rootfilesin:".as_ref(), PatternSyntax::RootFiles);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
347 m.insert(b"relglob:".as_ref(), PatternSyntax::RelGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
348 m.insert(b"relre:".as_ref(), PatternSyntax::RelRegexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
349 m.insert(b"glob:".as_ref(), PatternSyntax::Glob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
350 m.insert(b"rootglob:".as_ref(), PatternSyntax::RootGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
351 m.insert(b"include:".as_ref(), PatternSyntax::Include);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
352 m.insert(b"subinclude:".as_ref(), PatternSyntax::SubInclude);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
353
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
354 m
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
355 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
356 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
357
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
358 #[derive(Debug)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
359 pub enum PatternFileWarning {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
360 /// (file path, syntax bytes)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
361 InvalidSyntax(PathBuf, Vec<u8>),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
362 /// File path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
363 NoSuchFile(PathBuf),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
364 }
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
365
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
366 pub fn parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
367 pattern: &[u8],
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
368 source: &Path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
369 default: PatternSyntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
370 ) -> IgnorePattern {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
371 let mut pattern_bytes: &[u8] = pattern;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
372 let mut syntax = default;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
373
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
374 for (s, val) in SYNTAXES.iter() {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
375 if let Some(rest) = pattern_bytes.drop_prefix(s) {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
376 syntax = val.clone();
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
377 pattern_bytes = rest;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
378 break;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
379 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
380 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
381
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
382 let pattern = pattern_bytes.to_vec();
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
383
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
384 IgnorePattern {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
385 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
386 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
387 source: source.to_owned(),
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
388 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
389 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
390
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
391 pub fn parse_pattern_file_contents(
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
392 lines: &[u8],
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
393 file_path: &Path,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
394 default_syntax_override: Option<PatternSyntax>,
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
395 warn: bool,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
396 relativize: bool,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
397 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
398 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap();
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44834
diff changeset
399
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44834
diff changeset
400 #[allow(clippy::trivial_regex)]
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
401 let comment_escape_regex = Regex::new(r"\\#").unwrap();
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
402 let mut inputs: Vec<IgnorePattern> = vec![];
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
403 let mut warnings: Vec<PatternFileWarning> = vec![];
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
404
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
405 let mut current_syntax =
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
406 default_syntax_override.unwrap_or(PatternSyntax::RelRegexp);
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
407
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
408 for mut line in lines.split(|c| *c == b'\n') {
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
409 let line_buf;
42635
30f8e786868c rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents: 42634
diff changeset
410 if line.contains(&b'#') {
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
411 if let Some(cap) = comment_regex.captures(line) {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
412 line = &line[..cap.get(1).unwrap().end()]
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
413 }
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
414 line_buf = comment_escape_regex.replace_all(line, NoExpand(b"#"));
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
415 line = &line_buf;
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
416 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
417
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
418 let line = line.trim_end();
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
419
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
420 if line.is_empty() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
421 continue;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
422 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
423
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
424 if let Some(syntax) = line.drop_prefix(b"syntax:") {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
425 let syntax = syntax.trim();
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
426
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
427 if let Some(parsed) =
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
428 SYNTAXES.get([syntax, &b":"[..]].concat().as_slice())
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
429 {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
430 current_syntax = parsed.clone();
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
431 } else if warn {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
432 warnings.push(PatternFileWarning::InvalidSyntax(
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
433 file_path.to_owned(),
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
434 syntax.to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
435 ));
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
436 }
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
437 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
438 let pattern = parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
439 line,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
440 file_path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
441 current_syntax.clone(),
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
442 );
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
443 inputs.push(if relativize {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
444 pattern.to_relative()
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
445 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
446 pattern
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
447 })
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
448 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
449 }
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
450 Ok((inputs, warnings))
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
451 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
452
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
453 pub fn read_pattern_file(
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
454 file_path: &Path,
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
455 warn: bool,
49558
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
456 inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]),
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
457 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
458 match std::fs::read(file_path) {
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
459 Ok(contents) => {
49558
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
460 inspect_pattern_bytes(file_path, &contents);
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
461 parse_pattern_file_contents(&contents, file_path, None, warn, true)
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
462 }
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
463 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok((
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
464 vec![],
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
465 vec![PatternFileWarning::NoSuchFile(file_path.to_owned())],
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
466 )),
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
467 Err(e) => Err(e.into()),
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
468 }
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
469 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
470
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
471 /// Represents an entry in an "ignore" file.
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
472 #[derive(Debug, Eq, PartialEq, Clone)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
473 pub struct IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
474 pub syntax: PatternSyntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
475 pub pattern: Vec<u8>,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
476 pub source: PathBuf,
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
477 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
478
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
479 impl IgnorePattern {
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
480 pub fn new(syntax: PatternSyntax, pattern: &[u8], source: &Path) -> Self {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
481 Self {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
482 syntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
483 pattern: pattern.to_owned(),
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
484 source: source.to_owned(),
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
485 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
486 }
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
487
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
488 pub fn to_relative(self) -> Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
489 let Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
490 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
491 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
492 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
493 } = self;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
494 Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
495 syntax: match syntax {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
496 PatternSyntax::Regexp => PatternSyntax::RelRegexp,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
497 PatternSyntax::Glob => PatternSyntax::RelGlob,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
498 x => x,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
499 },
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
500 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
501 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
502 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
503 }
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
504 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
505
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
506 pub type PatternResult<T> = Result<T, PatternError>;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
507
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
508 /// Wrapper for `read_pattern_file` that also recursively expands `include:`
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
509 /// and `subinclude:` patterns.
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
510 ///
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
511 /// The former are expanded in place, while `PatternSyntax::ExpandedSubInclude`
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
512 /// is used for the latter to form a tree of patterns.
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
513 pub fn get_patterns_from_file(
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
514 pattern_file: &Path,
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
515 root_dir: &Path,
49558
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
516 inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]),
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
517 ) -> PatternResult<(Vec<IgnorePattern>, Vec<PatternFileWarning>)> {
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
518 let (patterns, mut warnings) =
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
519 read_pattern_file(pattern_file, true, inspect_pattern_bytes)?;
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
520 let patterns = patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
521 .into_iter()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
522 .flat_map(|entry| -> PatternResult<_> {
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
523 Ok(match &entry.syntax {
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
524 PatternSyntax::Include => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
525 let inner_include =
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
526 root_dir.join(get_path_from_bytes(&entry.pattern));
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
527 let (inner_pats, inner_warnings) = get_patterns_from_file(
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
528 &inner_include,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
529 root_dir,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
530 inspect_pattern_bytes,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
531 )?;
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
532 warnings.extend(inner_warnings);
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
533 inner_pats
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
534 }
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
535 PatternSyntax::SubInclude => {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
536 let mut sub_include = SubInclude::new(
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49928
diff changeset
537 root_dir,
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
538 &entry.pattern,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
539 &entry.source,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
540 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
541 let (inner_patterns, inner_warnings) =
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
542 get_patterns_from_file(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
543 &sub_include.path,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
544 &sub_include.root,
47409
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47379
diff changeset
545 inspect_pattern_bytes,
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
546 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
547 sub_include.included_patterns = inner_patterns;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
548 warnings.extend(inner_warnings);
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
549 vec![IgnorePattern {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
550 syntax: PatternSyntax::ExpandedSubInclude(Box::new(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
551 sub_include,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
552 )),
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
553 ..entry
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
554 }]
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
555 }
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
556 _ => vec![entry],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
557 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
558 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
559 .flatten()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
560 .collect();
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
561
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
562 Ok((patterns, warnings))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
563 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
564
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
565 /// Holds all the information needed to handle a `subinclude:` pattern.
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
566 #[derive(Debug, PartialEq, Eq, Clone)]
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
567 pub struct SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
568 /// Will be used for repository (hg) paths that start with this prefix.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
569 /// It is relative to the current working directory, so comparing against
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
570 /// repository paths is painless.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
571 pub prefix: HgPathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
572 /// The file itself, containing the patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
573 pub path: PathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
574 /// Folder in the filesystem where this it applies
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
575 pub root: PathBuf,
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
576
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
577 pub included_patterns: Vec<IgnorePattern>,
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
578 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
579
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
580 impl SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
581 pub fn new(
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
582 root_dir: &Path,
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
583 pattern: &[u8],
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
584 source: &Path,
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
585 ) -> Result<SubInclude, HgPathError> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
586 let normalized_source =
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
587 normalize_path_bytes(&get_bytes_from_path(source));
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
588
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
589 let source_root = get_path_from_bytes(&normalized_source);
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44834
diff changeset
590 let source_root =
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44834
diff changeset
591 source_root.parent().unwrap_or_else(|| source_root.deref());
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
592
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
593 let path = source_root.join(get_path_from_bytes(pattern));
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44834
diff changeset
594 let new_root = path.parent().unwrap_or_else(|| path.deref());
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
595
47378
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
596 let prefix = canonical_path(root_dir, root_dir, new_root)?;
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
597
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
598 Ok(Self {
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49928
diff changeset
599 prefix: path_to_hg_path_buf(prefix).map(|mut p| {
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
600 if !p.is_empty() {
48311
6d69e83e6b6e rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 47409
diff changeset
601 p.push_byte(b'/');
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
602 }
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49928
diff changeset
603 p
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
604 })?,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
605 path: path.to_owned(),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
606 root: new_root.to_owned(),
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
607 included_patterns: Vec::new(),
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
608 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
609 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
610 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
611
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
612 /// Separate and pre-process subincludes from other patterns for the "ignore"
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
613 /// phase.
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
614 pub fn filter_subincludes(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
615 ignore_patterns: Vec<IgnorePattern>,
49928
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Raphaël Gomès <rgomes@octobus.net>
parents: 49605
diff changeset
616 ) -> Result<(Vec<SubInclude>, Vec<IgnorePattern>), HgPathError> {
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
617 let mut subincludes = vec![];
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
618 let mut others = vec![];
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
619
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
620 for pattern in ignore_patterns {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
621 if let PatternSyntax::ExpandedSubInclude(sub_include) = pattern.syntax
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
622 {
49928
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Raphaël Gomès <rgomes@octobus.net>
parents: 49605
diff changeset
623 subincludes.push(*sub_include);
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
624 } else {
47379
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47378
diff changeset
625 others.push(pattern)
44304
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
626 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
627 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
628 Ok((subincludes, others))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
629 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44303
diff changeset
630
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
631 #[cfg(test)]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
632 mod tests {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
633 use super::*;
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
634 use pretty_assertions::assert_eq;
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
635
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
636 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
637 fn escape_pattern_test() {
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
638 let untouched =
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
639 br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#;
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
640 assert_eq!(escape_pattern(untouched), untouched.to_vec());
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
641 // All escape codes
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
642 assert_eq!(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
643 escape_pattern(br#"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"#),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
644 br#"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\ \\t\\n\\r\\v\\f"#
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
645 .to_vec()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
646 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
647 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
648
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
649 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
650 fn glob_test() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
651 assert_eq!(glob_to_re(br#"?"#), br#"."#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
652 assert_eq!(glob_to_re(br#"*"#), br#"[^/]*"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
653 assert_eq!(glob_to_re(br#"**"#), br#".*"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
654 assert_eq!(glob_to_re(br#"**/a"#), br#"(?:.*/)?a"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
655 assert_eq!(glob_to_re(br#"a/**/b"#), br#"a/(?:.*/)?b"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
656 assert_eq!(glob_to_re(br#"[a*?!^][^b][!c]"#), br#"[a*?!^][\^b][^c]"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
657 assert_eq!(glob_to_re(br#"{a,b}"#), br#"(?:a|b)"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
658 assert_eq!(glob_to_re(br#".\*\?"#), br#"\.\*\?"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
659 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
660
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
661 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
662 fn test_parse_pattern_file_contents() {
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
663 let lines = b"syntax: glob\n*.elc";
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
664
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
665 assert_eq!(
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
666 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
667 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
668 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
669 None,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
670 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
671 true,
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
672 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
673 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
674 .0,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
675 vec![IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
676 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
677 b"*.elc",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
678 Path::new("file_path")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
679 )],
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
680 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
681
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
682 let lines = b"syntax: include\nsyntax: glob";
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
683
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
684 assert_eq!(
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
685 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
686 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
687 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
688 None,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
689 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
690 true,
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
691 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
692 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
693 .0,
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
694 vec![]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
695 );
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
696 let lines = b"glob:**.o";
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
697 assert_eq!(
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
698 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
699 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
700 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
701 None,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
702 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
703 true,
49482
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
704 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
705 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 48311
diff changeset
706 .0,
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
707 vec![IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
708 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
709 b"**.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
710 Path::new("file_path")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
711 )]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
712 );
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
713 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
714
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
715 #[test]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
716 fn test_build_single_regex() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
717 assert_eq!(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
718 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
719 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
720 b"rust/target/",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
721 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
722 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
723 .unwrap(),
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
724 Some(br"(?:.*/)?rust/target(?:/|$)".to_vec()),
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents: 42327
diff changeset
725 );
44834
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
726 assert_eq!(
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
727 build_single_regex(&IgnorePattern::new(
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
728 PatternSyntax::Regexp,
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
729 br"rust/target/\d+",
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
730 Path::new("")
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
731 ))
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
732 .unwrap(),
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
733 Some(br"rust/target/\d+".to_vec()),
be6401a25726 rust-regex: add test for verbatim regex syntax
Raphaël Gomès <rgomes@octobus.net>
parents: 44833
diff changeset
734 );
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
735 }
42438
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
736
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
737 #[test]
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
738 fn test_build_single_regex_shortcut() {
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
739 assert_eq!(
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
740 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
741 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
742 b"",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
743 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
744 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
745 .unwrap(),
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
746 None,
42438
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
747 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
748 assert_eq!(
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
749 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
750 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
751 b"whatever",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
752 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
753 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
754 .unwrap(),
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44593
diff changeset
755 None,
42438
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
756 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
757 assert_eq!(
44303
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
758 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
759 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
760 b"*.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
761 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
762 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents: 43826
diff changeset
763 .unwrap(),
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44802
diff changeset
764 Some(br"[^/]*\.o(?:/|$)".to_vec()),
42438
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
765 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents: 42437
diff changeset
766 }
49604
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
767
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
768 #[test]
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
769 fn test_build_single_relregex() {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
770 assert_eq!(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
771 build_single_regex(&IgnorePattern::new(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
772 PatternSyntax::RelRegexp,
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
773 b"^ba{2}r",
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
774 Path::new("")
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
775 ))
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
776 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
777 Some(b"^ba{2}r".to_vec()),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
778 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
779 assert_eq!(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
780 build_single_regex(&IgnorePattern::new(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
781 PatternSyntax::RelRegexp,
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
782 b"ba{2}r",
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
783 Path::new("")
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
784 ))
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
785 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
786 Some(b".*ba{2}r".to_vec()),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
787 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
788 assert_eq!(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
789 build_single_regex(&IgnorePattern::new(
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
790 PatternSyntax::RelRegexp,
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
791 b"(?ia)ba{2}r",
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
792 Path::new("")
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
793 ))
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
794 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
795 Some(b"(?ia:.*ba{2}r)".to_vec()),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
796 );
49605
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
797 assert_eq!(
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
798 build_single_regex(&IgnorePattern::new(
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
799 PatternSyntax::RelRegexp,
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
800 b"(?ia)^ba{2}r",
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
801 Path::new("")
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
802 ))
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
803 .unwrap(),
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
804 Some(b"(?ia:^ba{2}r)".to_vec()),
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49604
diff changeset
805 );
49604
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49558
diff changeset
806 }
42327
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
807 }