1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package gitattributes
// Matcher defines a global multi-pattern matcher for gitattributes patterns
type Matcher interface {
// Match matches patterns in the order of priorities.
Match(path []string, attributes []string) (map[string]Attribute, bool)
}
type MatcherOptions struct{}
// NewMatcher constructs a new matcher. Patterns must be given in the order of
// increasing priority. That is the most generic settings files first, then the
// content of the repo .gitattributes, then content of .gitattributes down the
// path.
func NewMatcher(stack []MatchAttribute) Matcher {
m := &matcher{stack: stack}
m.init()
return m
}
type matcher struct {
stack []MatchAttribute
macros map[string]MatchAttribute
}
func (m *matcher) init() {
m.macros = make(map[string]MatchAttribute)
for _, attr := range m.stack {
if attr.Pattern == nil {
m.macros[attr.Name] = attr
}
}
}
// Match matches path against the patterns in gitattributes files and returns
// the attributes associated with the path.
//
// Specific attributes can be specified otherwise all attributes are returned.
//
// Matched is true if any path was matched to a rule, even if the results map
// is empty.
func (m *matcher) Match(path []string, attributes []string) (results map[string]Attribute, matched bool) {
results = make(map[string]Attribute, len(attributes))
n := len(m.stack)
for i := n - 1; i >= 0; i-- {
if len(attributes) > 0 && len(attributes) == len(results) {
return
}
pattern := m.stack[i].Pattern
if pattern == nil {
continue
}
if match := pattern.Match(path); match {
matched = true
for _, attr := range m.stack[i].Attributes {
if attr.IsSet() {
m.expandMacro(attr.Name(), results)
}
results[attr.Name()] = attr
}
}
}
return
}
func (m *matcher) expandMacro(name string, results map[string]Attribute) bool {
if macro, ok := m.macros[name]; ok {
for _, attr := range macro.Attributes {
results[attr.Name()] = attr
}
}
return false
}
|