rust/hg-core/src/matchers.rs
changeset 51120 532e74ad3ff6
parent 51109 687e192dae16
child 51274 bec6e9c108fd
equal deleted inserted replaced
51119:d58e754f2db0 51120:532e74ad3ff6
    26 
    26 
    27 use crate::dirstate::status::IgnoreFnType;
    27 use crate::dirstate::status::IgnoreFnType;
    28 use crate::filepatterns::normalize_path_bytes;
    28 use crate::filepatterns::normalize_path_bytes;
    29 use std::collections::HashSet;
    29 use std::collections::HashSet;
    30 use std::fmt::{Display, Error, Formatter};
    30 use std::fmt::{Display, Error, Formatter};
    31 use std::ops::Deref;
       
    32 use std::path::{Path, PathBuf};
    31 use std::path::{Path, PathBuf};
    33 use std::{borrow::ToOwned, collections::BTreeSet};
    32 use std::{borrow::ToOwned, collections::BTreeSet};
    34 
    33 
    35 #[derive(Debug, PartialEq)]
    34 #[derive(Debug, PartialEq)]
    36 pub enum VisitChildrenSet {
    35 pub enum VisitChildrenSet {
   181 
   180 
   182 impl FileMatcher {
   181 impl FileMatcher {
   183     pub fn new(files: Vec<HgPathBuf>) -> Result<Self, HgPathError> {
   182     pub fn new(files: Vec<HgPathBuf>) -> Result<Self, HgPathError> {
   184         let dirs = DirsMultiset::from_manifest(&files)?;
   183         let dirs = DirsMultiset::from_manifest(&files)?;
   185         Ok(Self {
   184         Ok(Self {
   186             files: HashSet::from_iter(files.into_iter()),
   185             files: HashSet::from_iter(files),
   187             dirs,
   186             dirs,
   188             sorted_visitchildrenset_candidates: OnceCell::new(),
   187             sorted_visitchildrenset_candidates: OnceCell::new(),
   189         })
   188         })
   190     }
   189     }
   191     fn inner_matches(&self, filename: &HgPath) -> bool {
   190     fn inner_matches(&self, filename: &HgPath) -> bool {
   314 
   313 
   315 impl<'a> PatternMatcher<'a> {
   314 impl<'a> PatternMatcher<'a> {
   316     pub fn new(ignore_patterns: Vec<IgnorePattern>) -> PatternResult<Self> {
   315     pub fn new(ignore_patterns: Vec<IgnorePattern>) -> PatternResult<Self> {
   317         let (files, _) = roots_and_dirs(&ignore_patterns);
   316         let (files, _) = roots_and_dirs(&ignore_patterns);
   318         let dirs = DirsMultiset::from_manifest(&files)?;
   317         let dirs = DirsMultiset::from_manifest(&files)?;
   319         let files: HashSet<HgPathBuf> = HashSet::from_iter(files.into_iter());
   318         let files: HashSet<HgPathBuf> = HashSet::from_iter(files);
   320 
   319 
   321         let prefix = ignore_patterns.iter().all(|k| {
   320         let prefix = ignore_patterns.iter().all(|k| {
   322             matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath)
   321             matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath)
   323         });
   322         });
   324         let (patterns, match_fn) = build_match(ignore_patterns, b"$")?;
   323         let (patterns, match_fn) = build_match(ignore_patterns, b"$")?;
   771     })
   770     })
   772 }
   771 }
   773 
   772 
   774 /// Returns the regex pattern and a function that matches an `HgPath` against
   773 /// Returns the regex pattern and a function that matches an `HgPath` against
   775 /// said regex formed by the given ignore patterns.
   774 /// said regex formed by the given ignore patterns.
   776 fn build_regex_match<'a, 'b>(
   775 fn build_regex_match<'a>(
   777     ignore_patterns: &'a [IgnorePattern],
   776     ignore_patterns: &[IgnorePattern],
   778     glob_suffix: &[u8],
   777     glob_suffix: &[u8],
   779 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'b>)> {
   778 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'a>)> {
   780     let mut regexps = vec![];
   779     let mut regexps = vec![];
   781     let mut exact_set = HashSet::new();
   780     let mut exact_set = HashSet::new();
   782 
   781 
   783     for pattern in ignore_patterns {
   782     for pattern in ignore_patterns {
   784         if let Some(re) = build_single_regex(pattern, glob_suffix)? {
   783         if let Some(re) = build_single_regex(pattern, glob_suffix)? {
   956                 let dir = if let Some(i) = i {
   955                 let dir = if let Some(i) = i {
   957                     &path[..*i as usize]
   956                     &path[..*i as usize]
   958                 } else {
   957                 } else {
   959                     b"."
   958                     b"."
   960                 };
   959                 };
   961                 dirs.contains(dir.deref())
   960                 dirs.contains(dir)
   962             };
   961             };
   963             match_funcs.push(Box::new(match_func));
   962             match_funcs.push(Box::new(match_func));
   964 
   963 
   965             patterns.extend(b"rootfilesin: ");
   964             patterns.extend(b"rootfilesin: ");
   966             dirs_vec.sort();
   965             dirs_vec.sort();