From af5f4ee724916345a35b42bc9529302b29ebf421 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 10 Jan 2016 11:42:53 +0000 Subject: [PATCH] Make --include rules add their implict exclude * at the end of the filter list This means you can mix `--include` and `--include-from` with the other filters (eg `--exclude`) but you must include all the files you want in the include statement. Fixes #280 --- docs/content/filtering.md | 12 ++++++++++-- fs/filter.go | 19 +++++++++---------- fs/filter_test.go | 5 ++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/docs/content/filtering.md b/docs/content/filtering.md index 4b971a2bc..12ead6f51 100644 --- a/docs/content/filtering.md +++ b/docs/content/filtering.md @@ -152,7 +152,11 @@ Add a single include rule with `--include`. Eg `--include *.{png,jpg}` to include all `png` and `jpg` files in the backup and no others. -This adds an implicit `--exclude *` at the end of the filter list. +This adds an implicit `--exclude *` at the very end of the filter +list. This means you can mix `--include` and `--include-from` with the +other filters (eg `--exclude`) but you must include all the files you +want in the include statement. If this doesn't provide enough +flexibility then you must use `--filter-from`. ### `--include-from` - Read include patterns from file ### @@ -170,7 +174,11 @@ Then use as `--include-from include-file.txt`. This will sync all This is useful if you have a lot of rules. -This adds an implicit `--exclude *` at the end of the filter list. +This adds an implicit `--exclude *` at the very end of the filter +list. This means you can mix `--include` and `--include-from` with the +other filters (eg `--exclude`) but you must include all the files you +want in the include statement. If this doesn't provide enough +flexibility then you must use `--filter-from`. ### `--filter` - Add a file-filtering rule ### diff --git a/fs/filter.go b/fs/filter.go index 0a6d689ad..32b6ba59d 100644 --- a/fs/filter.go +++ b/fs/filter.go @@ -117,17 +117,14 @@ func NewFilter() (f *Filter, err error) { MinSize: int64(minSize), MaxSize: int64(maxSize), } + addImplicitExclude := false if *includeRule != "" { err = f.Add(true, *includeRule) if err != nil { return nil, err } - // Add implicit exclude - err = f.Add(false, "*") - if err != nil { - return nil, err - } + addImplicitExclude = true } if *includeFrom != "" { err := forEachLine(*includeFrom, func(line string) error { @@ -136,11 +133,7 @@ func NewFilter() (f *Filter, err error) { if err != nil { return nil, err } - // Add implicit exclude - err = f.Add(false, "*") - if err != nil { - return nil, err - } + addImplicitExclude = true } if *excludeRule != "" { err = f.Add(false, *excludeRule) @@ -176,6 +169,12 @@ func NewFilter() (f *Filter, err error) { return nil, err } } + if addImplicitExclude { + err = f.Add(false, "*") + if err != nil { + return nil, err + } + } if *minAge != "" { duration, err := ParseDuration(*minAge) if err != nil { diff --git a/fs/filter_test.go b/fs/filter_test.go index cbfea924a..90304eae6 100644 --- a/fs/filter_test.go +++ b/fs/filter_test.go @@ -147,16 +147,15 @@ func TestNewFilterFull(t *testing.T) { } got := f.DumpFilters() want := `+ (^|/)include1$ -- (^|/)[^/]*$ + (^|/)include2$ + (^|/)include3$ -- (^|/)[^/]*$ - (^|/)exclude1$ - (^|/)exclude2$ - (^|/)exclude3$ - (^|/)filter1$ + (^|/)filter2$ -- (^|/)filter3$` +- (^|/)filter3$ +- (^|/)[^/]*$` if got != want { t.Errorf("rules want %s got %s", want, got) }