2022-01-11 12:59:39 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use futures::stream::{FuturesUnordered, StreamExt};
|
|
|
|
use ignore::{
|
2022-06-15 05:25:05 +02:00
|
|
|
gitignore::{Gitignore, GitignoreBuilder, Glob},
|
2022-01-11 12:59:39 +01:00
|
|
|
Match,
|
|
|
|
};
|
|
|
|
use tokio::fs::read_to_string;
|
|
|
|
use tracing::{trace, trace_span};
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
use crate::{Error, IgnoreFile};
|
2022-01-11 12:59:39 +01:00
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
/// A mutable filter dedicated to ignore files and trees of ignore files.
|
2022-01-11 12:59:39 +01:00
|
|
|
///
|
|
|
|
/// This reads and compiles ignore files, and should be used for handling ignore files. It's created
|
|
|
|
/// with a project origin and a list of ignore files, and new ignore files can be added later
|
2022-06-07 11:19:16 +02:00
|
|
|
/// (unless [`finish`](IgnoreFilterer::finish()) is called).
|
2022-01-16 02:49:14 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2022-06-15 05:25:05 +02:00
|
|
|
pub struct IgnoreFilter {
|
2022-01-11 12:59:39 +01:00
|
|
|
origin: PathBuf,
|
|
|
|
builder: Option<GitignoreBuilder>,
|
|
|
|
compiled: Gitignore,
|
|
|
|
}
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
impl IgnoreFilter {
|
2022-01-16 02:49:14 +01:00
|
|
|
/// Create a new empty filterer.
|
|
|
|
///
|
|
|
|
/// Prefer [`new()`](IgnoreFilterer::new()) if you have ignore files ready to use.
|
|
|
|
pub fn empty(origin: impl AsRef<Path>) -> Self {
|
|
|
|
let origin = origin.as_ref();
|
|
|
|
Self {
|
|
|
|
builder: Some(GitignoreBuilder::new(origin)),
|
|
|
|
origin: origin.to_owned(),
|
|
|
|
compiled: Gitignore::empty(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:59:39 +01:00
|
|
|
/// Read ignore files from disk and load them for filtering.
|
2022-01-16 02:49:14 +01:00
|
|
|
///
|
|
|
|
/// Use [`empty()`](IgnoreFilterer::empty()) if you want an empty filterer,
|
|
|
|
/// or to construct one outside an async environment.
|
2023-01-06 14:53:49 +01:00
|
|
|
pub async fn new(origin: impl AsRef<Path> + Send, files: &[IgnoreFile]) -> Result<Self, Error> {
|
2022-01-15 11:46:06 +01:00
|
|
|
let origin = origin.as_ref();
|
|
|
|
let _span = trace_span!("build_filterer", ?origin);
|
|
|
|
|
|
|
|
trace!(files=%files.len(), "loading file contents");
|
2022-01-11 12:59:39 +01:00
|
|
|
let (files_contents, errors): (Vec<_>, Vec<_>) = files
|
|
|
|
.iter()
|
|
|
|
.map(|file| async move {
|
|
|
|
trace!(?file, "loading ignore file");
|
2022-06-15 05:25:05 +02:00
|
|
|
let content = read_to_string(&file.path)
|
|
|
|
.await
|
|
|
|
.map_err(|err| Error::Read {
|
2022-01-11 12:59:39 +01:00
|
|
|
file: file.path.clone(),
|
|
|
|
err,
|
2022-06-15 05:25:05 +02:00
|
|
|
})?;
|
2022-01-11 12:59:39 +01:00
|
|
|
Ok((file.clone(), content))
|
|
|
|
})
|
|
|
|
.collect::<FuturesUnordered<_>>()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.map(|res| match res {
|
|
|
|
Ok(o) => (Some(o), None),
|
|
|
|
Err(e) => (None, Some(e)),
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
let errors: Vec<Error> = errors.into_iter().flatten().collect();
|
2022-01-11 12:59:39 +01:00
|
|
|
if !errors.is_empty() {
|
2022-01-15 11:46:06 +01:00
|
|
|
trace!("found {} errors", errors.len());
|
2022-06-15 05:25:05 +02:00
|
|
|
return Err(Error::Multi(errors));
|
2022-01-11 12:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: different parser/adapter for non-git-syntax ignore files?
|
|
|
|
|
2022-01-15 11:46:06 +01:00
|
|
|
trace!(files=%files_contents.len(), "building ignore list");
|
2022-01-11 12:59:39 +01:00
|
|
|
let mut builder = GitignoreBuilder::new(origin);
|
|
|
|
for (file, content) in files_contents.into_iter().flatten() {
|
|
|
|
let _span = trace_span!("loading ignore file", ?file).entered();
|
|
|
|
for line in content.lines() {
|
|
|
|
if line.is_empty() || line.starts_with('#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(?line, "adding ignore line");
|
|
|
|
builder
|
|
|
|
.add_line(file.applies_in.clone(), line)
|
2022-06-15 05:25:05 +02:00
|
|
|
.map_err(|err| Error::Glob {
|
2022-01-16 03:18:15 +01:00
|
|
|
file: Some(file.path.clone()),
|
2022-01-11 12:59:39 +01:00
|
|
|
err,
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!("compiling globset");
|
|
|
|
let compiled = builder
|
|
|
|
.build()
|
2022-06-15 05:25:05 +02:00
|
|
|
.map_err(|err| Error::Glob { file: None, err })?;
|
2022-01-11 12:59:39 +01:00
|
|
|
|
|
|
|
trace!(
|
|
|
|
files=%files.len(),
|
|
|
|
ignores=%compiled.num_ignores(),
|
|
|
|
allows=%compiled.num_whitelists(),
|
|
|
|
"ignore files loaded and compiled",
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
origin: origin.to_owned(),
|
|
|
|
builder: Some(builder),
|
|
|
|
compiled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-16 03:18:15 +01:00
|
|
|
/// Returns the number of ignores and allowlists loaded.
|
2023-01-06 14:53:49 +01:00
|
|
|
#[must_use]
|
2022-01-16 03:18:15 +01:00
|
|
|
pub fn num_ignores(&self) -> (u64, u64) {
|
|
|
|
(self.compiled.num_ignores(), self.compiled.num_whitelists())
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:59:39 +01:00
|
|
|
/// Deletes the internal builder, to save memory.
|
|
|
|
///
|
|
|
|
/// This makes it impossible to add new ignore files without re-compiling the whole set.
|
|
|
|
pub fn finish(&mut self) {
|
|
|
|
self.builder = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads and adds an ignore file, if the builder is available.
|
|
|
|
///
|
|
|
|
/// Does nothing silently otherwise.
|
2022-06-15 05:25:05 +02:00
|
|
|
pub async fn add_file(&mut self, file: &IgnoreFile) -> Result<(), Error> {
|
2022-01-11 12:59:39 +01:00
|
|
|
if let Some(ref mut builder) = self.builder {
|
|
|
|
trace!(?file, "reading ignore file");
|
2022-06-15 05:25:05 +02:00
|
|
|
let content = read_to_string(&file.path)
|
|
|
|
.await
|
|
|
|
.map_err(|err| Error::Read {
|
|
|
|
file: file.path.clone(),
|
|
|
|
err,
|
|
|
|
})?;
|
2022-01-11 12:59:39 +01:00
|
|
|
|
|
|
|
let _span = trace_span!("loading ignore file", ?file).entered();
|
|
|
|
for line in content.lines() {
|
|
|
|
if line.is_empty() || line.starts_with('#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(?line, "adding ignore line");
|
|
|
|
builder
|
|
|
|
.add_line(file.applies_in.clone(), line)
|
2022-06-15 05:25:05 +02:00
|
|
|
.map_err(|err| Error::Glob {
|
2022-01-16 03:18:15 +01:00
|
|
|
file: Some(file.path.clone()),
|
2022-01-11 12:59:39 +01:00
|
|
|
err,
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2022-01-15 14:36:22 +01:00
|
|
|
self.recompile(file.path.clone())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
fn recompile(&mut self, file: PathBuf) -> Result<(), Error> {
|
2022-01-15 14:36:22 +01:00
|
|
|
if let Some(builder) = &mut self.builder {
|
2022-01-11 12:59:39 +01:00
|
|
|
let pre_ignores = self.compiled.num_ignores();
|
|
|
|
let pre_allows = self.compiled.num_whitelists();
|
|
|
|
|
|
|
|
trace!("recompiling globset");
|
2022-06-15 05:25:05 +02:00
|
|
|
let recompiled = builder.build().map_err(|err| Error::Glob {
|
2022-01-16 04:09:53 +01:00
|
|
|
file: Some(file),
|
|
|
|
err,
|
|
|
|
})?;
|
2022-01-11 12:59:39 +01:00
|
|
|
|
|
|
|
trace!(
|
|
|
|
new_ignores=%(recompiled.num_ignores() - pre_ignores),
|
|
|
|
new_allows=%(recompiled.num_whitelists() - pre_allows),
|
|
|
|
"ignore file loaded and set recompiled",
|
|
|
|
);
|
|
|
|
self.compiled = recompiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-15 14:36:22 +01:00
|
|
|
/// Adds some globs manually, if the builder is available.
|
|
|
|
///
|
|
|
|
/// Does nothing silently otherwise.
|
2023-01-06 14:53:49 +01:00
|
|
|
pub fn add_globs(&mut self, globs: &[&str], applies_in: Option<&PathBuf>) -> Result<(), Error> {
|
2022-01-15 14:36:22 +01:00
|
|
|
if let Some(ref mut builder) = self.builder {
|
|
|
|
let _span = trace_span!("loading ignore globs", ?globs).entered();
|
|
|
|
for line in globs {
|
|
|
|
if line.is_empty() || line.starts_with('#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(?line, "adding ignore line");
|
2022-01-16 04:09:53 +01:00
|
|
|
builder
|
2023-01-06 14:53:49 +01:00
|
|
|
.add_line(applies_in.cloned(), line)
|
2022-06-15 05:25:05 +02:00
|
|
|
.map_err(|err| Error::Glob { file: None, err })?;
|
2022-01-15 14:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.recompile("manual glob".into())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-15 05:25:05 +02:00
|
|
|
/// Match a particular path against the ignore set.
|
|
|
|
pub fn match_path(&self, path: &Path, is_dir: bool) -> Match<&Glob> {
|
|
|
|
if path.strip_prefix(&self.origin).is_ok() {
|
|
|
|
trace!("checking against path or parents");
|
|
|
|
self.compiled.matched_path_or_any_parents(path, is_dir)
|
|
|
|
} else {
|
|
|
|
trace!("checking against path only");
|
|
|
|
self.compiled.matched(path, is_dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:59:39 +01:00
|
|
|
/// Check a particular folder path against the ignore set.
|
|
|
|
///
|
|
|
|
/// Returns `false` if the folder should be ignored.
|
|
|
|
///
|
|
|
|
/// Note that this is a slightly different implementation than the [`Filterer`] trait, as the
|
|
|
|
/// latter handles events with multiple associated paths.
|
|
|
|
pub fn check_dir(&self, path: &Path) -> bool {
|
|
|
|
let _span = trace_span!("check_dir", ?path).entered();
|
|
|
|
|
|
|
|
trace!("checking against compiled ignore files");
|
2022-06-15 05:25:05 +02:00
|
|
|
match self.match_path(path, true) {
|
2022-01-11 12:59:39 +01:00
|
|
|
Match::None => {
|
|
|
|
trace!("no match (pass)");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
Match::Ignore(glob) => {
|
|
|
|
if glob.from().map_or(true, |f| path.strip_prefix(f).is_ok()) {
|
|
|
|
trace!(?glob, "positive match (fail)");
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
trace!(?glob, "positive match, but not in scope (pass)");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Match::Whitelist(glob) => {
|
|
|
|
trace!(?glob, "negative match (pass)");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|