rust/hg-core/src/matchers.rs
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 26 May 2020 08:07:24 -0700
changeset 44911 fd3b94f1712d
parent 44891 ad1ec40975aa
parent 44762 b15a37d85dbe
child 44929 9f96beb9bafe
permissions -rw-r--r--
merge with stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43490
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
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
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: 44387
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: 44387
diff changeset
    12
use crate::{
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    13
    dirstate::dirs_multiset::DirsChildrenMultiset,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    14
    filepatterns::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
diff changeset
    16
        PatternFileWarning, PatternResult, SubInclude,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    17
    },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    18
    utils::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    19
        files::find_dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    20
        hg_path::{HgPath, HgPathBuf},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    21
        Escaped,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    22
    },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    23
    DirsMultiset, DirstateMapError, FastHashMap, IgnorePattern, PatternError,
44530
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
    24
    PatternSyntax,
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
    25
};
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    26
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
    27
use crate::filepatterns::normalize_path_bytes;
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
    28
use std::borrow::ToOwned;
43490
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
use std::collections::HashSet;
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
    30
use std::fmt::{Display, Error, Formatter};
43920
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
    31
use std::iter::FromIterator;
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
    32
use std::ops::Deref;
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
    33
use std::path::{Path, PathBuf};
43490
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
44861
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
    35
use micro_timer::timed;
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
    36
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
    37
#[derive(Debug, PartialEq)]
43850
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43655
diff changeset
    38
pub enum VisitChildrenSet<'a> {
43490
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`?
43850
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43655
diff changeset
    45
    Set(HashSet<&'a HgPath>),
43490
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
43850
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43655
diff changeset
    52
    fn file_set(&self) -> Option<&HashSet<&HgPath>>;
43490
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`
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
    54
    fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool;
43490
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
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
    56
    fn matches(&self, filename: impl AsRef<HgPath>) -> bool;
43490
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
    ///
44009
72bced4f2936 rust-matchers: fixing cargo doc
Georges Racinet <georges.racinet@octobus.net>
parents: 43920
diff changeset
    70
    /// ```text
43490
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,
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
    96
        directory: impl AsRef<HgPath>,
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
    97
    ) -> VisitChildrenSet;
43490
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.
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   100
    fn matches_everything(&self) -> bool;
43490
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.
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   103
    fn is_exact(&self) -> bool;
43490
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.
43852
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43850
diff changeset
   107
///```
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43850
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: 43850
diff changeset
   109
///
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43850
diff changeset
   110
/// let matcher = AlwaysMatcher;
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43850
diff changeset
   111
///
43920
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
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: 43874
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: 43874
diff changeset
   115
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
43852
542c8b277261 rust-matchers: add doctests for `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 43850
diff changeset
   116
/// ```
43490
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 {
43850
1bb4e9b02984 rust-matchers: improve `Matcher` trait ergonomics
Raphaël Gomès <rgomes@octobus.net>
parents: 43655
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: 43655
diff changeset
   122
        None
43490
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
    }
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
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: 43490
diff changeset
   125
        false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   126
    }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
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: 43490
diff changeset
   128
        true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   129
    }
43490
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
    }
43655
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
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: 43490
diff changeset
   137
        true
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   138
    }
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
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: 43490
diff changeset
   140
        false
27c25c0dc967 rust-matchers: remove default implementations for `Matcher` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 43490
diff changeset
   141
    }
43490
a77d4fe347a4 rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
}
43920
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   143
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   145
/// patterns.
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   146
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   147
///```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   149
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   151
/// let matcher = FileMatcher::new(&files).unwrap();
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   152
///
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
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: 43874
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: 43874
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: 43874
diff changeset
   157
