| 1 |
package gitignore |
| 2 |
|
| 3 |
// Matcher defines a global multi-pattern matcher for gitignore patterns |
| 4 |
type Matcher interface { |
| 5 |
// Match matches patterns in the order of priorities. As soon as an inclusion or |
| 6 |
// exclusion is found, not further matching is performed. |
| 7 |
Match(path []string, isDir bool) bool |
| 8 |
} |
| 9 |
|
| 10 |
// NewMatcher constructs a new global matcher. Patterns must be given in the order of |
| 11 |
// increasing priority. That is most generic settings files first, then the content of |
| 12 |
// the repo .gitignore, then content of .gitignore down the path or the repo and then |
| 13 |
// the content command line arguments. |
| 14 |
func NewMatcher(ps []Pattern) Matcher { |
| 15 |
return &matcher{ps} |
| 16 |
} |
| 17 |
|
| 18 |
type matcher struct { |
| 19 |
patterns []Pattern |
| 20 |
} |
| 21 |
|
| 22 |
func (m *matcher) Match(path []string, isDir bool) bool { |
| 23 |
n := len(m.patterns) |
| 24 |
for i := n - 1; i >= 0; i-- { |
| 25 |
if match := m.patterns[i].Match(path, isDir); match > NoMatch { |
| 26 |
return match == Exclude |
| 27 |
} |
| 28 |
} |
| 29 |
return false |
| 30 |
} |