More gitignore-like implementation of negative ignore rules

Signed-off-by: Evan Anderson <evan@stacklok.com>
pull/12265/head
Evan Anderson 2 years ago
parent 343389856b
commit e584e68e0c

@ -63,6 +63,6 @@ Notable differences from .gitignore:
- The globbing library is Go's 'filepath.Match', not fnmatch(3)
- Trailing spaces are always ignored (there is no supported escape sequence)
- The evaluation of escape sequences has not been tested for compatibility
- There is no support for '\!' as a special leading sequence.
- There is no support for '\\!' as a special leading sequence for files that begin with `!`
*/
package ignore // import "helm.sh/helm/v3/internal/ignore"

@ -102,30 +102,22 @@ func (r *Rules) Ignore(path string, fi os.FileInfo) bool {
}
for _, p := range r.patterns {
if p.match == nil {
log.Printf("ignore: no matcher supplied for %q", p.raw)
return false
}
// For negative rules, we need to capture and return non-matches,
// and continue for matches.
if p.negate {
if p.mustDir && !fi.IsDir() {
return true
}
if !p.match(path, fi) {
return true
}
continue
}
// If the rule is looking for directories, and this is not a directory,
// skip it.
if p.mustDir && !fi.IsDir() {
continue
}
// For negative rules, we need to capture and return non-matches,
// and continue for matches.
if p.match(path, fi) {
return true
// if negate, then return false to "should I ignore"
return !p.negate
}
}
return false
}

@ -113,10 +113,12 @@ func TestIgnore(t *testing.T) {
{`helm.txt/`, "helm.txt", false},
// Negation tests
{`!helm.txt`, "helm.txt", false},
{`!helm.txt`, "tiller.txt", true},
{`!*.txt`, "cargo", true},
{`!cargo/`, "mast/", true},
{"!helm.txt\n*", "helm.txt", false},
{"!helm.txt", "tiller.txt", false}, // Don't ignore files that match zero patterns
{"!helm.txt\n*", "tiller.txt", true},
{"!*.txt\n*", "cargo", true},
{"!cargo/\n*", "cargo", false},
{"!cargo/\n*", "cargo/a.txt", true},
// Absolute path tests
{`/a.txt`, "a.txt", true},

Loading…
Cancel
Save