Allow double star exclude patterns to match directories

Currently, double star exclude patterns are only able to match files. They
should also match directories.

Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
This commit is contained in:
Yann Soubeyrand 2020-05-08 20:10:52 +02:00
parent 694c692ecf
commit f832879256
2 changed files with 10 additions and 22 deletions

View File

@ -63,8 +63,6 @@ func NewPatternMatcher(patterns []string) (*PatternMatcher, error) {
func (pm *PatternMatcher) Matches(file string) (bool, error) {
matched := false
file = filepath.FromSlash(file)
parentPath := filepath.Dir(file)
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
for _, pattern := range pm.patterns {
negative := false
@ -78,13 +76,6 @@ func (pm *PatternMatcher) Matches(file string) (bool, error) {
return false, err
}
if !match && parentPath != "." {
// Check to see if the pattern matches one of our parent dirs.
if len(pattern.dirs) <= len(parentPathDirs) {
match, _ = pattern.match(strings.Join(parentPathDirs[:len(pattern.dirs)], string(os.PathSeparator)))
}
}
if match {
matched = !negative
}
@ -122,8 +113,6 @@ func (m *MatchResult) Excludes() uint {
// an error. This method is not safe to be called concurrently.
func (pm *PatternMatcher) MatchesResult(file string) (res *MatchResult, err error) {
file = filepath.FromSlash(file)
parentPath := filepath.Dir(file)
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
res = &MatchResult{false, 0, 0}
for _, pattern := range pm.patterns {
@ -138,16 +127,6 @@ func (pm *PatternMatcher) MatchesResult(file string) (res *MatchResult, err erro
return nil, err
}
if !match && parentPath != "." {
// Check to see if the pattern matches one of our parent dirs.
if len(pattern.dirs) <= len(parentPathDirs) {
match, _ = pattern.match(strings.Join(
parentPathDirs[:len(pattern.dirs)],
string(os.PathSeparator)),
)
}
}
if match {
res.isMatched = !negative
if negative {
@ -283,7 +262,7 @@ func (p *Pattern) compile() error {
}
}
regStr += "$"
regStr += "(/.*)?$"
re, err := regexp.Compile(regStr)
if err != nil {

View File

@ -333,6 +333,15 @@ func TestMatches(t *testing.T) {
{"dir/**", "dir/file/", false, true},
{"dir/**", "dir/dir2/file", false, true},
{"dir/**", "dir/dir2/file/", false, true},
{"**/dir", "dir/", false, true},
{"**/dir", "dir/file", false, true},
{"**/dir", "dir/dir2/file", false, true},
{"**/dir", "dir1/dir/", false, true},
{"**/dir", "dir1/dir/file", false, true},
{"**/dir", "dir1/dir/dir2/file", false, true},
{"**/dir", "dir1/dir2/dir/", false, true},
{"**/dir", "dir1/dir2/dir/file", false, true},
{"**/dir", "dir1/dir2/dir/dir3/file", false, true},
{"**/dir2/*", "dir/dir2/file", false, true},
{"**/dir2/*", "dir/dir2/file/", false, true},
{"**/dir2/**", "dir/dir2/dir3/file", false, true},