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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
package gitattributes
import (
"bufio"
"errors"
"io"
"strings"
)
const (
commentPrefix = "#"
eol = "\n"
macroPrefix = "[attr]"
)
var (
ErrMacroNotAllowed = errors.New("macro not allowed")
ErrInvalidAttributeName = errors.New("invalid attribute name")
)
type MatchAttribute struct {
Name string
Pattern Pattern
Attributes []Attribute
}
type attributeState byte
const (
attributeUnknown attributeState = 0
attributeSet attributeState = 1
attributeUnspecified attributeState = '!'
attributeUnset attributeState = '-'
attributeSetValue attributeState = '='
)
type Attribute interface {
Name() string
IsSet() bool
IsUnset() bool
IsUnspecified() bool
IsValueSet() bool
Value() string
String() string
}
type attribute struct {
name string
state attributeState
value string
}
func (a attribute) Name() string {
return a.name
}
func (a attribute) IsSet() bool {
return a.state == attributeSet
}
func (a attribute) IsUnset() bool {
return a.state == attributeUnset
}
func (a attribute) IsUnspecified() bool {
return a.state == attributeUnspecified
}
func (a attribute) IsValueSet() bool {
return a.state == attributeSetValue
}
func (a attribute) Value() string {
return a.value
}
func (a attribute) String() string {
switch a.state {
case attributeSet:
return a.name + ": set"
case attributeUnset:
return a.name + ": unset"
case attributeUnspecified:
return a.name + ": unspecified"
default:
return a.name + ": " + a.value
}
}
// ReadAttributes reads patterns and attributes from the gitattributes format.
func ReadAttributes(r io.Reader, domain []string, allowMacro bool) (attributes []MatchAttribute, err error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
attribute, err := ParseAttributesLine(scanner.Text(), domain, allowMacro)
if err != nil {
return attributes, err
}
if len(attribute.Name) == 0 {
continue
}
attributes = append(attributes, attribute)
}
if err := scanner.Err(); err != nil {
return attributes, err
}
return attributes, nil
}
// ParseAttributesLine parses a gitattribute line, extracting path pattern and
// attributes.
func ParseAttributesLine(line string, domain []string, allowMacro bool) (m MatchAttribute, err error) {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, commentPrefix) || len(line) == 0 {
return
}
name, unquoted := unquote(line)
attrs := strings.Fields(unquoted)
if len(name) == 0 {
name = attrs[0]
attrs = attrs[1:]
}
var macro bool
macro, name, err = checkMacro(name, allowMacro)
if err != nil {
return
}
m.Name = name
m.Attributes = make([]Attribute, 0, len(attrs))
for _, attrName := range attrs {
attr := attribute{
name: attrName,
state: attributeSet,
}
// ! and - prefixes
state := attributeState(attr.name[0])
if state == attributeUnspecified || state == attributeUnset {
attr.state = state
attr.name = attr.name[1:]
}
kv := strings.SplitN(attrName, "=", 2)
if len(kv) == 2 {
attr.name = kv[0]
attr.value = kv[1]
attr.state = attributeSetValue
}
if !validAttributeName(attr.name) {
return m, ErrInvalidAttributeName
}
m.Attributes = append(m.Attributes, attr)
}
if !macro {
m.Pattern = ParsePattern(name, domain)
}
return
}
func checkMacro(name string, allowMacro bool) (macro bool, macroName string, err error) {
if !strings.HasPrefix(name, macroPrefix) {
return false, name, nil
}
if !allowMacro {
return true, name, ErrMacroNotAllowed
}
macroName = name[len(macroPrefix):]
if !validAttributeName(macroName) {
return true, name, ErrInvalidAttributeName
}
return true, macroName, nil
}
func validAttributeName(name string) bool {
if len(name) == 0 || name[0] == '-' {
return false
}
for _, ch := range name {
if !(ch == '-' || ch == '.' || ch == '_' ||
('0' <= ch && ch <= '9') ||
('a' <= ch && ch <= 'z') ||
('A' <= ch && ch <= 'Z')) {
return false
}
}
return true
}
func unquote(str string) (string, string) {
if str[0] != '"' {
return "", str
}
for i := 1; i < len(str); i++ {
switch str[i] {
case '\\':
i++
case '"':
return str[1:i], str[i+1:]
}
}
return "", str
}
|