Skip to content

Commit

Permalink
fix: fix match when there is a escaped meta in the pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Jan 6, 2025
1 parent 1e20c6d commit 1e7ad31
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
21 changes: 11 additions & 10 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,17 @@ var matchTests = []MatchTest{
{"working-sym*/*", "working-symlink/c", true, true, nil, false, false, true, !onWindows, 1, 1},
{"b/**/f", "b/symlink-dir/f", true, true, nil, false, false, false, !onWindows, 2, 2},
{"*/symlink-dir/*", "b/symlink-dir/f", true, true, nil, !onWindows, false, true, !onWindows, 2, 2},
{"e/**", "e/**", true, true, nil, false, false, false, !onWindows, 11, 6},
{"e/**", "e/*", true, true, nil, false, false, false, !onWindows, 11, 6},
{"e/**", "e/?", true, true, nil, false, false, false, !onWindows, 11, 6},
{"e/**", "e/[", true, true, nil, false, false, false, true, 11, 6},
{"e/**", "e/]", true, true, nil, false, false, false, true, 11, 6},
{"e/**", "e/[]", true, true, nil, false, false, false, true, 11, 6},
{"e/**", "e/{", true, true, nil, false, false, false, true, 11, 6},
{"e/**", "e/}", true, true, nil, false, false, false, true, 11, 6},
{"e/**", "e/\\", true, true, nil, false, false, false, !onWindows, 11, 6},
{"e/*", "e/*", true, true, nil, false, false, true, !onWindows, 10, 5},
{"e/\\[owner\\]/*", "e/[owner]/p.tsx", true, true, nil, false, false, true, !onWindows, 1, 0},
{"e/**", "e/**", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/*", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/?", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/[", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/]", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/[]", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/{", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/}", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/\\", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/*", "e/*", true, true, nil, false, false, true, !onWindows, 11, 5},
{"e/?", "e/?", true, true, nil, false, false, true, !onWindows, 7, 4},
{"e/?", "e/*", true, true, nil, false, false, true, !onWindows, 7, 4},
{"e/?", "e/[", true, true, nil, false, false, true, true, 7, 4},
Expand Down
5 changes: 2 additions & 3 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
//
// Note: users should _not_ count on the returned error,
// doublestar.ErrBadPattern, being equal to path.ErrBadPattern.
//
func Glob(fsys fs.FS, pattern string, opts ...GlobOption) ([]string, error) {
if !ValidatePattern(pattern) {
return nil, ErrBadPattern
Expand Down Expand Up @@ -107,11 +106,11 @@ func (g *glob) doGlob(fsys fs.FS, pattern string, m []string, firstSegment, befo
// characters. They would be equal if they are both -1, which means `dir`
// will be ".", and we know that doesn't have meta characters either.
if splitIdx <= patternStart {
return g.globDir(fsys, dir, pattern, matches, firstSegment, beforeMeta)
return g.globDir(fsys, unescapeMeta(dir), pattern, matches, firstSegment, beforeMeta)
}

var dirs []string
dirs, err = g.doGlob(fsys, dir, matches, false, beforeMeta)
dirs, err = g.doGlob(fsys, unescapeMeta(dir), matches, false, beforeMeta)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions globwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func (g *glob) doGlobWalk(fsys fs.FS, pattern string, firstSegment, beforeMeta b
// characters. They would be equal if they are both -1, which means `dir`
// will be ".", and we know that doesn't have meta characters either.
if splitIdx <= patternStart {
return g.globDirWalk(fsys, dir, pattern, firstSegment, beforeMeta, fn)
return g.globDirWalk(fsys, unescapeMeta(dir), pattern, firstSegment, beforeMeta, fn)
}

return g.doGlobWalk(fsys, dir, false, beforeMeta, func(p string, d fs.DirEntry) error {
return g.doGlobWalk(fsys, unescapeMeta(dir), false, beforeMeta, func(p string, d fs.DirEntry) error {
if err := g.globDirWalk(fsys, p, pattern, firstSegment, false, fn); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ func FilepathGlob(pattern string, opts ...GlobOption) (matches []string, err err
return []string{filepath.FromSlash(pattern)}, nil
}

fs := os.DirFS(base)
fs := os.DirFS(unescapeMeta(base))
if matches, err = Glob(fs, f, opts...); err != nil {
return nil, err
}
for i := range matches {
// use path.Join because we used ToSlash above to ensure our paths are made
// of forward slashes, no matter what the system uses
matches[i] = filepath.FromSlash(path.Join(base, matches[i]))
matches[i] = filepath.FromSlash(path.Join(unescapeMeta(base), matches[i]))
}
return
}
Expand Down

0 comments on commit 1e7ad31

Please sign in to comment.