annotate rust/hg-core/src/filepatterns.rs @ 44601:496868f1030c

rust-matchers: use the `regex` crate Instead of falling back to Python when a code path with "ignore" functionality is reached and `Re2` is not installed, the default compilation (i.e. without the `with-re2` feature) will use the `regex` crate for all regular expressions business. As with the introduction of `Re2` in a previous series, this yields a big performance boost compared to the Python + C code in `status`, `diff`, `commit`, `update`, and maybe others. For now `Re2` looks to be faster at compiling the DFA (1.5ms vs 5ms for Netbeans' `.hgignore`) and a bit faster in actual use: (123ms vs 137ms for the parallel traversal of Netbeans' clean repo). I am in talks with the author of `regex` to see whether that performance difference is a bug, a "won't fix", or a tuning issue. The `regex` crate is already one of our dependencies and using this code does not require any additional work from the end-user than to use the Rust extensions. Differential Revision: https://phab.mercurial-scm.org/D8323
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 24 Mar 2020 17:55:59 +0100
parents 2fe89bec8011
children e0414fcd35e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42767
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
1 // filepatterns.rs
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
2 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
4 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
5 // This software may be used and distributed according to the terms of the
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
6 // GNU General Public License version 2 or any later version.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
7
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
8 //! Handling of Mercurial-specific patterns.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42636
diff changeset
9
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
10 use crate::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
11 utils::{
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
12 files::{canonical_path, get_bytes_from_path, get_path_from_bytes},
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
13 hg_path::{path_to_hg_path_buf, HgPathBuf, HgPathError},
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
14 SliceExt,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
15 },
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
16 FastHashMap, PatternError,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
17 };
42609
326fdce22fb2 rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42498
diff changeset
18 use lazy_static::lazy_static;
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
19 use regex::bytes::{NoExpand, Regex};
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
20 use std::fs::File;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
21 use std::io::Read;
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
22 use std::ops::Deref;
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
23 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
24 use std::vec::Vec;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
25
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
26 lazy_static! {
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
27 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
28 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect();
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
29 let to_escape = b"()[]{}?*+-|^$\\.&~# \t\n\r\x0b\x0c";
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
30 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
31 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
32 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
33 v
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
34 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
35 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
36
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
37 /// 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
38 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
39 &[(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
40
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
41 /// Appended to the regexp of globs
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
42 const GLOB_SUFFIX: &[u8; 7] = b"(?:/|$)";
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
43
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
44 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
45 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
46 /// 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
47 Regexp,
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 /// 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
49 RootGlob,
42851
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42767
diff changeset
50 /// 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
51 /// slashes)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
52 Glob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
53 /// 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
54 Path,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
55 /// 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
56 RelPath,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
57 /// 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
58 RelGlob,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
59 /// 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
60 RelRegexp,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
61 /// A path relative to repository root, which is matched non-recursively
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
62 /// (will not match subdirectories)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 RootFiles,
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
64 /// A file of patterns to read and include
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
65 Include,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
66 /// A file of patterns to match against files under the same directory
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
67 SubInclude,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
68 }
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 /// Transforms a glob pattern into a regex
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 fn glob_to_re(pat: &[u8]) -> Vec<u8> {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 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
73 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
74 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
75
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
76 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
77 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
78
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
79 match c {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
80 b'*' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 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
82 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
83 input = rest;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
84 res.extend(*repl);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
85 break;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
87 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
88 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
89 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
90 b'[' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
91 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
92 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
93 Some(end) => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
94 // 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
95 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
96
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
97 res.extend(b"[");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
98
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
99 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
100 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
101 res.extend(b"^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
102 } 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
103 res.extend(b"\\^")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
104 } 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
105 res.extend(b"\\\\")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
107 res.push(*b)
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
108 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
109 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
110 res.extend(b"]");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
111 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
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 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
115 b'{' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 group_depth += 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
117 res.extend(b"(?:")
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119 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
120 group_depth -= 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
121 res.extend(b")");
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
122 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 b',' 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
124 b'\\' => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125 let c = {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
126 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
127 input = rest;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128 c
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
129 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 c
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 };
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
133 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
134 }
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
135 _ => 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
136 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
137 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
138 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
139 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
140
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 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
142 pattern
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
143 .iter()
42498
a4a468b00d44 rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents: 42454
diff changeset
144 .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
145 .collect()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
146 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
147
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
148 pub fn parse_pattern_syntax(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
149 kind: &[u8],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
150 ) -> 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
151 match kind {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
152 b"re:" => Ok(PatternSyntax::Regexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
153 b"path:" => Ok(PatternSyntax::Path),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
154 b"relpath:" => Ok(PatternSyntax::RelPath),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
155 b"rootfilesin:" => Ok(PatternSyntax::RootFiles),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
156 b"relglob:" => Ok(PatternSyntax::RelGlob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
157 b"relre:" => Ok(PatternSyntax::RelRegexp),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
158 b"glob:" => Ok(PatternSyntax::Glob),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
159 b"rootglob:" => Ok(PatternSyntax::RootGlob),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
160 b"include:" => Ok(PatternSyntax::Include),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
161 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
162 _ => Err(PatternError::UnsupportedSyntax(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
163 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
164 )),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
165 }
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
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
168 /// 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
169 /// 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
170 /// otherwise, returns the corresponding regex.
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
171 fn _build_single_regex(entry: &IgnorePattern) -> Vec<u8> {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
172 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
173 syntax, pattern, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
174 } = entry;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
175 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
176 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
177 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
178 match syntax {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
179 // The `regex` crate adds `.*` to the start and end of expressions
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
180 // if there are no anchors, so add them.
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
181 PatternSyntax::Regexp => [b"^", &pattern[..], b"$"].concat(),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
182 PatternSyntax::RelRegexp => {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
183 // 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
184 // 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
185 // engines.
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
186 if pattern[0] == b'^' || pattern[0] == b'*' {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
187 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
188 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
189 [&b".*"[..], pattern].concat()
42349
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 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
192 if pattern == b"." {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
193 return vec![];
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
194 }
42871
69195b6f8f97 rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42870
diff changeset
195 [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
196 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
197 PatternSyntax::RootFiles => {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
198 let mut res = if pattern == b"." {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
199 vec![b'^']
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
200 } else {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
201 // Pattern is a directory name.
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
202 [b"^", 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
203 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
204
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
205 // 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
206 res.extend(b"[^/]+$");
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
207 res.push(b'$');
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
208 res
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
209 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
210 PatternSyntax::RelGlob => {
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
211 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
212 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
213 [b".*", rest, GLOB_SUFFIX].concat()
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
214 } else {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
215 [b"(?:.*/)?", glob_re.as_slice(), GLOB_SUFFIX].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
216 }
42870
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
217 }
72890d8f9860 match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42869
diff changeset
218 PatternSyntax::Glob | PatternSyntax::RootGlob => {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
219 [b"^", glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat()
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
220 }
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
221 PatternSyntax::Include | PatternSyntax::SubInclude => unreachable!(),
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
222 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
223 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
224
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
225 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
226 [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
227
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
228 /// 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
229 #[cfg(unix)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
230 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
231 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
232 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
233 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
234 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
235
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
236 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
237 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
238 // 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
239 // 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
240 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
241 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
242 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
243 .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
244 .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
245 .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
246 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
247 || (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
248 || (!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
249 {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
250 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
251 } 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
252 acc.pop();
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
253 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
254 acc
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
255 });
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
256 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
257
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
258 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
259 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
260 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
261 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
262 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
263 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
264 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
265 } else {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
266 new_bytes
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
267 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
268 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
269
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
270 /// 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
271 /// 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
272 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
273 entry: &IgnorePattern,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
274 ) -> Result<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
275 let IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
276 pattern, syntax, ..
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
277 } = entry;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
278 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
279 PatternSyntax::RootGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
280 | PatternSyntax::Path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
281 | PatternSyntax::RelGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
282 | PatternSyntax::RootFiles => normalize_path_bytes(&pattern),
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
283 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
284 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
285 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
286 _ => 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
287 };
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
288 if *syntax == PatternSyntax::RootGlob
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
289 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b))
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
290 {
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
291 // The `regex` crate adds `.*` to the start and end of expressions
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
292 // if there are no anchors, so add the start anchor.
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
293 let mut escaped = vec![b'^'];
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
294 escaped.extend(escape_pattern(&pattern));
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
295 escaped.extend(GLOB_SUFFIX);
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
296 Ok(escaped)
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
297 } else {
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
298 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
299 entry.pattern = pattern;
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
300 Ok(_build_single_regex(&entry))
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
301 }
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 lazy_static! {
43844
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42960
diff changeset
305 static ref SYNTAXES: FastHashMap<&'static [u8], &'static [u8]> = {
5ac243a92e37 rust-performance: introduce FastHashMap type alias for HashMap
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42960
diff changeset
306 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
307
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
308 m.insert(b"re".as_ref(), b"relre:".as_ref());
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
309 m.insert(b"regexp".as_ref(), b"relre:".as_ref());
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
310 m.insert(b"glob".as_ref(), b"relglob:".as_ref());
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
311 m.insert(b"rootglob".as_ref(), b"rootglob:".as_ref());
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
312 m.insert(b"include".as_ref(), b"include:".as_ref());
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
313 m.insert(b"subinclude".as_ref(), b"subinclude:".as_ref());
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
314 m
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
315 };
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
316 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
317
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
318 #[derive(Debug)]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
319 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
320 /// (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
321 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
322 /// File path
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
323 NoSuchFile(PathBuf),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
324 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
325
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
326 pub fn parse_pattern_file_contents<P: AsRef<Path>>(
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
327 lines: &[u8],
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
328 file_path: P,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
329 warn: bool,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
330 ) -> 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
331 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap();
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
332 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
333 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
334 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
335
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
336 let mut current_syntax = b"relre:".as_ref();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
337
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
338 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
339 let line_number = line_number + 1;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
340
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
341 let line_buf;
42635
30f8e786868c rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents: 42634
diff changeset
342 if line.contains(&b'#') {
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
343 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
344 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
345 }
42636
12addcc7956c rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents: 42635
diff changeset
346 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
347 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
348 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
350 let mut line = line.trim_end();
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
351
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
352 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
353 continue;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
354 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
355
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
356 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
357 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
358
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
359 if let Some(rel_syntax) = SYNTAXES.get(syntax) {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
360 current_syntax = rel_syntax;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
361 } 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
362 warnings.push(PatternFileWarning::InvalidSyntax(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
363 file_path.as_ref().to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
364 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
365 ));
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
366 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
367 continue;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
368 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
369
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
370 let mut line_syntax: &[u8] = &current_syntax;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
371
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
372 for (s, rels) in SYNTAXES.iter() {
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
373 if let Some(rest) = line.drop_prefix(rels) {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
374 line_syntax = rels;
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
375 line = rest;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
376 break;
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
377 }
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
378 if let Some(rest) = line.drop_prefix(&[s, &b":"[..]].concat()) {
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
379 line_syntax = rels;
42869
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42851
diff changeset
380 line = rest;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
381 break;
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
382 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
383 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
384
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
385 inputs.push(IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
386 parse_pattern_syntax(&line_syntax).map_err(|e| match e {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
387 PatternError::UnsupportedSyntax(syntax) => {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
388 PatternError::UnsupportedSyntaxInFile(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
389 syntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
390 file_path.as_ref().to_string_lossy().into(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
391 line_number,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
392 )
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
393 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
394 _ => e,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
395 })?,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
396 &line,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
397 &file_path,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
398 ));
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
399 }
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
400 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
401 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
402
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
403 pub fn read_pattern_file<P: AsRef<Path>>(
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
404 file_path: P,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
405 warn: bool,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
406 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
407 let mut f = match File::open(file_path.as_ref()) {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
408 Ok(f) => Ok(f),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
409 Err(e) => match e.kind() {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
410 std::io::ErrorKind::NotFound => {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
411 return Ok((
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
412 vec![],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
413 vec![PatternFileWarning::NoSuchFile(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
414 file_path.as_ref().to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
415 )],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
416 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
417 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
418 _ => Err(e),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
419 },
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
420 }?;
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
421 let mut contents = Vec::new();
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
422
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
423 f.read_to_end(&mut contents)?;
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
424
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
425 Ok(parse_pattern_file_contents(&contents, file_path, warn)?)
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
426 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
427
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
428 /// 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
429 #[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
430 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
431 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
432 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
433 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
434 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
435
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
436 impl IgnorePattern {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
437 pub fn new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
438 syntax: PatternSyntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
439 pattern: &[u8],
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
440 source: impl AsRef<Path>,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
441 ) -> Self {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
442 Self {
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
443 syntax,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
444 pattern: 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
445 source: source.as_ref().to_owned(),
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
446 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
447 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
448 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
449
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
450 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
451
44347
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
452 /// Wrapper for `read_pattern_file` that also recursively expands `include:`
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
453 /// patterns.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
454 ///
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
455 /// `subinclude:` is not treated as a special pattern here: unraveling them
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
456 /// needs to occur in the "ignore" phase.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
457 pub fn get_patterns_from_file(
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
458 pattern_file: impl AsRef<Path>,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
459 root_dir: impl AsRef<Path>,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
460 ) -> PatternResult<(Vec<IgnorePattern>, Vec<PatternFileWarning>)> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
461 let (patterns, mut warnings) = read_pattern_file(&pattern_file, true)?;
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
462 let patterns = patterns
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
463 .into_iter()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
464 .flat_map(|entry| -> PatternResult<_> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
465 let IgnorePattern {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
466 syntax,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
467 pattern,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
468 source: _,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
469 } = &entry;
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
470 Ok(match syntax {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
471 PatternSyntax::Include => {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
472 let inner_include =
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
473 root_dir.as_ref().join(get_path_from_bytes(&pattern));
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
474 let (inner_pats, inner_warnings) = get_patterns_from_file(
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
475 &inner_include,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
476 root_dir.as_ref(),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
477 )?;
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
478 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
479 inner_pats
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
480 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
481 _ => vec![entry],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
482 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
483 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
484 .flatten()
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
485 .collect();
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
486
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
487 Ok((patterns, warnings))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
488 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
489
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
490 /// Holds all the information needed to handle a `subinclude:` pattern.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
491 pub struct SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
492 /// 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
493 /// 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
494 /// 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
495 pub prefix: HgPathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
496 /// 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
497 pub path: PathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
498 /// 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
499 pub root: PathBuf,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
500 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
501
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
502 impl SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
503 pub fn new(
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
504 root_dir: impl AsRef<Path>,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
505 pattern: &[u8],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
506 source: impl AsRef<Path>,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
507 ) -> Result<SubInclude, HgPathError> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
508 let normalized_source =
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
509 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
510
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
511 let source_root = get_path_from_bytes(&normalized_source);
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
512 let source_root = source_root.parent().unwrap_or(source_root.deref());
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
513
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
514 let path = source_root.join(get_path_from_bytes(pattern));
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
515 let new_root = path.parent().unwrap_or(path.deref());
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
516
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
517 let prefix = canonical_path(&root_dir, &root_dir, new_root)?;
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
518
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
519 Ok(Self {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
520 prefix: path_to_hg_path_buf(prefix).and_then(|mut p| {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
521 if !p.is_empty() {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
522 p.push(b'/');
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
523 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
524 Ok(p)
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
525 })?,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
526 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
527 root: new_root.to_owned(),
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
528 })
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
529 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
530 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
531
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
532 /// 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
533 /// phase.
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
534 pub fn filter_subincludes(
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
535 ignore_patterns: &[IgnorePattern],
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
536 root_dir: impl AsRef<Path>,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
537 ) -> Result<(Vec<SubInclude>, Vec<&IgnorePattern>), HgPathError> {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
538 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
539 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
540
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
541 for ignore_pattern in ignore_patterns.iter() {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
542 let IgnorePattern {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
543 syntax,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
544 pattern,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
545 source,
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
546 } = ignore_pattern;
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
547 if *syntax == PatternSyntax::SubInclude {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
548 subincludes.push(SubInclude::new(&root_dir, pattern, &source)?);
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
549 } else {
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
550 others.push(ignore_pattern)
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
551 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
552 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
553 Ok((subincludes, others))
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
554 }
2fe89bec8011 rust-filepatterns: add support for `include` and `subinclude` patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44346
diff changeset
555
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
556 #[cfg(test)]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
557 mod tests {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
558 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
559 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
560
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
561 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
562 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
563 let untouched =
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
564 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
565 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
566 // All escape codes
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
567 assert_eq!(
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
568 escape_pattern(br#"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"#),
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
569 br#"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\ \\t\\n\\r\\v\\f"#
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
570 .to_vec()
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
571 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
572 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
573
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
574 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
575 fn glob_test() {
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
576 assert_eq!(glob_to_re(br#"?"#), br#"."#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
577 assert_eq!(glob_to_re(br#"*"#), br#"[^/]*"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
578 assert_eq!(glob_to_re(br#"**"#), br#".*"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
579 assert_eq!(glob_to_re(br#"**/a"#), br#"(?:.*/)?a"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
580 assert_eq!(glob_to_re(br#"a/**/b"#), br#"a/(?:.*/)?b"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
581 assert_eq!(glob_to_re(br#"[a*?!^][^b][!c]"#), br#"[a*?!^][\^b][^c]"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
582 assert_eq!(glob_to_re(br#"{a,b}"#), br#"(?:a|b)"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
583 assert_eq!(glob_to_re(br#".\*\?"#), br#"\.\*\?"#);
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
584 }
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
585
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
586 #[test]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
587 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
588 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
589
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
590 assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
591 parse_pattern_file_contents(lines, Path::new("file_path"), false)
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
592 .unwrap()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
593 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
594 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
595 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
596 b"*.elc",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
597 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
598 )],
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
599 );
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
600
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
601 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
602
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
603 assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
604 parse_pattern_file_contents(lines, Path::new("file_path"), false)
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
605 .unwrap()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
606 .0,
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
607 vec![]
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
608 );
42453
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
609 let lines = b"glob:**.o";
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42349
diff changeset
610 assert_eq!(
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
611 parse_pattern_file_contents(lines, Path::new("file_path"), false)
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
612 .unwrap()
42960
7a01778bc7b7 rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42871
diff changeset
613 .0,
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
614 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
615 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
616 b"**.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
617 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
618 )]
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 }
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
621
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
622 #[test]
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
623 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
624 assert_eq!(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
625 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
626 PatternSyntax::RelGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
627 b"rust/target/",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
628 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
629 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
630 .unwrap(),
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
631 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
632 );
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
633 }
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
634
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
635 #[test]
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
636 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
637 assert_eq!(
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
638 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
639 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
640 b"",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
641 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
642 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
643 .unwrap(),
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
644 br"^\.(?:/|$)".to_vec(),
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
645 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
646 assert_eq!(
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
647 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
648 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
649 b"whatever",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
650 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
651 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
652 .unwrap(),
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
653 br"^whatever(?:/|$)".to_vec(),
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
654 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
655 assert_eq!(
44346
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
656 build_single_regex(&IgnorePattern::new(
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
657 PatternSyntax::RootGlob,
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
658 b"*.o",
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
659 Path::new("")
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
660 ))
d42eea9a0494 rust-filepatterns: improve API and robustness for pattern files parsing
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43844
diff changeset
661 .unwrap(),
44601
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44347
diff changeset
662 br"^[^/]*\.o(?:/|$)".to_vec(),
42454
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
663 );
48f1f864d928 rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42453
diff changeset
664 }
42349
e8f3740cc067 rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
665 }