Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions pkg/filematcher/filematcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,23 @@ func (p *Pattern) regexpString() string {
escSL += `\`
}

inClass := false

for scan.Peek() != scanner.EOF {
ch := scan.Next()

switch ch {
case '[':
inClass = true
regStr += string(ch)
case ']':
inClass = false
regStr += string(ch)
case '*':
if inClass {
regStr += string(ch)
continue
}
if scan.Peek() == '*' {
// Is some flavor of "**".
scan.Next()
Expand All @@ -209,12 +221,19 @@ func (p *Pattern) regexpString() string {
regStr += "[^" + escSL + "]*"
}
case '?':
if inClass {
regStr += string(ch)
continue
}
// "?" is any char except "/".
regStr += "[^" + escSL + "]"
case '.', '$':
// Escape some regexp special chars that have no meaning
// in golang's filepath.Match.
regStr += `\` + string(ch)
case '.', '$', '+', '(', ')', '{', '}', '|', '^':
if inClass {
regStr += string(ch)
} else {
// Escape regexp special chars that have no meaning in filepath.Match.
regStr += `\` + string(ch)
}
case '\\':
// Escape next char. Note that a trailing \ in the pattern
// will be left alone (but need to escape it).
Expand Down
5 changes: 5 additions & 0 deletions pkg/filematcher/filematcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func TestMatches(t *testing.T) {
{"abc/**", "abc/def/ghi", true},
{"**/.foo", ".foo", true},
{"**/.foo", "bar.foo", false},
{"a+b.txt", "a+b.txt", true},
{"foo(1).txt", "foo(1).txt", true},
{"foo{1}.txt", "foo{1}.txt", true},
{"foo|bar.txt", "foo|bar.txt", true},
{"^foo.txt", "^foo.txt", true},
}

for _, test := range tests {
Expand Down
Loading