/// ```
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   158
#[derive(Debug)]
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   159
pub struct FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   160
    files: HashSet<&'a HgPath>,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   161
    dirs: DirsMultiset,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   162
}
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   163
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   164
impl<'a> FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   165
    pub fn new(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   166
        files: &'a [impl AsRef<HgPath>],
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   167
    ) -> Result<Self, DirstateMapError> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   168
        Ok(Self {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   170
            dirs: DirsMultiset::from_manifest(files)?,
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   171
        })
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   172
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   174
        self.files.contains(filename.as_ref())
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   175
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   176
}
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   177
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   178
impl<'a> Matcher for FileMatcher<'a> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   179
    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   180
        Some(&self.files)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   181
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   183
        self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   184
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
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: 43874
diff changeset
   186
        self.inner_matches(filename)
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   187
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   188
    fn visit_children_set(
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   189
        &self,
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   190
        directory: impl AsRef<HgPath>,
43920
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   191
    ) -> VisitChildrenSet {
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
diff changeset
   193
            return VisitChildrenSet::Empty;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   194
        }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
diff changeset
   196
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
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: 44009
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: 44009
diff changeset
   200
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
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: 44009
diff changeset
   203
            candidates = candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   204
                .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   205
                .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
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: 44009
diff changeset
   208
                    } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   209
                        None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   210
                    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   211
                })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   212
                .collect();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   213
        }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   214
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
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: 44009
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: 44009
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: 44009
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: 44009
diff changeset
   220
        VisitChildrenSet::Set(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   221
            candidates
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   222
                .iter()
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   223
                .filter_map(|c| {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
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: 44009
diff changeset
   225
                        Some(*c)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   226
                    } else {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   227
                        None
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   228
                    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   229
                })
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   230
                .collect(),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   231
        )
43920
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   232
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   233
    fn matches_everything(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   234
        false
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   235
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   236
    fn is_exact(&self) -> bool {
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   237
        true
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   238
    }
69c4f3cf2cdf rust-matchers: add `FileMatcher` implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 43874
diff changeset
   239
}
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   240
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
diff changeset
   242
