-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
globpath: only walk tree if ** is defined
closes #1517
- Loading branch information
Showing
2 changed files
with
26 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,21 +12,23 @@ import ( | |
var sepStr = fmt.Sprintf("%v", string(os.PathSeparator)) | ||
|
||
type GlobPath struct { | ||
path string | ||
hasMeta bool | ||
g glob.Glob | ||
root string | ||
path string | ||
hasMeta bool | ||
hasSuperMeta bool | ||
g glob.Glob | ||
root string | ||
} | ||
|
||
func Compile(path string) (*GlobPath, error) { | ||
out := GlobPath{ | ||
hasMeta: hasMeta(path), | ||
path: path, | ||
hasMeta: hasMeta(path), | ||
hasSuperMeta: hasSuperMeta(path), | ||
path: path, | ||
} | ||
|
||
// if there are no glob meta characters in the path, don't bother compiling | ||
// a glob object or finding the root directory. (see short-circuit in Match) | ||
if !out.hasMeta { | ||
if !out.hasMeta || !out.hasSuperMeta { | ||
return &out, nil | ||
} | ||
|
||
|
@@ -48,6 +50,17 @@ func (g *GlobPath) Match() map[string]os.FileInfo { | |
} | ||
return out | ||
} | ||
if !g.hasSuperMeta { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
danielnelson
Contributor
|
||
out := make(map[string]os.FileInfo) | ||
files, _ := filepath.Glob(g.path) | ||
for _, file := range files { | ||
info, err := os.Stat(file) | ||
if !os.IsNotExist(err) { | ||
out[file] = info | ||
} | ||
} | ||
return out | ||
} | ||
return walkFilePath(g.root, g.g) | ||
} | ||
|
||
|
@@ -96,3 +109,8 @@ func findRootDir(path string) string { | |
func hasMeta(path string) bool { | ||
return strings.IndexAny(path, "*?[") >= 0 | ||
} | ||
|
||
// hasSuperMeta reports whether path contains any super magic glob characters (**). | ||
func hasSuperMeta(path string) bool { | ||
return strings.Index(path, "**") >= 0 | ||
} |
I'm learning about the wonderful code~
Why not use filepath.Glob directly?
Using
gobwas/glob
for performance?