From 8770ae39679a7cb1c858659a88d95da6880a34f6 Mon Sep 17 00:00:00 2001 From: Matt Green Date: Sat, 7 Oct 2017 15:50:47 -0400 Subject: [PATCH] Handle relative ignores better? --- src/cli.rs | 8 ++++---- src/notification_filter.rs | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 2916650..6568ccd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -145,12 +145,12 @@ pub fn get_args() -> Args { } let mut ignores = vec![]; - let default_ignores = vec![format!("*{}.DS_Store", MAIN_SEPARATOR), + let default_ignores = vec![format!("**{}.DS_Store", MAIN_SEPARATOR), String::from("*.pyc"), String::from("*.swp"), - format!("*{}.git{}**", MAIN_SEPARATOR, MAIN_SEPARATOR), - format!("*{}.hg{}**", MAIN_SEPARATOR, MAIN_SEPARATOR), - format!("*{}.svn{}**", MAIN_SEPARATOR, MAIN_SEPARATOR)]; + format!("**{}.git{}**", MAIN_SEPARATOR, MAIN_SEPARATOR), + format!("**{}.hg{}**", MAIN_SEPARATOR, MAIN_SEPARATOR), + format!("**{}.svn{}**", MAIN_SEPARATOR, MAIN_SEPARATOR)]; if args.occurrences_of("no-default-ignore") == 0 { diff --git a/src/notification_filter.rs b/src/notification_filter.rs index 8c0c607..10d7127 100644 --- a/src/notification_filter.rs +++ b/src/notification_filter.rs @@ -35,9 +35,14 @@ impl NotificationFilter { let mut ignore_set_builder = GlobSetBuilder::new(); for i in &ignores { - ignore_set_builder.add(try!(Glob::new(i))); + 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: \"{}\"", i); + debug!("Adding ignore: \"{}\"", pattern); } let filter_set = try!(filter_set_builder.build()); @@ -99,6 +104,14 @@ mod tests { assert!(!filter.is_excluded(&Path::new("foo"))); } + #[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"))); + } + #[test] fn test_multiple_filters() { let filters = vec![String::from("*.rs"), String::from("*.toml")];