annotate rust/hg-core/src/filepatterns.rs @ 51446:406b413e3cf2 stable

rust-filepatterns: export glob_to_re function Making this function public should not risk freezing the internal API, and it can be useful for all downstream code that needs to perform glob matching against byte strings, such as RHGitaly where it will be useful to match on branches and tags.
author Georges Racinet <georges.racinet@octobus.net>
date Mon, 11 Mar 2024 13:23:18 +0100
parents 532e74ad3ff6
children 2a89d2f6336f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42767
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
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
10 use crate::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
11 utils::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
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: 44346
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: 44346
diff changeset
14 SliceExt,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
15 },
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
16 FastHashMap, PatternError,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
17 };
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42498
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};
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
20 use std::ops::Deref;
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
21 use std::path::{Path, PathBuf};
42349
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! {
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
25 static ref RE_ESCAPE: Vec<Vec<u8>> = {
42349
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();
50883
2b4bcdc948e7 rust: don't escape spaces in regex
Spencer Baugh <sbaugh@janestreet.com>
parents: 50882
diff changeset
27 let to_escape = b"()[]{}?*+-|^$\\.&~#\t\n\r\x0b\x0c";
42349
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
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
39 #[derive(Debug, Clone, PartialEq, Eq)]
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
40 pub enum PatternSyntax {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
41 /// A regular expression
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
42 Regexp,
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
43 /// 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
44 RootGlob,
42851
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42767
diff changeset
45 /// Glob that matches at any suffix of the path (still anchored at
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42767
diff changeset
46 /// slashes)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
47 Glob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
48 /// a path relative to repository root, which is matched recursively
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
49 Path,
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50003
diff changeset
50 /// 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: 50003
diff changeset
51 FilePath,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
52 /// A path relative to cwd
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
53 RelPath,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
54 /// an unrooted glob (*.rs matches Rust files in all dirs)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 RelGlob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
56 /// A regexp that needn't match the start of a name
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
57 RelRegexp,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
58 /// 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: 43844
diff changeset
59 /// (will not match subdirectories)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
60 RootFiles,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
61 /// 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: 44346
diff changeset
62 Include,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
63 /// 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: 44346
diff changeset
64 SubInclude,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
65 /// 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: 47384
diff changeset
66 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
67 /// 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: 47384
diff changeset
68 /// 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: 47384
diff changeset
69 /// SubInclude requires more handling.
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
70 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
71 /// 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: 47384
diff changeset
72 ExpandedSubInclude(Box<SubInclude>),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
73 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
74
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
75 /// Transforms a glob pattern into a regex
51446
406b413e3cf2 rust-filepatterns: export glob_to_re function
Georges Racinet <georges.racinet@octobus.net>
parents: 51118
diff changeset
76 pub fn glob_to_re(pat: &[u8]) -> Vec<u8> {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
77 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
78 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
79 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
80
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 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
82 input = rest;
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 match c {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
85 b'*' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 for (source, repl) in GLOB_REPLACEMENTS {
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
87 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: 42851
diff changeset
88 input = rest;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
89 res.extend(*repl);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
90 break;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
91 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
92 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
93 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
94 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
95 b'[' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
96 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
97 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
98 Some(end) => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
99 // 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
100 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
101
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
102 res.extend(b"[");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
103
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
104 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
105 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
106 res.extend(b"^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
107 } 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
108 res.extend(b"\\^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
109 } 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
110 res.extend(b"\\\\")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
111 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
112 res.push(*b)
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
113 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
114 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
115 res.extend(b"]");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 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
117 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 b'{' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
121 group_depth += 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
122 res.extend(b"(?:")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
124 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
125 group_depth -= 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
126 res.extend(b")");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
127 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128 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
129 b'\\' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 let c = {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
131 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
132 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
133 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
134 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
136 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
137 };
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
138 res.extend(&RE_ESCAPE[*c as usize])
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
139 }
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
140 _ => res.extend(&RE_ESCAPE[*c as usize]),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
142 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
143 res
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 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
147 pattern
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
148 .iter()
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
149 .flat_map(|c| RE_ESCAPE[*c as usize].clone())
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
150 .collect()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
151 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
152
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
153 pub fn parse_pattern_syntax(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
154 kind: &[u8],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
155 ) -> Result<PatternSyntax, PatternError> {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
156 match kind {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
157 b"re:" => Ok(PatternSyntax::Regexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
158 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: 50003
diff changeset
159 b"filepath:" => Ok(PatternSyntax::FilePath),
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
160 b"relpath:" => Ok(PatternSyntax::RelPath),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
161 b"rootfilesin:" => Ok(PatternSyntax::RootFiles),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
162 b"relglob:" => Ok(PatternSyntax::RelGlob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
163 b"relre:" => Ok(PatternSyntax::RelRegexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
164 b"glob:" => Ok(PatternSyntax::Glob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
165 b"rootglob:" => Ok(PatternSyntax::RootGlob),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
166 b"include:" => Ok(PatternSyntax::Include),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
167 b"subinclude:" => Ok(PatternSyntax::SubInclude),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
168 _ => Err(PatternError::UnsupportedSyntax(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
169 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
170 )),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
171 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
172 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
173
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
174 lazy_static! {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
175 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: 49550
diff changeset
176 }
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
177
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
178 /// 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
179 /// 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
180 /// otherwise, returns the corresponding regex.
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
181 fn _build_single_regex(entry: &IgnorePattern, glob_suffix: &[u8]) -> Vec<u8> {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
182 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
183 syntax, pattern, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
184 } = entry;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
185 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
186 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
187 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
188 match syntax {
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
189 PatternSyntax::Regexp => pattern.to_owned(),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
190 PatternSyntax::RelRegexp => {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
191 // 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: 44347
diff changeset
192 // 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: 44347
diff changeset
193 // engines.
44892
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
194 if pattern[0] == b'^'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
195 || pattern[0] == b'*'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
196 || pattern.starts_with(b".*")
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
197 {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
198 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
199 }
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
200 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: 49550
diff changeset
201 Some(mat) => {
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
202 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: 49550
diff changeset
203 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: 49550
diff changeset
204 [
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
205 &b"(?"[..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
206 &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: 49550
diff changeset
207 &b":"[..],
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
208 if pattern[e] == b'^'
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
209 || pattern[e] == b'*'
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
210 || pattern[e..].starts_with(b".*")
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
211 {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
212 &b""[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
213 } else {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
214 &b".*"[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
215 },
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
216 &pattern[e..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
217 &b")"[..],
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
218 ]
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
219 .concat()
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
220 }
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
221 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: 49550
diff changeset
222 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
223 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
224 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
225 if pattern == b"." {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
226 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
227 }
42871
69195b6f8f97 rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42870
diff changeset
228 [escape_pattern(pattern).as_slice(), b"(?:/|$)"].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
229 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
230 PatternSyntax::RootFiles => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
231 let mut res = if pattern == b"." {
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
232 vec![]
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
233 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
234 // Pattern is a directory name.
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
235 [escape_pattern(pattern).as_slice(), b"/"].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
236 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
237
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
238 // 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
239 res.extend(b"[^/]+$");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
240 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
241 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
242 PatternSyntax::RelGlob => {
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
243 let glob_re = glob_to_re(pattern);
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
244 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") {
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
245 [b".*", rest, glob_suffix].concat()
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
246 } else {
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
247 [b"(?:.*/)?", glob_re.as_slice(), glob_suffix].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
248 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
249 }
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
250 PatternSyntax::Glob | PatternSyntax::RootGlob => {
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
251 [glob_to_re(pattern).as_slice(), glob_suffix].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
252 }
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
253 PatternSyntax::Include
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
254 | 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: 50003
diff changeset
255 | PatternSyntax::ExpandedSubInclude(_)
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50003
diff changeset
256 | PatternSyntax::FilePath => unreachable!(),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
257 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
258 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
259
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
260 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
261 [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
262
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
263 /// TODO support other platforms
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
264 #[cfg(unix)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
265 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: 43844
diff changeset
266 if bytes.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
267 return b".".to_vec();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
268 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
269 let sep = b'/';
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
270
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
271 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: 43844
diff changeset
272 if initial_slashes > 2 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
273 // 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: 43844
diff changeset
274 // as single slash.
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
275 initial_slashes = 1;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
276 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
277 let components = bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
278 .split(|b| *b == sep)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
279 .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: 43844
diff changeset
280 .fold(vec![], |mut acc, component| {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
281 if component != b".."
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
282 || (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: 43844
diff changeset
283 || (!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: 43844
diff changeset
284 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
285 acc.push(component)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
286 } else if !acc.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
287 acc.pop();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
288 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
289 acc
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
290 });
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
291 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: 43844
diff changeset
292
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
293 if initial_slashes > 0 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
294 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: 43844
diff changeset
295 buf.extend(new_bytes);
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
296 new_bytes = buf;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
297 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
298 if new_bytes.is_empty() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
299 b".".to_vec()
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
300 } else {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
301 new_bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
302 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
303 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
304
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
305 /// 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
306 /// 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
307 pub fn build_single_regex(
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
308 entry: &IgnorePattern,
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
309 glob_suffix: &[u8],
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44601
diff changeset
310 ) -> Result<Option<Vec<u8>>, PatternError> {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
311 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
312 pattern, syntax, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
313 } = entry;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
314 let pattern = match syntax {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
315 PatternSyntax::RootGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
316 | PatternSyntax::Path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
317 | PatternSyntax::RelGlob
50882
df6dfad5009a rust-filepatterns: also normalize RelPath
Spencer Baugh <sbaugh@janestreet.com>
parents: 50881
diff changeset
318 | PatternSyntax::RelPath
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
319 | PatternSyntax::RootFiles => normalize_path_bytes(pattern),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
320 PatternSyntax::Include | PatternSyntax::SubInclude => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
321 return Err(PatternError::NonRegexPattern(entry.clone()))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
322 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
323 _ => pattern.to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
324 };
50695
1c31b343e514 match: add `filepath:` pattern to match an exact filepath relative to the root
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50003
diff changeset
325 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: 50003
diff changeset
326 && !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: 50003
diff changeset
327 if is_simple_rootglob || syntax == &PatternSyntax::FilePath {
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44601
diff changeset
328 Ok(None)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
329 } else {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
330 let mut entry = entry.clone();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
331 entry.pattern = pattern;
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
332 Ok(Some(_build_single_regex(&entry, glob_suffix)))
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
333 }
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 lazy_static! {
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
337 static ref SYNTAXES: FastHashMap<&'static [u8], PatternSyntax> = {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42960
diff changeset
338 let mut m = FastHashMap::default();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
339
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
340 m.insert(b"re:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
341 m.insert(b"regexp:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
342 m.insert(b"path:".as_ref(), PatternSyntax::Path);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
343 m.insert(b"filepath:".as_ref(), PatternSyntax::FilePath);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
344 m.insert(b"relpath:".as_ref(), PatternSyntax::RelPath);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
345 m.insert(b"rootfilesin:".as_ref(), PatternSyntax::RootFiles);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
346 m.insert(b"relglob:".as_ref(), PatternSyntax::RelGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
347 m.insert(b"relre:".as_ref(), PatternSyntax::RelRegexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
348 m.insert(b"glob:".as_ref(), PatternSyntax::Glob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
349 m.insert(b"rootglob:".as_ref(), PatternSyntax::RootGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
350 m.insert(b"include:".as_ref(), PatternSyntax::Include);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
351 m.insert(b"subinclude:".as_ref(), PatternSyntax::SubInclude);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
352
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
353 m
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
354 };
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
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
357 #[derive(Debug)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
358 pub enum PatternFileWarning {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
359 /// (file path, syntax bytes)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
360 InvalidSyntax(PathBuf, Vec<u8>),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
361 /// File path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
362 NoSuchFile(PathBuf),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
363 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
364
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
365 pub fn parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
366 pattern: &[u8],
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
367 source: &Path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
368 default: PatternSyntax,
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
369 normalize: bool,
50881
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
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
382 let pattern = match syntax {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
383 PatternSyntax::RootGlob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
384 | PatternSyntax::Path
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
385 | PatternSyntax::Glob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
386 | PatternSyntax::RelGlob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
387 | PatternSyntax::RelPath
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
388 | PatternSyntax::RootFiles
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
389 if normalize =>
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
390 {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
391 normalize_path_bytes(pattern_bytes)
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
392 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
393 _ => pattern_bytes.to_vec(),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
394 };
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
395
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
396 IgnorePattern {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
397 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
398 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
399 source: source.to_owned(),
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
400 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
401 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
402
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
403 pub fn parse_pattern_file_contents(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
404 lines: &[u8],
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
405 file_path: &Path,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
406 default_syntax_override: Option<PatternSyntax>,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
407 warn: bool,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
408 relativize: bool,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
409 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
410 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap();
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44893
diff changeset
411
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44893
diff changeset
412 #[allow(clippy::trivial_regex)]
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
413 let comment_escape_regex = Regex::new(r"\\#").unwrap();
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
414 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: 43844
diff changeset
415 let mut warnings: Vec<PatternFileWarning> = vec![];
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
416
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
417 let mut current_syntax =
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
418 default_syntax_override.unwrap_or(PatternSyntax::RelRegexp);
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
419
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
420 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
421 let line_buf;
42635
30f8e786868c rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents: 42634
diff changeset
422 if line.contains(&b'#') {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
423 if let Some(cap) = comment_regex.captures(line) {
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
424 line = &line[..cap.get(1).unwrap().end()]
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
425 }
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
426 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
427 line = &line_buf;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
428 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
429
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
430 let line = line.trim_end();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
431
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
432 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
433 continue;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
434 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
435
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
436 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: 42851
diff changeset
437 let syntax = syntax.trim();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
438
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
439 if let Some(parsed) =
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
440 SYNTAXES.get([syntax, &b":"[..]].concat().as_slice())
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
441 {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
442 current_syntax = parsed.clone();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
443 } else if warn {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
444 warnings.push(PatternFileWarning::InvalidSyntax(
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
445 file_path.to_owned(),
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
446 syntax.to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
447 ));
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
448 }
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
449 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
450 let pattern = parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
451 line,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
452 file_path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
453 current_syntax.clone(),
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
454 false,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
455 );
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
456 inputs.push(if relativize {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
457 pattern.to_relative()
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
458 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
459 pattern
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
460 })
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
461 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
462 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
463 Ok((inputs, warnings))
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
464 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
465
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
466 pub fn parse_pattern_args(
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
467 patterns: Vec<Vec<u8>>,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
468 cwd: &Path,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
469 root: &Path,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
470 ) -> Result<Vec<IgnorePattern>, HgPathError> {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
471 let mut ignore_patterns: Vec<IgnorePattern> = Vec::new();
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
472 for pattern in patterns {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
473 let pattern = parse_one_pattern(
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
474 &pattern,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
475 Path::new("<args>"),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
476 PatternSyntax::RelPath,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
477 true,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
478 );
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
479 match pattern.syntax {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
480 PatternSyntax::RelGlob | PatternSyntax::RelPath => {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
481 let name = get_path_from_bytes(&pattern.pattern);
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
482 let canon = canonical_path(root, cwd, name)?;
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
483 ignore_patterns.push(IgnorePattern {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
484 syntax: pattern.syntax,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
485 pattern: get_bytes_from_path(canon),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
486 source: pattern.source,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
487 })
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
488 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
489 _ => ignore_patterns.push(pattern.to_owned()),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
490 };
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
491 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
492 Ok(ignore_patterns)
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
493 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
494
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
495 pub fn read_pattern_file(
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
496 file_path: &Path,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
497 warn: bool,
49550
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49502
diff changeset
498 inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]),
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
499 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
500 match std::fs::read(file_path) {
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
501 Ok(contents) => {
49550
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49502
diff changeset
502 inspect_pattern_bytes(file_path, &contents);
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
503 parse_pattern_file_contents(&contents, file_path, None, warn, true)
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
504 }
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
505 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: 47385
diff changeset
506 vec![],
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
507 vec![PatternFileWarning::NoSuchFile(file_path.to_owned())],
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
508 )),
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
509 Err(e) => Err(e.into()),
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
510 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
511 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
512
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
513 /// 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: 43844
diff changeset
514 #[derive(Debug, Eq, PartialEq, Clone)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
515 pub struct IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
516 pub syntax: PatternSyntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
517 pub pattern: Vec<u8>,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
518 pub source: PathBuf,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
519 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
520
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
521 impl IgnorePattern {
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
522 pub fn new(syntax: PatternSyntax, pattern: &[u8], source: &Path) -> Self {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
523 Self {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
524 syntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
525 pattern: pattern.to_owned(),
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
526 source: source.to_owned(),
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
527 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
528 }
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
529
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
530 pub fn to_relative(self) -> Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
531 let Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
532 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
533 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
534 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
535 } = self;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
536 Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
537 syntax: match syntax {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
538 PatternSyntax::Regexp => PatternSyntax::RelRegexp,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
539 PatternSyntax::Glob => PatternSyntax::RelGlob,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
540 x => x,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
541 },
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
542 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
543 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
544 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
545 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
546 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
547
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
548 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: 43844
diff changeset
549
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
550 /// Wrapper for `read_pattern_file` that also recursively expands `include:`
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
551 /// and `subinclude:` patterns.
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
552 ///
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
553 /// 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: 47384
diff changeset
554 /// is used for the latter to form a tree of patterns.
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
555 pub fn get_patterns_from_file(
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
556 pattern_file: &Path,
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
557 root_dir: &Path,
49550
363923bd51cd dirstate-v2: hash the source of the ignore patterns as well
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49502
diff changeset
558 inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
559 ) -> PatternResult<(Vec<IgnorePattern>, Vec<PatternFileWarning>)> {
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
560 let (patterns, mut warnings) =
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
561 read_pattern_file(pattern_file, true, inspect_pattern_bytes)?;
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
562 let patterns = patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
563 .into_iter()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
564 .flat_map(|entry| -> PatternResult<_> {
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
565 Ok(match &entry.syntax {
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
566 PatternSyntax::Include => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
567 let inner_include =
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
568 root_dir.join(get_path_from_bytes(&entry.pattern));
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
569 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: 47385
diff changeset
570 &inner_include,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
571 root_dir,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
572 inspect_pattern_bytes,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
573 )?;
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
574 warnings.extend(inner_warnings);
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
575 inner_pats
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
576 }
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
577 PatternSyntax::SubInclude => {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
578 let mut sub_include = SubInclude::new(
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
579 root_dir,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
580 &entry.pattern,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
581 &entry.source,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
582 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
583 let (inner_patterns, inner_warnings) =
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
584 get_patterns_from_file(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
585 &sub_include.path,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
586 &sub_include.root,
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
587 inspect_pattern_bytes,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
588 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
589 sub_include.included_patterns = inner_patterns;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
590 warnings.extend(inner_warnings);
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
591 vec![IgnorePattern {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
592 syntax: PatternSyntax::ExpandedSubInclude(Box::new(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
593 sub_include,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
594 )),
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
595 ..entry
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
596 }]
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
597 }
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
598 _ => vec![entry],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
599 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
600 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
601 .flatten()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
602 .collect();
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
603
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
604 Ok((patterns, warnings))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
605 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
606
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
607 /// Holds all the information needed to handle a `subinclude:` pattern.
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
608 #[derive(Debug, PartialEq, Eq, Clone)]
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
609 pub struct SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
610 /// 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: 44346
diff changeset
611 /// 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: 44346
diff changeset
612 /// repository paths is painless.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
613 pub prefix: HgPathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
614 /// The file itself, containing the patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
615 pub path: PathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
616 /// 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: 44346
diff changeset
617 pub root: PathBuf,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
618
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
619 pub included_patterns: Vec<IgnorePattern>,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
620 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
621
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
622 impl SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
623 pub fn new(
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
624 root_dir: &Path,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
625 pattern: &[u8],
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
626 source: &Path,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
627 ) -> Result<SubInclude, HgPathError> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
628 let normalized_source =
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
629 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: 44346
diff changeset
630
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
631 let source_root = get_path_from_bytes(&normalized_source);
51118
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
632 let source_root = source_root.parent().unwrap_or(source_root);
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
633
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
634 let path = source_root.join(get_path_from_bytes(pattern));
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44893
diff changeset
635 let new_root = path.parent().unwrap_or_else(|| path.deref());
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
636
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
637 let prefix = canonical_path(root_dir, root_dir, new_root)?;
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
638
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
639 Ok(Self {
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
640 prefix: path_to_hg_path_buf(prefix).map(|mut p| {
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
641 if !p.is_empty() {
48311
6d69e83e6b6e rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 47415
diff changeset
642 p.push_byte(b'/');
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
643 }
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
644 p
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
645 })?,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
646 path: path.to_owned(),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
647 root: new_root.to_owned(),
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
648 included_patterns: Vec::new(),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
649 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
650 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
651 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
652
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
653 /// 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: 44346
diff changeset
654 /// phase.
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
655 pub fn filter_subincludes(
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
656 ignore_patterns: Vec<IgnorePattern>,
50001
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49577
diff changeset
657 ) -> Result<(Vec<SubInclude>, Vec<IgnorePattern>), HgPathError> {
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
658 let mut subincludes = vec![];
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
659 let mut others = vec![];
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
660
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
661 for pattern in ignore_patterns {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
662 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: 47384
diff changeset
663 {
50001
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49577
diff changeset
664 subincludes.push(*sub_include);
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
665 } else {
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
666 others.push(pattern)
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
667 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
668 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
669 Ok((subincludes, others))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
670 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
671
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
672 #[cfg(test)]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
673 mod tests {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
674 use super::*;
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
675 use pretty_assertions::assert_eq;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
676
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
677 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
678 fn escape_pattern_test() {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
679 let untouched =
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
680 br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
681 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
682 // All escape codes
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
683 assert_eq!(
51118
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
684 escape_pattern(br"()[]{}?*+-|^$\\.&~#\t\n\r\v\f"),
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
685 br"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\\t\\n\\r\\v\\f".to_vec()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
686 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
687 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
688
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
689 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
690 fn glob_test() {
51118
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
691 assert_eq!(glob_to_re(br"?"), br".");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
692 assert_eq!(glob_to_re(br"*"), br"[^/]*");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
693 assert_eq!(glob_to_re(br"**"), br".*");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
694 assert_eq!(glob_to_re(br"**/a"), br"(?:.*/)?a");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
695 assert_eq!(glob_to_re(br"a/**/b"), br"a/(?:.*/)?b");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
696 assert_eq!(glob_to_re(br"[a*?!^][^b][!c]"), br"[a*?!^][\^b][^c]");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
697 assert_eq!(glob_to_re(br"{a,b}"), br"(?:a|b)");
532e74ad3ff6 rust: run a clippy pass with the latest stable version
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50890
diff changeset
698 assert_eq!(glob_to_re(br".\*\?"), br"\.\*\?");
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
699 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
700
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
701 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
702 fn test_parse_pattern_file_contents() {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
703 let lines = b"syntax: glob\n*.elc";
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
704
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
705 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
706 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
707 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
708 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
709 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
710 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
711 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
712 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
713 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
714 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
715 vec![IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
716 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
717 b"*.elc",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
718 Path::new("file_path")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
719 )],
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
720 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
721
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
722 let lines = b"syntax: include\nsyntax: glob";
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
723
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
724 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
725 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
726 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
727 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
728 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
729 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
730 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
731 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
732 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
733 .0,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
734 vec![]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
735 );
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
736 let lines = b"glob:**.o";
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
737 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
738 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
739 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
740 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
741 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
742 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
743 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
744 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
745 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
746 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
747 vec![IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
748 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
749 b"**.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
750 Path::new("file_path")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
751 )]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
752 );
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
753 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
754
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
755 #[test]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
756 fn test_build_single_regex() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
757 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
758 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
759 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
760 PatternSyntax::RelGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
761 b"rust/target/",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
762 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
763 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
764 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
765 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
766 .unwrap(),
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44601
diff changeset
767 Some(br"(?:.*/)?rust/target(?:/|$)".to_vec()),
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
768 );
44893
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
769 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
770 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
771 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
772 PatternSyntax::Regexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
773 br"rust/target/\d+",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
774 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
775 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
776 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
777 )
44893
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
778 .unwrap(),
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
779 Some(br"rust/target/\d+".to_vec()),
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
780 );
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
781 }
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
782
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
783 #[test]
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
784 fn test_build_single_regex_shortcut() {
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
785 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
786 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
787 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
788 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
789 b"",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
790 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
791 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
792 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
793 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
794 .unwrap(),
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44601
diff changeset
795 None,
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
796 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
797 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
798 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
799 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
800 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
801 b"whatever",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
802 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
803 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
804 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
805 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
806 .unwrap(),
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44601
diff changeset
807 None,
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
808 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
809 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
810 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
811 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
812 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
813 b"*.o",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
814 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
815 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
816 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
817 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
818 .unwrap(),
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
819 Some(br"[^/]*\.o(?:/|$)".to_vec()),
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
820 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
821 }
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
822
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
823 #[test]
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
824 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: 49550
diff changeset
825 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
826 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
827 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
828 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
829 b"^ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
830 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
831 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
832 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
833 )
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
834 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
835 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: 49550
diff changeset
836 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
837 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
838 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
839 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
840 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
841 b"ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
842 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
843 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
844 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
845 )
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
846 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
847 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: 49550
diff changeset
848 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
849 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
850 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
851 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
852 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
853 b"(?ia)ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
854 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
855 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
856 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
857 )
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
858 .unwrap(),
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
859 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: 49550
diff changeset
860 );
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
861 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
862 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
863 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
864 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
865 b"(?ia)^ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
866 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
867 ),
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
868 b"(?:/|$)"
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
869 )
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
870 .unwrap(),
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
871 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: 49576
diff changeset
872 );
49576
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
873 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
874 }