Prevent glob from accessing hidden files
Hidden files are often source control related (".git", ".repo", etc) or editor related (vim's .*.swp), so are not guaranteed to exist, and may be temporary. The build system shouldn't be using these files by default. If the glob pattern explicitly uses "." at the beginning of a path component, allow returning hidden files for that component. Because of this behavior, non-wildcard globs remain unchanged. The one behavior that cannot be handled anymore is including hidden files in recursive globs. Change-Id: I583c506e9a18ed2ff7ca011a791165d9582de90f
This commit is contained in:
parent
f3218f61b6
commit
53f4950eea
5 changed files with 45 additions and 0 deletions
|
@ -131,6 +131,9 @@ func glob(pattern string, hasRecursive bool) (matches, dirs []string, err error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
if file[0] != '.' {
|
||||||
|
newMatches = filterDotFiles(newMatches)
|
||||||
|
}
|
||||||
matches = append(matches, newMatches...)
|
matches = append(matches, newMatches...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,6 +173,11 @@ func walkAllDirs(dir string) (dirs []string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.Mode().IsDir() {
|
if info.Mode().IsDir() {
|
||||||
|
name := info.Name()
|
||||||
|
if name[0] == '.' && name != "." {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
|
||||||
dirs = append(dirs, path)
|
dirs = append(dirs, path)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -203,6 +211,21 @@ matchLoop:
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterDotFiles filters out files that start with '.'
|
||||||
|
func filterDotFiles(matches []string) []string {
|
||||||
|
ret := make([]string, 0, len(matches))
|
||||||
|
|
||||||
|
for _, match := range matches {
|
||||||
|
_, name := filepath.Split(match)
|
||||||
|
if name[0] == '.' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ret = append(ret, match)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// match returns true if name matches pattern using the same rules as filepath.Match, but supporting
|
// match returns true if name matches pattern using the same rules as filepath.Match, but supporting
|
||||||
// hierarchical patterns (a/*) and recursive globs (**).
|
// hierarchical patterns (a/*) and recursive globs (**).
|
||||||
func match(pattern, name string) (bool, error) {
|
func match(pattern, name string) (bool, error) {
|
||||||
|
|
|
@ -417,6 +417,28 @@ var globTestCases = []struct {
|
||||||
excludes: []string{"**/**"},
|
excludes: []string{"**/**"},
|
||||||
err: GlobLastRecursiveErr,
|
err: GlobLastRecursiveErr,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// If names are excluded by default, but referenced explicitly, they should return results
|
||||||
|
{
|
||||||
|
pattern: ".test/*",
|
||||||
|
matches: []string{".test/a"},
|
||||||
|
dirs: []string{".test"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pattern: ".t*/a",
|
||||||
|
matches: []string{".test/a"},
|
||||||
|
dirs: []string{".", ".test"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pattern: ".*/.*",
|
||||||
|
matches: []string{".test/.ing"},
|
||||||
|
dirs: []string{".", ".test"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pattern: ".t*",
|
||||||
|
matches: []string{".test", ".testing"},
|
||||||
|
dirs: []string{"."},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGlob(t *testing.T) {
|
func TestGlob(t *testing.T) {
|
||||||
|
|
0
pathtools/testdata/.test/.ing
vendored
Normal file
0
pathtools/testdata/.test/.ing
vendored
Normal file
0
pathtools/testdata/.test/a
vendored
Normal file
0
pathtools/testdata/.test/a
vendored
Normal file
0
pathtools/testdata/.testing
vendored
Normal file
0
pathtools/testdata/.testing
vendored
Normal file
Loading…
Reference in a new issue