Add globset filterer

This commit is contained in:
Félix Saparelli 2021-10-16 16:26:29 +13:00
parent b2f4d0f244
commit 19b27959ed
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
3 changed files with 86 additions and 8 deletions

View file

@ -96,9 +96,9 @@ impl Event {
}
/// Return all paths in the event's tags.
pub fn paths(&self) -> impl Iterator<Item = &Path> {
pub fn paths(&self) -> impl Iterator<Item = (&Path, Option<&FileType>)> {
self.tags.iter().filter_map(|p| match p {
Tag::Path { path, .. } => Some(path.as_path()),
Tag::Path { path, file_type } => Some((path.as_path(), file_type.as_ref())),
_ => None,
})
}

View file

@ -1,18 +1,89 @@
//! The watchexec v1 filter implementation, using globset.
//! A simple filterer in the style of the watchexec v1 filter.
use std::path::PathBuf;
use std::path::Path;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use tracing::{debug, trace};
use crate::error::RuntimeError;
use crate::event::Event;
use crate::filter::Filterer;
/// A path-only filterer based on globsets.
///
/// This filterer mimics the behavior of the `watchexec` v1 filter, but does not match it exactly,
/// due to differing internals. It is intended to be used as a stopgap until the tagged filter
/// reaches a stable state or becomes the default. As such it does not have an updatable
/// configuration.
#[derive(Debug)]
pub struct GlobsetFilterer {
_root: PathBuf,
filters: Gitignore,
ignores: Gitignore,
}
impl GlobsetFilterer {
/// Create a new `GlobsetFilterer` from a project origin and two lists of glob patterns.
///
/// The first list is used to filter paths (only matching paths will pass the filter), the
/// second is used to ignore paths (matching paths will fail the pattern). If the filter list is
/// empty, only the ignore list will be used. If both lists are empty, the filter always passes.
///
/// Non-path events are always passed.
pub fn new<FI, F, II, P>(
origin: impl AsRef<Path>,
filters: FI,
ignores: II,
) -> Result<Self, ignore::Error>
where
FI: IntoIterator<Item = (F, Option<P>)>,
F: AsRef<str>,
II: IntoIterator<Item = (F, Option<P>)>,
P: AsRef<Path>,
{
let mut filters_builder = GitignoreBuilder::new(origin);
let mut ignores_builder = filters_builder.clone();
for (filter, in_path) in filters {
let filter = filter.as_ref();
trace!(filter, "add filter to globset filterer");
filters_builder.add_line(in_path.map(|p| p.as_ref().to_owned()), filter)?;
}
for (ignore, in_path) in ignores {
let ignore = ignore.as_ref();
trace!(ignore, "add ignore to globset filterer");
ignores_builder.add_line(in_path.map(|p| p.as_ref().to_owned()), ignore)?;
}
let filters = filters_builder.build()?;
let ignores = ignores_builder.build()?;
debug!(
num_filters=%filters.num_ignores(),
num_neg_filters=%filters.num_whitelists(),
num_ignores=%ignores.num_ignores(),
num_neg_ignores=%ignores.num_whitelists(),
"globset filterer built");
Ok(Self { filters, ignores })
}
}
impl Filterer for GlobsetFilterer {
fn check_event(&self, _event: &Event) -> Result<bool, RuntimeError> {
todo!()
fn check_event(&self, event: &Event) -> Result<bool, RuntimeError> {
for (path, file_type) in event.paths() {
let is_dir = file_type.map(|t| t.is_dir()).unwrap_or(false);
if self.ignores.matched(path, is_dir).is_ignore() {
trace!(?path, "ignored by globset ignore");
return Ok(false);
}
if self.filters.num_ignores() > 0 && !self.filters.matched(path, is_dir).is_ignore() {
trace!(?path, "ignored by globset filters");
return Ok(false);
}
}
Ok(true)
}
}

View file

@ -60,6 +60,8 @@ where
/// - `CREATED` -> `Create(_)`
/// - `RENAMED` -> `Modify(Name(_))`
/// - `OTHERWISE_CHANGED` -> anything else
/// - plus `COMMON_PATH` if there is a common prefix of all paths, in which case all paths are also
/// truncated to omit that common prefix.
///
/// It ignores non-path events and pathed events without event kind.
pub fn summarise_events_to_env<I, E>(events: I) -> HashMap<&'static OsStr, OsString>
@ -72,14 +74,17 @@ where
#[cfg(not(unix))]
const ENV_SEP: &str = ";";
let mut all_paths = Vec::new();
let mut kind_buckets = HashMap::new();
for event in events {
let event = event.as_ref();
let paths = event.paths().map(|p| p.to_owned()).collect::<Vec<_>>();
let paths = event.paths().map(|(p, _)| p.to_owned()).collect::<Vec<_>>();
if paths.is_empty() {
continue;
}
all_paths.extend(paths.clone());
// usually there's only one but just in case
for kind in event.tags.iter().filter_map(|t| {
if let Tag::FileEventKind(kind) = t {
@ -95,6 +100,8 @@ where
}
}
let common_path = common_prefix(all_paths);
let mut grouped_buckets = HashMap::new();
for (kind, paths) in kind_buckets {
use notify::event::{AccessKind::*, AccessMode::*, EventKind::*, ModifyKind::*};