#[cfg_attr(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   243
    feature = "with-re2",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   244
    doc = r##"
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   245
```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   246
use hg::{
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   247
    matchers::{IncludeMatcher, Matcher},
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   248
    IgnorePattern,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   249
    PatternSyntax,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   250
    utils::hg_path::HgPath
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   251
};
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   252
use std::path::Path;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   253
///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   254
let ignore_patterns =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
diff changeset
   256
let (matcher, _) = IncludeMatcher::new(ignore_patterns, "").unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   257
///
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
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: 44531
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: 44531
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: 44531
diff changeset
   262
```
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   263
"##
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   264
)]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   265
pub struct IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   266
    patterns: Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
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: 44531
diff changeset
   269
    prefix: bool,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   270
    roots: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   271
    dirs: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   272
    parents: HashSet<HgPathBuf>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   273
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   274
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   275
impl<'a> Matcher for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   276
    fn file_set(&self) -> Option<&HashSet<&HgPath>> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   277
        None
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   278
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   279
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
diff changeset
   281
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   282
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   283
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   284
    fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   285
        (self.match_fn)(filename.as_ref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   286
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   287
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   288
    fn visit_children_set(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   289
        &self,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   290
        directory: impl AsRef<HgPath>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   291
    ) -> VisitChildrenSet {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   292
        let dir = directory.as_ref();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   293
        if self.prefix && self.roots.contains(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   294
            return VisitChildrenSet::Recursive;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   295
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   296
        if self.roots.contains(HgPath::new(b""))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   297
            || self.roots.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   298
            || self.dirs.contains(dir)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
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: 44531
diff changeset
   300
        {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   301
            return VisitChildrenSet::This;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   302
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   303
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   304
        if self.parents.contains(directory.as_ref()) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   305
            let multiset = self.get_all_parents_children();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   306
            if let Some(children) = multiset.get(dir) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   307
                return VisitChildrenSet::Set(children.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   308
            }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   309
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   310
        VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   311
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   312
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   313
    fn matches_everything(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   314
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   315
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   316
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   317
    fn is_exact(&self) -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   318
        false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   319
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   320
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   321
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
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: 44387
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: 44387
diff changeset
   324
/// pattern.
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   325
///
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
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: 44387
diff changeset
   327
/// underlying engine `Re2`, for instance anything with back-references.
44861
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   328
#[timed]
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
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: 44387
diff changeset
   330
    pattern: &[u8],
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
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: 44387
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: 44387
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: 44387
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: 44387
diff changeset
   335
}
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   336
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   337
#[cfg(not(feature = "with-re2"))]
44601
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
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: 44551
diff changeset
   339
/// pattern.
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   340
///
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
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: 44551
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: 44551
diff changeset
   343
/// back-references.
44861
83c97c0bd319 rust-matchers: add timing tracing to regex compilation
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   344
#[timed]
44601
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   345
fn re_matcher(
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   346
    pattern: &[u8],
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   347
) -> PatternResult<impl Fn(&HgPath) -> bool + Sync> {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   348
    use std::io::Write;
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   349
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44880
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: 44880
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: 44880
diff changeset
   352
    let mut escaped_bytes = vec![b'^', b'(', b'?', b':'];
44601
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   353
    for byte in pattern {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   354
        if *byte > 127 {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
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: 44551
diff changeset
   356
        } else {
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   357
            escaped_bytes.push(*byte);
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   358
        }
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   359
    }
44891
ad1ec40975aa rust-regex: fix issues with regex anchoring and performance
Raphaël Gomès <rgomes@octobus.net>
parents: 44880
diff changeset
   360
    escaped_bytes.push(b')');
44601
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   361
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   362
    // Avoid the cost of UTF8 checking
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   363
    //
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   364
    // # Safety
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
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: 44551
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: 44551
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: 44551
diff changeset
   368
        .unicode(false)
44762
b15a37d85dbe rust-regex: increase the DFA size limit for the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   369
        // Big repos with big `.hgignore` will hit the default limit and
b15a37d85dbe rust-regex: increase the DFA size limit for the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   370
        // incur a significant performance hit. One repo's `hg status` hit
b15a37d85dbe rust-regex: increase the DFA size limit for the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   371
        // multiple *minutes*.
b15a37d85dbe rust-regex: increase the DFA size limit for the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44604
diff changeset
   372
        .dfa_size_limit(50 * (1 << 20))
44601
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   373
        .build()
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   374
        .map_err(|e| PatternError::UnsupportedSyntax(e.to_string()))?;
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   375
496868f1030c rust-matchers: use the `regex` crate
Raphaël Gomès <rgomes@octobus.net>
parents: 44551
diff changeset
   376
    Ok(move |path: &HgPath| re.is_match(path.as_bytes()))
44529
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   377
}
52d40f8fb82d rust-matchers: add function to generate a regex matcher function
Raphaël Gomès <rgomes@octobus.net>
parents: 44387
diff changeset
   378
44531
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   379
/// 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: 44530
diff changeset
   380
/// said regex formed by the given ignore patterns.
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   381
fn build_regex_match<'a>(
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   382
    ignore_patterns: &'a [&'a IgnorePattern],
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   383
) -> PatternResult<(Vec<u8>, Box<dyn Fn(&HgPath) -> bool + Sync>)> {
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   384
    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: 44861
diff changeset
   385
    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: 44861
diff changeset
   386
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   387
    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: 44861
diff changeset
   388
        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: 44861
diff changeset
   389
            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: 44861
diff changeset
   390
        } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   391
            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: 44861
diff changeset
   392
            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: 44861
diff changeset
   393
        }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   394
    }
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   395
44531
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   396
    let full_regex = regexps.join(&b'|');
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   397
44879
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   398
    // 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: 44861
diff changeset
   399
    // (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: 44861
diff changeset
   400
    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: 44861
diff changeset
   401
        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: 44861
diff changeset
   402
        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: 44861
diff changeset
   403
            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: 44861
diff changeset
   404
        };
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   405
        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: 44861
diff changeset
   406
    } else {
e0414fcd35e0 rust-filepatterns: match exact `rootglob`s with a `HashSet`, not in the regex
Raphaël Gomès <rgomes@octobus.net>
parents: 44861
diff changeset
   407
        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: 44861
diff changeset
   408
        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: 44861
diff changeset
   409
    };
44531
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   410
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   411
    Ok((full_regex, func))
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   412
}
a21881b40942 rust-matchers: add `build_regex_match` function
Raphaël Gomès <rgomes@octobus.net>
parents: 44530
diff changeset
   413
44530
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   414
/// 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: 44529
diff changeset
   415
///
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   416
/// 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: 44529
diff changeset
   417
/// 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: 44529
diff changeset
   418
