From 7c37301c910dd3cf54a44a7ac677aef52f402e6d Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Fri, 21 Oct 2016 07:33:40 +0000 Subject: [PATCH] lib/ignore: Add directory separator to glob.Compile call GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3674 LGTM: calmh --- lib/ignore/ignore.go | 10 +++++----- lib/ignore/ignore_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/ignore/ignore.go b/lib/ignore/ignore.go index e83e888f..31f70e74 100644 --- a/lib/ignore/ignore.go +++ b/lib/ignore/ignore.go @@ -279,14 +279,14 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([] var err error if strings.HasPrefix(line, "/") { // Pattern is rooted in the current dir only - pattern.match, err = glob.Compile(line[1:]) + pattern.match, err = glob.Compile(line[1:], '/') if err != nil { return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err) } patterns = append(patterns, pattern) } else if strings.HasPrefix(line, "**/") { // Add the pattern as is, and without **/ so it matches in current dir - pattern.match, err = glob.Compile(line) + pattern.match, err = glob.Compile(line, '/') if err != nil { return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err) } @@ -294,7 +294,7 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([] line = line[3:] pattern.pattern = line - pattern.match, err = glob.Compile(line) + pattern.match, err = glob.Compile(line, '/') if err != nil { return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err) } @@ -310,7 +310,7 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([] } else { // Path name or pattern, add it so it matches files both in // current directory and subdirs. - pattern.match, err = glob.Compile(line) + pattern.match, err = glob.Compile(line, '/') if err != nil { return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err) } @@ -318,7 +318,7 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) ([] line := "**/" + line pattern.pattern = line - pattern.match, err = glob.Compile(line) + pattern.match, err = glob.Compile(line, '/') if err != nil { return fmt.Errorf("invalid pattern %q in ignore file (%v)", line, err) } diff --git a/lib/ignore/ignore_test.go b/lib/ignore/ignore_test.go index 1439262e..db59999a 100644 --- a/lib/ignore/ignore_test.go +++ b/lib/ignore/ignore_test.go @@ -737,3 +737,35 @@ func TestIssue3639(t *testing.T) { t.Error("Should not match 'foo'") } } + +func TestIssue3674(t *testing.T) { + stignore := ` + a*b + a**c + ` + + testcases := []struct { + file string + matches bool + }{ + {"ab", true}, + {"asdfb", true}, + {"ac", true}, + {"asdfc", true}, + {"as/db", false}, + {"as/dc", true}, + } + + pats := New(true) + err := pats.Parse(bytes.NewBufferString(stignore), ".stignore") + if err != nil { + t.Fatal(err) + } + + for _, tc := range testcases { + res := pats.Match(tc.file).IsIgnored() + if res != tc.matches { + t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches) + } + } +}