pull/12265/merge
Evan Anderson 3 days ago committed by GitHub
commit 25d90ef8f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -37,6 +37,8 @@ The formatting rules are as follows:
- If a pattern contains no slashes, file basenames are tested (not paths)
- The pattern sequence "**", while legal in a glob, will cause an error here
(to indicate incompatibility with .gitignore).
- The last matching rule will determine whether a file is included or excluded.
It is recommended to write rules from most-general to most-specific to match this pattern.
Example:
@ -49,8 +51,9 @@ Example:
# Match only directories named mydir
mydir/
# Match only text files in the top-level directory
# Match text files in the top-level directory, except license.txt
/*.txt
!license.txt
# Match only the file foo.txt in the top-level directory
/foo.txt
@ -63,6 +66,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/v4/pkg/ignore"

@ -85,8 +85,9 @@ func Parse(file io.Reader) (*Rules, error) {
// Ignore evaluates the file at the given path, and returns true if it should be ignored.
//
// Ignore evaluates path against the rules in order. Evaluation stops when a match
// is found. Matching a negative rule will stop evaluation.
// Ignore evaluates path against the rules in order, with the last matching rule dictating
// whether the file is ignored. This follows the pattern of `.gitignore` -- note that
// "true" means to ignore the file, and "false" means to _keep_ the file.
func (r *Rules) Ignore(path string, fi os.FileInfo) bool {
// Don't match on empty dirs.
if path == "" {
@ -99,21 +100,11 @@ func (r *Rules) Ignore(path string, fi os.FileInfo) bool {
if path == "." || path == "./" {
return false
}
ignore := false
for _, p := range r.patterns {
if p.match == nil {
// This is a logic error; p.match should always be set in parseRule.
slog.Info("this will be ignored no matcher supplied", "patterns", 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
}
@ -122,11 +113,14 @@ func (r *Rules) Ignore(path string, fi os.FileInfo) bool {
if p.mustDir && !fi.IsDir() {
continue
}
// `.gitignore` semantics are last match wins, so we can't early-return on a match.
if p.match(path, fi) {
return true
ignore = !p.negate
}
}
return false
return ignore
}
// parseRule parses a rule string and creates a pattern, which is then stored in the Rules object.

@ -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},
{"*\n!helm.txt", "helm.txt", false},
{"!helm.txt", "tiller.txt", false}, // Don't ignore files that match zero patterns
{"*\n!helm.txt", "tiller.txt", true},
{"*\n!*.txt", "cargo", true},
{"*\n!cargo/", "cargo", false},
{"*\n!cargo/", "cargo/a.txt", true},
// Absolute path tests
{`/a.txt`, "a.txt", true},

Loading…
Cancel
Save