Skip to content

Commit

Permalink
fixed bug with SkipDir and {alts}
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk committed Jul 10, 2022
1 parent 2fd1ca0 commit cfa46a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
22 changes: 17 additions & 5 deletions globwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"
"path"
"path/filepath"
"strings"
)

// If returned from GlobWalkFunc, will cause GlobWalk to skip the current
Expand Down Expand Up @@ -133,19 +134,30 @@ func globAltsWalk(fsys fs.FS, pattern string, openingIdx, closingIdx int, firstS
if skip != "" {
// Because matches are sorted, we know that descendants of the skipped
// item must come immediately after the skipped item. If we find an item
// for which the parent directory does not match the skipped item, we
// know we're done skipping
if filepath.Dir(m.Path) == skip {
// that does not have a prefix matching the skipped item, we know we're
// done skipping. I'm using strings.HasPrefix here because
// filepath.HasPrefix has been marked deprecated (and just calls
// strings.HasPrefix anyway). The reason it's deprecated is because it
// doesn't handle case-insensitive paths, nor does it guarantee that the
// prefix is actually a parent directory. Neither is an issue here: the
// paths come from the system so their cases will match, and we guarantee
// a parent directory by appending a slash to the prefix.
//
// NOTE: m.Path will always use slashes as path separators.
if strings.HasPrefix(m.Path, skip) {
continue
}
skip = ""
}
if err = fn(m.Path, m.Entry); err != nil {
if err == SkipDir {
if isDir(fsys, "", m.Path, m.Entry) {
skip = m.Path
// append a slash to guarantee `skip` will be treated as a parent dir
skip = m.Path + "/"
} else {
skip = filepath.Dir(m.Path)
// Dir() calls Clean() which calls FromSlash(), so we need to convert
// back to slashes
skip = filepath.ToSlash(filepath.Dir(m.Path)) + "/"
}
err = nil
continue
Expand Down
1 change: 1 addition & 0 deletions globwalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var skipTests = []SkipTest{
{"a/**", "a/abc", "a/b", 1, 1},
{"a/**", "a/b/c", "a/b/c/d", 5, 5},
{"a/{**,c/*}", "a/b/c", "a/b/c/d", 5, 5},
{"a/{**,c/*}", "a/abc", "a/b", 1, 1},
}

func TestSkipDirInGlobWalk(t *testing.T) {
Expand Down

0 comments on commit cfa46a9

Please sign in to comment.