watchexec/crates/filterer/globset/src/lib.rs

202 lines
6.0 KiB
Rust
Raw Normal View History

2022-06-15 05:25:05 +02:00
//! A path-only Watchexec 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 used as the default filterer in Watchexec CLI currently.
#![doc(html_favicon_url = "https://watchexec.github.io/logo:watchexec.svg")]
#![doc(html_logo_url = "https://watchexec.github.io/logo:watchexec.svg")]
#![warn(clippy::unwrap_used, missing_docs)]
#![deny(rust_2018_idioms)]
2022-06-15 05:25:05 +02:00
use std::{
ffi::OsString,
path::{Path, PathBuf},
2022-06-15 05:25:05 +02:00
};
2021-10-16 05:26:29 +02:00
use ignore::gitignore::{Gitignore, GitignoreBuilder};
2022-06-15 05:25:05 +02:00
use ignore_files::{Error, IgnoreFile, IgnoreFilter};
2021-10-27 14:03:24 +02:00
use tracing::{debug, trace, trace_span};
2022-06-15 05:25:05 +02:00
use watchexec::{
error::RuntimeError,
event::{Event, FileType, Priority},
filter::Filterer,
};
use watchexec_filterer_ignore::IgnoreFilterer;
2022-06-15 05:25:05 +02:00
/// A simple filterer in the style of the watchexec v1.17 filter.
2021-09-29 17:03:46 +02:00
#[derive(Debug)]
pub struct GlobsetFilterer {
#[cfg_attr(not(unix), allow(dead_code))]
origin: PathBuf,
2021-10-16 05:26:29 +02:00
filters: Gitignore,
ignores: Gitignore,
ignore_files: IgnoreFilterer,
2021-10-16 05:37:29 +02:00
extensions: Vec<OsString>,
2021-10-16 05:26:29 +02:00
}
impl GlobsetFilterer {
2021-10-16 05:37:29 +02:00
/// Create a new `GlobsetFilterer` from a project origin, allowed extensions, and lists of globs.
2021-10-16 05:26:29 +02:00
///
/// 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.
///
/// Ignores and filters are passed as a tuple of the glob pattern as a string and an optional
/// path of the folder the pattern should apply in (e.g. the folder a gitignore file is in).
/// A `None` to the latter will mark the pattern as being global.
///
2021-10-16 05:37:29 +02:00
/// The extensions list is used to filter files by extension.
///
2021-10-16 05:26:29 +02:00
/// Non-path events are always passed.
2023-01-06 14:53:49 +01:00
#[allow(clippy::future_not_send)]
pub async fn new(
2021-10-16 05:26:29 +02:00
origin: impl AsRef<Path>,
filters: impl IntoIterator<Item = (String, Option<PathBuf>)>,
ignores: impl IntoIterator<Item = (String, Option<PathBuf>)>,
ignore_files: impl IntoIterator<Item = IgnoreFile>,
extensions: impl IntoIterator<Item = OsString>,
2022-06-15 05:25:05 +02:00
) -> Result<Self, Error> {
let origin = origin.as_ref();
2022-09-07 04:15:38 +02:00
let mut filters_builder = GitignoreBuilder::new(origin);
let mut ignores_builder = GitignoreBuilder::new(origin);
2021-10-16 05:26:29 +02:00
for (filter, in_path) in filters {
trace!(filter=?&filter, "add filter to globset filterer");
2022-01-16 04:09:53 +01:00
filters_builder
.add_line(in_path.clone(), &filter)
2022-06-15 05:25:05 +02:00
.map_err(|err| Error::Glob { file: in_path, err })?;
2021-10-16 05:26:29 +02:00
}
for (ignore, in_path) in ignores {
trace!(ignore=?&ignore, "add ignore to globset filterer");
2022-01-16 04:09:53 +01:00
ignores_builder
.add_line(in_path.clone(), &ignore)
2022-06-15 05:25:05 +02:00
.map_err(|err| Error::Glob { file: in_path, err })?;
2021-10-16 05:26:29 +02:00
}
2022-01-16 04:09:53 +01:00
let filters = filters_builder
.build()
2022-06-15 05:25:05 +02:00
.map_err(|err| Error::Glob { file: None, err })?;
2022-01-16 04:09:53 +01:00
let ignores = ignores_builder
.build()
2022-06-15 05:25:05 +02:00
.map_err(|err| Error::Glob { file: None, err })?;
let extensions: Vec<OsString> = extensions.into_iter().collect();
2022-01-16 04:09:53 +01:00
let mut ignore_files =
2022-06-15 05:25:05 +02:00
IgnoreFilter::new(origin, &ignore_files.into_iter().collect::<Vec<_>>()).await?;
ignore_files.finish();
2022-06-15 05:25:05 +02:00
let ignore_files = IgnoreFilterer(ignore_files);
2021-10-16 05:26:29 +02:00
debug!(
?origin,
2021-10-16 05:26:29 +02:00
num_filters=%filters.num_ignores(),
num_neg_filters=%filters.num_whitelists(),
num_ignores=%ignores.num_ignores(),
2022-06-15 05:25:05 +02:00
num_in_ignore_files=?ignore_files.0.num_ignores(),
2021-10-16 05:26:29 +02:00
num_neg_ignores=%ignores.num_whitelists(),
2021-10-16 05:37:29 +02:00
num_extensions=%extensions.len(),
2021-10-16 05:26:29 +02:00
"globset filterer built");
2021-10-16 05:37:29 +02:00
Ok(Self {
origin: origin.into(),
2021-10-16 05:37:29 +02:00
filters,
ignores,
ignore_files,
2021-10-16 05:37:29 +02:00
extensions,
})
2021-10-16 05:26:29 +02:00
}
}
impl Filterer for GlobsetFilterer {
2021-10-16 15:32:43 +02:00
/// Filter an event.
///
/// This implementation never errors.
fn check_event(&self, event: &Event, priority: Priority) -> Result<bool, RuntimeError> {
2021-10-27 14:03:24 +02:00
let _span = trace_span!("filterer_check").entered();
{
trace!("checking internal ignore filterer");
2022-01-16 04:09:53 +01:00
if !self
.ignore_files
.check_event(event, priority)
2022-01-16 04:09:53 +01:00
.expect("IgnoreFilterer never errors")
{
trace!("internal ignore filterer matched (fail)");
return Ok(false);
}
}
2022-01-28 13:54:03 +01:00
let mut paths = event.paths().peekable();
if paths.peek().is_none() {
trace!("non-path event (pass)");
Ok(true)
} else {
Ok(paths.any(|(path, file_type)| {
let _span = trace_span!("path", ?path).entered();
2023-01-06 14:53:49 +01:00
let is_dir = file_type.map_or(false, |t| matches!(t, FileType::Dir));
if self.ignores.matched(path, is_dir).is_ignore() {
trace!("ignored by globset ignore");
return false;
}
let mut filtered = false;
if self.filters.num_ignores() > 0 {
trace!("running through glob filters");
filtered = true;
if self.filters.matched(path, is_dir).is_ignore() {
trace!("allowed by globset filters");
return true;
}
// Watchexec 1.x bug, TODO remove at 2.0
#[cfg(unix)]
if let Ok(based) = path.strip_prefix(&self.origin) {
let rebased = {
use std::path::MAIN_SEPARATOR;
let mut b = self.origin.clone().into_os_string();
b.push(PathBuf::from(String::from(MAIN_SEPARATOR)));
b.push(PathBuf::from(String::from(MAIN_SEPARATOR)));
b.push(based.as_os_str());
b
};
trace!(?rebased, "testing on rebased path, 1.x bug compat (#258)");
if self.filters.matched(rebased, is_dir).is_ignore() {
trace!("allowed by globset filters, 1.x bug compat (#258)");
return true;
}
}
2021-10-16 05:37:29 +02:00
}
if !self.extensions.is_empty() {
trace!("running through extension filters");
filtered = true;
if is_dir {
trace!("failed on extension check due to being a dir");
return false;
}
if let Some(ext) = path.extension() {
if self.extensions.iter().any(|e| e == ext) {
trace!("allowed by extension filter");
return true;
}
} else {
trace!(
?path,
"failed on extension check due to having no extension"
);
return false;
2021-10-16 05:37:29 +02:00
}
}
2021-10-16 05:26:29 +02:00
!filtered
}))
}
}
}