Skip to content

Commit 8a36c1e

Browse files
committed
pattern: "**" only acts as globstar when alone as a path element
See the parent commit for a detailed explanation of the context. Fixes #1149.
1 parent fbc5da3 commit 8a36c1e

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pattern/pattern.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ writeLoop:
8181
switch c := pat[i]; c {
8282
case '*':
8383
if mode&Filenames != 0 {
84+
// "**" only acts as globstar if it is alone as a path element.
85+
singleBefore := i == 0 || pat[i-1] == '/'
8486
if i++; i < len(pat) && pat[i] == '*' {
85-
if mode&NoGlobStar != 0 {
87+
singleAfter := i == len(pat)-1 || pat[i+1] == '/'
88+
if mode&NoGlobStar != 0 || !singleBefore || !singleAfter {
8689
buf.WriteString("[^/]*")
8790
} else if i++; i < len(pat) && pat[i] == '/' {
8891
buf.WriteString("(.*/|)")

pattern/pattern_test.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ var regexpTests = []struct {
3737
{pat: `/**/foo`, mode: Filenames | NoGlobStar, want: `/[^/]*/foo`},
3838
{pat: `/**/à`, mode: Filenames, want: `(?s)/(.*/|)à`},
3939
{
40-
pat: `/**foo`, mode: Filenames, want: `(?s)/.*foo`,
40+
pat: `/**foo`, mode: Filenames, want: `/[^/]*foo`,
4141
// These all match because without EntireString, we match substrings.
4242
mustMatch: []string{"/foo", "/prefix-foo", "/foo-suffix", "/sub/foo"},
4343
},
4444
{
45-
pat: `/**foo`, mode: Filenames | EntireString, want: `(?s)^/.*foo$`,
46-
// TODO: /sub/foo should not match; see issue 1149
47-
mustMatch: []string{"/foo", "/prefix-foo", "/sub/foo"},
48-
mustNotMatch: []string{"/foo-suffix"},
45+
pat: `/**foo`, mode: Filenames | EntireString, want: `^/[^/]*foo$`,
46+
mustMatch: []string{"/foo", "/prefix-foo"},
47+
mustNotMatch: []string{"/foo-suffix", "/sub/foo"},
4948
},
5049
{
51-
pat: `/foo**`, mode: Filenames | EntireString, want: `(?s)^/foo.*$`,
52-
// TODO: /foo/sub should not match; see issue 1149
53-
mustMatch: []string{"/foo", "/foo-suffix", "/foo/sub"},
54-
mustNotMatch: []string{"/prefix-foo"},
50+
pat: `/foo**`, mode: Filenames | EntireString, want: `^/foo[^/]*$`,
51+
mustMatch: []string{"/foo", "/foo-suffix"},
52+
mustNotMatch: []string{"/prefix-foo", "/foo/sub"},
5553
},
5654
{pat: `\*`, want: `\*`},
5755
{pat: `\`, wantErr: true},

0 commit comments

Comments
 (0)