| 1 |
// matchers.rs |
| 2 |
// |
| 3 |
// Copyright 2019 Raphaël Gomès <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! Structs and types for matching files and directories. |
| 9 |
|
| 10 |
use std::borrow::ToOwned; |
| 11 |
use std::collections::BTreeSet; |
| 12 |
use std::collections::HashSet; |
| 13 |
use std::fmt::Display; |
| 14 |
use std::fmt::Error; |
| 15 |
use std::fmt::Formatter; |
| 16 |
use std::path::Path; |
| 17 |
use std::path::PathBuf; |
| 18 |
use std::str::FromStr; |
| 19 |
|
| 20 |
use format_bytes::format_bytes; |
| 21 |
use itertools::Itertools; |
| 22 |
use once_cell::sync::OnceCell; |
| 23 |
use regex_automata::meta::Regex; |
| 24 |
use regex_syntax::hir::Hir; |
| 25 |
|
| 26 |
use crate::dirstate::dirs_multiset::DirsChildrenMultiset; |
| 27 |
use crate::dirstate::dirs_multiset::DirsMultiset; |
| 28 |
use crate::dirstate::status::IgnoreFnType; |
| 29 |
use crate::file_patterns::build_single_regex; |
| 30 |
use crate::file_patterns::filter_subincludes; |
| 31 |
use crate::file_patterns::get_patterns_from_file; |
| 32 |
use crate::file_patterns::normalize_path_bytes; |
| 33 |
use crate::file_patterns::FilePattern; |
| 34 |
use crate::file_patterns::GlobSuffix; |
| 35 |
use crate::file_patterns::PatternError; |
| 36 |
use crate::file_patterns::PatternResult; |
| 37 |
use crate::file_patterns::PatternSyntax; |
| 38 |
use crate::file_patterns::RegexCompleteness; |
| 39 |
use crate::narrow::shape::deepest_prefix_node; |
| 40 |
use crate::narrow::shape::Shape; |
| 41 |
use crate::pre_regex::PreRegex; |
| 42 |
use crate::repo::Repo; |
| 43 |
use crate::utils::files::dir_ancestors; |
| 44 |
use crate::utils::files::find_dirs; |
| 45 |
use crate::utils::files::get_path_from_bytes; |
| 46 |
use crate::utils::hg_path::HgPath; |
| 47 |
use crate::utils::hg_path::HgPathBuf; |
| 48 |
use crate::utils::hg_path::HgPathError; |
| 49 |
use crate::utils::strings::Escaped; |
| 50 |
use crate::warnings::HgWarningSender; |
| 51 |
use crate::FastHashMap; |
| 52 |
|
| 53 |
#[derive(Debug, PartialEq)] |
| 54 |
pub enum VisitChildrenSet { |
| 55 |
/// Don't visit anything |
| 56 |
Empty, |
| 57 |
/// Visit this directory and probably its children |
| 58 |
This, |
| 59 |
/// Only visit the children (both files and directories) if they |
| 60 |
/// are mentioned in this set. (empty set corresponds to [`Self::Empty`]) |
| 61 |
/// TODO Should we implement a `NonEmptyHashSet`? |
| 62 |
Set(HashSet<HgPathBuf>), |
| 63 |
/// Visit this directory and all subdirectories |
| 64 |
/// (you can stop asking about the children set) |
| 65 |
Recursive, |
| 66 |
} |
| 67 |
|
| 68 |
pub trait Matcher: core::fmt::Debug + Sync { |
| 69 |
/// Explicitly listed files |
| 70 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>>; |
| 71 |
/// Returns whether `filename` is in `file_set` |
| 72 |
fn exact_match(&self, filename: &HgPath) -> bool; |
| 73 |
/// Returns whether `filename` is matched by this matcher |
| 74 |
fn matches(&self, filename: &HgPath) -> bool; |
| 75 |
/// Decides whether a directory should be visited based on whether it |
| 76 |
/// has potential matches in it or one of its subdirectories, and |
| 77 |
/// potentially lists which subdirectories of that directory should be |
| 78 |
/// visited. This is based on the match's primary, included, and excluded |
| 79 |
/// patterns. |
| 80 |
/// |
| 81 |
/// # Example |
| 82 |
/// |
| 83 |
/// Assume matchers `['path:foo/bar', 'rootfilesin:qux']`, we would |
| 84 |
/// return the following values (assuming the implementation of |
| 85 |
/// visit_children_set is capable of recognizing this; some implementations |
| 86 |
/// are not). |
| 87 |
/// |
| 88 |
/// ```text |
| 89 |
/// ```ignore |
| 90 |
/// '' -> {'foo', 'qux'} |
| 91 |
/// 'baz' -> set() |
| 92 |
/// 'foo' -> {'bar'} |
| 93 |
/// // Ideally this would be `Recursive`, but since the prefix nature of |
| 94 |
/// // matchers is applied to the entire matcher, we have to downgrade this |
| 95 |
/// // to `This` due to the (yet to be implemented in Rust) non-prefix |
| 96 |
/// // `RootFilesIn'-kind matcher being mixed in. |
| 97 |
/// 'foo/bar' -> 'this' |
| 98 |
/// 'qux' -> 'this' |
| 99 |
/// ``` |
| 100 |
/// # Important |
| 101 |
/// |
| 102 |
/// Most matchers do not know if they're representing files or |
| 103 |
/// directories. They see `['path:dir/f']` and don't know whether `f` is a |
| 104 |
/// file or a directory, so `visit_children_set('dir')` for most matchers |
| 105 |
/// will return `HashSet{ HgPath { "f" } }`, but if the matcher knows it's |
| 106 |
/// a file (like the yet to be implemented in Rust `ExactMatcher` does), |
| 107 |
/// it may return `VisitChildrenSet::This`. |
| 108 |
/// Do not rely on the return being a `HashSet` indicating that there are |
| 109 |
/// no files in this dir to investigate (or equivalently that if there are |
| 110 |
/// files to investigate in 'dir' that it will always return |
| 111 |
/// `VisitChildrenSet::This`). |
| 112 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet; |
| 113 |
/// Matcher will match everything and `files_set()` will be empty: |
| 114 |
/// optimization might be possible. |
| 115 |
fn matches_everything(&self) -> bool; |
| 116 |
/// Matcher will match exactly the files in `files_set()`: optimization |
| 117 |
/// might be possible. |
| 118 |
fn is_exact(&self) -> bool; |
| 119 |
} |
| 120 |
|
| 121 |
impl<T: Matcher + ?Sized> Matcher for &T { |
| 122 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 123 |
(*self).file_set() |
| 124 |
} |
| 125 |
|
| 126 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 127 |
(*self).exact_match(filename) |
| 128 |
} |
| 129 |
|
| 130 |
fn matches(&self, filename: &HgPath) -> bool { |
| 131 |
(*self).matches(filename) |
| 132 |
} |
| 133 |
|
| 134 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 135 |
(*self).visit_children_set(directory) |
| 136 |
} |
| 137 |
|
| 138 |
fn matches_everything(&self) -> bool { |
| 139 |
(*self).matches_everything() |
| 140 |
} |
| 141 |
|
| 142 |
fn is_exact(&self) -> bool { |
| 143 |
(*self).is_exact() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl<T: Matcher + ?Sized> Matcher for Box<T> { |
| 148 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 149 |
(**self).file_set() |
| 150 |
} |
| 151 |
|
| 152 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 153 |
(**self).exact_match(filename) |
| 154 |
} |
| 155 |
|
| 156 |
fn matches(&self, filename: &HgPath) -> bool { |
| 157 |
(**self).matches(filename) |
| 158 |
} |
| 159 |
|
| 160 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 161 |
(**self).visit_children_set(directory) |
| 162 |
} |
| 163 |
|
| 164 |
fn matches_everything(&self) -> bool { |
| 165 |
(**self).matches_everything() |
| 166 |
} |
| 167 |
|
| 168 |
fn is_exact(&self) -> bool { |
| 169 |
(**self).is_exact() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/// Matches everything. |
| 174 |
///``` |
| 175 |
/// use hg::{ matchers::{Matcher, AlwaysMatcher}, utils::hg_path::HgPath }; |
| 176 |
/// |
| 177 |
/// let matcher = AlwaysMatcher; |
| 178 |
/// |
| 179 |
/// assert_eq!(matcher.matches(HgPath::new(b"whatever")), true); |
| 180 |
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), true); |
| 181 |
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), true); |
| 182 |
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true); |
| 183 |
/// ``` |
| 184 |
#[derive(Debug)] |
| 185 |
pub struct AlwaysMatcher; |
| 186 |
|
| 187 |
impl Matcher for AlwaysMatcher { |
| 188 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 189 |
None |
| 190 |
} |
| 191 |
fn exact_match(&self, _filename: &HgPath) -> bool { |
| 192 |
false |
| 193 |
} |
| 194 |
fn matches(&self, _filename: &HgPath) -> bool { |
| 195 |
true |
| 196 |
} |
| 197 |
fn visit_children_set(&self, _directory: &HgPath) -> VisitChildrenSet { |
| 198 |
VisitChildrenSet::Recursive |
| 199 |
} |
| 200 |
fn matches_everything(&self) -> bool { |
| 201 |
true |
| 202 |
} |
| 203 |
fn is_exact(&self) -> bool { |
| 204 |
false |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/// Matches nothing. |
| 209 |
#[derive(Debug)] |
| 210 |
pub struct NeverMatcher; |
| 211 |
|
| 212 |
impl Matcher for NeverMatcher { |
| 213 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 214 |
None |
| 215 |
} |
| 216 |
fn exact_match(&self, _filename: &HgPath) -> bool { |
| 217 |
false |
| 218 |
} |
| 219 |
fn matches(&self, _filename: &HgPath) -> bool { |
| 220 |
false |
| 221 |
} |
| 222 |
fn visit_children_set(&self, _directory: &HgPath) -> VisitChildrenSet { |
| 223 |
VisitChildrenSet::Empty |
| 224 |
} |
| 225 |
fn matches_everything(&self) -> bool { |
| 226 |
false |
| 227 |
} |
| 228 |
fn is_exact(&self) -> bool { |
| 229 |
true |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/// Matches the input files exactly. They are interpreted as paths, not |
| 234 |
/// patterns. |
| 235 |
/// |
| 236 |
///``` |
| 237 |
/// use hg::{ matchers::{Matcher, FileMatcher}, utils::hg_path::{HgPath, HgPathBuf} }; |
| 238 |
/// |
| 239 |
/// let files = vec![HgPathBuf::from_bytes(b"a.txt"), HgPathBuf::from_bytes(br"re:.*\.c$")]; |
| 240 |
/// let matcher = FileMatcher::new(files).unwrap(); |
| 241 |
/// |
| 242 |
/// assert_eq!(matcher.matches(HgPath::new(b"a.txt")), true); |
| 243 |
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), false); |
| 244 |
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), false); |
| 245 |
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true); |
| 246 |
/// ``` |
| 247 |
#[derive(Debug)] |
| 248 |
pub struct FileMatcher { |
| 249 |
files: HashSet<HgPathBuf>, |
| 250 |
dirs: DirsMultiset, |
| 251 |
sorted_visitchildrenset_candidates: OnceCell<BTreeSet<HgPathBuf>>, |
| 252 |
} |
| 253 |
|
| 254 |
impl FileMatcher { |
| 255 |
pub fn new(files: Vec<HgPathBuf>) -> Result<Self, HgPathError> { |
| 256 |
let dirs = DirsMultiset::from_manifest(&files)?; |
| 257 |
Ok(Self { |
| 258 |
files: HashSet::from_iter(files), |
| 259 |
dirs, |
| 260 |
sorted_visitchildrenset_candidates: OnceCell::new(), |
| 261 |
}) |
| 262 |
} |
| 263 |
fn inner_matches(&self, filename: &HgPath) -> bool { |
| 264 |
self.files.contains(filename.as_ref()) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl Matcher for FileMatcher { |
| 269 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 270 |
Some(&self.files) |
| 271 |
} |
| 272 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 273 |
self.inner_matches(filename) |
| 274 |
} |
| 275 |
fn matches(&self, filename: &HgPath) -> bool { |
| 276 |
self.inner_matches(filename) |
| 277 |
} |
| 278 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 279 |
if self.files.is_empty() || !self.dirs.contains(directory) { |
| 280 |
return VisitChildrenSet::Empty; |
| 281 |
} |
| 282 |
|
| 283 |
let compute_candidates = || -> BTreeSet<HgPathBuf> { |
| 284 |
let mut candidates: BTreeSet<HgPathBuf> = |
| 285 |
self.dirs.iter().cloned().collect(); |
| 286 |
candidates.extend(self.files.iter().cloned()); |
| 287 |
candidates.remove(HgPath::new(b"")); |
| 288 |
candidates |
| 289 |
}; |
| 290 |
let candidates = if directory.as_ref().is_empty() { |
| 291 |
compute_candidates() |
| 292 |
} else { |
| 293 |
let sorted_candidates = self |
| 294 |
.sorted_visitchildrenset_candidates |
| 295 |
.get_or_init(compute_candidates); |
| 296 |
let directory_bytes = directory.as_ref().as_bytes(); |
| 297 |
let start: HgPathBuf = |
| 298 |
format_bytes!(b"{}/", directory_bytes).into(); |
| 299 |
let start_len = start.len(); |
| 300 |
// `0` sorts after `/` |
| 301 |
let end = format_bytes!(b"{}0", directory_bytes).into(); |
| 302 |
BTreeSet::from_iter( |
| 303 |
sorted_candidates |
| 304 |
.range(start..end) |
| 305 |
.map(|c| HgPathBuf::from_bytes(&c.as_bytes()[start_len..])), |
| 306 |
) |
| 307 |
}; |
| 308 |
|
| 309 |
// `self.dirs` includes all of the directories, recursively, so if |
| 310 |
// we're attempting to match 'foo/bar/baz.txt', it'll have '', 'foo', |
| 311 |
// 'foo/bar' in it. Thus we can safely ignore a candidate that has a |
| 312 |
// '/' in it, indicating it's for a subdir-of-a-subdir; the immediate |
| 313 |
// subdir will be in there without a slash. |
| 314 |
VisitChildrenSet::Set( |
| 315 |
candidates |
| 316 |
.into_iter() |
| 317 |
.filter_map(|c| { |
| 318 |
if c.bytes().all(|b| *b != b'/') { |
| 319 |
Some(c) |
| 320 |
} else { |
| 321 |
None |
| 322 |
} |
| 323 |
}) |
| 324 |
.collect(), |
| 325 |
) |
| 326 |
} |
| 327 |
fn matches_everything(&self) -> bool { |
| 328 |
false |
| 329 |
} |
| 330 |
fn is_exact(&self) -> bool { |
| 331 |
true |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
/// Matches a set of (kind, pat, source) against a 'root' directory. |
| 336 |
/// (Currently the 'root' directory is effectively always empty) |
| 337 |
/// ``` |
| 338 |
/// use hg::{ |
| 339 |
/// matchers::{PatternMatcher, Matcher}, |
| 340 |
/// file_patterns::{FilePattern, PatternSyntax}, |
| 341 |
/// utils::hg_path::{HgPath, HgPathBuf} |
| 342 |
/// }; |
| 343 |
/// use std::collections::HashSet; |
| 344 |
/// use std::path::Path; |
| 345 |
/// /// |
| 346 |
/// let file_patterns : Vec<FilePattern> = |
| 347 |
/// vec![FilePattern::new(PatternSyntax::Regexp, br".*\.c$", Path::new("")), |
| 348 |
/// FilePattern::new(PatternSyntax::Path, b"foo/a", Path::new("")), |
| 349 |
/// FilePattern::new(PatternSyntax::RelPath, b"b", Path::new("")), |
| 350 |
/// FilePattern::new(PatternSyntax::Glob, b"*.h", Path::new("")), |
| 351 |
/// ]; |
| 352 |
/// let matcher = PatternMatcher::new(file_patterns).unwrap(); |
| 353 |
/// /// |
| 354 |
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), true); // matches re:.*\.c$ |
| 355 |
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), false); |
| 356 |
/// assert_eq!(matcher.matches(HgPath::new(b"foo/a")), true); // matches path:foo/a |
| 357 |
/// assert_eq!(matcher.matches(HgPath::new(b"a")), false); // does not match path:b, since 'root' is 'foo' |
| 358 |
/// assert_eq!(matcher.matches(HgPath::new(b"b")), true); // matches relpath:b, since 'root' is 'foo' |
| 359 |
/// assert_eq!(matcher.matches(HgPath::new(b"lib.h")), true); // matches glob:*.h |
| 360 |
/// assert_eq!(matcher.file_set().unwrap(), |
| 361 |
/// &HashSet::from([HgPathBuf::from_bytes(b""), HgPathBuf::from_bytes(b"foo/a"), |
| 362 |
/// HgPathBuf::from_bytes(b""), HgPathBuf::from_bytes(b"b")])); |
| 363 |
/// assert_eq!(matcher.exact_match(HgPath::new(b"foo/a")), true); |
| 364 |
/// assert_eq!(matcher.exact_match(HgPath::new(b"b")), true); |
| 365 |
/// assert_eq!(matcher.exact_match(HgPath::new(b"lib.h")), false); // exact matches are for (rel)path kinds |
| 366 |
/// ``` |
| 367 |
pub struct PatternMatcher<'a> { |
| 368 |
patterns: PatternsDesc, |
| 369 |
match_fn: IgnoreFnType<'a>, |
| 370 |
/// Whether all the patterns match a prefix (i.e. recursively) |
| 371 |
prefix: bool, |
| 372 |
files: HashSet<HgPathBuf>, |
| 373 |
dirs_explicit: HashSet<HgPathBuf>, |
| 374 |
dirs: DirsMultiset, |
| 375 |
} |
| 376 |
|
| 377 |
enum PatternsDesc { |
| 378 |
Re(PreRegex), |
| 379 |
RootFilesIn(Vec<Vec<u8>>, GlobSuffix), |
| 380 |
} |
| 381 |
|
| 382 |
pub enum ReSyntax { |
| 383 |
Tidy, |
| 384 |
Internal, |
| 385 |
} |
| 386 |
|
| 387 |
impl PatternsDesc { |
| 388 |
fn to_re(&self) -> PreRegex { |
| 389 |
match self { |
| 390 |
Self::Re(re) => re.clone(), |
| 391 |
Self::RootFilesIn(patterns, glob_suffix) => { |
| 392 |
let patterns = patterns |
| 393 |
.clone() |
| 394 |
.into_iter() |
| 395 |
.map(|pattern: Vec<u8>| FilePattern { |
| 396 |
syntax: PatternSyntax::RootFilesIn, |
| 397 |
source: PathBuf::from_str("<rootfilesin-matcher>") |
| 398 |
.unwrap(), |
| 399 |
raw: pattern, |
| 400 |
}) |
| 401 |
.collect_vec(); |
| 402 |
build_regex_match_for_debug(&patterns, *glob_suffix).unwrap() |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
fn to_pattern_bytes(&self, syntax: ReSyntax) -> Vec<u8> { |
| 408 |
match syntax { |
| 409 |
ReSyntax::Tidy => self.to_re().to_bytes(), |
| 410 |
ReSyntax::Internal => match self { |
| 411 |
PatternsDesc::Re(re) => re.to_hir().to_string().into_bytes(), |
| 412 |
PatternsDesc::RootFilesIn(dirs, _) => { |
| 413 |
let mut patterns = vec![]; |
| 414 |
patterns.extend(b"rootfilesin: "); |
| 415 |
let mut dirs_vec = dirs.clone(); |
| 416 |
dirs_vec.sort(); |
| 417 |
patterns.extend(dirs_vec.escaped_bytes()); |
| 418 |
patterns |
| 419 |
} |
| 420 |
}, |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
impl core::fmt::Debug for PatternMatcher<'_> { |
| 426 |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 427 |
f.debug_struct("PatternMatcher") |
| 428 |
.field( |
| 429 |
"patterns", |
| 430 |
&String::from_utf8_lossy( |
| 431 |
&self.patterns.to_pattern_bytes(ReSyntax::Internal), |
| 432 |
), |
| 433 |
) |
| 434 |
.field("prefix", &self.prefix) |
| 435 |
.field("files", &self.files) |
| 436 |
.field("dirs", &self.dirs) |
| 437 |
.finish() |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
impl PatternMatcher<'_> { |
| 442 |
pub fn new(file_patterns: Vec<FilePattern>) -> PatternResult<Self> { |
| 443 |
let RootsDirsAndParents { roots, dirs: dirs_explicit, parents } = |
| 444 |
roots_dirs_and_parents(&file_patterns)?; |
| 445 |
let files = roots; |
| 446 |
let dirs = parents; |
| 447 |
let files: HashSet<HgPathBuf> = HashSet::from_iter(files); |
| 448 |
|
| 449 |
let prefix = file_patterns.iter().all(|k| { |
| 450 |
matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath) |
| 451 |
}); |
| 452 |
let (patterns, match_fn) = build_match( |
| 453 |
file_patterns, |
| 454 |
GlobSuffix::Empty, |
| 455 |
RegexCompleteness::ExcludeExactFiles, |
| 456 |
)?; |
| 457 |
|
| 458 |
Ok(Self { patterns, match_fn, prefix, files, dirs, dirs_explicit }) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
impl Matcher for PatternMatcher<'_> { |
| 463 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 464 |
Some(&self.files) |
| 465 |
} |
| 466 |
|
| 467 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 468 |
self.files.contains(filename) |
| 469 |
} |
| 470 |
|
| 471 |
fn matches(&self, filename: &HgPath) -> bool { |
| 472 |
if self.files.contains(filename) { |
| 473 |
return true; |
| 474 |
} |
| 475 |
(self.match_fn)(filename) |
| 476 |
} |
| 477 |
|
| 478 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 479 |
if self.prefix && self.files.contains(directory) { |
| 480 |
return VisitChildrenSet::Recursive; |
| 481 |
} |
| 482 |
if self.dirs.contains(directory) { |
| 483 |
return VisitChildrenSet::This; |
| 484 |
} |
| 485 |
if dir_ancestors(directory).any(|parent_dir| { |
| 486 |
self.files.contains(parent_dir) |
| 487 |
|| self.dirs_explicit.contains(parent_dir) |
| 488 |
}) { |
| 489 |
VisitChildrenSet::This |
| 490 |
} else { |
| 491 |
VisitChildrenSet::Empty |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
fn matches_everything(&self) -> bool { |
| 496 |
false |
| 497 |
} |
| 498 |
|
| 499 |
fn is_exact(&self) -> bool { |
| 500 |
false |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/// A collection of patterns sufficient to construct an `IncludeMatcher`. |
| 505 |
pub struct IncludeMatcherPre { |
| 506 |
patterns: Vec<FilePattern>, |
| 507 |
} |
| 508 |
|
| 509 |
impl IncludeMatcherPre { |
| 510 |
pub fn build_matcher(self) -> PatternResult<IncludeMatcher<'static>> { |
| 511 |
IncludeMatcher::new(self.patterns) |
| 512 |
} |
| 513 |
|
| 514 |
/// Used to print the full hgignore regex in `hg debugignorerhg`. |
| 515 |
pub fn build_debug_matcher( |
| 516 |
self, |
| 517 |
regex_config: RegexCompleteness, |
| 518 |
) -> PatternResult<IncludeMatcher<'static>> { |
| 519 |
IncludeMatcher::new_gen(self.patterns, regex_config) |
| 520 |
} |
| 521 |
|
| 522 |
fn new(patterns: Vec<FilePattern>) -> Self { |
| 523 |
Self { patterns } |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
/// Matches files that are included in the ignore rules. |
| 528 |
/// ``` |
| 529 |
/// use hg::{ |
| 530 |
/// matchers::{IncludeMatcher, Matcher}, |
| 531 |
/// file_patterns::{FilePattern, PatternSyntax}, |
| 532 |
/// utils::hg_path::HgPath |
| 533 |
/// }; |
| 534 |
/// use std::path::Path; |
| 535 |
/// /// |
| 536 |
/// let file_patterns = |
| 537 |
/// vec![FilePattern::new(PatternSyntax::RootGlob, b"this*", Path::new(""))]; |
| 538 |
/// let matcher = IncludeMatcher::new(file_patterns).unwrap(); |
| 539 |
/// /// |
| 540 |
/// assert_eq!(matcher.matches(HgPath::new(b"testing")), false); |
| 541 |
/// assert_eq!(matcher.matches(HgPath::new(b"this should work")), true); |
| 542 |
/// assert_eq!(matcher.matches(HgPath::new(b"this also")), true); |
| 543 |
/// assert_eq!(matcher.matches(HgPath::new(b"but not this")), false); |
| 544 |
/// /// |
| 545 |
/// let file_patterns = |
| 546 |
/// vec![FilePattern::new(PatternSyntax::RootFilesIn, b"dir/subdir", Path::new(""))]; |
| 547 |
/// let matcher = IncludeMatcher::new(file_patterns).unwrap(); |
| 548 |
/// /// |
| 549 |
/// assert!(!matcher.matches(HgPath::new(b"file"))); |
| 550 |
/// assert!(!matcher.matches(HgPath::new(b"dir/file"))); |
| 551 |
/// assert!(matcher.matches(HgPath::new(b"dir/subdir/file"))); |
| 552 |
/// assert!(!matcher.matches(HgPath::new(b"dir/subdir/subsubdir/file"))); |
| 553 |
/// ``` |
| 554 |
pub struct IncludeMatcher<'a> { |
| 555 |
patterns: PatternsDesc, |
| 556 |
match_fn: IgnoreFnType<'a>, |
| 557 |
/// Whether all the patterns match a prefix (i.e. recursively) |
| 558 |
prefix: bool, |
| 559 |
roots: HashSet<HgPathBuf>, |
| 560 |
dirs: HashSet<HgPathBuf>, |
| 561 |
parents: DirsMultiset, |
| 562 |
} |
| 563 |
|
| 564 |
impl core::fmt::Debug for IncludeMatcher<'_> { |
| 565 |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 566 |
f.debug_struct("IncludeMatcher") |
| 567 |
.field( |
| 568 |
"patterns", |
| 569 |
&String::from_utf8_lossy(&self.patterns.to_re().to_bytes()), |
| 570 |
) |
| 571 |
.field("prefix", &self.prefix) |
| 572 |
.field("roots", &self.roots) |
| 573 |
.field("dirs", &self.dirs) |
| 574 |
.field("parents", &self.parents) |
| 575 |
.finish() |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
impl Matcher for IncludeMatcher<'_> { |
| 580 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 581 |
None |
| 582 |
} |
| 583 |
|
| 584 |
fn exact_match(&self, _filename: &HgPath) -> bool { |
| 585 |
false |
| 586 |
} |
| 587 |
|
| 588 |
fn matches(&self, filename: &HgPath) -> bool { |
| 589 |
(self.match_fn)(filename) |
| 590 |
} |
| 591 |
|
| 592 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 593 |
let dir = directory; |
| 594 |
if self.prefix && self.roots.contains(dir) { |
| 595 |
return VisitChildrenSet::Recursive; |
| 596 |
} |
| 597 |
if self.roots.contains(HgPath::new(b"")) |
| 598 |
|| self.roots.contains(dir) |
| 599 |
|| self.dirs.contains(dir) |
| 600 |
|| find_dirs(dir).any(|parent_dir| self.roots.contains(parent_dir)) |
| 601 |
{ |
| 602 |
return VisitChildrenSet::This; |
| 603 |
} |
| 604 |
|
| 605 |
if self.parents.contains(dir.as_ref()) { |
| 606 |
let multiset = self.get_all_parents_children(); |
| 607 |
if let Some(children) = multiset.get(dir) { |
| 608 |
return VisitChildrenSet::Set( |
| 609 |
children.iter().map(HgPathBuf::from).collect(), |
| 610 |
); |
| 611 |
} |
| 612 |
} |
| 613 |
VisitChildrenSet::Empty |
| 614 |
} |
| 615 |
|
| 616 |
fn matches_everything(&self) -> bool { |
| 617 |
false |
| 618 |
} |
| 619 |
|
| 620 |
fn is_exact(&self) -> bool { |
| 621 |
false |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
/// The union of multiple matchers. Will match if any of the matchers match. |
| 626 |
#[derive(Debug)] |
| 627 |
pub struct UnionMatcher<M> { |
| 628 |
matchers: Vec<M>, |
| 629 |
} |
| 630 |
|
| 631 |
impl<M: Matcher> Matcher for UnionMatcher<M> { |
| 632 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 633 |
None |
| 634 |
} |
| 635 |
|
| 636 |
fn exact_match(&self, _filename: &HgPath) -> bool { |
| 637 |
false |
| 638 |
} |
| 639 |
|
| 640 |
fn matches(&self, filename: &HgPath) -> bool { |
| 641 |
self.matchers.iter().any(|m| m.matches(filename)) |
| 642 |
} |
| 643 |
|
| 644 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 645 |
let mut result = HashSet::new(); |
| 646 |
let mut this = false; |
| 647 |
for matcher in self.matchers.iter() { |
| 648 |
let visit = matcher.visit_children_set(directory); |
| 649 |
match visit { |
| 650 |
VisitChildrenSet::Empty => continue, |
| 651 |
VisitChildrenSet::This => { |
| 652 |
this = true; |
| 653 |
// Don't break, we might have an 'all' in here. |
| 654 |
continue; |
| 655 |
} |
| 656 |
VisitChildrenSet::Set(set) => { |
| 657 |
result.extend(set); |
| 658 |
} |
| 659 |
VisitChildrenSet::Recursive => { |
| 660 |
return visit; |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
if this { |
| 665 |
return VisitChildrenSet::This; |
| 666 |
} |
| 667 |
if result.is_empty() { |
| 668 |
VisitChildrenSet::Empty |
| 669 |
} else { |
| 670 |
VisitChildrenSet::Set(result) |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
fn matches_everything(&self) -> bool { |
| 675 |
// TODO Maybe if all are AlwaysMatcher? |
| 676 |
false |
| 677 |
} |
| 678 |
|
| 679 |
fn is_exact(&self) -> bool { |
| 680 |
false |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
impl<M> UnionMatcher<M> { |
| 685 |
pub fn new(matchers: Vec<M>) -> Self { |
| 686 |
Self { matchers } |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
#[derive(Debug)] |
| 691 |
pub struct IntersectionMatcher<M1, M2> { |
| 692 |
m1: M1, |
| 693 |
m2: M2, |
| 694 |
files: Option<HashSet<HgPathBuf>>, |
| 695 |
} |
| 696 |
|
| 697 |
impl<M1: Matcher, M2: Matcher> Matcher for IntersectionMatcher<M1, M2> { |
| 698 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 699 |
self.files.as_ref() |
| 700 |
} |
| 701 |
|
| 702 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 703 |
self.files.as_ref().is_some_and(|f| f.contains(filename)) |
| 704 |
} |
| 705 |
|
| 706 |
fn matches(&self, filename: &HgPath) -> bool { |
| 707 |
self.m1.matches(filename) && self.m2.matches(filename) |
| 708 |
} |
| 709 |
|
| 710 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 711 |
let m1_set = self.m1.visit_children_set(directory); |
| 712 |
if m1_set == VisitChildrenSet::Empty { |
| 713 |
return VisitChildrenSet::Empty; |
| 714 |
} |
| 715 |
let m2_set = self.m2.visit_children_set(directory); |
| 716 |
if m2_set == VisitChildrenSet::Empty { |
| 717 |
return VisitChildrenSet::Empty; |
| 718 |
} |
| 719 |
|
| 720 |
if m1_set == VisitChildrenSet::Recursive { |
| 721 |
return m2_set; |
| 722 |
} else if m2_set == VisitChildrenSet::Recursive { |
| 723 |
return m1_set; |
| 724 |
} |
| 725 |
|
| 726 |
match (&m1_set, &m2_set) { |
| 727 |
(VisitChildrenSet::Recursive, _) => m2_set, |
| 728 |
(_, VisitChildrenSet::Recursive) => m1_set, |
| 729 |
(VisitChildrenSet::This, _) | (_, VisitChildrenSet::This) => { |
| 730 |
VisitChildrenSet::This |
| 731 |
} |
| 732 |
(VisitChildrenSet::Set(m1), VisitChildrenSet::Set(m2)) => { |
| 733 |
let set: HashSet<_> = m1.intersection(m2).cloned().collect(); |
| 734 |
if set.is_empty() { |
| 735 |
VisitChildrenSet::Empty |
| 736 |
} else { |
| 737 |
VisitChildrenSet::Set(set) |
| 738 |
} |
| 739 |
} |
| 740 |
_ => unreachable!(), |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
fn matches_everything(&self) -> bool { |
| 745 |
self.m1.matches_everything() && self.m2.matches_everything() |
| 746 |
} |
| 747 |
|
| 748 |
fn is_exact(&self) -> bool { |
| 749 |
self.m1.is_exact() || self.m2.is_exact() |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
fn filter_fileset<M1: Matcher, M2: Matcher>( |
| 754 |
m1_exact: M1, |
| 755 |
m2: M2, |
| 756 |
) -> Option<HashSet<HgPathBuf>> { |
| 757 |
assert!(m1_exact.is_exact()); |
| 758 |
m1_exact.file_set().map(|m1_files| { |
| 759 |
m1_files.iter().filter(|&f| m2.matches(f)).cloned().collect() |
| 760 |
}) |
| 761 |
} |
| 762 |
|
| 763 |
impl<M1: Matcher, M2: Matcher> IntersectionMatcher<M1, M2> { |
| 764 |
pub fn new(m1: M1, m2: M2) -> Self { |
| 765 |
let files = if m1.is_exact() || m2.is_exact() { |
| 766 |
if !m1.is_exact() { |
| 767 |
filter_fileset(&m2, &m1) |
| 768 |
} else { |
| 769 |
filter_fileset(&m1, &m2) |
| 770 |
} |
| 771 |
} else { |
| 772 |
// without exact input file sets, we can't do an exact |
| 773 |
// intersection, so we must over-approximate by |
| 774 |
// unioning instead |
| 775 |
m1.file_set().map(|m1_files| match m2.file_set() { |
| 776 |
Some(m2_files) => m1_files.union(m2_files).cloned().collect(), |
| 777 |
None => m1_files.iter().cloned().collect(), |
| 778 |
}) |
| 779 |
}; |
| 780 |
Self { m1, m2, files } |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
/// Matches files that correspond to a narrow [`Shape`] |
| 785 |
#[derive(Debug)] |
| 786 |
pub struct ShapeMatcher { |
| 787 |
shape: Shape, |
| 788 |
} |
| 789 |
|
| 790 |
impl ShapeMatcher { |
| 791 |
pub fn new(shape: Shape) -> Self { |
| 792 |
Self { shape } |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
impl Matcher for ShapeMatcher { |
| 797 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 798 |
None |
| 799 |
} |
| 800 |
|
| 801 |
fn exact_match(&self, _filename: &HgPath) -> bool { |
| 802 |
false |
| 803 |
} |
| 804 |
|
| 805 |
fn matches(&self, filename: &HgPath) -> bool { |
| 806 |
let tree = &self.shape.tree; |
| 807 |
if filename.is_empty() { |
| 808 |
return tree.included; |
| 809 |
} |
| 810 |
// Filename matches only if the deepest matching prefix node is included |
| 811 |
deepest_prefix_node(tree, filename).included |
| 812 |
} |
| 813 |
|
| 814 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 815 |
let node = deepest_prefix_node(&self.shape.tree, directory); |
| 816 |
let exact_match = node.path.as_ref() == directory; |
| 817 |
if node.included { |
| 818 |
// Regardless of whether it's an exact or only a prefix match, |
| 819 |
// we've matched the best node and it's included |
| 820 |
VisitChildrenSet::Recursive |
| 821 |
} else if exact_match { |
| 822 |
// This node is excluded |
| 823 |
if node.children.is_empty() { |
| 824 |
// ... and has no relevant children |
| 825 |
VisitChildrenSet::Empty |
| 826 |
} else { |
| 827 |
// ... and has these children, which could be relevant, we |
| 828 |
// don't know yet |
| 829 |
VisitChildrenSet::Set( |
| 830 |
node.children |
| 831 |
.iter() |
| 832 |
.map(|child| child.path.to_owned()) |
| 833 |
.collect(), |
| 834 |
) |
| 835 |
} |
| 836 |
} else { |
| 837 |
// This prefix node is excluded |
| 838 |
VisitChildrenSet::Empty |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
fn matches_everything(&self) -> bool { |
| 843 |
self.shape.tree.included && self.shape.tree.children.is_empty() |
| 844 |
} |
| 845 |
|
| 846 |
fn is_exact(&self) -> bool { |
| 847 |
false |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
#[derive(Debug)] |
| 852 |
pub struct DifferenceMatcher<M1, M2> { |
| 853 |
base: M1, |
| 854 |
excluded: M2, |
| 855 |
files: Option<HashSet<HgPathBuf>>, |
| 856 |
} |
| 857 |
|
| 858 |
impl<M1: Matcher, M2: Matcher> Matcher for DifferenceMatcher<M1, M2> { |
| 859 |
fn file_set(&self) -> Option<&HashSet<HgPathBuf>> { |
| 860 |
self.files.as_ref() |
| 861 |
} |
| 862 |
|
| 863 |
fn exact_match(&self, filename: &HgPath) -> bool { |
| 864 |
self.files.as_ref().is_some_and(|f| f.contains(filename)) |
| 865 |
} |
| 866 |
|
| 867 |
fn matches(&self, filename: &HgPath) -> bool { |
| 868 |
self.base.matches(filename) && !self.excluded.matches(filename) |
| 869 |
} |
| 870 |
|
| 871 |
fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { |
| 872 |
let excluded_set = self.excluded.visit_children_set(directory); |
| 873 |
if excluded_set == VisitChildrenSet::Recursive { |
| 874 |
return VisitChildrenSet::Empty; |
| 875 |
} |
| 876 |
let base_set = self.base.visit_children_set(directory); |
| 877 |
// Possible values for base: 'recursive', 'this', set(...), set() |
| 878 |
// Possible values for excluded: 'this', set(...), set() |
| 879 |
// If excluded has nothing under here that we care about, return base, |
| 880 |
// even if it's 'recursive'. |
| 881 |
if excluded_set == VisitChildrenSet::Empty { |
| 882 |
return base_set; |
| 883 |
} |
| 884 |
match base_set { |
| 885 |
VisitChildrenSet::This | VisitChildrenSet::Recursive => { |
| 886 |
// Never return 'recursive' here if excluded_set is any kind of |
| 887 |
// non-empty (either 'this' or set(foo)), since excluded might |
| 888 |
// return set() for a subdirectory. |
| 889 |
VisitChildrenSet::This |
| 890 |
} |
| 891 |
set => { |
| 892 |
// Possible values for base: set(...), set() |
| 893 |
// Possible values for excluded: 'this', set(...) |
| 894 |
// We ignore excluded set results. They're possibly incorrect: |
| 895 |
// base = path:dir/subdir |
| 896 |
// excluded=rootfilesin:dir, |
| 897 |
// visit_children_set(''): |
| 898 |
// base returns {'dir'}, excluded returns {'dir'}, if we |
| 899 |
// subtracted we'd return set(), which is *not* correct, we |
| 900 |
// still need to visit 'dir'! |
| 901 |
set |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
fn matches_everything(&self) -> bool { |
| 907 |
false |
| 908 |
} |
| 909 |
|
| 910 |
fn is_exact(&self) -> bool { |
| 911 |
self.base.is_exact() |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
impl<M1: Matcher, M2: Matcher> DifferenceMatcher<M1, M2> { |
| 916 |
pub fn new(base: M1, excluded: M2) -> Self { |
| 917 |
let base_is_exact = base.is_exact(); |
| 918 |
let base_files = base.file_set().map(ToOwned::to_owned); |
| 919 |
let mut new = Self { base, excluded, files: None }; |
| 920 |
if base_is_exact { |
| 921 |
new.files = base_files.map(|files| { |
| 922 |
files.iter().filter(|&f| new.matches(f)).cloned().collect() |
| 923 |
}); |
| 924 |
} |
| 925 |
new |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
/// Wraps [`regex::bytes::Regex`] to improve performance in multithreaded |
| 930 |
/// contexts. |
| 931 |
/// |
| 932 |
/// The `status` algorithm makes heavy use of threads, and calling `is_match` |
| 933 |
/// from many threads at once is prone to contention, probably within the |
| 934 |
/// scratch space needed as the regex DFA is built lazily. |
| 935 |
/// |
| 936 |
/// We are in the process of raising the issue upstream, but for now |
| 937 |
/// the workaround used here is to store the `Regex` in a lazily populated |
| 938 |
/// thread-local variable, sharing the initial read-only compilation, but |
| 939 |
/// not the lazy dfa scratch space mentioned above. |
| 940 |
/// |
| 941 |
/// This reduces the contention observed with 16+ threads, but does not |
| 942 |
/// completely remove it. Hopefully this can be addressed upstream. |
| 943 |
struct RegexMatcher { |
| 944 |
/// Compiled at the start of the status algorithm, used as a base for |
| 945 |
/// cloning in each thread-local `self.local`, thus sharing the expensive |
| 946 |
/// first compilation. |
| 947 |
base: regex_automata::meta::Regex, |
| 948 |
/// Thread-local variable that holds the `Regex` that is actually queried |
| 949 |
/// from each thread. |
| 950 |
local: thread_local::ThreadLocal<regex_automata::meta::Regex>, |
| 951 |
} |
| 952 |
|
| 953 |
impl RegexMatcher { |
| 954 |
/// Returns whether the path matches the stored `Regex`. |
| 955 |
pub fn is_match(&self, path: &HgPath) -> bool { |
| 956 |
self.local.get_or(|| self.base.clone()).is_match(path.as_bytes()) |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
/// Returns a function that matches an `HgPath` against the given regex |
| 961 |
/// pattern. |
| 962 |
/// |
| 963 |
/// This can fail when the pattern is invalid or not supported by the |
| 964 |
/// underlying engine (the `regex` crate), for instance anything with |
| 965 |
/// back-references. |
| 966 |
#[tracing::instrument(level = "debug", skip_all)] |
| 967 |
fn re_matcher(pattern: &Hir) -> PatternResult<RegexMatcher> { |
| 968 |
let re = regex_automata::meta::Builder::new() |
| 969 |
.configure( |
| 970 |
Regex::config() |
| 971 |
// Big repos with big `.hgignore` will hit the default limit |
| 972 |
// and incur a significant performance hit. One |
| 973 |
// repo's `hg status` hit multiple *minutes*. |
| 974 |
.dfa_size_limit(Some(50 * (1 << 20))), |
| 975 |
) |
| 976 |
.build_from_hir(pattern) |
| 977 |
.map_err(|e| PatternError::UnsupportedSyntax(e.to_string()))?; |
| 978 |
|
| 979 |
Ok(RegexMatcher { base: re, local: Default::default() }) |
| 980 |
} |
| 981 |
|
| 982 |
#[tracing::instrument(level = "debug", skip_all)] |
| 983 |
/// Returns the regex pattern and a function that matches an `HgPath` against |
| 984 |
/// said regex formed by the given ignore patterns. |
| 985 |
fn build_regex_match<'a>( |
| 986 |
file_patterns: &[FilePattern], |
| 987 |
glob_suffix: GlobSuffix, |
| 988 |
regex_config: RegexCompleteness, |
| 989 |
) -> PatternResult<(PreRegex, IgnoreFnType<'a>)> { |
| 990 |
let mut regexps = vec![]; |
| 991 |
let mut exact_set = HashSet::new(); |
| 992 |
|
| 993 |
for pattern in file_patterns { |
| 994 |
if let Some(re) = |
| 995 |
build_single_regex(pattern, glob_suffix, regex_config)? |
| 996 |
{ |
| 997 |
regexps.push(re); |
| 998 |
} else { |
| 999 |
let exact = normalize_path_bytes(&pattern.raw); |
| 1000 |
exact_set.insert(HgPathBuf::from_bytes(&exact)); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
let is_empty = regexps.is_empty(); |
| 1005 |
|
| 1006 |
let full_regex = PreRegex::Sequence(vec![ |
| 1007 |
PreRegex::parse(&b"^"[..])?, |
| 1008 |
PreRegex::Alternation(regexps), |
| 1009 |
]); |
| 1010 |
|
| 1011 |
// An empty pattern would cause the regex engine to incorrectly match the |
| 1012 |
// (empty) root directory |
| 1013 |
let func = if !is_empty { |
| 1014 |
let matcher = re_matcher(&full_regex.to_hir())?; |
| 1015 |
let func = move |filename: &HgPath| { |
| 1016 |
exact_set.contains(filename) || matcher.is_match(filename) |
| 1017 |
}; |
| 1018 |
Box::new(func) as IgnoreFnType |
| 1019 |
} else { |
| 1020 |
let func = move |filename: &HgPath| exact_set.contains(filename); |
| 1021 |
Box::new(func) as IgnoreFnType |
| 1022 |
}; |
| 1023 |
|
| 1024 |
Ok((full_regex, func)) |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[tracing::instrument(level = "debug", skip_all)] |
| 1028 |
fn build_regex_match_for_debug<'a>( |
| 1029 |
file_patterns: &[FilePattern], |
| 1030 |
glob_suffix: GlobSuffix, |
| 1031 |
) -> PatternResult<PreRegex> { |
| 1032 |
let mut regexps = vec![]; |
| 1033 |
|
| 1034 |
for pattern in file_patterns { |
| 1035 |
if let Some(re) = build_single_regex( |
| 1036 |
pattern, |
| 1037 |
glob_suffix, |
| 1038 |
RegexCompleteness::Complete, |
| 1039 |
)? { |
| 1040 |
regexps.push(re); |
| 1041 |
} else { |
| 1042 |
panic!("RegexCompleteness::Complete should prevent this branch"); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
Ok(PreRegex::Sequence(vec![ |
| 1047 |
PreRegex::parse(&b"^"[..])?, |
| 1048 |
PreRegex::Alternation(regexps), |
| 1049 |
])) |
| 1050 |
} |
| 1051 |
|
| 1052 |
/// Returns roots and directories corresponding to each pattern. |
| 1053 |
/// |
| 1054 |
/// This calculates the roots and directories exactly matching the patterns and |
| 1055 |
/// returns a tuple of (roots, dirs). It does not return other directories |
| 1056 |
/// which may also need to be considered, like the parent directories. |
| 1057 |
fn roots_and_dirs( |
| 1058 |
file_patterns: &[FilePattern], |
| 1059 |
) -> (Vec<HgPathBuf>, Vec<HgPathBuf>) { |
| 1060 |
let mut roots = Vec::new(); |
| 1061 |
let mut dirs = Vec::new(); |
| 1062 |
|
| 1063 |
for ignore_pattern in file_patterns { |
| 1064 |
let FilePattern { syntax, raw: pattern, .. } = ignore_pattern; |
| 1065 |
match syntax { |
| 1066 |
PatternSyntax::RootGlob | PatternSyntax::Glob => { |
| 1067 |
let mut root = HgPathBuf::new(); |
| 1068 |
for p in pattern.split(|c| *c == b'/') { |
| 1069 |
if p.iter().any(|c| matches!(*c, b'[' | b'{' | b'*' | b'?')) |
| 1070 |
{ |
| 1071 |
break; |
| 1072 |
} |
| 1073 |
root.push(HgPathBuf::from_bytes(p).as_ref()); |
| 1074 |
} |
| 1075 |
roots.push(root); |
| 1076 |
} |
| 1077 |
PatternSyntax::Path |
| 1078 |
| PatternSyntax::RelPath |
| 1079 |
| PatternSyntax::FilePath => { |
| 1080 |
let pat = HgPath::new(if pattern == b"." { |
| 1081 |
&[] as &[u8] |
| 1082 |
} else { |
| 1083 |
pattern |
| 1084 |
}); |
| 1085 |
roots.push(pat.to_owned()); |
| 1086 |
} |
| 1087 |
PatternSyntax::RootFilesIn => { |
| 1088 |
let pat = if pattern == b"." { |
| 1089 |
&[] as &[u8] |
| 1090 |
} else { |
| 1091 |
pattern |
| 1092 |
}; |
| 1093 |
dirs.push(HgPathBuf::from_bytes(pat)); |
| 1094 |
} |
| 1095 |
_ => { |
| 1096 |
roots.push(HgPathBuf::new()); |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} |
| 1100 |
(roots, dirs) |
| 1101 |
} |
| 1102 |
|
| 1103 |
/// Paths extracted from patterns |
| 1104 |
#[derive(Debug, PartialEq)] |
| 1105 |
struct RootsDirsAndParents { |
| 1106 |
/// Directories to match recursively |
| 1107 |
pub roots: HashSet<HgPathBuf>, |
| 1108 |
/// Directories to match non-recursively |
| 1109 |
pub dirs: HashSet<HgPathBuf>, |
| 1110 |
/// Implicitly required directories to go to items in either roots or dirs |
| 1111 |
pub parents: DirsMultiset, |
| 1112 |
} |
| 1113 |
|
| 1114 |
/// Extract roots, dirs and parents from patterns. |
| 1115 |
fn roots_dirs_and_parents( |
| 1116 |
file_patterns: &[FilePattern], |
| 1117 |
) -> PatternResult<RootsDirsAndParents> { |
| 1118 |
let (roots, dirs) = roots_and_dirs(file_patterns); |
| 1119 |
|
| 1120 |
let mut parents = DirsMultiset::from_manifest(&dirs)?; |
| 1121 |
|
| 1122 |
for path in &roots { |
| 1123 |
parents.add_path(path)? |
| 1124 |
} |
| 1125 |
|
| 1126 |
Ok(RootsDirsAndParents { |
| 1127 |
roots: HashSet::from_iter(roots), |
| 1128 |
dirs: HashSet::from_iter(dirs), |
| 1129 |
parents, |
| 1130 |
}) |
| 1131 |
} |
| 1132 |
|
| 1133 |
/// Returns a function that checks whether a given file (in the general sense) |
| 1134 |
/// should be matched. |
| 1135 |
fn build_match<'a>( |
| 1136 |
file_patterns: Vec<FilePattern>, |
| 1137 |
glob_suffix: GlobSuffix, |
| 1138 |
regex_config: RegexCompleteness, |
| 1139 |
) -> PatternResult<(PatternsDesc, IgnoreFnType<'a>)> { |
| 1140 |
let mut match_funcs: Vec<IgnoreFnType<'a>> = vec![]; |
| 1141 |
// For debugging and printing |
| 1142 |
let patterns; |
| 1143 |
|
| 1144 |
let (subincludes, file_patterns) = filter_subincludes(file_patterns)?; |
| 1145 |
|
| 1146 |
if !subincludes.is_empty() { |
| 1147 |
// Build prefix-based matcher functions for subincludes |
| 1148 |
let mut submatchers = FastHashMap::default(); |
| 1149 |
let mut prefixes = vec![]; |
| 1150 |
|
| 1151 |
for sub_include in subincludes { |
| 1152 |
let matcher = IncludeMatcher::new(sub_include.included_patterns)?; |
| 1153 |
let match_fn = Box::new(move |path: &HgPath| matcher.matches(path)); |
| 1154 |
prefixes.push(sub_include.prefix.clone()); |
| 1155 |
submatchers.insert(sub_include.prefix.clone(), match_fn); |
| 1156 |
} |
| 1157 |
|
| 1158 |
let match_subinclude = move |filename: &HgPath| { |
| 1159 |
for prefix in prefixes.iter() { |
| 1160 |
if let Some(rel) = filename.relative_to(prefix) { |
| 1161 |
if (submatchers[prefix])(rel) { |
| 1162 |
return true; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
} |
| 1166 |
false |
| 1167 |
}; |
| 1168 |
|
| 1169 |
match_funcs.push(Box::new(match_subinclude)); |
| 1170 |
} |
| 1171 |
|
| 1172 |
// Either do dumb matching if all patterns are rootfiles, or match |
| 1173 |
// with a regex. |
| 1174 |
if file_patterns.iter().all(|k| k.syntax == PatternSyntax::RootFilesIn) { |
| 1175 |
let dirs: HashSet<_> = |
| 1176 |
file_patterns.iter().map(|k| k.raw.to_owned()).collect(); |
| 1177 |
let mut dirs_vec: Vec<_> = dirs.iter().cloned().collect(); |
| 1178 |
|
| 1179 |
let match_func = move |path: &HgPath| -> bool { |
| 1180 |
let path = path.as_bytes(); |
| 1181 |
let i = path.iter().rposition(|a| *a == b'/'); |
| 1182 |
let dir = if let Some(i) = i { &path[..i] } else { b"." }; |
| 1183 |
dirs.contains(dir) |
| 1184 |
}; |
| 1185 |
match_funcs.push(Box::new(match_func)); |
| 1186 |
|
| 1187 |
dirs_vec.sort(); |
| 1188 |
patterns = PatternsDesc::RootFilesIn(dirs_vec, glob_suffix); |
| 1189 |
} else { |
| 1190 |
let (new_re, match_func) = |
| 1191 |
build_regex_match(&file_patterns, glob_suffix, regex_config)?; |
| 1192 |
patterns = PatternsDesc::Re(new_re); |
| 1193 |
match_funcs.push(match_func) |
| 1194 |
} |
| 1195 |
|
| 1196 |
Ok(if match_funcs.len() == 1 { |
| 1197 |
(patterns, match_funcs.remove(0)) |
| 1198 |
} else { |
| 1199 |
( |
| 1200 |
patterns, |
| 1201 |
Box::new(move |f: &HgPath| -> bool { |
| 1202 |
match_funcs.iter().any(|match_func| match_func(f)) |
| 1203 |
}), |
| 1204 |
) |
| 1205 |
}) |
| 1206 |
} |
| 1207 |
|
| 1208 |
/// Parses all "ignore" files with their recursive includes and returns a |
| 1209 |
/// function that checks whether a given file (in the general sense) should be |
| 1210 |
/// ignored. |
| 1211 |
pub fn get_ignore_matcher_pre( |
| 1212 |
mut all_pattern_files: Vec<PathBuf>, |
| 1213 |
root_dir: &Path, |
| 1214 |
inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]), |
| 1215 |
warnings: &HgWarningSender, |
| 1216 |
) -> PatternResult<IncludeMatcherPre> { |
| 1217 |
let mut all_patterns = vec![]; |
| 1218 |
|
| 1219 |
// Sort to make the ordering of calls to `inspect_pattern_bytes` |
| 1220 |
// deterministic even if the ordering of `all_pattern_files` is not (such |
| 1221 |
// as when a iteration order of a Python dict or Rust HashMap is involved). |
| 1222 |
// Sort by "string" representation instead of the default by component |
| 1223 |
// (with a Rust-specific definition of a component) |
| 1224 |
all_pattern_files.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str())); |
| 1225 |
|
| 1226 |
for pattern_file in &all_pattern_files { |
| 1227 |
let patterns = get_patterns_from_file( |
| 1228 |
pattern_file, |
| 1229 |
root_dir, |
| 1230 |
inspect_pattern_bytes, |
| 1231 |
warnings, |
| 1232 |
)?; |
| 1233 |
|
| 1234 |
all_patterns.extend(patterns.to_owned()); |
| 1235 |
} |
| 1236 |
let matcher = IncludeMatcherPre::new(all_patterns); |
| 1237 |
Ok(matcher) |
| 1238 |
} |
| 1239 |
|
| 1240 |
pub fn get_ignore_matcher<'a>( |
| 1241 |
all_pattern_files: Vec<PathBuf>, |
| 1242 |
root_dir: &Path, |
| 1243 |
inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]), |
| 1244 |
warnings: &HgWarningSender, |
| 1245 |
) -> PatternResult<IncludeMatcher<'a>> { |
| 1246 |
let pre_matcher = get_ignore_matcher_pre( |
| 1247 |
all_pattern_files, |
| 1248 |
root_dir, |
| 1249 |
inspect_pattern_bytes, |
| 1250 |
warnings, |
| 1251 |
)?; |
| 1252 |
pre_matcher.build_matcher() |
| 1253 |
} |
| 1254 |
|
| 1255 |
/// Parses all "ignore" files with their recursive includes and returns a |
| 1256 |
/// function that checks whether a given file (in the general sense) should be |
| 1257 |
/// ignored. |
| 1258 |
pub fn get_ignore_function<'a>( |
| 1259 |
all_pattern_files: Vec<PathBuf>, |
| 1260 |
root_dir: &Path, |
| 1261 |
inspect_pattern_bytes: &mut impl FnMut(&Path, &[u8]), |
| 1262 |
warnings: &HgWarningSender, |
| 1263 |
) -> PatternResult<IgnoreFnType<'a>> { |
| 1264 |
let res = get_ignore_matcher( |
| 1265 |
all_pattern_files, |
| 1266 |
root_dir, |
| 1267 |
inspect_pattern_bytes, |
| 1268 |
warnings, |
| 1269 |
); |
| 1270 |
res.map(|matcher| { |
| 1271 |
let res: IgnoreFnType<'a> = |
| 1272 |
Box::new(move |path: &HgPath| matcher.matches(path)); |
| 1273 |
|
| 1274 |
res |
| 1275 |
}) |
| 1276 |
} |
| 1277 |
|
| 1278 |
pub fn get_ignore_files(repo: &Repo) -> Vec<PathBuf> { |
| 1279 |
let mut ignore_files = Vec::new(); |
| 1280 |
let repo_ignore = repo.working_directory_vfs().join(".hgignore"); |
| 1281 |
if repo_ignore.exists() { |
| 1282 |
ignore_files.push(repo_ignore) |
| 1283 |
} |
| 1284 |
for (key, value) in repo.config().iter_section(b"ui") { |
| 1285 |
if key == b"ignore" || key.starts_with(b"ignore.") { |
| 1286 |
let path = get_path_from_bytes(value); |
| 1287 |
let path = shellexpand::path::full_with_context_no_errors( |
| 1288 |
path, |
| 1289 |
home::home_dir, |
| 1290 |
|s| std::env::var(s).ok(), |
| 1291 |
); |
| 1292 |
let joined = repo.working_directory_path().join(path); |
| 1293 |
ignore_files.push(joined); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
ignore_files |
| 1297 |
} |
| 1298 |
|
| 1299 |
impl IncludeMatcher<'_> { |
| 1300 |
fn new_gen( |
| 1301 |
file_patterns: Vec<FilePattern>, |
| 1302 |
regex_config: RegexCompleteness, |
| 1303 |
) -> PatternResult<Self> { |
| 1304 |
let RootsDirsAndParents { roots, dirs, parents } = |
| 1305 |
roots_dirs_and_parents(&file_patterns)?; |
| 1306 |
let prefix = file_patterns.iter().all(|k| { |
| 1307 |
matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath) |
| 1308 |
}); |
| 1309 |
let (patterns, match_fn) = build_match( |
| 1310 |
file_patterns, |
| 1311 |
GlobSuffix::MoreComponents, |
| 1312 |
regex_config, |
| 1313 |
)?; |
| 1314 |
|
| 1315 |
Ok(Self { patterns, match_fn, prefix, roots, dirs, parents }) |
| 1316 |
} |
| 1317 |
|
| 1318 |
pub fn new(file_patterns: Vec<FilePattern>) -> PatternResult<Self> { |
| 1319 |
Self::new_gen(file_patterns, RegexCompleteness::ExcludeExactFiles) |
| 1320 |
} |
| 1321 |
|
| 1322 |
fn get_all_parents_children(&self) -> DirsChildrenMultiset { |
| 1323 |
// TODO cache |
| 1324 |
let thing = self |
| 1325 |
.dirs |
| 1326 |
.iter() |
| 1327 |
.chain(self.roots.iter()) |
| 1328 |
.chain(self.parents.iter()); |
| 1329 |
DirsChildrenMultiset::new(thing, Some(self.parents.iter())) |
| 1330 |
} |
| 1331 |
|
| 1332 |
pub fn debug_get_patterns(&self, syntax: ReSyntax) -> Vec<u8> { |
| 1333 |
self.patterns.to_pattern_bytes(syntax) |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
impl Display for IncludeMatcher<'_> { |
| 1338 |
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { |
| 1339 |
// XXX What about exact matches? |
| 1340 |
// I'm not sure it's worth it to clone the HashSet and keep it |
| 1341 |
// around just in case someone wants to display the matcher, plus |
| 1342 |
// it's going to be unreadable after a few entries, but we need to |
| 1343 |
// inform in this display that exact matches are being used and are |
| 1344 |
// (on purpose) missing from the `includes`. |
| 1345 |
write!( |
| 1346 |
f, |
| 1347 |
"IncludeMatcher(includes='{}')", |
| 1348 |
&String::from_utf8_lossy( |
| 1349 |
&self.patterns.to_pattern_bytes(ReSyntax::Internal) |
| 1350 |
) |
| 1351 |
) |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
#[cfg(test)] |
| 1356 |
mod tests { |
| 1357 |
use std::collections::BTreeMap; |
| 1358 |
use std::collections::BTreeSet; |
| 1359 |
use std::fmt::Debug; |
| 1360 |
use std::path::Path; |
| 1361 |
|
| 1362 |
use pretty_assertions::assert_eq; |
| 1363 |
|
| 1364 |
use super::*; |
| 1365 |
use crate::narrow::shape::StoreShards; |
| 1366 |
|
| 1367 |
#[test] |
| 1368 |
fn test_roots_and_dirs() { |
| 1369 |
let pats = vec![ |
| 1370 |
FilePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")), |
| 1371 |
FilePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")), |
| 1372 |
FilePattern::new(PatternSyntax::Glob, b"g*", Path::new("")), |
| 1373 |
]; |
| 1374 |
let (roots, dirs) = roots_and_dirs(&pats); |
| 1375 |
|
| 1376 |
assert_eq!( |
| 1377 |
roots, |
| 1378 |
vec!( |
| 1379 |
HgPathBuf::from_bytes(b"g/h"), |
| 1380 |
HgPathBuf::from_bytes(b"g/h"), |
| 1381 |
HgPathBuf::new() |
| 1382 |
), |
| 1383 |
); |
| 1384 |
assert_eq!(dirs, vec!()); |
| 1385 |
} |
| 1386 |
|
| 1387 |
#[test] |
| 1388 |
fn test_roots_dirs_and_parents() { |
| 1389 |
let pats = vec![ |
| 1390 |
FilePattern::new(PatternSyntax::Glob, b"g/h/*", Path::new("")), |
| 1391 |
FilePattern::new(PatternSyntax::Glob, b"g/h", Path::new("")), |
| 1392 |
FilePattern::new(PatternSyntax::Glob, b"g*", Path::new("")), |
| 1393 |
]; |
| 1394 |
|
| 1395 |
let mut roots = HashSet::new(); |
| 1396 |
roots.insert(HgPathBuf::from_bytes(b"g/h")); |
| 1397 |
roots.insert(HgPathBuf::new()); |
| 1398 |
|
| 1399 |
let dirs = HashSet::new(); |
| 1400 |
|
| 1401 |
let parents = DirsMultiset::from_manifest(&[ |
| 1402 |
HgPathBuf::from_bytes(b"x"), |
| 1403 |
HgPathBuf::from_bytes(b"g/x"), |
| 1404 |
HgPathBuf::from_bytes(b"g/y"), |
| 1405 |
]) |
| 1406 |
.unwrap(); |
| 1407 |
|
| 1408 |
assert_eq!( |
| 1409 |
roots_dirs_and_parents(&pats).unwrap(), |
| 1410 |
RootsDirsAndParents { roots, dirs, parents } |
| 1411 |
); |
| 1412 |
} |
| 1413 |
|
| 1414 |
#[test] |
| 1415 |
fn test_filematcher_visit_children_set() { |
| 1416 |
// Visitchildrenset |
| 1417 |
let files = vec![HgPathBuf::from_bytes(b"dir/subdir/foo.txt")]; |
| 1418 |
let matcher = FileMatcher::new(files).unwrap(); |
| 1419 |
|
| 1420 |
let mut set = HashSet::new(); |
| 1421 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1422 |
assert_eq!( |
| 1423 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1424 |
VisitChildrenSet::Set(set) |
| 1425 |
); |
| 1426 |
|
| 1427 |
let mut set = HashSet::new(); |
| 1428 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 1429 |
assert_eq!( |
| 1430 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1431 |
VisitChildrenSet::Set(set) |
| 1432 |
); |
| 1433 |
|
| 1434 |
let mut set = HashSet::new(); |
| 1435 |
set.insert(HgPathBuf::from_bytes(b"foo.txt")); |
| 1436 |
assert_eq!( |
| 1437 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1438 |
VisitChildrenSet::Set(set) |
| 1439 |
); |
| 1440 |
|
| 1441 |
assert_eq!( |
| 1442 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1443 |
VisitChildrenSet::Empty |
| 1444 |
); |
| 1445 |
assert_eq!( |
| 1446 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/foo.txt")), |
| 1447 |
VisitChildrenSet::Empty |
| 1448 |
); |
| 1449 |
assert_eq!( |
| 1450 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1451 |
VisitChildrenSet::Empty |
| 1452 |
); |
| 1453 |
} |
| 1454 |
|
| 1455 |
#[test] |
| 1456 |
fn test_filematcher_visit_children_set_files_and_dirs() { |
| 1457 |
let files = vec![ |
| 1458 |
HgPathBuf::from_bytes(b"rootfile.txt"), |
| 1459 |
HgPathBuf::from_bytes(b"a/file1.txt"), |
| 1460 |
HgPathBuf::from_bytes(b"a/b/file2.txt"), |
| 1461 |
// No file in a/b/c |
| 1462 |
HgPathBuf::from_bytes(b"a/b/c/d/file4.txt"), |
| 1463 |
]; |
| 1464 |
let matcher = FileMatcher::new(files).unwrap(); |
| 1465 |
|
| 1466 |
let mut set = HashSet::new(); |
| 1467 |
set.insert(HgPathBuf::from_bytes(b"a")); |
| 1468 |
set.insert(HgPathBuf::from_bytes(b"rootfile.txt")); |
| 1469 |
assert_eq!( |
| 1470 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1471 |
VisitChildrenSet::Set(set) |
| 1472 |
); |
| 1473 |
|
| 1474 |
let mut set = HashSet::new(); |
| 1475 |
set.insert(HgPathBuf::from_bytes(b"b")); |
| 1476 |
set.insert(HgPathBuf::from_bytes(b"file1.txt")); |
| 1477 |
assert_eq!( |
| 1478 |
matcher.visit_children_set(HgPath::new(b"a")), |
| 1479 |
VisitChildrenSet::Set(set) |
| 1480 |
); |
| 1481 |
|
| 1482 |
let mut set = HashSet::new(); |
| 1483 |
set.insert(HgPathBuf::from_bytes(b"c")); |
| 1484 |
set.insert(HgPathBuf::from_bytes(b"file2.txt")); |
| 1485 |
assert_eq!( |
| 1486 |
matcher.visit_children_set(HgPath::new(b"a/b")), |
| 1487 |
VisitChildrenSet::Set(set) |
| 1488 |
); |
| 1489 |
|
| 1490 |
let mut set = HashSet::new(); |
| 1491 |
set.insert(HgPathBuf::from_bytes(b"d")); |
| 1492 |
assert_eq!( |
| 1493 |
matcher.visit_children_set(HgPath::new(b"a/b/c")), |
| 1494 |
VisitChildrenSet::Set(set) |
| 1495 |
); |
| 1496 |
let mut set = HashSet::new(); |
| 1497 |
set.insert(HgPathBuf::from_bytes(b"file4.txt")); |
| 1498 |
assert_eq!( |
| 1499 |
matcher.visit_children_set(HgPath::new(b"a/b/c/d")), |
| 1500 |
VisitChildrenSet::Set(set) |
| 1501 |
); |
| 1502 |
|
| 1503 |
assert_eq!( |
| 1504 |
matcher.visit_children_set(HgPath::new(b"a/b/c/d/e")), |
| 1505 |
VisitChildrenSet::Empty |
| 1506 |
); |
| 1507 |
assert_eq!( |
| 1508 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1509 |
VisitChildrenSet::Empty |
| 1510 |
); |
| 1511 |
} |
| 1512 |
|
| 1513 |
#[test] |
| 1514 |
fn test_patternmatcher() { |
| 1515 |
// VisitdirPrefix |
| 1516 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1517 |
PatternSyntax::Path, |
| 1518 |
b"dir/subdir", |
| 1519 |
Path::new(""), |
| 1520 |
)]) |
| 1521 |
.unwrap(); |
| 1522 |
assert_eq!( |
| 1523 |
m.visit_children_set(HgPath::new(b"")), |
| 1524 |
VisitChildrenSet::This |
| 1525 |
); |
| 1526 |
assert_eq!( |
| 1527 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1528 |
VisitChildrenSet::This |
| 1529 |
); |
| 1530 |
assert_eq!( |
| 1531 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1532 |
VisitChildrenSet::Recursive |
| 1533 |
); |
| 1534 |
// OPT: This should probably be Recursive if its parent is? |
| 1535 |
assert_eq!( |
| 1536 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1537 |
VisitChildrenSet::This |
| 1538 |
); |
| 1539 |
assert_eq!( |
| 1540 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1541 |
VisitChildrenSet::Empty |
| 1542 |
); |
| 1543 |
|
| 1544 |
// VisitchildrensetPrefix |
| 1545 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1546 |
PatternSyntax::Path, |
| 1547 |
b"dir/subdir", |
| 1548 |
Path::new(""), |
| 1549 |
)]) |
| 1550 |
.unwrap(); |
| 1551 |
assert_eq!( |
| 1552 |
m.visit_children_set(HgPath::new(b"")), |
| 1553 |
VisitChildrenSet::This |
| 1554 |
); |
| 1555 |
assert_eq!( |
| 1556 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1557 |
VisitChildrenSet::This |
| 1558 |
); |
| 1559 |
assert_eq!( |
| 1560 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1561 |
VisitChildrenSet::Recursive |
| 1562 |
); |
| 1563 |
// OPT: This should probably be Recursive if its parent is? |
| 1564 |
assert_eq!( |
| 1565 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1566 |
VisitChildrenSet::This |
| 1567 |
); |
| 1568 |
assert_eq!( |
| 1569 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1570 |
VisitChildrenSet::Empty |
| 1571 |
); |
| 1572 |
|
| 1573 |
// VisitdirRootfilesin |
| 1574 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1575 |
PatternSyntax::RootFilesIn, |
| 1576 |
b"dir/subdir", |
| 1577 |
Path::new(""), |
| 1578 |
)]) |
| 1579 |
.unwrap(); |
| 1580 |
assert_eq!( |
| 1581 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1582 |
VisitChildrenSet::This |
| 1583 |
); |
| 1584 |
assert_eq!( |
| 1585 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1586 |
VisitChildrenSet::Empty |
| 1587 |
); |
| 1588 |
assert_eq!( |
| 1589 |
m.visit_children_set(HgPath::new(b"")), |
| 1590 |
VisitChildrenSet::This |
| 1591 |
); |
| 1592 |
assert_eq!( |
| 1593 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1594 |
VisitChildrenSet::This |
| 1595 |
); |
| 1596 |
assert_eq!( |
| 1597 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1598 |
VisitChildrenSet::This |
| 1599 |
); |
| 1600 |
|
| 1601 |
// VisitchildrensetRootfilesin |
| 1602 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1603 |
PatternSyntax::RootFilesIn, |
| 1604 |
b"dir/subdir", |
| 1605 |
Path::new(""), |
| 1606 |
)]) |
| 1607 |
.unwrap(); |
| 1608 |
assert_eq!( |
| 1609 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1610 |
VisitChildrenSet::This |
| 1611 |
); |
| 1612 |
assert_eq!( |
| 1613 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1614 |
VisitChildrenSet::Empty |
| 1615 |
); |
| 1616 |
// FIXME: These should probably be {'dir'}, {'subdir'} and This, |
| 1617 |
// respectively |
| 1618 |
assert_eq!( |
| 1619 |
m.visit_children_set(HgPath::new(b"")), |
| 1620 |
VisitChildrenSet::This |
| 1621 |
); |
| 1622 |
assert_eq!( |
| 1623 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1624 |
VisitChildrenSet::This |
| 1625 |
); |
| 1626 |
assert_eq!( |
| 1627 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1628 |
VisitChildrenSet::This |
| 1629 |
); |
| 1630 |
|
| 1631 |
// VisitdirGlob |
| 1632 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1633 |
PatternSyntax::Glob, |
| 1634 |
b"dir/z*", |
| 1635 |
Path::new(""), |
| 1636 |
)]) |
| 1637 |
.unwrap(); |
| 1638 |
assert_eq!( |
| 1639 |
m.visit_children_set(HgPath::new(b"")), |
| 1640 |
VisitChildrenSet::This |
| 1641 |
); |
| 1642 |
assert_eq!( |
| 1643 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1644 |
VisitChildrenSet::This |
| 1645 |
); |
| 1646 |
assert_eq!( |
| 1647 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1648 |
VisitChildrenSet::Empty |
| 1649 |
); |
| 1650 |
// OPT: these should probably be False. |
| 1651 |
assert_eq!( |
| 1652 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1653 |
VisitChildrenSet::This |
| 1654 |
); |
| 1655 |
assert_eq!( |
| 1656 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1657 |
VisitChildrenSet::This |
| 1658 |
); |
| 1659 |
|
| 1660 |
// VisitchildrensetGlob |
| 1661 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1662 |
PatternSyntax::Glob, |
| 1663 |
b"dir/z*", |
| 1664 |
Path::new(""), |
| 1665 |
)]) |
| 1666 |
.unwrap(); |
| 1667 |
assert_eq!( |
| 1668 |
m.visit_children_set(HgPath::new(b"")), |
| 1669 |
VisitChildrenSet::This |
| 1670 |
); |
| 1671 |
assert_eq!( |
| 1672 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1673 |
VisitChildrenSet::Empty |
| 1674 |
); |
| 1675 |
assert_eq!( |
| 1676 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1677 |
VisitChildrenSet::This |
| 1678 |
); |
| 1679 |
// OPT: these should probably be Empty |
| 1680 |
assert_eq!( |
| 1681 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1682 |
VisitChildrenSet::This |
| 1683 |
); |
| 1684 |
assert_eq!( |
| 1685 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1686 |
VisitChildrenSet::This |
| 1687 |
); |
| 1688 |
|
| 1689 |
// VisitdirFilepath |
| 1690 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1691 |
PatternSyntax::FilePath, |
| 1692 |
b"dir/z", |
| 1693 |
Path::new(""), |
| 1694 |
)]) |
| 1695 |
.unwrap(); |
| 1696 |
assert_eq!( |
| 1697 |
m.visit_children_set(HgPath::new(b"")), |
| 1698 |
VisitChildrenSet::This |
| 1699 |
); |
| 1700 |
assert_eq!( |
| 1701 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1702 |
VisitChildrenSet::This |
| 1703 |
); |
| 1704 |
assert_eq!( |
| 1705 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1706 |
VisitChildrenSet::Empty |
| 1707 |
); |
| 1708 |
assert_eq!( |
| 1709 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1710 |
VisitChildrenSet::Empty |
| 1711 |
); |
| 1712 |
assert_eq!( |
| 1713 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1714 |
VisitChildrenSet::Empty |
| 1715 |
); |
| 1716 |
|
| 1717 |
// VisitchildrensetFilepath |
| 1718 |
let m = PatternMatcher::new(vec![FilePattern::new( |
| 1719 |
PatternSyntax::FilePath, |
| 1720 |
b"dir/z", |
| 1721 |
Path::new(""), |
| 1722 |
)]) |
| 1723 |
.unwrap(); |
| 1724 |
assert_eq!( |
| 1725 |
m.visit_children_set(HgPath::new(b"")), |
| 1726 |
VisitChildrenSet::This |
| 1727 |
); |
| 1728 |
assert_eq!( |
| 1729 |
m.visit_children_set(HgPath::new(b"folder")), |
| 1730 |
VisitChildrenSet::Empty |
| 1731 |
); |
| 1732 |
assert_eq!( |
| 1733 |
m.visit_children_set(HgPath::new(b"dir")), |
| 1734 |
VisitChildrenSet::This |
| 1735 |
); |
| 1736 |
assert_eq!( |
| 1737 |
m.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1738 |
VisitChildrenSet::Empty |
| 1739 |
); |
| 1740 |
assert_eq!( |
| 1741 |
m.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1742 |
VisitChildrenSet::Empty |
| 1743 |
); |
| 1744 |
} |
| 1745 |
|
| 1746 |
#[test] |
| 1747 |
fn test_includematcher() { |
| 1748 |
// VisitchildrensetPrefix |
| 1749 |
let matcher = IncludeMatcher::new(vec![FilePattern::new( |
| 1750 |
PatternSyntax::RelPath, |
| 1751 |
b"dir/subdir", |
| 1752 |
Path::new(""), |
| 1753 |
)]) |
| 1754 |
.unwrap(); |
| 1755 |
|
| 1756 |
let mut set = HashSet::new(); |
| 1757 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1758 |
assert_eq!( |
| 1759 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1760 |
VisitChildrenSet::Set(set) |
| 1761 |
); |
| 1762 |
|
| 1763 |
let mut set = HashSet::new(); |
| 1764 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 1765 |
assert_eq!( |
| 1766 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1767 |
VisitChildrenSet::Set(set) |
| 1768 |
); |
| 1769 |
assert_eq!( |
| 1770 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1771 |
VisitChildrenSet::Recursive |
| 1772 |
); |
| 1773 |
// OPT: This should probably be 'all' if its parent is? |
| 1774 |
assert_eq!( |
| 1775 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1776 |
VisitChildrenSet::This |
| 1777 |
); |
| 1778 |
assert_eq!( |
| 1779 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1780 |
VisitChildrenSet::Empty |
| 1781 |
); |
| 1782 |
|
| 1783 |
// VisitchildrensetRootfilesin |
| 1784 |
let matcher = IncludeMatcher::new(vec![FilePattern::new( |
| 1785 |
PatternSyntax::RootFilesIn, |
| 1786 |
b"dir/subdir", |
| 1787 |
Path::new(""), |
| 1788 |
)]) |
| 1789 |
.unwrap(); |
| 1790 |
|
| 1791 |
let mut set = HashSet::new(); |
| 1792 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1793 |
assert_eq!( |
| 1794 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1795 |
VisitChildrenSet::Set(set) |
| 1796 |
); |
| 1797 |
|
| 1798 |
let mut set = HashSet::new(); |
| 1799 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 1800 |
assert_eq!( |
| 1801 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1802 |
VisitChildrenSet::Set(set) |
| 1803 |
); |
| 1804 |
|
| 1805 |
assert_eq!( |
| 1806 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1807 |
VisitChildrenSet::This |
| 1808 |
); |
| 1809 |
assert_eq!( |
| 1810 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1811 |
VisitChildrenSet::Empty |
| 1812 |
); |
| 1813 |
assert_eq!( |
| 1814 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1815 |
VisitChildrenSet::Empty |
| 1816 |
); |
| 1817 |
|
| 1818 |
// VisitchildrensetGlob |
| 1819 |
let matcher = IncludeMatcher::new(vec![FilePattern::new( |
| 1820 |
PatternSyntax::Glob, |
| 1821 |
b"dir/z*", |
| 1822 |
Path::new(""), |
| 1823 |
)]) |
| 1824 |
.unwrap(); |
| 1825 |
|
| 1826 |
let mut set = HashSet::new(); |
| 1827 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1828 |
assert_eq!( |
| 1829 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1830 |
VisitChildrenSet::Set(set) |
| 1831 |
); |
| 1832 |
assert_eq!( |
| 1833 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1834 |
VisitChildrenSet::Empty |
| 1835 |
); |
| 1836 |
assert_eq!( |
| 1837 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1838 |
VisitChildrenSet::This |
| 1839 |
); |
| 1840 |
// OPT: these should probably be set(). |
| 1841 |
assert_eq!( |
| 1842 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1843 |
VisitChildrenSet::This |
| 1844 |
); |
| 1845 |
assert_eq!( |
| 1846 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1847 |
VisitChildrenSet::This |
| 1848 |
); |
| 1849 |
|
| 1850 |
// VisitchildrensetFilePath |
| 1851 |
let matcher = IncludeMatcher::new(vec![FilePattern::new( |
| 1852 |
PatternSyntax::FilePath, |
| 1853 |
b"dir/z", |
| 1854 |
Path::new(""), |
| 1855 |
)]) |
| 1856 |
.unwrap(); |
| 1857 |
|
| 1858 |
let mut set = HashSet::new(); |
| 1859 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1860 |
assert_eq!( |
| 1861 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1862 |
VisitChildrenSet::Set(set) |
| 1863 |
); |
| 1864 |
assert_eq!( |
| 1865 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1866 |
VisitChildrenSet::Empty |
| 1867 |
); |
| 1868 |
let mut set = HashSet::new(); |
| 1869 |
set.insert(HgPathBuf::from_bytes(b"z")); |
| 1870 |
assert_eq!( |
| 1871 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1872 |
VisitChildrenSet::Set(set) |
| 1873 |
); |
| 1874 |
// OPT: these should probably be set(). |
| 1875 |
assert_eq!( |
| 1876 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1877 |
VisitChildrenSet::Empty |
| 1878 |
); |
| 1879 |
assert_eq!( |
| 1880 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1881 |
VisitChildrenSet::Empty |
| 1882 |
); |
| 1883 |
|
| 1884 |
// Test multiple patterns |
| 1885 |
let matcher = IncludeMatcher::new(vec![ |
| 1886 |
FilePattern::new(PatternSyntax::RelPath, b"foo", Path::new("")), |
| 1887 |
FilePattern::new(PatternSyntax::Glob, b"g*", Path::new("")), |
| 1888 |
]) |
| 1889 |
.unwrap(); |
| 1890 |
|
| 1891 |
assert_eq!( |
| 1892 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1893 |
VisitChildrenSet::This |
| 1894 |
); |
| 1895 |
|
| 1896 |
// Test multiple patterns |
| 1897 |
let matcher = IncludeMatcher::new(vec![FilePattern::new( |
| 1898 |
PatternSyntax::Glob, |
| 1899 |
b"**/*.exe", |
| 1900 |
Path::new(""), |
| 1901 |
)]) |
| 1902 |
.unwrap(); |
| 1903 |
|
| 1904 |
assert_eq!( |
| 1905 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1906 |
VisitChildrenSet::This |
| 1907 |
); |
| 1908 |
} |
| 1909 |
|
| 1910 |
#[test] |
| 1911 |
fn test_unionmatcher() { |
| 1912 |
// Path + Rootfiles |
| 1913 |
let m1 = IncludeMatcher::new(vec![FilePattern::new( |
| 1914 |
PatternSyntax::RelPath, |
| 1915 |
b"dir/subdir", |
| 1916 |
Path::new(""), |
| 1917 |
)]) |
| 1918 |
.unwrap(); |
| 1919 |
let m2 = IncludeMatcher::new(vec![FilePattern::new( |
| 1920 |
PatternSyntax::RootFilesIn, |
| 1921 |
b"dir", |
| 1922 |
Path::new(""), |
| 1923 |
)]) |
| 1924 |
.unwrap(); |
| 1925 |
let matcher = UnionMatcher::new(vec![Box::new(m1), Box::new(m2)]); |
| 1926 |
|
| 1927 |
let mut set = HashSet::new(); |
| 1928 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1929 |
assert_eq!( |
| 1930 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1931 |
VisitChildrenSet::Set(set) |
| 1932 |
); |
| 1933 |
assert_eq!( |
| 1934 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1935 |
VisitChildrenSet::This |
| 1936 |
); |
| 1937 |
assert_eq!( |
| 1938 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1939 |
VisitChildrenSet::Recursive |
| 1940 |
); |
| 1941 |
assert_eq!( |
| 1942 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 1943 |
VisitChildrenSet::Empty |
| 1944 |
); |
| 1945 |
assert_eq!( |
| 1946 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1947 |
VisitChildrenSet::Empty |
| 1948 |
); |
| 1949 |
assert_eq!( |
| 1950 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 1951 |
VisitChildrenSet::Empty |
| 1952 |
); |
| 1953 |
|
| 1954 |
// OPT: These next two could be 'all' instead of 'this'. |
| 1955 |
assert_eq!( |
| 1956 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 1957 |
VisitChildrenSet::This |
| 1958 |
); |
| 1959 |
assert_eq!( |
| 1960 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 1961 |
VisitChildrenSet::This |
| 1962 |
); |
| 1963 |
|
| 1964 |
// Path + unrelated Path |
| 1965 |
let m1 = IncludeMatcher::new(vec![FilePattern::new( |
| 1966 |
PatternSyntax::RelPath, |
| 1967 |
b"dir/subdir", |
| 1968 |
Path::new(""), |
| 1969 |
)]) |
| 1970 |
.unwrap(); |
| 1971 |
let m2 = IncludeMatcher::new(vec![FilePattern::new( |
| 1972 |
PatternSyntax::RelPath, |
| 1973 |
b"folder", |
| 1974 |
Path::new(""), |
| 1975 |
)]) |
| 1976 |
.unwrap(); |
| 1977 |
let matcher = UnionMatcher::new(vec![Box::new(m1), Box::new(m2)]); |
| 1978 |
|
| 1979 |
let mut set = HashSet::new(); |
| 1980 |
set.insert(HgPathBuf::from_bytes(b"folder")); |
| 1981 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 1982 |
assert_eq!( |
| 1983 |
matcher.visit_children_set(HgPath::new(b"")), |
| 1984 |
VisitChildrenSet::Set(set) |
| 1985 |
); |
| 1986 |
let mut set = HashSet::new(); |
| 1987 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 1988 |
assert_eq!( |
| 1989 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 1990 |
VisitChildrenSet::Set(set) |
| 1991 |
); |
| 1992 |
|
| 1993 |
assert_eq!( |
| 1994 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 1995 |
VisitChildrenSet::Recursive |
| 1996 |
); |
| 1997 |
assert_eq!( |
| 1998 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 1999 |
VisitChildrenSet::Empty |
| 2000 |
); |
| 2001 |
|
| 2002 |
assert_eq!( |
| 2003 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2004 |
VisitChildrenSet::Recursive |
| 2005 |
); |
| 2006 |
// OPT: These next two could be 'all' instead of 'this'. |
| 2007 |
assert_eq!( |
| 2008 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2009 |
VisitChildrenSet::This |
| 2010 |
); |
| 2011 |
assert_eq!( |
| 2012 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2013 |
VisitChildrenSet::This |
| 2014 |
); |
| 2015 |
|
| 2016 |
// Path + subpath |
| 2017 |
let m1 = IncludeMatcher::new(vec![FilePattern::new( |
| 2018 |
PatternSyntax::RelPath, |
| 2019 |
b"dir/subdir/x", |
| 2020 |
Path::new(""), |
| 2021 |
)]) |
| 2022 |
.unwrap(); |
| 2023 |
let m2 = IncludeMatcher::new(vec![FilePattern::new( |
| 2024 |
PatternSyntax::RelPath, |
| 2025 |
b"dir/subdir", |
| 2026 |
Path::new(""), |
| 2027 |
)]) |
| 2028 |
.unwrap(); |
| 2029 |
let matcher = UnionMatcher::new(vec![Box::new(m1), Box::new(m2)]); |
| 2030 |
|
| 2031 |
let mut set = HashSet::new(); |
| 2032 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 2033 |
assert_eq!( |
| 2034 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2035 |
VisitChildrenSet::Set(set) |
| 2036 |
); |
| 2037 |
let mut set = HashSet::new(); |
| 2038 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 2039 |
assert_eq!( |
| 2040 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2041 |
VisitChildrenSet::Set(set) |
| 2042 |
); |
| 2043 |
|
| 2044 |
assert_eq!( |
| 2045 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2046 |
VisitChildrenSet::Recursive |
| 2047 |
); |
| 2048 |
assert_eq!( |
| 2049 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2050 |
VisitChildrenSet::Empty |
| 2051 |
); |
| 2052 |
|
| 2053 |
assert_eq!( |
| 2054 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2055 |
VisitChildrenSet::Empty |
| 2056 |
); |
| 2057 |
assert_eq!( |
| 2058 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2059 |
VisitChildrenSet::Recursive |
| 2060 |
); |
| 2061 |
// OPT: this should probably be 'all' not 'this'. |
| 2062 |
assert_eq!( |
| 2063 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2064 |
VisitChildrenSet::This |
| 2065 |
); |
| 2066 |
} |
| 2067 |
|
| 2068 |
#[test] |
| 2069 |
fn test_intersectionmatcher() { |
| 2070 |
// Include path + Include rootfiles |
| 2071 |
let m1 = Box::new( |
| 2072 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2073 |
PatternSyntax::RelPath, |
| 2074 |
b"dir/subdir", |
| 2075 |
Path::new(""), |
| 2076 |
)]) |
| 2077 |
.unwrap(), |
| 2078 |
); |
| 2079 |
let m2 = Box::new( |
| 2080 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2081 |
PatternSyntax::RootFilesIn, |
| 2082 |
b"dir", |
| 2083 |
Path::new(""), |
| 2084 |
)]) |
| 2085 |
.unwrap(), |
| 2086 |
); |
| 2087 |
let matcher = IntersectionMatcher::new(m1, m2); |
| 2088 |
|
| 2089 |
let mut set = HashSet::new(); |
| 2090 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 2091 |
assert_eq!( |
| 2092 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2093 |
VisitChildrenSet::Set(set) |
| 2094 |
); |
| 2095 |
assert_eq!( |
| 2096 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2097 |
VisitChildrenSet::This |
| 2098 |
); |
| 2099 |
assert_eq!( |
| 2100 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2101 |
VisitChildrenSet::Empty |
| 2102 |
); |
| 2103 |
assert_eq!( |
| 2104 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2105 |
VisitChildrenSet::Empty |
| 2106 |
); |
| 2107 |
assert_eq!( |
| 2108 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2109 |
VisitChildrenSet::Empty |
| 2110 |
); |
| 2111 |
assert_eq!( |
| 2112 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2113 |
VisitChildrenSet::Empty |
| 2114 |
); |
| 2115 |
assert_eq!( |
| 2116 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2117 |
VisitChildrenSet::Empty |
| 2118 |
); |
| 2119 |
|
| 2120 |
// Non intersecting paths |
| 2121 |
let m1 = Box::new( |
| 2122 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2123 |
PatternSyntax::RelPath, |
| 2124 |
b"dir/subdir", |
| 2125 |
Path::new(""), |
| 2126 |
)]) |
| 2127 |
.unwrap(), |
| 2128 |
); |
| 2129 |
let m2 = Box::new( |
| 2130 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2131 |
PatternSyntax::RelPath, |
| 2132 |
b"folder", |
| 2133 |
Path::new(""), |
| 2134 |
)]) |
| 2135 |
.unwrap(), |
| 2136 |
); |
| 2137 |
let matcher = IntersectionMatcher::new(m1, m2); |
| 2138 |
|
| 2139 |
assert_eq!( |
| 2140 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2141 |
VisitChildrenSet::Empty |
| 2142 |
); |
| 2143 |
assert_eq!( |
| 2144 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2145 |
VisitChildrenSet::Empty |
| 2146 |
); |
| 2147 |
assert_eq!( |
| 2148 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2149 |
VisitChildrenSet::Empty |
| 2150 |
); |
| 2151 |
assert_eq!( |
| 2152 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2153 |
VisitChildrenSet::Empty |
| 2154 |
); |
| 2155 |
assert_eq!( |
| 2156 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2157 |
VisitChildrenSet::Empty |
| 2158 |
); |
| 2159 |
assert_eq!( |
| 2160 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2161 |
VisitChildrenSet::Empty |
| 2162 |
); |
| 2163 |
assert_eq!( |
| 2164 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2165 |
VisitChildrenSet::Empty |
| 2166 |
); |
| 2167 |
|
| 2168 |
// Nested paths |
| 2169 |
let m1 = Box::new( |
| 2170 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2171 |
PatternSyntax::RelPath, |
| 2172 |
b"dir/subdir/x", |
| 2173 |
Path::new(""), |
| 2174 |
)]) |
| 2175 |
.unwrap(), |
| 2176 |
); |
| 2177 |
let m2 = Box::new( |
| 2178 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2179 |
PatternSyntax::RelPath, |
| 2180 |
b"dir/subdir", |
| 2181 |
Path::new(""), |
| 2182 |
)]) |
| 2183 |
.unwrap(), |
| 2184 |
); |
| 2185 |
let matcher = IntersectionMatcher::new(m1, m2); |
| 2186 |
|
| 2187 |
let mut set = HashSet::new(); |
| 2188 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 2189 |
assert_eq!( |
| 2190 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2191 |
VisitChildrenSet::Set(set) |
| 2192 |
); |
| 2193 |
|
| 2194 |
let mut set = HashSet::new(); |
| 2195 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 2196 |
assert_eq!( |
| 2197 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2198 |
VisitChildrenSet::Set(set) |
| 2199 |
); |
| 2200 |
let mut set = HashSet::new(); |
| 2201 |
set.insert(HgPathBuf::from_bytes(b"x")); |
| 2202 |
assert_eq!( |
| 2203 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2204 |
VisitChildrenSet::Set(set) |
| 2205 |
); |
| 2206 |
assert_eq!( |
| 2207 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2208 |
VisitChildrenSet::Empty |
| 2209 |
); |
| 2210 |
assert_eq!( |
| 2211 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2212 |
VisitChildrenSet::Empty |
| 2213 |
); |
| 2214 |
assert_eq!( |
| 2215 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2216 |
VisitChildrenSet::Empty |
| 2217 |
); |
| 2218 |
// OPT: this should probably be 'all' not 'this'. |
| 2219 |
assert_eq!( |
| 2220 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2221 |
VisitChildrenSet::This |
| 2222 |
); |
| 2223 |
|
| 2224 |
// Diverging paths |
| 2225 |
let m1 = Box::new( |
| 2226 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2227 |
PatternSyntax::RelPath, |
| 2228 |
b"dir/subdir/x", |
| 2229 |
Path::new(""), |
| 2230 |
)]) |
| 2231 |
.unwrap(), |
| 2232 |
); |
| 2233 |
let m2 = Box::new( |
| 2234 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2235 |
PatternSyntax::RelPath, |
| 2236 |
b"dir/subdir/z", |
| 2237 |
Path::new(""), |
| 2238 |
)]) |
| 2239 |
.unwrap(), |
| 2240 |
); |
| 2241 |
let matcher = IntersectionMatcher::new(m1, m2); |
| 2242 |
|
| 2243 |
// OPT: these next two could probably be Empty as well. |
| 2244 |
let mut set = HashSet::new(); |
| 2245 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 2246 |
assert_eq!( |
| 2247 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2248 |
VisitChildrenSet::Set(set) |
| 2249 |
); |
| 2250 |
// OPT: these next two could probably be Empty as well. |
| 2251 |
let mut set = HashSet::new(); |
| 2252 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 2253 |
assert_eq!( |
| 2254 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2255 |
VisitChildrenSet::Set(set) |
| 2256 |
); |
| 2257 |
assert_eq!( |
| 2258 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2259 |
VisitChildrenSet::Empty |
| 2260 |
); |
| 2261 |
assert_eq!( |
| 2262 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2263 |
VisitChildrenSet::Empty |
| 2264 |
); |
| 2265 |
assert_eq!( |
| 2266 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2267 |
VisitChildrenSet::Empty |
| 2268 |
); |
| 2269 |
assert_eq!( |
| 2270 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2271 |
VisitChildrenSet::Empty |
| 2272 |
); |
| 2273 |
assert_eq!( |
| 2274 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2275 |
VisitChildrenSet::Empty |
| 2276 |
); |
| 2277 |
} |
| 2278 |
|
| 2279 |
#[test] |
| 2280 |
fn test_differencematcher() { |
| 2281 |
// Two alwaysmatchers should function like a nevermatcher |
| 2282 |
let m1 = AlwaysMatcher; |
| 2283 |
let m2 = AlwaysMatcher; |
| 2284 |
let matcher = DifferenceMatcher::new(Box::new(m1), Box::new(m2)); |
| 2285 |
|
| 2286 |
for case in &[ |
| 2287 |
&b""[..], |
| 2288 |
b"dir", |
| 2289 |
b"dir/subdir", |
| 2290 |
b"dir/subdir/z", |
| 2291 |
b"dir/foo", |
| 2292 |
b"dir/subdir/x", |
| 2293 |
b"folder", |
| 2294 |
] { |
| 2295 |
assert_eq!( |
| 2296 |
matcher.visit_children_set(HgPath::new(case)), |
| 2297 |
VisitChildrenSet::Empty |
| 2298 |
); |
| 2299 |
} |
| 2300 |
|
| 2301 |
// One always and one never should behave the same as an always |
| 2302 |
let m1 = AlwaysMatcher; |
| 2303 |
let m2 = NeverMatcher; |
| 2304 |
let matcher = DifferenceMatcher::new(Box::new(m1), Box::new(m2)); |
| 2305 |
|
| 2306 |
for case in &[ |
| 2307 |
&b""[..], |
| 2308 |
b"dir", |
| 2309 |
b"dir/subdir", |
| 2310 |
b"dir/subdir/z", |
| 2311 |
b"dir/foo", |
| 2312 |
b"dir/subdir/x", |
| 2313 |
b"folder", |
| 2314 |
] { |
| 2315 |
assert_eq!( |
| 2316 |
matcher.visit_children_set(HgPath::new(case)), |
| 2317 |
VisitChildrenSet::Recursive |
| 2318 |
); |
| 2319 |
} |
| 2320 |
|
| 2321 |
// Two include matchers |
| 2322 |
let m1 = Box::new( |
| 2323 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2324 |
PatternSyntax::RelPath, |
| 2325 |
b"dir/subdir", |
| 2326 |
Path::new("/repo"), |
| 2327 |
)]) |
| 2328 |
.unwrap(), |
| 2329 |
); |
| 2330 |
let m2 = Box::new( |
| 2331 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2332 |
PatternSyntax::RootFilesIn, |
| 2333 |
b"dir", |
| 2334 |
Path::new("/repo"), |
| 2335 |
)]) |
| 2336 |
.unwrap(), |
| 2337 |
); |
| 2338 |
|
| 2339 |
let matcher = DifferenceMatcher::new(m1, m2); |
| 2340 |
|
| 2341 |
let mut set = HashSet::new(); |
| 2342 |
set.insert(HgPathBuf::from_bytes(b"dir")); |
| 2343 |
assert_eq!( |
| 2344 |
matcher.visit_children_set(HgPath::new(b"")), |
| 2345 |
VisitChildrenSet::Set(set) |
| 2346 |
); |
| 2347 |
|
| 2348 |
let mut set = HashSet::new(); |
| 2349 |
set.insert(HgPathBuf::from_bytes(b"subdir")); |
| 2350 |
assert_eq!( |
| 2351 |
matcher.visit_children_set(HgPath::new(b"dir")), |
| 2352 |
VisitChildrenSet::Set(set) |
| 2353 |
); |
| 2354 |
assert_eq!( |
| 2355 |
matcher.visit_children_set(HgPath::new(b"dir/subdir")), |
| 2356 |
VisitChildrenSet::Recursive |
| 2357 |
); |
| 2358 |
assert_eq!( |
| 2359 |
matcher.visit_children_set(HgPath::new(b"dir/foo")), |
| 2360 |
VisitChildrenSet::Empty |
| 2361 |
); |
| 2362 |
assert_eq!( |
| 2363 |
matcher.visit_children_set(HgPath::new(b"folder")), |
| 2364 |
VisitChildrenSet::Empty |
| 2365 |
); |
| 2366 |
assert_eq!( |
| 2367 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/z")), |
| 2368 |
VisitChildrenSet::This |
| 2369 |
); |
| 2370 |
assert_eq!( |
| 2371 |
matcher.visit_children_set(HgPath::new(b"dir/subdir/x")), |
| 2372 |
VisitChildrenSet::This |
| 2373 |
); |
| 2374 |
} |
| 2375 |
|
| 2376 |
mod invariants { |
| 2377 |
pub mod visit_children_set { |
| 2378 |
|
| 2379 |
use crate::matchers::tests::Tree; |
| 2380 |
use crate::matchers::Matcher; |
| 2381 |
use crate::matchers::VisitChildrenSet; |
| 2382 |
use crate::utils::hg_path::HgPath; |
| 2383 |
|
| 2384 |
#[allow(dead_code)] |
| 2385 |
#[derive(Debug)] |
| 2386 |
struct Error<'a, M> { |
| 2387 |
matcher: &'a M, |
| 2388 |
path: &'a HgPath, |
| 2389 |
matching: &'a Tree, |
| 2390 |
visit_children_set: &'a VisitChildrenSet, |
| 2391 |
} |
| 2392 |
|
| 2393 |
fn holds( |
| 2394 |
matching: &Tree, |
| 2395 |
not_matching: &Tree, |
| 2396 |
vcs: &VisitChildrenSet, |
| 2397 |
) -> bool { |
| 2398 |
match vcs { |
| 2399 |
VisitChildrenSet::Empty => matching.is_empty(), |
| 2400 |
VisitChildrenSet::This => { |
| 2401 |
// `This` does not come with any obligations. |
| 2402 |
true |
| 2403 |
} |
| 2404 |
VisitChildrenSet::Recursive => { |
| 2405 |
// `Recursive` requires that *everything* in the |
| 2406 |
// subtree matches. This |
| 2407 |
// requirement is relied on for example in |
| 2408 |
// DifferenceMatcher implementation. |
| 2409 |
not_matching.is_empty() |
| 2410 |
} |
| 2411 |
VisitChildrenSet::Set(allowed_children) => { |
| 2412 |
// `allowed_children` does not distinguish between |
| 2413 |
// files and directories: if it's not included, it |
| 2414 |
// must not be matched. |
| 2415 |
for k in matching.dirs.keys() { |
| 2416 |
if !(allowed_children.contains(k)) { |
| 2417 |
return false; |
| 2418 |
} |
| 2419 |
} |
| 2420 |
for k in matching.files.iter() { |
| 2421 |
if !(allowed_children.contains(k)) { |
| 2422 |
return false; |
| 2423 |
} |
| 2424 |
} |
| 2425 |
true |
| 2426 |
} |
| 2427 |
} |
| 2428 |
} |
| 2429 |
|
| 2430 |
pub fn check<M: Matcher + std::fmt::Debug>( |
| 2431 |
matcher: &M, |
| 2432 |
path: &HgPath, |
| 2433 |
matching: &Tree, |
| 2434 |
not_matching: &Tree, |
| 2435 |
visit_children_set: &VisitChildrenSet, |
| 2436 |
) { |
| 2437 |
if !holds(matching, not_matching, visit_children_set) { |
| 2438 |
panic!( |
| 2439 |
"{:#?}", |
| 2440 |
Error { matcher, path, visit_children_set, matching } |
| 2441 |
) |
| 2442 |
} |
| 2443 |
} |
| 2444 |
} |
| 2445 |
} |
| 2446 |
|
| 2447 |
#[derive(Debug, Clone)] |
| 2448 |
pub struct Tree { |
| 2449 |
files: BTreeSet<HgPathBuf>, |
| 2450 |
dirs: BTreeMap<HgPathBuf, Tree>, |
| 2451 |
} |
| 2452 |
|
| 2453 |
impl Tree { |
| 2454 |
fn len(&self) -> usize { |
| 2455 |
let mut n = 0; |
| 2456 |
n += self.files.len(); |
| 2457 |
for d in self.dirs.values() { |
| 2458 |
n += d.len(); |
| 2459 |
} |
| 2460 |
n |
| 2461 |
} |
| 2462 |
|
| 2463 |
fn is_empty(&self) -> bool { |
| 2464 |
self.files.is_empty() && self.dirs.is_empty() |
| 2465 |
} |
| 2466 |
|
| 2467 |
fn make( |
| 2468 |
files: BTreeSet<HgPathBuf>, |
| 2469 |
dirs: BTreeMap<HgPathBuf, Tree>, |
| 2470 |
) -> Self { |
| 2471 |
Self { |
| 2472 |
files, |
| 2473 |
dirs: dirs |
| 2474 |
.into_iter() |
| 2475 |
.filter(|(_k, v)| (!(v.is_empty()))) |
| 2476 |
.collect(), |
| 2477 |
} |
| 2478 |
} |
| 2479 |
|
| 2480 |
fn filter_and_check<M: Matcher + Debug>( |
| 2481 |
&self, |
| 2482 |
m: &M, |
| 2483 |
path: &HgPath, |
| 2484 |
) -> (Self, Self) { |
| 2485 |
let (files1, files2): (BTreeSet<HgPathBuf>, BTreeSet<HgPathBuf>) = |
| 2486 |
self.files |
| 2487 |
.iter() |
| 2488 |
.map(|v| v.to_owned()) |
| 2489 |
.partition(|v| m.matches(&path.join(v))); |
| 2490 |
let (dirs1, dirs2): ( |
| 2491 |
BTreeMap<HgPathBuf, Tree>, |
| 2492 |
BTreeMap<HgPathBuf, Tree>, |
| 2493 |
) = self |
| 2494 |
.dirs |
| 2495 |
.iter() |
| 2496 |
.map(|(k, v)| { |
| 2497 |
let path = path.join(k); |
| 2498 |
let (t1, t2) = v.filter_and_check(m, &path); |
| 2499 |
((k.clone(), t1), (k.clone(), t2)) |
| 2500 |
}) |
| 2501 |
.unzip(); |
| 2502 |
let matching = Self::make(files1, dirs1); |
| 2503 |
let not_matching = Self::make(files2, dirs2); |
| 2504 |
let vcs = m.visit_children_set(path); |
| 2505 |
invariants::visit_children_set::check( |
| 2506 |
m, |
| 2507 |
path, |
| 2508 |
&matching, |
| 2509 |
¬_matching, |
| 2510 |
&vcs, |
| 2511 |
); |
| 2512 |
(matching, not_matching) |
| 2513 |
} |
| 2514 |
|
| 2515 |
fn check_matcher<M: Matcher + Debug>( |
| 2516 |
&self, |
| 2517 |
m: &M, |
| 2518 |
expect_count: usize, |
| 2519 |
) { |
| 2520 |
let res = self.filter_and_check(m, &HgPathBuf::new()); |
| 2521 |
if expect_count != res.0.len() { |
| 2522 |
eprintln!( |
| 2523 |
"warning: expected {} matches, got {} for {:#?}", |
| 2524 |
expect_count, |
| 2525 |
res.0.len(), |
| 2526 |
m |
| 2527 |
); |
| 2528 |
} |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
fn mkdir(children: &[(&[u8], &Tree)]) -> Tree { |
| 2533 |
let p = HgPathBuf::from_bytes; |
| 2534 |
let names = [ |
| 2535 |
p(b"a"), |
| 2536 |
p(b"b.txt"), |
| 2537 |
p(b"file.txt"), |
| 2538 |
p(b"c.c"), |
| 2539 |
p(b"c.h"), |
| 2540 |
p(b"dir1"), |
| 2541 |
p(b"dir2"), |
| 2542 |
p(b"subdir"), |
| 2543 |
]; |
| 2544 |
let files: BTreeSet<HgPathBuf> = BTreeSet::from(names); |
| 2545 |
let dirs = |
| 2546 |
children.iter().map(|(name, t)| (p(name), (*t).clone())).collect(); |
| 2547 |
Tree { files, dirs } |
| 2548 |
} |
| 2549 |
|
| 2550 |
fn make_example_tree() -> Tree { |
| 2551 |
let leaf = mkdir(&[]); |
| 2552 |
let abc = mkdir(&[(b"d", &leaf)]); |
| 2553 |
let ab = mkdir(&[(b"c", &abc)]); |
| 2554 |
let a = mkdir(&[(b"b", &ab)]); |
| 2555 |
let dir = mkdir(&[(b"subdir", &leaf), (b"subdir.c", &leaf)]); |
| 2556 |
mkdir(&[(b"dir", &dir), (b"dir1", &dir), (b"dir2", &dir), (b"a", &a)]) |
| 2557 |
} |
| 2558 |
|
| 2559 |
#[test] |
| 2560 |
fn test_pattern_matcher_visit_children_set() { |
| 2561 |
let tree = make_example_tree(); |
| 2562 |
let pattern_dir1_glob_c = PatternMatcher::new(vec![FilePattern::new( |
| 2563 |
PatternSyntax::Glob, |
| 2564 |
b"dir1/*.c", |
| 2565 |
Path::new(""), |
| 2566 |
)]) |
| 2567 |
.unwrap(); |
| 2568 |
let pattern_dir1 = || { |
| 2569 |
PatternMatcher::new(vec![FilePattern::new( |
| 2570 |
PatternSyntax::Path, |
| 2571 |
b"dir1", |
| 2572 |
Path::new(""), |
| 2573 |
)]) |
| 2574 |
.unwrap() |
| 2575 |
}; |
| 2576 |
let pattern_dir1_a = PatternMatcher::new(vec![FilePattern::new( |
| 2577 |
PatternSyntax::Glob, |
| 2578 |
b"dir1/a", |
| 2579 |
Path::new(""), |
| 2580 |
)]) |
| 2581 |
.unwrap(); |
| 2582 |
let pattern_relglob_c = || { |
| 2583 |
PatternMatcher::new(vec![FilePattern::new( |
| 2584 |
PatternSyntax::RelGlob, |
| 2585 |
b"*.c", |
| 2586 |
Path::new(""), |
| 2587 |
)]) |
| 2588 |
.unwrap() |
| 2589 |
}; |
| 2590 |
let files = vec![HgPathBuf::from_bytes(b"dir/subdir/b.txt")]; |
| 2591 |
let file_dir_subdir_b = FileMatcher::new(files).unwrap(); |
| 2592 |
|
| 2593 |
let files = vec![ |
| 2594 |
HgPathBuf::from_bytes(b"file.txt"), |
| 2595 |
HgPathBuf::from_bytes(b"a/file.txt"), |
| 2596 |
HgPathBuf::from_bytes(b"a/b/file.txt"), |
| 2597 |
// No file in a/b/c |
| 2598 |
HgPathBuf::from_bytes(b"a/b/c/d/file.txt"), |
| 2599 |
]; |
| 2600 |
let file_abcdfile = FileMatcher::new(files).unwrap(); |
| 2601 |
let rootfilesin_dir = PatternMatcher::new(vec![FilePattern::new( |
| 2602 |
PatternSyntax::RootFilesIn, |
| 2603 |
b"dir", |
| 2604 |
Path::new(""), |
| 2605 |
)]) |
| 2606 |
.unwrap(); |
| 2607 |
|
| 2608 |
let pattern_filepath_dir_subdir = |
| 2609 |
PatternMatcher::new(vec![FilePattern::new( |
| 2610 |
PatternSyntax::FilePath, |
| 2611 |
b"dir/subdir", |
| 2612 |
Path::new(""), |
| 2613 |
)]) |
| 2614 |
.unwrap(); |
| 2615 |
|
| 2616 |
let include_dir_subdir = IncludeMatcher::new(vec![FilePattern::new( |
| 2617 |
PatternSyntax::RelPath, |
| 2618 |
b"dir/subdir", |
| 2619 |
Path::new(""), |
| 2620 |
)]) |
| 2621 |
.unwrap(); |
| 2622 |
|
| 2623 |
let more_includematchers = [ |
| 2624 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2625 |
PatternSyntax::Glob, |
| 2626 |
b"dir/s*", |
| 2627 |
Path::new(""), |
| 2628 |
)]) |
| 2629 |
.unwrap(), |
| 2630 |
// Test multiple patterns |
| 2631 |
IncludeMatcher::new(vec![ |
| 2632 |
FilePattern::new(PatternSyntax::RelPath, b"dir", Path::new("")), |
| 2633 |
FilePattern::new(PatternSyntax::Glob, b"s*", Path::new("")), |
| 2634 |
]) |
| 2635 |
.unwrap(), |
| 2636 |
// Test multiple patterns |
| 2637 |
IncludeMatcher::new(vec![FilePattern::new( |
| 2638 |
PatternSyntax::Glob, |
| 2639 |
b"**/*.c", |
| 2640 |
Path::new(""), |
| 2641 |
)]) |
| 2642 |
.unwrap(), |
| 2643 |
]; |
| 2644 |
|
| 2645 |
tree.check_matcher(&pattern_dir1(), 25); |
| 2646 |
tree.check_matcher(&pattern_dir1_a, 1); |
| 2647 |
tree.check_matcher(&pattern_dir1_glob_c, 2); |
| 2648 |
tree.check_matcher(&pattern_relglob_c(), 14); |
| 2649 |
tree.check_matcher(&AlwaysMatcher, 112); |
| 2650 |
tree.check_matcher(&NeverMatcher, 0); |
| 2651 |
tree.check_matcher( |
| 2652 |
&IntersectionMatcher::new( |
| 2653 |
Box::new(pattern_relglob_c()), |
| 2654 |
Box::new(pattern_dir1()), |
| 2655 |
), |
| 2656 |
3, |
| 2657 |
); |
| 2658 |
tree.check_matcher( |
| 2659 |
&UnionMatcher::new(vec![ |
| 2660 |
Box::new(pattern_relglob_c()), |
| 2661 |
Box::new(pattern_dir1()), |
| 2662 |
]), |
| 2663 |
36, |
| 2664 |
); |
| 2665 |
tree.check_matcher( |
| 2666 |
&DifferenceMatcher::new( |
| 2667 |
Box::new(pattern_relglob_c()), |
| 2668 |
Box::new(pattern_dir1()), |
| 2669 |
), |
| 2670 |
11, |
| 2671 |
); |
| 2672 |
tree.check_matcher(&file_dir_subdir_b, 1); |
| 2673 |
tree.check_matcher(&file_abcdfile, 4); |
| 2674 |
tree.check_matcher(&rootfilesin_dir, 8); |
| 2675 |
tree.check_matcher(&pattern_filepath_dir_subdir, 1); |
| 2676 |
tree.check_matcher(&include_dir_subdir, 9); |
| 2677 |
tree.check_matcher(&more_includematchers[0], 17); |
| 2678 |
tree.check_matcher(&more_includematchers[1], 25); |
| 2679 |
tree.check_matcher(&more_includematchers[2], 35); |
| 2680 |
} |
| 2681 |
|
| 2682 |
#[test] |
| 2683 |
fn test_shape_matcher() { |
| 2684 |
// This test is quite simple to help with debugging (and was useful) |
| 2685 |
// when writing the matcher. More complex cases can be found in the |
| 2686 |
// `shape` module. |
| 2687 |
let config = r#" |
| 2688 |
version = 0 |
| 2689 |
[[shards]] |
| 2690 |
name = "a" |
| 2691 |
paths = ["a"] |
| 2692 |
[[shards]] |
| 2693 |
name = "ab" |
| 2694 |
paths = ["a/b"] |
| 2695 |
[[shards]] |
| 2696 |
name = "abc" |
| 2697 |
paths = ["a/b/c"] |
| 2698 |
[[shards]] |
| 2699 |
name = "test" |
| 2700 |
requires = ["a", "abc"] |
| 2701 |
shape = true |
| 2702 |
[[shards]] |
| 2703 |
name = "default" |
| 2704 |
requires = ["base"] |
| 2705 |
shape = true |
| 2706 |
"#; |
| 2707 |
|
| 2708 |
let store_shards = |
| 2709 |
StoreShards::from_config(toml::from_str(config).unwrap()).unwrap(); |
| 2710 |
let shape = store_shards.shape("test").unwrap().unwrap(); |
| 2711 |
let matcher = ShapeMatcher::new(shape); |
| 2712 |
assert!(matcher.matches(HgPath::new("a"))); |
| 2713 |
assert!(!matcher.matches(HgPath::new("a/b"))); |
| 2714 |
assert!(matcher.matches(HgPath::new("a/b/c"))); |
| 2715 |
assert!(matcher.matches(HgPath::new("a/b/c/d/e/f/nested thing"))); |
| 2716 |
assert!(!matcher.matches(HgPath::new("rootfile"))); |
| 2717 |
let shape = store_shards.shape("default").unwrap().unwrap(); |
| 2718 |
let matcher = ShapeMatcher::new(shape); |
| 2719 |
assert!(!matcher.matches(HgPath::new("a"))); |
| 2720 |
assert!(matcher.matches(HgPath::new("rootfile"))); |
| 2721 |
assert!(matcher.matches(HgPath::new("otherdir/file"))); |
| 2722 |
} |
| 2723 |
} |