Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

Fix filters to add ** rules to directory rules

Nick Craig-Wood78c1f2839e1a
diff --git a/fs/filter.go b/fs/filter.go
index 20e16e2de..ee14d2d22 100644
--- a/fs/filter.go
+++ b/fs/filter.go
@@ -273,7 +273,7 @@ func (f *Filter) addDirGlobs(Include bool, glob string) error {
func (f *Filter) Add(Include bool, glob string) error {
isDirRule := strings.HasSuffix(glob, "/")
isFileRule := !isDirRule
- if strings.HasSuffix(glob, "**") {
+ if strings.Contains(glob, "**") {
isDirRule, isFileRule = true, true
}
re, err := globToRegexp(glob)
diff --git a/fs/filter_test.go b/fs/filter_test.go
index b0fe17bc8..3e24b6b84 100644
--- a/fs/filter_test.go
+++ b/fs/filter_test.go
@@ -1,6 +1,7 @@
package fs
import (
+ "fmt"
"io/ioutil"
"os"
"strings"
@@ -338,6 +339,71 @@ func TestNewFilterMatches(t *testing.T) {
assert.False(t, f.InActive())
}
+func TestFilterAddDirRuleOrFileRule(t *testing.T) {
+ for _, test := range []struct {
+ included bool
+ glob string
+ want string
+ }{
+ {
+ false,
+ "potato",
+ `--- File filter rules ---
+- (^|/)potato$
+--- Directory filter rules ---`,
+ },
+ {
+ true,
+ "potato",
+ `--- File filter rules ---
++ (^|/)potato$
+--- Directory filter rules ---
++ ^.*$`,
+ },
+ {
+ false,
+ "*",
+ `--- File filter rules ---
+- (^|/)[^/]*$
+--- Directory filter rules ---
+- ^.*$`,
+ },
+ {
+ true,
+ "*",
+ `--- File filter rules ---
++ (^|/)[^/]*$
+--- Directory filter rules ---
++ ^.*$`,
+ },
+ {
+ false,
+ ".*{,/**}",
+ `--- File filter rules ---
+- (^|/)\.[^/]*(|/.*)$
+--- Directory filter rules ---
+- (^|/)\.[^/]*(|/.*)$`,
+ },
+ {
+ true,
+ "a/b/c/d",
+ `--- File filter rules ---
++ (^|/)a/b/c/d$
+--- Directory filter rules ---
++ (^|/)a/b/c/$
++ (^|/)a/b/$
++ (^|/)a/$`,
+ },
+ } {
+ f, err := NewFilter()
+ require.NoError(t, err)
+ err = f.Add(test.included, test.glob)
+ require.NoError(t, err)
+ got := f.DumpFilters()
+ assert.Equal(t, test.want, got, fmt.Sprintf("Add(%v, %q)", test.included, test.glob))
+ }
+}
+
func TestFilterForEachLine(t *testing.T) {
file := testFile(t, `; comment
one