annotate rust/hg-core/src/filepatterns.rs @ 52385:e2e49069eeb6

rust-ignore: make `debugignorerhg` command show a full regex, with exact files
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Tue, 03 Dec 2024 13:51:51 +0000
parents 2ff004fb491c
children 1866119cbad7
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 },
52338
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
16 FastHashMap,
44347
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};
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
20 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
21 use std::vec::Vec;
52338
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
22 use std::{fmt, ops::Deref};
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
23
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
24 #[derive(Debug, derive_more::From)]
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
25 pub enum PatternError {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
26 #[from]
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
27 Path(HgPathError),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
28 UnsupportedSyntax(String),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
29 UnsupportedSyntaxInFile(String, String, usize),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
30 TooLong(usize),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
31 #[from]
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
32 IO(std::io::Error),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
33 /// Needed a pattern that can be turned into a regex but got one that
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
34 /// can't. This should only happen through programmer error.
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
35 NonRegexPattern(IgnorePattern),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
36 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
37
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
38 impl fmt::Display for PatternError {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
40 match self {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
41 PatternError::UnsupportedSyntax(syntax) => {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
42 write!(f, "Unsupported syntax {}", syntax)
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
43 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
44 PatternError::UnsupportedSyntaxInFile(syntax, file_path, line) => {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
45 write!(
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
46 f,
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
47 "{}:{}: unsupported syntax {}",
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
48 file_path, line, syntax
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
49 )
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
50 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
51 PatternError::TooLong(size) => {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
52 write!(f, "matcher pattern is too long ({} bytes)", size)
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
53 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
54 PatternError::IO(error) => error.fmt(f),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
55 PatternError::Path(error) => error.fmt(f),
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
56 PatternError::NonRegexPattern(pattern) => {
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
57 write!(f, "'{:?}' cannot be turned into a regex", pattern)
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
58 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
59 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
60 }
f33b87b46135 rust-lib: move `PatternError` to the `filepatterns` module
Rapha?l Gom?s <rgomes@octobus.net>
parents: 51630
diff changeset
61 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
62
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 lazy_static! {
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
64 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
65 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
66 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
67 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
68 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
69 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
70 v
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 }
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 /// 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
75 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
76 &[(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
77
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
78 #[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
79 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
80 /// 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
81 Regexp,
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
82 /// 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
83 RootGlob,
42851
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42767
diff changeset
84 /// 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
85 /// slashes)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 Glob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
87 /// 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
88 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
89 /// 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
90 FilePath,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
91 /// 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
92 RelPath,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
93 /// 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
94 RelGlob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
95 /// 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
96 RelRegexp,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
97 /// 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
98 /// (will not match subdirectories)
51479
2a89d2f6336f match: rename RootFiles to RootFilesIn for more consistency
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51446
diff changeset
99 RootFilesIn,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
100 /// 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
101 Include,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
102 /// 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
103 SubInclude,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
104 /// 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
105 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
106 /// 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
107 /// 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
108 /// 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
109 ///
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
110 /// 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
111 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
112 }
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 /// 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
115 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
116 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
117 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
118 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
119
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 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
121 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
122
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 match c {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
124 b'*' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125 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
126 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
127 input = rest;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128 res.extend(*repl);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
129 break;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
131 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
132 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
133 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
134 b'[' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135 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
136 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
137 Some(end) => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
138 // 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
139 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
140
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 res.extend(b"[");
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 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
144 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
145 res.extend(b"^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
146 } 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
147 res.extend(b"\\^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
148 } 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
149 res.extend(b"\\\\")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
150 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
151 res.push(*b)
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
152 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
153 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
154 res.extend(b"]");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
155 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
156 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
157 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
158 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
159 b'{' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
160 group_depth += 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
161 res.extend(b"(?:")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
162 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
163 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
164 group_depth -= 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
165 res.extend(b")");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
166 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
167 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
168 b'\\' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
169 let c = {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
170 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
171 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
172 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
173 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
174 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
175 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
176 };
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
177 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
178 }
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
179 _ => 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
180 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
181 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
182 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
183 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
184
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
185 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
186 pattern
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
187 .iter()
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
188 .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
189 .collect()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
190 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
191
51630
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
192 pub fn parse_pattern_syntax_kind(
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
193 kind: &[u8],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
194 ) -> 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
195 match kind {
51630
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
196 b"re" => Ok(PatternSyntax::Regexp),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
197 b"path" => Ok(PatternSyntax::Path),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
198 b"filepath" => Ok(PatternSyntax::FilePath),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
199 b"relpath" => Ok(PatternSyntax::RelPath),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
200 b"rootfilesin" => Ok(PatternSyntax::RootFilesIn),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
201 b"relglob" => Ok(PatternSyntax::RelGlob),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
202 b"relre" => Ok(PatternSyntax::RelRegexp),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
203 b"glob" => Ok(PatternSyntax::Glob),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
204 b"rootglob" => Ok(PatternSyntax::RootGlob),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
205 b"include" => Ok(PatternSyntax::Include),
e4b9f8a74d5f match: simplify the rust-side file pattern kind parsing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51479
diff changeset
206 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
207 _ => Err(PatternError::UnsupportedSyntax(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
208 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
209 )),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
210 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
211 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
212
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
213 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
214 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
215 }
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
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
217 /// Extra path components to match at the end of the pattern
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
218 #[derive(Clone, Copy)]
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
219 pub enum GlobSuffix {
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
220 /// `Empty` means the pattern only matches files, not directories,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
221 /// so the path needs to match exactly.
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
222 Empty,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
223 /// `MoreComponents` means the pattern matches directories as well,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
224 /// so any path that has the pattern as a prefix, should match.
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
225 MoreComponents,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
226 }
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
227
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
228 impl GlobSuffix {
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
229 fn to_re(self) -> &'static [u8] {
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
230 match self {
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
231 Self::Empty => b"$",
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
232 Self::MoreComponents => b"(?:/|$)",
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
233 }
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
234 }
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
235 }
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
236
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
237 /// 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
238 /// 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
239 /// otherwise, returns the corresponding regex.
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
240 fn _build_single_regex(
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
241 entry: &IgnorePattern,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
242 glob_suffix: GlobSuffix,
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
243 ) -> 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
244 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
245 syntax, pattern, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
246 } = entry;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
247 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
248 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
249 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
250 match syntax {
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
251 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
252 PatternSyntax::RelRegexp => {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
253 // 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
254 // 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
255 // engines.
44892
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
256 if pattern[0] == b'^'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
257 || pattern[0] == b'*'
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
258 || pattern.starts_with(b".*")
1e9bfeaec9ba rust-regex: prevent nonsensical `.*.*` pattern from happening
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44891
diff changeset
259 {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
260 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
261 }
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
262 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
263 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
264 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
265 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
266 [
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
267 &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
268 &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
269 &b":"[..],
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
270 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
271 || 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
272 || 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
273 {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
274 &b""[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
275 } else {
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
276 &b".*"[..]
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
277 },
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
278 &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
279 &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
280 ]
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
281 .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
282 }
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
283 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
284 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
285 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
286 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
287 if pattern == b"." {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
288 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
289 }
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
290 [
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
291 escape_pattern(pattern).as_slice(),
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
292 GlobSuffix::MoreComponents.to_re(),
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
293 ]
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
294 .concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
295 }
51479
2a89d2f6336f match: rename RootFiles to RootFilesIn for more consistency
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51446
diff changeset
296 PatternSyntax::RootFilesIn => {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
297 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
298 vec![]
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
299 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
300 // 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
301 [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
302 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
303
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
304 // 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
305 res.extend(b"[^/]+$");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
306 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
307 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
308 PatternSyntax::RelGlob => {
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
309 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
310 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") {
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
311 [b".*", rest, glob_suffix.to_re()].concat()
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
312 } else {
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
313 [b"(?:.*/)?", glob_re.as_slice(), glob_suffix.to_re()].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
314 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
315 }
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
316 PatternSyntax::Glob | PatternSyntax::RootGlob => {
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
317 [glob_to_re(pattern).as_slice(), glob_suffix.to_re()].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
318 }
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
319 PatternSyntax::Include
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
320 | 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
321 | 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
322 | 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
323 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
324 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
325
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
326 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
327 [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
328
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
329 /// 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
330 #[cfg(unix)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
331 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
332 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
333 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
334 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
335 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
336
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
337 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
338 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
339 // 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
340 // 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
341 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
342 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
343 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
344 .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
345 .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
346 .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
347 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
348 || (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
349 || (!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
350 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
351 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
352 } 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
353 acc.pop();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
354 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
355 acc
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
356 });
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
357 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
358
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
359 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
360 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
361 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
362 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
363 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
364 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
365 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
366 } else {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
367 new_bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
368 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
369 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
370
52385
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
371 /// Controls whether we want the emitted regex to cover all cases
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
372 /// or just the cases that are not covered by optimized code paths.
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
373 #[derive(Debug, Clone, Copy)]
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
374 pub enum RegexCompleteness {
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
375 /// `Complete` emits a regex that handles all files, including the ones
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
376 /// that are typically handled by a different code path.
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
377 /// This is used in `hg debugignorerhg -a` to avoid missing some rules.
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
378 Complete,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
379 /// `ExcludeExactFiles` excludes the patterns that correspond to exact
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
380 /// file matches. This is the normal behavior, and gives a potentially
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
381 /// much smaller regex.
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
382 ExcludeExactFiles,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
383 }
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
384
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
385 impl RegexCompleteness {
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
386 fn may_exclude_exact_files(self) -> bool {
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
387 match self {
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
388 Self::Complete => false,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
389 Self::ExcludeExactFiles => true,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
390 }
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
391 }
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
392 }
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
393
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
394 /// 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
395 /// 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
396 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
397 entry: &IgnorePattern,
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
398 glob_suffix: GlobSuffix,
52385
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
399 regex_config: RegexCompleteness,
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
400 ) -> 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
401 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
402 pattern, syntax, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
403 } = entry;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
404 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
405 PatternSyntax::RootGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
406 | PatternSyntax::Path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
407 | PatternSyntax::RelGlob
50882
df6dfad5009a rust-filepatterns: also normalize RelPath
Spencer Baugh <sbaugh@janestreet.com>
parents: 50881
diff changeset
408 | PatternSyntax::RelPath
51479
2a89d2f6336f match: rename RootFiles to RootFilesIn for more consistency
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51446
diff changeset
409 | PatternSyntax::RootFilesIn => 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
410 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
411 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
412 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
413 _ => 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
414 };
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
415 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
416 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b));
52385
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
417 if regex_config.may_exclude_exact_files()
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
418 && (is_simple_rootglob || syntax == &PatternSyntax::FilePath)
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
419 {
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
420 Ok(None)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
421 } else {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
422 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
423 entry.pattern = pattern;
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
424 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
425 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
426 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
427
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
428 lazy_static! {
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
429 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
430 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
431
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
432 m.insert(b"re:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
433 m.insert(b"regexp:".as_ref(), PatternSyntax::Regexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
434 m.insert(b"path:".as_ref(), PatternSyntax::Path);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
435 m.insert(b"filepath:".as_ref(), PatternSyntax::FilePath);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
436 m.insert(b"relpath:".as_ref(), PatternSyntax::RelPath);
51479
2a89d2f6336f match: rename RootFiles to RootFilesIn for more consistency
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51446
diff changeset
437 m.insert(b"rootfilesin:".as_ref(), PatternSyntax::RootFilesIn);
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
438 m.insert(b"relglob:".as_ref(), PatternSyntax::RelGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
439 m.insert(b"relre:".as_ref(), PatternSyntax::RelRegexp);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
440 m.insert(b"glob:".as_ref(), PatternSyntax::Glob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
441 m.insert(b"rootglob:".as_ref(), PatternSyntax::RootGlob);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
442 m.insert(b"include:".as_ref(), PatternSyntax::Include);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
443 m.insert(b"subinclude:".as_ref(), PatternSyntax::SubInclude);
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
444
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
445 m
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
446 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
447 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
448
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
449 #[derive(Debug)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
450 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
451 /// (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
452 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
453 /// File path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
454 NoSuchFile(PathBuf),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
455 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
456
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
457 pub fn parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
458 pattern: &[u8],
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
459 source: &Path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
460 default: PatternSyntax,
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
461 normalize: bool,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
462 ) -> IgnorePattern {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
463 let mut pattern_bytes: &[u8] = pattern;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
464 let mut syntax = default;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
465
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
466 for (s, val) in SYNTAXES.iter() {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
467 if let Some(rest) = pattern_bytes.drop_prefix(s) {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
468 syntax = val.clone();
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
469 pattern_bytes = rest;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
470 break;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
471 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
472 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
473
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
474 let pattern = match syntax {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
475 PatternSyntax::RootGlob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
476 | PatternSyntax::Path
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
477 | PatternSyntax::Glob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
478 | PatternSyntax::RelGlob
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
479 | PatternSyntax::RelPath
51479
2a89d2f6336f match: rename RootFiles to RootFilesIn for more consistency
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 51446
diff changeset
480 | PatternSyntax::RootFilesIn
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
481 if normalize =>
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
482 {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
483 normalize_path_bytes(pattern_bytes)
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
484 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
485 _ => pattern_bytes.to_vec(),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
486 };
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
487
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
488 IgnorePattern {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
489 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
490 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
491 source: source.to_owned(),
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
492 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
493 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
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 parse_pattern_file_contents(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
496 lines: &[u8],
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
497 file_path: &Path,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
498 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
499 warn: bool,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
500 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
501 ) -> 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
502 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
503
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44893
diff changeset
504 #[allow(clippy::trivial_regex)]
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
505 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
506 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
507 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
508
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
509 let mut current_syntax =
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
510 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
511
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
512 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
513 let line_buf;
42635
30f8e786868c rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents: 42634
diff changeset
514 if line.contains(&b'#') {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
515 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
516 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
517 }
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
518 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
519 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
520 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
521
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
522 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
523
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
524 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
525 continue;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
526 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
527
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
528 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
529 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
530
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
531 if let Some(parsed) =
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
532 SYNTAXES.get([syntax, &b":"[..]].concat().as_slice())
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
533 {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
534 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
535 } 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
536 warnings.push(PatternFileWarning::InvalidSyntax(
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
537 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
538 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
539 ));
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
540 }
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
541 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
542 let pattern = parse_one_pattern(
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
543 line,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
544 file_path,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
545 current_syntax.clone(),
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
546 false,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
547 );
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
548 inputs.push(if relativize {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
549 pattern.to_relative()
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
550 } else {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
551 pattern
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
552 })
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
553 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
554 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
555 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
556 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
557
50890
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
558 pub fn parse_pattern_args(
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
559 patterns: Vec<Vec<u8>>,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
560 cwd: &Path,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
561 root: &Path,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
562 ) -> Result<Vec<IgnorePattern>, HgPathError> {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
563 let mut ignore_patterns: Vec<IgnorePattern> = Vec::new();
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
564 for pattern in patterns {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
565 let pattern = parse_one_pattern(
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
566 &pattern,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
567 Path::new("<args>"),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
568 PatternSyntax::RelPath,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
569 true,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
570 );
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
571 match pattern.syntax {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
572 PatternSyntax::RelGlob | PatternSyntax::RelPath => {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
573 let name = get_path_from_bytes(&pattern.pattern);
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
574 let canon = canonical_path(root, cwd, name)?;
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
575 ignore_patterns.push(IgnorePattern {
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
576 syntax: pattern.syntax,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
577 pattern: get_bytes_from_path(canon),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
578 source: pattern.source,
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
579 })
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
580 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
581 _ => ignore_patterns.push(pattern.to_owned()),
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
582 };
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
583 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
584 Ok(ignore_patterns)
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
585 }
c112cc9effdc rhg: support "status FILE"
Spencer Baugh <sbaugh@janestreet.com>
parents: 50885
diff changeset
586
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
587 pub fn read_pattern_file(
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
588 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
589 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
590 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
591 ) -> 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
592 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
593 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
594 inspect_pattern_bytes(file_path, &contents);
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
595 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
596 }
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
597 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
598 vec![],
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
599 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
600 )),
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
601 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
602 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
603 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
604
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
605 /// 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
606 #[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
607 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
608 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
609 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
610 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
611 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
612
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
613 impl IgnorePattern {
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
614 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
615 Self {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
616 syntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
617 pattern: pattern.to_owned(),
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
618 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
619 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
620 }
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
621
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
622 pub fn to_relative(self) -> Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
623 let Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
624 syntax,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
625 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
626 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
627 } = self;
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
628 Self {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
629 syntax: match syntax {
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
630 PatternSyntax::Regexp => PatternSyntax::RelRegexp,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
631 PatternSyntax::Glob => PatternSyntax::RelGlob,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
632 x => x,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
633 },
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
634 pattern,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
635 source,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
636 }
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
637 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
638 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
639
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
640 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
641
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
642 /// 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
643 /// 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
644 ///
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
645 /// 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
646 /// 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
647 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
648 pattern_file: &Path,
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
649 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
650 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
651 ) -> 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
652 let (patterns, mut warnings) =
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
653 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
654 let patterns = patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
655 .into_iter()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
656 .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
657 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
658 PatternSyntax::Include => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
659 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
660 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
661 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
662 &inner_include,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
663 root_dir,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
664 inspect_pattern_bytes,
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
665 )?;
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
666 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
667 inner_pats
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
668 }
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
669 PatternSyntax::SubInclude => {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
670 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
671 root_dir,
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
672 &entry.pattern,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
673 &entry.source,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
674 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
675 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
676 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
677 &sub_include.path,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
678 &sub_include.root,
47415
0ef8231e413f dirstate-v2: Store a hash of ignore patterns (.hgignore)
Simon Sapin <simon.sapin@octobus.net>
parents: 47385
diff changeset
679 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
680 )?;
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
681 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
682 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
683 vec![IgnorePattern {
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
684 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
685 sub_include,
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
686 )),
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
687 ..entry
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
688 }]
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
689 }
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
690 _ => vec![entry],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
691 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
692 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
693 .flatten()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
694 .collect();
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
695
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
696 Ok((patterns, warnings))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
697 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
698
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
699 /// 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
700 #[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
701 pub struct SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
702 /// 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
703 /// 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
704 /// 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
705 pub prefix: HgPathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
706 /// 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
707 pub path: PathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
708 /// 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
709 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
710
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
711 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
712 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
713
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
714 impl SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
715 pub fn new(
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
716 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
717 pattern: &[u8],
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
718 source: &Path,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
719 ) -> Result<SubInclude, HgPathError> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
720 let normalized_source =
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
721 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
722
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
723 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
724 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
725
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
726 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
727 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
728
47384
777c3d231913 rust: Make some file path parameters less generic
Simon Sapin <simon.sapin@octobus.net>
parents: 44998
diff changeset
729 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
730
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
731 Ok(Self {
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
732 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
733 if !p.is_empty() {
48311
6d69e83e6b6e rhg: more efficient `HgPath::join`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 47415
diff changeset
734 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
735 }
50003
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 50001
diff changeset
736 p
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
737 })?,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
738 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
739 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
740 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
741 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
742 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
743 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
744
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
745 /// 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
746 /// phase.
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
747 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
748 ignore_patterns: Vec<IgnorePattern>,
50001
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49577
diff changeset
749 ) -> 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
750 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
751 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
752
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
753 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
754 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
755 {
50001
ccb6cfb0f2c0 rust-filepatterns: don't `Box` subincludes unnecessarily
Rapha?l Gom?s <rgomes@octobus.net>
parents: 49577
diff changeset
756 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
757 } else {
47385
f6bb181c75f8 rust: Parse "subinclude"d files along the way, not later
Simon Sapin <simon.sapin@octobus.net>
parents: 47384
diff changeset
758 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
759 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
760 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
761 Ok((subincludes, others))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
762 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
763
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
764 #[cfg(test)]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
765 mod tests {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
766 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
767 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
768
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
769 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
770 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
771 let untouched =
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
772 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
773 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
774 // All escape codes
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
775 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
776 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
777 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
778 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
779 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
780
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
781 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
782 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
783 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
784 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
785 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
786 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
787 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
788 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
789 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
790 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
791 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
792
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
793 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
794 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
795 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
796
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
797 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
798 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
799 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
800 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
801 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
802 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
803 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
804 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
805 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
806 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
807 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
808 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
809 b"*.elc",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
810 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
811 )],
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
812 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
813
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
814 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
815
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
816 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
817 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
818 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
819 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
820 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
821 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
822 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
823 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
824 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
825 .0,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
826 vec![]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
827 );
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
828 let lines = b"glob:**.o";
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
829 assert_eq!(
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
830 parse_pattern_file_contents(
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
831 lines,
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
832 Path::new("file_path"),
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
833 None,
50881
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
834 false,
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 50695
diff changeset
835 true,
49496
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
836 )
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
837 .unwrap()
5fbdd88824dc rust-filepatterns: allow overriding default syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 48311
diff changeset
838 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
839 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
840 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
841 b"**.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
842 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
843 )]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
844 );
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
845 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
846
52385
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
847 pub fn build_single_regex(
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
848 entry: &IgnorePattern,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
849 glob_suffix: GlobSuffix,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
850 ) -> Result<Option<Vec<u8>>, PatternError> {
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
851 super::build_single_regex(
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
852 entry,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
853 glob_suffix,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
854 RegexCompleteness::ExcludeExactFiles,
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
855 )
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
856 }
e2e49069eeb6 rust-ignore: make `debugignorerhg` command show a full regex, with exact files
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52384
diff changeset
857
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
858 #[test]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
859 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
860 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
861 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
862 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
863 PatternSyntax::RelGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
864 b"rust/target/",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
865 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
866 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
867 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
868 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
869 .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
870 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
871 );
44893
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
872 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
873 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
874 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
875 PatternSyntax::Regexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
876 br"rust/target/\d+",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
877 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
878 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
879 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
880 )
44893
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
881 .unwrap(),
be6401a25726 rust-regex: add test for verbatim regex syntax
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44892
diff changeset
882 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
883 );
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
884 }
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
885
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
886 #[test]
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
887 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
888 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
889 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
890 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
891 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
892 b"",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
893 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
894 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
895 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
896 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
897 .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
898 None,
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
899 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
900 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
901 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
902 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
903 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
904 b"whatever",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
905 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
906 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
907 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
908 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
909 .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
910 None,
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
911 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
912 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
913 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
914 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
915 PatternSyntax::RootGlob,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
916 b"*.o",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
917 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
918 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
919 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
920 )
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
921 .unwrap(),
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44879
diff changeset
922 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
923 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
924 }
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
925
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
926 #[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
927 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
928 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
929 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
930 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
931 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
932 b"^ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
933 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
934 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
935 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
936 )
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
937 .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
938 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
939 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
940 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
941 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
942 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
943 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
944 b"ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
945 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
946 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
947 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
948 )
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
949 .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
950 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
951 );
086b0c4f8663 matcher: fix the issue with regex inline-flag in rust oo
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49550
diff changeset
952 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
953 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
954 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
955 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
956 b"(?ia)ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
957 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
958 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
959 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
960 )
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
961 .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
962 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
963 );
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
964 assert_eq!(
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
965 build_single_regex(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
966 &IgnorePattern::new(
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
967 PatternSyntax::RelRegexp,
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
968 b"(?ia)^ba{2}r",
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
969 Path::new("")
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
970 ),
52384
2ff004fb491c hgignore: add a GlobSuffix type, instead of passing byte arrays
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 52338
diff changeset
971 GlobSuffix::MoreComponents
50885
090658724abf rust: de-hardcode glob_suffix
Spencer Baugh <sbaugh@janestreet.com>
parents: 50883
diff changeset
972 )
49577
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
973 .unwrap(),
b3480822a251 matcher: do not prepend '.*' to pattern using ^ after flags
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 49576
diff changeset
974 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
975 );
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
976 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
977 }