annotate rust/hg-core/src/matchers.rs @ 44832:ad1ec40975aa

rust-regex: fix issues with regex anchoring and performance It turns out that the way I tried to work around `regex`'s behavior difference with `re2` and Python's `re` was 1) buggy and 2) much more complicated than needed. In a few words: `regex` adds `.*` on either side of patterns when no start or end anchor is present. My previous workaround put `^` or `$` for every pattern, which is wrong even without the other 2 bugs on top of it. Using `^(?:<patterns>)` right at the end of the `regex` path fixes the issue. I've opened an issue to get a build option instead: https://github.com/rust-lang/regex/issues/675 Differential Revision: https://phab.mercurial-scm.org/D8506
author Rapha?l Gom?s <rgomes@octobus.net>
date Thu, 07 May 2020 23:52:08 +0200
parents de0fb4463a3d
children fd3b94f1712d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
1 // matchers.rs
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
2 //
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
4 //
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
7
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
8 //! Structs and types for matching files and directories.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
9
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
10 #[cfg(feature = "with-re2")]
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
11 use crate::re2::Re2;
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
12 use crate::{
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
13 dirstate::dirs_multiset::DirsChildrenMultiset,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
14 filepatterns::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
15 build_single_regex, filter_subincludes, get_patterns_from_file,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
16 PatternFileWarning, PatternResult, SubInclude,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
17 },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
18 utils::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
19 files::find_dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
20 hg_path::{HgPath, HgPathBuf},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
21 Escaped,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
22 },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
23 DirsMultiset, DirstateMapError, FastHashMap, IgnorePattern, PatternError,
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
24 PatternSyntax,
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
25 };
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
26
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
27 use crate::filepatterns::normalize_path_bytes;
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
28 use std::borrow::ToOwned;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
29 use std::collections::HashSet;
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
30 use std::fmt::{Display, Error, Formatter};
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
31 use std::iter::FromIterator;
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
32 use std::ops::Deref;
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
33 use std::path::{Path, PathBuf};
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
34
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44597
diff changeset
35 use micro_timer::timed;
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44597
diff changeset
36
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
37 #[derive(Debug, PartialEq)]
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
38 pub enum VisitChildrenSet<'a> {
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
39 /// Don't visit anything
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
40 Empty,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
41 /// Only visit this directory
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
42 This,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
43 /// Visit this directory and these subdirectories
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
44 /// TODO Should we implement a `NonEmptyHashSet`?
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
45 Set(HashSet<&'a HgPath>),
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
46 /// Visit this directory and all subdirectories
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
47 Recursive,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 }
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
49
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
50 pub trait Matcher {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
51 /// Explicitly listed files
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
52 fn file_set(&self) -> Option<&HashSet<&HgPath>>;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
53 /// Returns whether `filename` is in `file_set`
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
54 fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 /// Returns whether `filename` is matched by this matcher
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
56 fn matches(&self, filename: impl AsRef<HgPath>) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
57 /// Decides whether a directory should be visited based on whether it
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
58 /// has potential matches in it or one of its subdirectories, and
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
59 /// potentially lists which subdirectories of that directory should be
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
60 /// visited. This is based on the match's primary, included, and excluded
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
61 /// patterns.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
62 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 /// # Example
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
64 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
65 /// Assume matchers `['path:foo/bar', 'rootfilesin:qux']`, we would
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
66 /// return the following values (assuming the implementation of
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67 /// visit_children_set is capable of recognizing this; some implementations
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
68 /// are not).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
69 ///
44006
72bced4f2936 rust-matchers: fixing cargo doc
Georges Racinet <georges.racinet@octobus.net>
parents: 43914
diff changeset
70 /// ```text
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 /// ```ignore
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 /// '' -> {'foo', 'qux'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
73 /// 'baz' -> set()
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
74 /// 'foo' -> {'bar'}
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
75 /// // Ideally this would be `Recursive`, but since the prefix nature of
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
76 /// // matchers is applied to the entire matcher, we have to downgrade this
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
77 /// // to `This` due to the (yet to be implemented in Rust) non-prefix
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
78 /// // `RootFilesIn'-kind matcher being mixed in.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
79 /// 'foo/bar' -> 'this'
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
80 /// 'qux' -> 'this'
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 /// ```
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
82 /// # Important
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
83 ///
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
84 /// Most matchers do not know if they're representing files or
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
85 /// directories. They see `['path:dir/f']` and don't know whether `f` is a
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 /// file or a directory, so `visit_children_set('dir')` for most matchers
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
87 /// will return `HashSet{ HgPath { "f" } }`, but if the matcher knows it's
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
88 /// a file (like the yet to be implemented in Rust `ExactMatcher` does),
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
89 /// it may return `VisitChildrenSet::This`.
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
90 /// Do not rely on the return being a `HashSet` indicating that there are
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
91 /// no files in this dir to investigate (or equivalently that if there are
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
92 /// files to investigate in 'dir' that it will always return
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
93 /// `VisitChildrenSet::This`).
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
94 fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
95 &self,
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
96 directory: impl AsRef<HgPath>,
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
97 ) -> VisitChildrenSet;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
98 /// Matcher will match everything and `files_set()` will be empty:
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
99 /// optimization might be possible.
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
100 fn matches_everything(&self) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
101 /// Matcher will match exactly the files in `files_set()`: optimization
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
102 /// might be possible.
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
103 fn is_exact(&self) -> bool;
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
104 }
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
105
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106 /// Matches everything.
43834
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
107 ///```
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
108 /// use hg::{ matchers::{Matcher, AlwaysMatcher}, utils::hg_path::HgPath };
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
109 ///
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
110 /// let matcher = AlwaysMatcher;
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
111 ///
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
112 /// assert_eq!(matcher.matches(HgPath::new(b"whatever")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
113 /// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
114 /// assert_eq!(matcher.matches(HgPath::new(b"main.c")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
115 /// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
43834
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43832
diff changeset
116 /// ```
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
117 #[derive(Debug)]
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 pub struct AlwaysMatcher;
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 impl Matcher for AlwaysMatcher {
43832
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
121 fn file_set(&self) -> Option<&HashSet<&HgPath>> {
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43611
diff changeset
122 None
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
124 fn exact_match(&self, _filename: impl AsRef<HgPath>) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
125 false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
126 }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
127 fn matches(&self, _filename: impl AsRef<HgPath>) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
128 true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
129 }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 fn visit_children_set(
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
131 &self,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
132 _directory: impl AsRef<HgPath>,
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
133 ) -> VisitChildrenSet {
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
134 VisitChildrenSet::Recursive
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135 }
43611
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
136 fn matches_everything(&self) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
137 true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
138 }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
139 fn is_exact(&self) -> bool {
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
140 false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43438
diff changeset
141 }
43438
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
142 }
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
143
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
144 /// Matches the input files exactly. They are interpreted as paths, not
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
145 /// patterns.
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
146 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
147 ///```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
148 /// use hg::{ matchers::{Matcher, FileMatcher}, utils::hg_path::HgPath };
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
149 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
150 /// let files = [HgPath::new(b"a.txt"), HgPath::new(br"re:.*\.c$")];
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
151 /// let matcher = FileMatcher::new(&files).unwrap();
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
152 ///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
153 /// assert_eq!(matcher.matches(HgPath::new(b"a.txt")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
154 /// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), false);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
155 /// assert_eq!(matcher.matches(HgPath::new(b"main.c")), false);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
156 /// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
157 /// ```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
158 #[derive(Debug)]
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
159 pub struct FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
160 files: HashSet<&'a HgPath>,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
161 dirs: DirsMultiset,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
162 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
163
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
164 impl<'a> FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
165 pub fn new(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
166 files: &'a [impl AsRef<HgPath>],
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
167 ) -> Result<Self, DirstateMapError> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
168 Ok(Self {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
169 files: HashSet::from_iter(files.iter().map(|f| f.as_ref())),
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
170 dirs: DirsMultiset::from_manifest(files)?,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
171 })
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
172 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
173 fn inner_matches(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
174 self.files.contains(filename.as_ref())
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
175 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
176 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
177
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
178 impl<'a> Matcher for FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
179 fn file_set(&self) -> Option<&HashSet<&HgPath>> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
180 Some(&self.files)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
181 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
182 fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
183 self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
184 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
185 fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
186 self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
187 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
188 fn visit_children_set(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
189 &self,
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
190 directory: impl AsRef<HgPath>,
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
191 ) -> VisitChildrenSet {
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
192 if self.files.is_empty() || !self.dirs.contains(&directory) {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
193 return VisitChildrenSet::Empty;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
194 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
195 let dirs_as_set = self.dirs.iter().map(|k| k.deref()).collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
196
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
197 let mut candidates: HashSet<&HgPath> =
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
198 self.files.union(&dirs_as_set).map(|k| *k).collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
199 candidates.remove(HgPath::new(b""));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
200
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
201 if !directory.as_ref().is_empty() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
202 let directory = [directory.as_ref().as_bytes(), b"/"].concat();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
203 candidates = candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
204 .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
205 .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
206 if c.as_bytes().starts_with(&directory) {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
207 Some(HgPath::new(&c.as_bytes()[directory.len()..]))
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
208 } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
209 None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
210 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
211 })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
212 .collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
213 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
214
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
215 // `self.dirs` includes all of the directories, recursively, so if
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
216 // we're attempting to match 'foo/bar/baz.txt', it'll have '', 'foo',
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
217 // 'foo/bar' in it. Thus we can safely ignore a candidate that has a
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
218 // '/' in it, indicating it's for a subdir-of-a-subdir; the immediate
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
219 // subdir will be in there without a slash.
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
220 VisitChildrenSet::Set(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
221 candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
222 .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
223 .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
224 if c.bytes().all(|b| *b != b'/') {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
225 Some(*c)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
226 } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
227 None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
228 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
229 })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
230 .collect(),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
231 )
43914
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
232 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
233 fn matches_everything(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
234 false
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
235 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
236 fn is_exact(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
237 true
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
238 }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43863
diff changeset
239 }
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
240
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
241 /// Matches files that are included in the ignore rules.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
242 #[cfg_attr(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
243 feature = "with-re2",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
244 doc = r##"
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
245 ```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
246 use hg::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
247 matchers::{IncludeMatcher, Matcher},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
248 IgnorePattern,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
249 PatternSyntax,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
250 utils::hg_path::HgPath
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
251 };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
252 use std::path::Path;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
253 ///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
254 let ignore_patterns =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
255 vec![IgnorePattern::new(PatternSyntax::RootGlob, b"this*", Path::new(""))];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
256 let (matcher, _) = IncludeMatcher::new(ignore_patterns, "").unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
257 ///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
258 assert_eq!(matcher.matches(HgPath::new(b"testing")), false);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
259 assert_eq!(matcher.matches(HgPath::new(b"this should work")), true);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
260 assert_eq!(matcher.matches(HgPath::new(b"this also")), true);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
261 assert_eq!(matcher.matches(HgPath::new(b"but not this")), false);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
262 ```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
263 "##
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
264 )]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
265 pub struct IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
266 patterns: Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
267 match_fn: Box<dyn for<'r> Fn(&'r HgPath) -> bool + 'a + Sync>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
268 /// Whether all the patterns match a prefix (i.e. recursively)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
269 prefix: bool,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
270 roots: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
271 dirs: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
272 parents: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
273 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
274
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
275 impl<'a> Matcher for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
276 fn file_set(&self) -> Option<&HashSet<&HgPath>> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
277 None
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
278 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
279
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
280 fn exact_match(&self, _filename: impl AsRef<HgPath>) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
281 false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
282 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
283
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
284 fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
285 (self.match_fn)(filename.as_ref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
286 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
287
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
288 fn visit_children_set(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
289 &self,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
290 directory: impl AsRef<HgPath>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
291 ) -> VisitChildrenSet {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
292 let dir = directory.as_ref();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
293 if self.prefix && self.roots.contains(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
294 return VisitChildrenSet::Recursive;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
295 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
296 if self.roots.contains(HgPath::new(b""))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
297 || self.roots.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
298 || self.dirs.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
299 || find_dirs(dir).any(|parent_dir| self.roots.contains(parent_dir))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
300 {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
301 return VisitChildrenSet::This;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
302 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
303
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
304 if self.parents.contains(directory.as_ref()) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
305 let multiset = self.get_all_parents_children();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
306 if let Some(children) = multiset.get(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
307 return VisitChildrenSet::Set(children.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
308 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
309 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
310 VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
311 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
312
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
313 fn matches_everything(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
314 false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
315 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
316
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
317 fn is_exact(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
318 false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
319 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
320 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
321
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
322 #[cfg(feature = "with-re2")]
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
323 /// Returns a function that matches an `HgPath` against the given regex
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
324 /// pattern.
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
325 ///
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
326 /// This can fail when the pattern is invalid or not supported by the
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
327 /// underlying engine `Re2`, for instance anything with back-references.
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44597
diff changeset
328 #[timed]
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
329 fn re_matcher(
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
330 pattern: &[u8],
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
331 ) -> PatternResult<impl Fn(&HgPath) -> bool + Sync> {
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
332 let regex = Re2::new(pattern);
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
333 let regex = regex.map_err(|e| PatternError::UnsupportedSyntax(e))?;
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
334 Ok(move |path: &HgPath| regex.is_match(path.as_bytes()))
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
335 }
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
336
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
337 #[cfg(not(feature = "with-re2"))]
44593
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
338 /// Returns a function that matches an `HgPath` against the given regex
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
339 /// pattern.
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
340 ///
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
341 /// This can fail when the pattern is invalid or not supported by the
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
342 /// underlying engine (the `regex` crate), for instance anything with
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
343 /// back-references.
44784
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44597
diff changeset
344 #[timed]
44593
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
345 fn re_matcher(
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
346 pattern: &[u8],
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
347 ) -> PatternResult<impl Fn(&HgPath) -> bool + Sync> {
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
348 use std::io::Write;
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
349
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44803
diff changeset
350 // The `regex` crate adds `.*` to the start and end of expressions if there
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44803
diff changeset
351 // are no anchors, so add the start anchor.
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44803
diff changeset
352 let mut escaped_bytes = vec![b'^', b'(', b'?', b':'];
44593
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
353 for byte in pattern {
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
354 if *byte > 127 {
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
355 write!(escaped_bytes, "\\x{:x}", *byte).unwrap();
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
356 } else {
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
357 escaped_bytes.push(*byte);
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
358 }
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
359 }
44832
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44803
diff changeset
360 escaped_bytes.push(b')');
44593
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
361
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
362 // Avoid the cost of UTF8 checking
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
363 //
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
364 // # Safety
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
365 // This is safe because we escaped all non-ASCII bytes.
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
366 let pattern_string = unsafe { String::from_utf8_unchecked(escaped_bytes) };
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
367 let re = regex::bytes::RegexBuilder::new(&pattern_string)
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
368 .unicode(false)
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
369 .build()
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
370 .map_err(|e| PatternError::UnsupportedSyntax(e.to_string()))?;
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
371
496868f1030c rust-matchers: use the `regex` crate
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44541
diff changeset
372 Ok(move |path: &HgPath| re.is_match(path.as_bytes()))
44519
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
373 }
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44353
diff changeset
374
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
375 /// Returns the regex pattern and a function that matches an `HgPath` against
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
376 /// said regex formed by the given ignore patterns.
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
377 fn build_regex_match<'a>(
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
378 ignore_patterns: &'a [&'a IgnorePattern],
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
379 ) -> PatternResult<(Vec<u8>, Box<dyn Fn(&HgPath) -> bool + Sync>)> {
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
380 let mut regexps = vec![];
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
381 let mut exact_set = HashSet::new();
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
382
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
383 for pattern in ignore_patterns {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
384 if let Some(re) = build_single_regex(pattern)? {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
385 regexps.push(re);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
386 } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
387 let exact = normalize_path_bytes(&pattern.pattern);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
388 exact_set.insert(HgPathBuf::from_bytes(&exact));
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
389 }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
390 }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
391
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
392 let full_regex = regexps.join(&b'|');
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
393
44802
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
394 // An empty pattern would cause the regex engine to incorrectly match the
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
395 // (empty) root directory
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
396 let func = if !(regexps.is_empty()) {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
397 let matcher = re_matcher(&full_regex)?;
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
398 let func = move |filename: &HgPath| {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
399 exact_set.contains(filename) || matcher(filename)
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
400 };
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
401 Box::new(func) as Box<dyn Fn(&HgPath) -> bool + Sync>
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
402 } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
403 let func = move |filename: &HgPath| exact_set.contains(filename);
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
404 Box::new(func) as Box<dyn Fn(&HgPath) -> bool + Sync>
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44784
diff changeset
405 };
44521
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
406
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
407 Ok((full_regex, func))
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
408 }
a21881b40942 rust-matchers: add `build_regex_match` function
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44520
diff changeset
409
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
410 /// Returns roots and directories corresponding to each pattern.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
411 ///
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
412 /// This calculates the roots and directories exactly matching the patterns and
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
413 /// returns a tuple of (roots, dirs). It does not return other directories
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
414 /// which may also need to be considered, like the parent directories.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
415 fn roots_and_dirs(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
416 ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
417 ) -> (Vec<HgPathBuf>, Vec<HgPathBuf>) {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
418 let mut roots = Vec::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
419 let mut dirs = Vec::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
420
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
421 for ignore_pattern in ignore_patterns {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
422 let IgnorePattern {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
423 syntax, pattern, ..
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
424 } = ignore_pattern;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
425 match syntax {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
426 PatternSyntax::RootGlob | PatternSyntax::Glob => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
427 let mut root = vec![];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
428
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
429 for p in pattern.split(|c| *c == b'/') {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
430 if p.iter().any(|c| match *c {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
431 b'[' | b'{' | b'*' | b'?' => true,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
432 _ => false,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
433 }) {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
434 break;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
435 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
436 root.push(HgPathBuf::from_bytes(p));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
437 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
438 let buf =
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
439 root.iter().fold(HgPathBuf::new(), |acc, r| acc.join(r));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
440 roots.push(buf);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
441 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
442 PatternSyntax::Path | PatternSyntax::RelPath => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
443 let pat = HgPath::new(if pattern == b"." {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
444 &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
445 } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
446 pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
447 });
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
448 roots.push(pat.to_owned());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
449 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
450 PatternSyntax::RootFiles => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
451 let pat = if pattern == b"." {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
452 &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
453 } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
454 pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
455 };
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
456 dirs.push(HgPathBuf::from_bytes(pat));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
457 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
458 _ => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
459 roots.push(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
460 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
461 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
462 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
463 (roots, dirs)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
464 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
465
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
466 /// Paths extracted from patterns
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
467 #[derive(Debug, PartialEq)]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
468 struct RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
469 /// Directories to match recursively
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
470 pub roots: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
471 /// Directories to match non-recursively
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
472 pub dirs: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
473 /// Implicitly required directories to go to items in either roots or dirs
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
474 pub parents: HashSet<HgPathBuf>,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
475 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
476
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
477 /// Extract roots, dirs and parents from patterns.
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
478 fn roots_dirs_and_parents(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
479 ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
480 ) -> PatternResult<RootsDirsAndParents> {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
481 let (roots, dirs) = roots_and_dirs(ignore_patterns);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
482
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
483 let mut parents = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
484
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
485 parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
486 DirsMultiset::from_manifest(&dirs)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
487 .map_err(|e| match e {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
488 DirstateMapError::InvalidPath(e) => e,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
489 _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
490 })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
491 .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
492 .map(|k| k.to_owned()),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
493 );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
494 parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
495 DirsMultiset::from_manifest(&roots)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
496 .map_err(|e| match e {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
497 DirstateMapError::InvalidPath(e) => e,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
498 _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
499 })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
500 .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
501 .map(|k| k.to_owned()),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
502 );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
503
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
504 Ok(RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
505 roots: HashSet::from_iter(roots),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
506 dirs: HashSet::from_iter(dirs),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
507 parents,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
508 })
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
509 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
510
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
511 /// Returns a function that checks whether a given file (in the general sense)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
512 /// should be matched.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
513 fn build_match<'a, 'b>(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
514 ignore_patterns: &'a [IgnorePattern],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
515 root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
516 ) -> PatternResult<(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
517 Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
518 Box<dyn Fn(&HgPath) -> bool + 'b + Sync>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
519 Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
520 )> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
521 let mut match_funcs: Vec<Box<dyn Fn(&HgPath) -> bool + Sync>> = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
522 // For debugging and printing
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
523 let mut patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
524 let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
525
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
526 let (subincludes, ignore_patterns) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
527 filter_subincludes(ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
528
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
529 if !subincludes.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
530 // Build prefix-based matcher functions for subincludes
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
531 let mut submatchers = FastHashMap::default();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
532 let mut prefixes = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
533
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
534 for SubInclude { prefix, root, path } in subincludes.into_iter() {
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
535 let (match_fn, warnings) =
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
536 get_ignore_function(vec![path.to_path_buf()], root)?;
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
537 all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
538 prefixes.push(prefix.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
539 submatchers.insert(prefix.to_owned(), match_fn);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
540 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
541
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
542 let match_subinclude = move |filename: &HgPath| {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
543 for prefix in prefixes.iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
544 if let Some(rel) = filename.relative_to(prefix) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
545 if (submatchers.get(prefix).unwrap())(rel) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
546 return true;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
547 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
548 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
549 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
550 false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
551 };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
552
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
553 match_funcs.push(Box::new(match_subinclude));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
554 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
555
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
556 if !ignore_patterns.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
557 // Either do dumb matching if all patterns are rootfiles, or match
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
558 // with a regex.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
559 if ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
560 .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
561 .all(|k| k.syntax == PatternSyntax::RootFiles)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
562 {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
563 let dirs: HashSet<_> = ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
564 .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
565 .map(|k| k.pattern.to_owned())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
566 .collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
567 let mut dirs_vec: Vec<_> = dirs.iter().cloned().collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
568
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
569 let match_func = move |path: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
570 let path = path.as_bytes();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
571 let i = path.iter().rfind(|a| **a == b'/');
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
572 let dir = if let Some(i) = i {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
573 &path[..*i as usize]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
574 } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
575 b"."
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
576 };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
577 dirs.contains(dir.deref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
578 };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
579 match_funcs.push(Box::new(match_func));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
580
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
581 patterns.extend(b"rootfilesin: ");
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
582 dirs_vec.sort();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
583 patterns.extend(dirs_vec.escaped_bytes());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
584 } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
585 let (new_re, match_func) = build_regex_match(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
586 patterns = new_re;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
587 match_funcs.push(match_func)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
588 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
589 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
590
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
591 Ok(if match_funcs.len() == 1 {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
592 (patterns, match_funcs.remove(0), all_warnings)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
593 } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
594 (
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
595 patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
596 Box::new(move |f: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
597 match_funcs.iter().any(|match_func| match_func(f))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
598 }),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
599 all_warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
600 )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
601 })
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
602 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
603
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
604 /// Parses all "ignore" files with their recursive includes and returns a
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
605 /// function that checks whether a given file (in the general sense) should be
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
606 /// ignored.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
607 pub fn get_ignore_function<'a>(
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
608 all_pattern_files: Vec<PathBuf>,
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
609 root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
610 ) -> PatternResult<(
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
611 Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + 'a>,
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
612 Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
613 )> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
614 let mut all_patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
615 let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
616
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
617 for pattern_file in all_pattern_files.into_iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
618 let (patterns, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
619 get_patterns_from_file(pattern_file, &root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
620
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
621 all_patterns.extend(patterns.to_owned());
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
622 all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
623 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
624 let (matcher, warnings) = IncludeMatcher::new(all_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
625 all_warnings.extend(warnings);
44597
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
626 Ok((
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
627 Box::new(move |path: &HgPath| matcher.matches(path)),
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
628 all_warnings,
e62052d0f377 rust-status: only involve ignore mechanism when needed
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44593
diff changeset
629 ))
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
630 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
631
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
632 impl<'a> IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
633 pub fn new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
634 ignore_patterns: Vec<IgnorePattern>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
635 root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
636 ) -> PatternResult<(Self, Vec<PatternFileWarning>)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
637 let (patterns, match_fn, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
638 build_match(&ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
639 let RootsDirsAndParents {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
640 roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
641 dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
642 parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
643 } = roots_dirs_and_parents(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
644
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
645 let prefix = ignore_patterns.iter().any(|k| match k.syntax {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
646 PatternSyntax::Path | PatternSyntax::RelPath => true,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
647 _ => false,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
648 });
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
649
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
650 Ok((
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
651 Self {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
652 patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
653 match_fn,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
654 prefix,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
655 roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
656 dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
657 parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
658 },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
659 warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
660 ))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
661 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
662
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
663 fn get_all_parents_children(&self) -> DirsChildrenMultiset {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
664 // TODO cache
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
665 let thing = self
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
666 .dirs
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
667 .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
668 .chain(self.roots.iter())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
669 .chain(self.parents.iter());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
670 DirsChildrenMultiset::new(thing, Some(&self.parents))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
671 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
672 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
673
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
674 impl<'a> Display for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
675 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
44803
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
676 // XXX What about exact matches?
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
677 // I'm not sure it's worth it to clone the HashSet and keep it
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
678 // around just in case someone wants to display the matcher, plus
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
679 // it's going to be unreadable after a few entries, but we need to
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
680 // inform in this display that exact matches are being used and are
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44802
diff changeset
681 // (on purpose) missing from the `includes`.
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
682 write!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
683 f,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
684 "IncludeMatcher(includes='{}')",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
685 String::from_utf8_lossy(&self.patterns.escaped_bytes())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
686 )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
687 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
688 }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
689
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
690 #[cfg(test)]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
691 mod tests {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
692 use super::*;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
693 use pretty_assertions::assert_eq;
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
694 use std::path::Path;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
695
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
696 #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
697 fn test_roots_and_dirs() {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
698 let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
699 IgnorePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
700 IgnorePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
701 IgnorePattern::new(PatternSyntax::Glob, b"g*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
702 ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
703 let (roots, dirs) = roots_and_dirs(&pats);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
704
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
705 assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
706 roots,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
707 vec!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
708 HgPathBuf::from_bytes(b"g/h"),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
709 HgPathBuf::from_bytes(b"g/h"),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
710 HgPathBuf::new()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
711 ),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
712 );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
713 assert_eq!(dirs, vec!());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
714 }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
715
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
716 #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
717 fn test_roots_dirs_and_parents() {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
718 let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
719 IgnorePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
720 IgnorePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
721 IgnorePattern::new(PatternSyntax::Glob, b"g*", Path::new("")),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
722 ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
723
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
724 let mut roots = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
725 roots.insert(HgPathBuf::from_bytes(b"g/h"));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
726 roots.insert(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
727
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
728 let dirs = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
729
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
730 let mut parents = HashSet::new();
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
731 parents.insert(HgPathBuf::new());
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
732 parents.insert(HgPathBuf::from_bytes(b"g"));
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
733
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
734 assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
735 roots_dirs_and_parents(&pats).unwrap(),
44524
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44522
diff changeset
736 RootsDirsAndParents {
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44522
diff changeset
737 roots,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44522
diff changeset
738 dirs,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44522
diff changeset
739 parents
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44522
diff changeset
740 }
44520
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
741 );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44519
diff changeset
742 }
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
743
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
744 #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
745 fn test_filematcher_visit_children_set() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
746 // Visitchildrenset
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
747 let files = vec![HgPath::new(b"dir/subdir/foo.txt")];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
748 let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
749
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
750 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
751 set.insert(HgPath::new(b"dir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
752 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
753 matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
754 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
755 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
756
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
757 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
758 set.insert(HgPath::new(b"subdir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
759 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
760 matcher.visit_children_set(HgPath::new(b"dir")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
761 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
762 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
763
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
764 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
765 set.insert(HgPath::new(b"foo.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
766 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
767 matcher.visit_children_set(HgPath::new(b"dir/subdir")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
768 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
769 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
770
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
771 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
772 matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
773 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
774 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
775 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
776 matcher.visit_children_set(HgPath::new(b"dir/subdir/foo.txt")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
777 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
778 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
779 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
780 matcher.visit_children_set(HgPath::new(b"folder")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
781 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
782 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
783 }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
784
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
785 #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
786 fn test_filematcher_visit_children_set_files_and_dirs() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
787 let files = vec![
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
788 HgPath::new(b"rootfile.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
789 HgPath::new(b"a/file1.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
790 HgPath::new(b"a/b/file2.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
791 // No file in a/b/c
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
792 HgPath::new(b"a/b/c/d/file4.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
793 ];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
794 let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
795
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
796 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
797 set.insert(HgPath::new(b"a"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
798 set.insert(HgPath::new(b"rootfile.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
799 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
800 matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
801 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
802 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
803
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
804 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
805 set.insert(HgPath::new(b"b"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
806 set.insert(HgPath::new(b"file1.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
807 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
808 matcher.visit_children_set(HgPath::new(b"a")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
809 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
810 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
811
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
812 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
813 set.insert(HgPath::new(b"c"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
814 set.insert(HgPath::new(b"file2.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
815 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
816 matcher.visit_children_set(HgPath::new(b"a/b")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
817 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
818 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
819
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
820 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
821 set.insert(HgPath::new(b"d"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
822 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
823 matcher.visit_children_set(HgPath::new(b"a/b/c")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
824 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
825 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
826 let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
827 set.insert(HgPath::new(b"file4.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
828 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
829 matcher.visit_children_set(HgPath::new(b"a/b/c/d")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
830 VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
831 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
832
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
833 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
834 matcher.visit_children_set(HgPath::new(b"a/b/c/d/e")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
835 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
836 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
837 assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
838 matcher.visit_children_set(HgPath::new(b"folder")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
839 VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
840 );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
841 }
44522
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
842
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
843 #[cfg(feature = "with-re2")]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
844 #[test]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
845 fn test_includematcher() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
846 // VisitchildrensetPrefix
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
847 let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
848 vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
849 PatternSyntax::RelPath,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
850 b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
851 Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
852 )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
853 "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
854 )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
855 .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
856
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
857 let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
858 set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
859 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
860 matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
861 VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
862 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
863
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
864 let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
865 set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
866 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
867 matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
868 VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
869 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
870 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
871 matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
872 VisitChildrenSet::Recursive
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
873 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
874 // OPT: This should probably be 'all' if its parent is?
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
875 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
876 matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
877 VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
878 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
879 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
880 matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
881 VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
882 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
883
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
884 // VisitchildrensetRootfilesin
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
885 let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
886 vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
887 PatternSyntax::RootFiles,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
888 b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
889 Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
890 )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
891 "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
892 )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
893 .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
894
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
895 let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
896 set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
897 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
898 matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
899 VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
900 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
901
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
902 let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
903 set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
904 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
905 matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
906 VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
907 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
908
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
909 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
910 matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
911 VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
912 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
913 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
914 matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
915 VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
916 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
917 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
918 matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
919 VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
920 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
921
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
922 // VisitchildrensetGlob
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
923 let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
924 vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
925 PatternSyntax::Glob,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
926 b"dir/z*",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
927 Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
928 )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
929 "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
930 )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
931 .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
932
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
933 let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
934 set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
935 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
936 matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
937 VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
938 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
939 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
940 matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
941 VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
942 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
943 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
944 matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
945 VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
946 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
947 // OPT: these should probably be set().
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
948 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
949 matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
950 VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
951 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
952 assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
953 matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
954 VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
955 );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44521
diff changeset
956 }
44353
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44006
diff changeset
957 }