##// END OF EJS Templates
help: add a mechanism to change flags' help depending on config...
help: add a mechanism to change flags' help depending on config It seems reasonable to have a similar mechanism for the rest of the help, but no such thing is implemented. The goal is to make the help of commands clearer in the presence of significant default changes, like tweakdefaults or with company-wide hgrcs. In these cases, a user looking at the help of a command doesn't exactly know what his hgrc is doing. Apply to this to the --git option of commands that display diffs, as this option in particular causes confusion for some reason. Differential Revision: https://phab.mercurial-scm.org/D8100

File last commit:

r44458:72bced4f default
r44777:142d2a4c default
Show More
matchers.rs
179 lines | 6.0 KiB | application/rls-services+xml | RustLexer
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 // matchers.rs
//
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Structs and types for matching files and directories.
Raphaël Gomès
rust-matchers: add `FileMatcher` implementation...
r44366 use crate::{utils::hg_path::HgPath, DirsMultiset, DirstateMapError};
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 use std::collections::HashSet;
Raphaël Gomès
rust-matchers: add `FileMatcher` implementation...
r44366 use std::iter::FromIterator;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742
Raphaël Gomès
rust-matchers: improve `Matcher` trait ergonomics...
r44284 pub enum VisitChildrenSet<'a> {
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Don't visit anything
Empty,
/// Only visit this directory
This,
/// Visit this directory and these subdirectories
/// TODO Should we implement a `NonEmptyHashSet`?
Raphaël Gomès
rust-matchers: improve `Matcher` trait ergonomics...
r44284 Set(HashSet<&'a HgPath>),
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Visit this directory and all subdirectories
Recursive,
}
pub trait Matcher {
/// Explicitly listed files
Raphaël Gomès
rust-matchers: improve `Matcher` trait ergonomics...
r44284 fn file_set(&self) -> Option<&HashSet<&HgPath>>;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Returns whether `filename` is in `file_set`
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Returns whether `filename` is matched by this matcher
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn matches(&self, filename: impl AsRef<HgPath>) -> bool;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Decides whether a directory should be visited based on whether it
/// has potential matches in it or one of its subdirectories, and
/// potentially lists which subdirectories of that directory should be
/// visited. This is based on the match's primary, included, and excluded
/// patterns.
///
/// # Example
///
/// Assume matchers `['path:foo/bar', 'rootfilesin:qux']`, we would
/// return the following values (assuming the implementation of
/// visit_children_set is capable of recognizing this; some implementations
/// are not).
///
Georges Racinet
rust-matchers: fixing cargo doc...
r44458 /// ```text
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// ```ignore
/// '' -> {'foo', 'qux'}
/// 'baz' -> set()
/// 'foo' -> {'bar'}
/// // Ideally this would be `Recursive`, but since the prefix nature of
/// // matchers is applied to the entire matcher, we have to downgrade this
/// // to `This` due to the (yet to be implemented in Rust) non-prefix
/// // `RootFilesIn'-kind matcher being mixed in.
/// 'foo/bar' -> 'this'
/// 'qux' -> 'this'
/// ```
/// # Important
///
/// Most matchers do not know if they're representing files or
/// directories. They see `['path:dir/f']` and don't know whether `f` is a
/// file or a directory, so `visit_children_set('dir')` for most matchers
/// will return `HashSet{ HgPath { "f" } }`, but if the matcher knows it's
/// a file (like the yet to be implemented in Rust `ExactMatcher` does),
/// it may return `VisitChildrenSet::This`.
/// Do not rely on the return being a `HashSet` indicating that there are
/// no files in this dir to investigate (or equivalently that if there are
/// files to investigate in 'dir' that it will always return
/// `VisitChildrenSet::This`).
fn visit_children_set(
&self,
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 directory: impl AsRef<HgPath>,
) -> VisitChildrenSet;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Matcher will match everything and `files_set()` will be empty:
/// optimization might be possible.
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn matches_everything(&self) -> bool;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 /// Matcher will match exactly the files in `files_set()`: optimization
/// might be possible.
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn is_exact(&self) -> bool;
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 }
/// Matches everything.
Raphaël Gomès
rust-matchers: add doctests for `AlwaysMatcher`...
r44286 ///```
/// use hg::{ matchers::{Matcher, AlwaysMatcher}, utils::hg_path::HgPath };
///
/// let matcher = AlwaysMatcher;
///
Raphaël Gomès
rust-matchers: add `FileMatcher` implementation...
r44366 /// assert_eq!(matcher.matches(HgPath::new(b"whatever")), true);
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), true);
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), true);
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
Raphaël Gomès
rust-matchers: add doctests for `AlwaysMatcher`...
r44286 /// ```
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 #[derive(Debug)]
pub struct AlwaysMatcher;
impl Matcher for AlwaysMatcher {
Raphaël Gomès
rust-matchers: improve `Matcher` trait ergonomics...
r44284 fn file_set(&self) -> Option<&HashSet<&HgPath>> {
None
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 }
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn exact_match(&self, _filename: impl AsRef<HgPath>) -> bool {
false
}
fn matches(&self, _filename: impl AsRef<HgPath>) -> bool {
true
}
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 fn visit_children_set(
&self,
_directory: impl AsRef<HgPath>,
) -> VisitChildrenSet {
VisitChildrenSet::Recursive
}
Raphaël Gomès
rust-matchers: remove default implementations for `Matcher` trait...
r44009 fn matches_everything(&self) -> bool {
true
}
fn is_exact(&self) -> bool {
false
}
Raphaël Gomès
rust-matchers: add `Matcher` trait and implement `AlwaysMatcher`...
r43742 }
Raphaël Gomès
rust-matchers: add `FileMatcher` implementation...
r44366
/// Matches the input files exactly. They are interpreted as paths, not
/// patterns.
///
///```
/// use hg::{ matchers::{Matcher, FileMatcher}, utils::hg_path::HgPath };
///
/// let files = [HgPath::new(b"a.txt"), HgPath::new(br"re:.*\.c$")];
/// let matcher = FileMatcher::new(&files).unwrap();
///
/// assert_eq!(matcher.matches(HgPath::new(b"a.txt")), true);
/// assert_eq!(matcher.matches(HgPath::new(b"b.txt")), false);
/// assert_eq!(matcher.matches(HgPath::new(b"main.c")), false);
/// assert_eq!(matcher.matches(HgPath::new(br"re:.*\.c$")), true);
/// ```
#[derive(Debug)]
pub struct FileMatcher<'a> {
files: HashSet<&'a HgPath>,
dirs: DirsMultiset,
}
impl<'a> FileMatcher<'a> {
pub fn new(
files: &'a [impl AsRef<HgPath>],
) -> Result<Self, DirstateMapError> {
Ok(Self {
files: HashSet::from_iter(files.iter().map(|f| f.as_ref())),
dirs: DirsMultiset::from_manifest(files)?,
})
}
fn inner_matches(&self, filename: impl AsRef<HgPath>) -> bool {
self.files.contains(filename.as_ref())
}
}
impl<'a> Matcher for FileMatcher<'a> {
fn file_set(&self) -> Option<&HashSet<&HgPath>> {
Some(&self.files)
}
fn exact_match(&self, filename: impl AsRef<HgPath>) -> bool {
self.inner_matches(filename)
}
fn matches(&self, filename: impl AsRef<HgPath>) -> bool {
self.inner_matches(filename)
}
fn visit_children_set(
&self,
_directory: impl AsRef<HgPath>,
) -> VisitChildrenSet {
// TODO implement once we have `status.traverse`
// This is useless until unknown files are taken into account
// Which will not need to happen before the `IncludeMatcher`.
unimplemented!()
}
fn matches_everything(&self) -> bool {
false
}
fn is_exact(&self) -> bool {
true
}
}