/// 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: 44529
diff changeset
   419
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: 44529
diff changeset
   420
    ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   421
) -> (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: 44529
diff changeset
   422
    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: 44529
diff changeset
   423
    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: 44529
diff changeset
   424
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   425
    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: 44529
diff changeset
   426
        let IgnorePattern {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   427
            syntax, pattern, ..
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   428
        } = ignore_pattern;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   429
        match syntax {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   430
            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: 44529
diff changeset
   431
                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: 44529
diff changeset
   432
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   433
                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: 44529
diff changeset
   434
                    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: 44529
diff changeset
   435
                        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: 44529
diff changeset
   436
                        _ => false,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   437
                    }) {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   438
                        break;
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   439
                    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   440
                    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: 44529
diff changeset
   441
                }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   442
                let buf =
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   443
                    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: 44529
diff changeset
   444
                roots.push(buf);
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   445
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   446
            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: 44529
diff changeset
   447
                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: 44529
diff changeset
   448
                    &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   449
                } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   450
                    pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   451
                });
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   452
                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: 44529
diff changeset
   453
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   454
            PatternSyntax::RootFiles => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   455
                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: 44529
diff changeset
   456
                    &[] as &[u8]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   457
                } else {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   458
                    pattern
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   459
                };
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   460
                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: 44529
diff changeset
   461
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   462
            _ => {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   463
                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: 44529
diff changeset
   464
            }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   465
        }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   466
    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   467
    (roots, dirs)
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   468
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   469
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   470
/// 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: 44529
diff changeset
   471
#[derive(Debug, PartialEq)]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   472
struct RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   473
    /// 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: 44529
diff changeset
   474
    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: 44529
diff changeset
   475
    /// 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: 44529
diff changeset
   476
    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: 44529
diff changeset
   477
    /// 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: 44529
diff changeset
   478
    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: 44529
diff changeset
   479
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   480
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   481
/// 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: 44529
diff changeset
   482
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: 44529
diff changeset
   483
    ignore_patterns: &[IgnorePattern],
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   484
) -> PatternResult<RootsDirsAndParents> {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   485
    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: 44529
diff changeset
   486
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   487
    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: 44529
diff changeset
   488
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   489
    parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   490
        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: 44529
diff changeset
   491
            .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: 44529
diff changeset
   492
                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: 44529
diff changeset
   493
                _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   494
            })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   495
            .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   496
            .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: 44529
diff changeset
   497
    );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   498
    parents.extend(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   499
        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: 44529
diff changeset
   500
            .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: 44529
diff changeset
   501
                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: 44529
diff changeset
   502
                _ => unreachable!(),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   503
            })?
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   504
            .iter()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   505
            .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: 44529
diff changeset
   506
    );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   507
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   508
    Ok(RootsDirsAndParents {
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   509
        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: 44529
diff changeset
   510
        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: 44529
diff changeset
   511
        parents,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   512
    })
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   513
}
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   514
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   515
/// 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: 44531
diff changeset
   516
/// should be matched.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   517
fn build_match<'a, 'b>(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   518
    ignore_patterns: &'a [IgnorePattern],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   519
    root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   520
) -> PatternResult<(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   521
    Vec<u8>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   522
    Box<dyn Fn(&HgPath) -> bool + 'b + Sync>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   523
    Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   524
)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   525
    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: 44531
