watchexec/lib/src/filter/globset.rs

143 lines
4.2 KiB
Rust
Raw Normal View History

2021-10-16 05:26:29 +02:00
//! A simple filterer in the style of the watchexec v1 filter.
use std::ffi::OsString;
use std::path::{Path, PathBuf};
2021-10-16 05:26:29 +02:00
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use tokio::fs::read_to_string;
2021-10-27 14:03:24 +02:00
use tracing::{debug, trace, trace_span};
use crate::error::RuntimeError;
use crate::event::{Event, FileType};
use crate::filter::Filterer;
2022-01-15 03:12:32 +01:00
use crate::ignore::IgnoreFile;
2021-10-16 05:26:29 +02:00
/// 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.
2021-09-29 17:03:46 +02:00
#[derive(Debug)]
pub struct GlobsetFilterer {
2021-10-16 05:26:29 +02:00
filters: Gitignore,
ignores: Gitignore,
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.
///
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.
pub 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>)>,
extensions: impl IntoIterator<Item = OsString>,
) -> Result<Self, ignore::Error> {
2021-10-16 05:26:29 +02:00
let mut filters_builder = GitignoreBuilder::new(origin);
let mut ignores_builder = filters_builder.clone();
for (filter, in_path) in filters {
trace!(filter=?&filter, "add filter to globset filterer");
filters_builder.add_line(in_path, &filter)?;
2021-10-16 05:26:29 +02:00
}
for (ignore, in_path) in ignores {
trace!(ignore=?&ignore, "add ignore to globset filterer");
ignores_builder.add_line(in_path, &ignore)?;
2021-10-16 05:26:29 +02:00
}
let filters = filters_builder.build()?;
let ignores = ignores_builder.build()?;
let extensions: Vec<OsString> = extensions.into_iter().collect();
2021-10-16 05:26:29 +02:00
debug!(
num_filters=%filters.num_ignores(),
num_neg_filters=%filters.num_whitelists(),
num_ignores=%ignores.num_ignores(),
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 {
filters,
ignores,
extensions,
})
2021-10-16 05:26:29 +02:00
}
/// Produces a list of ignore patterns compatible with [`new`][GlobsetFilterer::new()] from an [`IgnoreFile`].
pub async fn list_from_ignore_file(
ig: &IgnoreFile,
) -> Result<Vec<(String, Option<PathBuf>)>, RuntimeError> {
let content = read_to_string(&ig.path).await?;
let lines = content.lines();
let mut ignores = Vec::with_capacity(lines.size_hint().0);
for line in lines {
if line.is_empty() || line.starts_with('#') {
continue;
}
ignores.push((line.to_owned(), ig.applies_in.clone()));
}
Ok(ignores)
}
}
impl Filterer for GlobsetFilterer {
2021-10-16 15:32:43 +02:00
/// Filter an event.
///
/// This implementation never errors.
2021-10-16 05:26:29 +02:00
fn check_event(&self, event: &Event) -> Result<bool, RuntimeError> {
2022-01-11 12:59:39 +01:00
// TODO: integrate ignore::Filter
2021-10-27 14:03:24 +02:00
let _span = trace_span!("filterer_check").entered();
2021-10-16 05:26:29 +02:00
for (path, file_type) in event.paths() {
2021-10-27 14:03:24 +02:00
let _span = trace_span!("path", ?path).entered();
let is_dir = file_type
.map(|t| matches!(t, FileType::Dir))
.unwrap_or(false);
2021-10-16 05:26:29 +02:00
if self.ignores.matched(path, is_dir).is_ignore() {
2021-10-27 14:03:24 +02:00
trace!("ignored by globset ignore");
2021-10-16 05:26:29 +02:00
return Ok(false);
}
if self.filters.num_ignores() > 0 && !self.filters.matched(path, is_dir).is_ignore() {
2021-10-27 14:03:24 +02:00
trace!("ignored by globset filters");
2021-10-16 05:26:29 +02:00
return Ok(false);
}
2021-10-16 05:37:29 +02:00
if !self.extensions.is_empty() {
if is_dir {
2021-10-27 14:03:24 +02:00
trace!("omitted from extension check due to being a dir");
2021-10-16 05:37:29 +02:00
continue;
}
if let Some(ext) = path.extension() {
if !self.extensions.iter().any(|e| e == ext) {
2021-10-27 14:03:24 +02:00
trace!("ignored by extension filter");
2021-10-16 05:37:29 +02:00
return Ok(false);
}
} else {
trace!(
?path,
"omitted from extension check due to having no extension"
);
continue;
}
}
2021-10-16 05:26:29 +02:00
}
Ok(true)
}
}