Skip to content

Commit 7093c8e

Browse files
Refactor match package code
1 parent 4a2279e commit 7093c8e

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/cmd/go/internal/load/search.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,31 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
4343
return false
4444
}
4545

46-
cwdRel, err := filepath.Rel(cwd, p.Dir)
47-
if err == nil {
48-
if cwdRel != "." && !strings.HasPrefix(cwdRel, "../") {
49-
for _, elem := range strings.Split(cwdRel, string(filepath.Separator)) {
50-
// Avoid .foo, _foo, and testdata subdirectory trees.
51-
if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
52-
return false
53-
}
46+
relToWorkingDir, err := filepath.Rel(cwd, p.Dir)
47+
if err != nil {
48+
return matchPath(rel)
49+
}
50+
51+
hasAnyPrefix := func(dir string, prefixes ...string) bool {
52+
for _, prefix := range prefixes {
53+
if strings.HasPrefix(dir, prefix) {
54+
return true
5455
}
5556
}
57+
return false
58+
}
59+
60+
if relToWorkingDir == "." || hasAnyPrefix(relToWorkingDir, ".."+string(filepath.Separator)) {
61+
// Not a special directory so can return immediately
62+
return matchPath(rel)
63+
}
64+
65+
// Otherwise avoid special directories "testdata" or beginning with ".", "_".
66+
pathComponents := strings.Split(relToWorkingDir, string(filepath.Separator))
67+
for _, elem := range pathComponents {
68+
if hasAnyPrefix(elem, ".", "_") || elem == "testdata" {
69+
return false
70+
}
5671
}
5772

5873
return matchPath(rel)

0 commit comments

Comments
 (0)