2016-09-21 23:02:20 +02:00
|
|
|
extern crate glob;
|
|
|
|
|
2016-09-27 15:43:28 +02:00
|
|
|
use std::io;
|
2016-10-29 16:24:31 +02:00
|
|
|
use std::path::Path;
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
use globset;
|
|
|
|
use globset::{Glob, GlobSet, GlobSetBuilder};
|
|
|
|
|
|
|
|
use gitignore;
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
pub struct NotificationFilter {
|
2016-11-03 22:04:39 +01:00
|
|
|
filters: GlobSet,
|
|
|
|
filter_count: usize,
|
|
|
|
ignores: GlobSet,
|
2017-02-04 20:52:38 +01:00
|
|
|
ignore_files: gitignore::Gitignore,
|
2016-09-21 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 15:43:28 +02:00
|
|
|
#[derive(Debug)]
|
2016-10-27 14:27:16 +02:00
|
|
|
pub enum Error {
|
2016-11-03 22:04:39 +01:00
|
|
|
Glob(globset::Error),
|
2016-10-24 02:12:48 +02:00
|
|
|
Io(io::Error),
|
2016-09-27 15:43:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
impl NotificationFilter {
|
2016-10-29 16:24:31 +02:00
|
|
|
pub fn new(filters: Vec<String>,
|
2016-10-27 14:27:16 +02:00
|
|
|
ignores: Vec<String>,
|
2017-02-04 20:52:38 +01:00
|
|
|
ignore_files: gitignore::Gitignore)
|
2016-10-27 14:27:16 +02:00
|
|
|
-> Result<NotificationFilter, Error> {
|
2016-11-03 22:04:39 +01:00
|
|
|
let mut filter_set_builder = GlobSetBuilder::new();
|
|
|
|
for f in &filters {
|
|
|
|
filter_set_builder.add(try!(Glob::new(f)));
|
2016-10-28 14:46:35 +02:00
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
debug!("Adding filter: \"{}\"", f);
|
2016-10-27 14:27:16 +02:00
|
|
|
}
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
let mut ignore_set_builder = GlobSetBuilder::new();
|
|
|
|
for i in &ignores {
|
2017-10-07 21:50:47 +02:00
|
|
|
let mut ignore_path = Path::new(i).to_path_buf();
|
|
|
|
if ignore_path.is_relative() && !i.starts_with("*") {
|
|
|
|
ignore_path = Path::new("**").join(&ignore_path);
|
|
|
|
}
|
|
|
|
let pattern = ignore_path.to_str().unwrap();
|
|
|
|
ignore_set_builder.add(try!(Glob::new(pattern)));
|
|
|
|
|
|
|
|
debug!("Adding ignore: \"{}\"", pattern);
|
2016-10-27 14:27:16 +02:00
|
|
|
}
|
2016-10-14 01:47:04 +02:00
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
let filter_set = try!(filter_set_builder.build());
|
|
|
|
let ignore_set = try!(ignore_set_builder.build());
|
|
|
|
|
2016-10-27 14:27:16 +02:00
|
|
|
Ok(NotificationFilter {
|
2017-03-23 23:39:38 +01:00
|
|
|
filters: filter_set,
|
|
|
|
filter_count: filters.len(),
|
|
|
|
ignores: ignore_set,
|
|
|
|
ignore_files: ignore_files,
|
|
|
|
})
|
2016-09-21 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_excluded(&self, path: &Path) -> bool {
|
2016-11-03 22:04:39 +01:00
|
|
|
if self.ignores.is_match(path) {
|
|
|
|
debug!("Ignoring {:?}: matched ignore filter", path);
|
|
|
|
return true;
|
2016-09-21 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
if self.filters.is_match(path) {
|
|
|
|
return false;
|
2016-09-21 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
2017-02-04 20:52:38 +01:00
|
|
|
if self.ignore_files.is_excluded(path) {
|
|
|
|
debug!("Ignoring {:?}: matched gitignore file", path);
|
|
|
|
return true;
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
if self.filter_count > 0 {
|
2016-10-14 02:21:29 +02:00
|
|
|
debug!("Ignoring {:?}: did not match any given filters", path);
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
self.filter_count > 0
|
2016-09-21 23:02:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 01:47:04 +02:00
|
|
|
|
2016-10-27 14:27:16 +02:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Error {
|
|
|
|
Error::Io(err)
|
2016-10-14 01:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:04:39 +01:00
|
|
|
impl From<globset::Error> for Error {
|
|
|
|
fn from(err: globset::Error) -> Error {
|
|
|
|
Error::Glob(err)
|
2016-10-14 01:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-29 15:37:50 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-02-04 20:52:38 +01:00
|
|
|
use gitignore;
|
2016-10-29 15:37:50 +02:00
|
|
|
use super::NotificationFilter;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_allows_everything_by_default() {
|
2017-02-04 22:18:02 +01:00
|
|
|
let filter = NotificationFilter::new(vec![], vec![], gitignore::load(&vec![])).unwrap();
|
2016-10-29 15:37:50 +02:00
|
|
|
|
|
|
|
assert!(!filter.is_excluded(&Path::new("foo")));
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:50:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_filename() {
|
|
|
|
let filter = NotificationFilter::new(vec![], vec![String::from("test.json")], gitignore::load(&vec![])).unwrap();
|
|
|
|
|
|
|
|
assert!(filter.is_excluded(&Path::new("/path/to/test.json")));
|
|
|
|
assert!(filter.is_excluded(&Path::new("test.json")));
|
|
|
|
}
|
|
|
|
|
2016-10-29 15:37:50 +02:00
|
|
|
#[test]
|
|
|
|
fn test_multiple_filters() {
|
|
|
|
let filters = vec![String::from("*.rs"), String::from("*.toml")];
|
2017-02-04 22:18:02 +01:00
|
|
|
let filter = NotificationFilter::new(filters, vec![], gitignore::load(&vec![])).unwrap();
|
2016-10-29 15:37:50 +02:00
|
|
|
|
2016-10-29 16:24:31 +02:00
|
|
|
assert!(!filter.is_excluded(&Path::new("hello.rs")));
|
|
|
|
assert!(!filter.is_excluded(&Path::new("Cargo.toml")));
|
|
|
|
assert!(filter.is_excluded(&Path::new("README.md")));
|
2016-10-29 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_ignores() {
|
|
|
|
let ignores = vec![String::from("*.rs"), String::from("*.toml")];
|
2017-02-04 22:18:02 +01:00
|
|
|
let filter = NotificationFilter::new(vec![], ignores, gitignore::load(&vec![])).unwrap();
|
2016-10-29 15:37:50 +02:00
|
|
|
|
2016-10-29 16:24:31 +02:00
|
|
|
assert!(filter.is_excluded(&Path::new("hello.rs")));
|
|
|
|
assert!(filter.is_excluded(&Path::new("Cargo.toml")));
|
|
|
|
assert!(!filter.is_excluded(&Path::new("README.md")));
|
2016-10-29 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ignores_take_precedence() {
|
|
|
|
let ignores = vec![String::from("*.rs"), String::from("*.toml")];
|
2017-02-04 22:18:02 +01:00
|
|
|
let filter = NotificationFilter::new(ignores.clone(), ignores, gitignore::load(&vec![]))
|
2017-02-04 20:52:38 +01:00
|
|
|
.unwrap();
|
2016-10-29 15:37:50 +02:00
|
|
|
|
2016-10-29 16:24:31 +02:00
|
|
|
assert!(filter.is_excluded(&Path::new("hello.rs")));
|
|
|
|
assert!(filter.is_excluded(&Path::new("Cargo.toml")));
|
|
|
|
assert!(filter.is_excluded(&Path::new("README.md")));
|
2016-10-29 15:37:50 +02:00
|
|
|
}
|
|
|
|
}
|