@@ -43,16 +43,31 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
43
43
return false
44
44
}
45
45
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
54
55
}
55
56
}
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
+ }
56
71
}
57
72
58
73
return matchPath (rel )
0 commit comments