diff changeset
   526
    // For debugging and printing
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   527
    let mut patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   528
    let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   529
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   530
    let (subincludes, ignore_patterns) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   531
        filter_subincludes(ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   533
    if !subincludes.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   534
        // Build prefix-based matcher functions for subincludes
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   535
        let mut submatchers = FastHashMap::default();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   536
        let mut prefixes = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   537
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   538
        for SubInclude { prefix, root, path } in subincludes.into_iter() {
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   539
            let (match_fn, warnings) =
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   540
                get_ignore_function(vec![path.to_path_buf()], root)?;
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   541
            all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   542
            prefixes.push(prefix.to_owned());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   543
            submatchers.insert(prefix.to_owned(), match_fn);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   544
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   545
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   546
        let match_subinclude = move |filename: &HgPath| {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   547
            for prefix in prefixes.iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   548
                if let Some(rel) = filename.relative_to(prefix) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   549
                    if (submatchers.get(prefix).unwrap())(rel) {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   550
                        return true;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   551
                    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   552
                }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   553
            }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   554
            false
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   555
        };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   556
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   557
        match_funcs.push(Box::new(match_subinclude));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   558
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   559
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   560
    if !ignore_patterns.is_empty() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   561
        // Either do dumb matching if all patterns are rootfiles, or match
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   562
        // with a regex.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   563
        if ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   564
            .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   565
            .all(|k| k.syntax == PatternSyntax::RootFiles)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   566
        {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   567
            let dirs: HashSet<_> = ignore_patterns
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   568
                .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   569
                .map(|k| k.pattern.to_owned())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   570
                .collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   571
            let mut dirs_vec: Vec<_> = dirs.iter().cloned().collect();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   572
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   573
            let match_func = move |path: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   574
                let path = path.as_bytes();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   575
                let i = path.iter().rfind(|a| **a == b'/');
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   576
                let dir = if let Some(i) = i {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   577
                    &path[..*i as usize]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   578
                } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   579
                    b"."
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   580
                };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   581
                dirs.contains(dir.deref())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   582
            };
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   583
            match_funcs.push(Box::new(match_func));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   584
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   585
            patterns.extend(b"rootfilesin: ");
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   586
            dirs_vec.sort();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   587
            patterns.extend(dirs_vec.escaped_bytes());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   588
        } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   589
            let (new_re, match_func) = build_regex_match(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   590
            patterns = new_re;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   591
            match_funcs.push(match_func)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   592
        }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   593
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   594
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   595
    Ok(if match_funcs.len() == 1 {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   596
        (patterns, match_funcs.remove(0), all_warnings)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   597
    } else {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   598
        (
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   599
            patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   600
            Box::new(move |f: &HgPath| -> bool {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   601
                match_funcs.iter().any(|match_func| match_func(f))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   602
            }),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   603
            all_warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   604
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   605
    })
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   606
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   607
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   608
/// Parses all "ignore" files with their recursive includes and returns a
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   609
/// 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: 44531
diff changeset
   610
/// ignored.
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   611
pub fn get_ignore_function<'a>(
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   612
    all_pattern_files: Vec<PathBuf>,
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   613
    root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   614
) -> PatternResult<(
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   615
    Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + 'a>,
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   616
    Vec<PatternFileWarning>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   617
)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   618
    let mut all_patterns = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   619
    let mut all_warnings = vec![];
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   620
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   621
    for pattern_file in all_pattern_files.into_iter() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   622
        let (patterns, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   623
            get_patterns_from_file(pattern_file, &root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   624
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   625
        all_patterns.extend(patterns.to_owned());
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   626
        all_warnings.extend(warnings);
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   627
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   628
    let (matcher, warnings) = IncludeMatcher::new(all_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   629
    all_warnings.extend(warnings);
44604
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   630
    Ok((
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   631
        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: 44601
diff changeset
   632
        all_warnings,
e62052d0f377 rust-status: only involve ignore mechanism when needed
Raphaël Gomès <rgomes@octobus.net>
parents: 44601
diff changeset
   633
    ))
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   634
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   635
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   636
impl<'a> IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   637
    pub fn new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   638
        ignore_patterns: Vec<IgnorePattern>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   639
        root_dir: impl AsRef<Path>,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   640
    ) -> PatternResult<(Self, Vec<PatternFileWarning>)> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   641
        let (patterns, match_fn, warnings) =
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   642
            build_match(&ignore_patterns, root_dir)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   643
        let RootsDirsAndParents {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   644
            roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   645
            dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   646
            parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   647
        } = roots_dirs_and_parents(&ignore_patterns)?;
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   648
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   649
        let prefix = ignore_patterns.iter().any(|k| match k.syntax {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   650
            PatternSyntax::Path | PatternSyntax::RelPath => true,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   651
            _ => false,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   652
        });
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   653
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   654
        Ok((
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   655
            Self {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   656
                patterns,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   657
                match_fn,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   658
                prefix,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   659
                roots,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   660
                dirs,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   661
                parents,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   662
            },
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   663
            warnings,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   664
        ))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   665
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   666
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   667
    fn get_all_parents_children(&self) -> DirsChildrenMultiset {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   668
        // TODO cache
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   669
        let thing = self
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   670
            .dirs
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   671
            .iter()
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   672
            .chain(self.roots.iter())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   673
            .chain(self.parents.iter());
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   674
        DirsChildrenMultiset::new(thing, Some(&self.parents))
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   675
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   676
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   677
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   678
impl<'a> Display for IncludeMatcher<'a> {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   679
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
44880
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44879
diff changeset
   680
        // XXX What about exact matches?
de0fb4463a3d rust-matchers: add TODO about incomplete `Display` for `IncludeMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44879
diff changeset
   681
        // 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: 44879
diff changeset
   682
        // 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: 44879
diff changeset
   683
        // 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: 44879
diff changeset
   684
        // 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: 44879
diff changeset
   685
        // (on purpose) missing from the `includes`.
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   686
        write!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   687
            f,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   688
            "IncludeMatcher(includes='{}')",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   689
            String::from_utf8_lossy(&self.patterns.escaped_bytes())
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   690
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   691
    }
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   692
}
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   693
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   694
#[cfg(test)]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   695
mod tests {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   696
    use super::*;
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   697
    use pretty_assertions::assert_eq;
44530
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   698
    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: 44529
diff changeset
   699
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   700
    #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   701
    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: 44529
diff changeset
   702
        let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   703
            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: 44529
diff changeset
   704
            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: 44529
diff changeset
   705
            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: 44529
diff changeset
   706
        ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   707
        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: 44529
diff changeset
   708
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   709
        assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   710
            roots,
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   711
            vec!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   712
                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: 44529
diff changeset
   713
                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: 44529
diff changeset
   714
                HgPathBuf::new()
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   715
            ),
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   716
        );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   717
        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: 44529
diff changeset
   718
    }
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   719
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   720
    #[test]
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   721
    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: 44529
diff changeset
   722
        let pats = vec![
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   723
            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: 44529
diff changeset
   724
            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: 44529
diff changeset
   725
            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: 44529
diff changeset
   726
        ];
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   727
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   728
        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: 44529
diff changeset
   729
        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: 44529
diff changeset
   730
        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: 44529
diff changeset
   731
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   732
        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: 44529
diff changeset
   733
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   734
        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: 44529
diff changeset
   735
        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: 44529
diff changeset
   736
        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: 44529
diff changeset
   737
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   738
        assert_eq!(
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   739
            roots_dirs_and_parents(&pats).unwrap(),
44534
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44532
diff changeset
   740
            RootsDirsAndParents {
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44532
diff changeset
   741
                roots,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44532
diff changeset
   742
                dirs,
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44532
diff changeset
   743
                parents
483fce658e43 rust-status: refactor options into a `StatusOptions` struct
Raphaël Gomès <rgomes@octobus.net>
parents: 44532
diff changeset
   744
            }
44530
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   745
        );
d4e8cfcde012 rust-matchers: add functions to get roots, dirs and parents from patterns
Raphaël Gomès <rgomes@octobus.net>
parents: 44529
diff changeset
   746
    }
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   747
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   748
    #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   749
    fn test_filematcher_visit_children_set() {
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   750
        // Visitchildrenset
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   751
        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: 44009
diff changeset
   752
        let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   753
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   754
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   755
        set.insert(HgPath::new(b"dir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   756
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   757
            matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   758
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   759
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   760
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   761
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   762
        set.insert(HgPath::new(b"subdir"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   763
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   764
            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: 44009
diff changeset
   765
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   766
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   767
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   768
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   769
        set.insert(HgPath::new(b"foo.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   770
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   771
            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: 44009
diff changeset
   772
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   773
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   774
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   775
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   776
            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: 44009
diff changeset
   777
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   778
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   779
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   780
            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: 44009
diff changeset
   781
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   782
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   783
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   784
            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: 44009
diff changeset
   785
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   786
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   787
    }
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   788
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   789
    #[test]
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   790
    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: 44009
diff changeset
   791
        let files = vec![
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   792
            HgPath::new(b"rootfile.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   793
            HgPath::new(b"a/file1.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   794
            HgPath::new(b"a/b/file2.txt"),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   795
            // No file in a/b/c
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   796
            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: 44009
diff changeset
   797
        ];
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   798
        let matcher = FileMatcher::new(&files).unwrap();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   799
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   800
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   801
        set.insert(HgPath::new(b"a"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   802
        set.insert(HgPath::new(b"rootfile.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   803
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   804
            matcher.visit_children_set(HgPath::new(b"")),
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   805
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   806
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   807
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   808
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   809
        set.insert(HgPath::new(b"b"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   810
        set.insert(HgPath::new(b"file1.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   811
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   812
            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: 44009
diff changeset
   813
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   814
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   815
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   816
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   817
        set.insert(HgPath::new(b"c"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   818
        set.insert(HgPath::new(b"file2.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   819
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   820
            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: 44009
diff changeset
   821
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   822
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   823
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   824
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   825
        set.insert(HgPath::new(b"d"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   826
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   827
            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: 44009
diff changeset
   828
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   829
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   830
        let mut set = HashSet::new();
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   831
        set.insert(HgPath::new(b"file4.txt"));
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   832
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   833
            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: 44009
diff changeset
   834
            VisitChildrenSet::Set(set)
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   835
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   836
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   837
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   838
            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: 44009
diff changeset
   839
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   840
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   841
        assert_eq!(
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   842
            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: 44009
diff changeset
   843
            VisitChildrenSet::Empty
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   844
        );
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   845
    }
44532
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   846
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   847
    #[cfg(feature = "with-re2")]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   848
    #[test]
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   849
    fn test_includematcher() {
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   850
        // VisitchildrensetPrefix
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   851
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   852
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   853
                PatternSyntax::RelPath,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   854
                b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   855
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   856
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   857
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   858
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   859
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   860
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   861
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   862
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   863
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   864
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   865
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   866
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   867
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   868
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   869
        set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   870
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   871
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   872
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   873
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   874
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   875
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   876
            VisitChildrenSet::Recursive
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   877
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   878
        // OPT: This should probably be 'all' if its parent is?
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   879
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   880
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   881
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   882
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   883
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   884
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   885
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   886
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   887
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   888
        // VisitchildrensetRootfilesin
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   889
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   890
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   891
                PatternSyntax::RootFiles,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   892
                b"dir/subdir",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   893
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   894
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   895
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   896
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   897
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   898
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   899
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   900
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   901
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   902
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   903
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   904
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   905
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   906
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   907
        set.insert(HgPath::new(b"subdir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   908
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   909
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   910
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   911
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   912
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   913
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   914
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   915
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   916
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   917
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   918
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   919
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   920
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   921
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   922
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   923
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   924
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   925
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   926
        // VisitchildrensetGlob
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   927
        let (matcher, _) = IncludeMatcher::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   928
            vec![IgnorePattern::new(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   929
                PatternSyntax::Glob,
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   930
                b"dir/z*",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   931
                Path::new(""),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   932
            )],
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   933
            "",
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   934
        )
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   935
        .unwrap();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   936
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   937
        let mut set = HashSet::new();
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   938
        set.insert(HgPath::new(b"dir"));
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   939
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   940
            matcher.visit_children_set(HgPath::new(b"")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   941
            VisitChildrenSet::Set(set)
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   942
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   943
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   944
            matcher.visit_children_set(HgPath::new(b"folder")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   945
            VisitChildrenSet::Empty
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   946
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   947
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   948
            matcher.visit_children_set(HgPath::new(b"dir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   949
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   950
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   951
        // OPT: these should probably be set().
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   952
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   953
            matcher.visit_children_set(HgPath::new(b"dir/subdir")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   954
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   955
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   956
        assert_eq!(
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   957
            matcher.visit_children_set(HgPath::new(b"dir/subdir/x")),
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   958
            VisitChildrenSet::This
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   959
        );
c697638e0e91 rust-matchers: add `IgnoreMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44531
diff changeset
   960
    }
44387
54d185eb24b5 rust-matchers: implement `visit_children_set` for `FileMatcher`
Raphaël Gomès <rgomes@octobus.net>
parents: 44009
diff changeset
   961
}