MCPcopy Create free account
hub / github.com/go-git/go-git / ParseAttributesLine

Function ParseAttributesLine

plumbing/format/gitattributes/attributes.go:115–168  ·  view source on GitHub ↗

ParseAttributesLine parses a gitattribute line, extracting path pattern and attributes.

(line string, domain []string, allowMacro bool)

Source from the content-addressed store, hash-verified

113// ParseAttributesLine parses a gitattribute line, extracting path pattern and
114// attributes.
115func ParseAttributesLine(line string, domain []string, allowMacro bool) (m MatchAttribute, err error) {
116 line = strings.TrimSpace(line)
117
118 if strings.HasPrefix(line, commentPrefix) || len(line) == 0 {
119 return
120 }
121
122 name, unquoted := unquote(line)
123 attrs := strings.Fields(unquoted)
124 if len(name) == 0 {
125 name = attrs[0]
126 attrs = attrs[1:]
127 }
128
129 var macro bool
130 macro, name, err = checkMacro(name, allowMacro)
131 if err != nil {
132 return
133 }
134
135 m.Name = name
136 m.Attributes = make([]Attribute, 0, len(attrs))
137
138 for _, attrName := range attrs {
139 attr := attribute{
140 name: attrName,
141 state: attributeSet,
142 }
143
144 // ! and - prefixes
145 state := attributeState(attr.name[0])
146 if state == attributeUnspecified || state == attributeUnset {
147 attr.state = state
148 attr.name = attr.name[1:]
149 }
150
151 kv := strings.SplitN(attrName, "=", 2)
152 if len(kv) == 2 {
153 attr.name = kv[0]
154 attr.value = kv[1]
155 attr.state = attributeSetValue
156 }
157
158 if !validAttributeName(attr.name) {
159 return m, ErrInvalidAttributeName
160 }
161 m.Attributes = append(m.Attributes, attr)
162 }
163
164 if !macro {
165 m.Pattern = ParsePattern(name, domain)
166 }
167 return
168}
169
170func checkMacro(name string, allowMacro bool) (macro bool, macroName string, err error) {
171 if !strings.HasPrefix(name, macroPrefix) {

Callers 1

ReadAttributesFunction · 0.85

Calls 5

unquoteFunction · 0.85
checkMacroFunction · 0.85
attributeStateTypeAlias · 0.85
validAttributeNameFunction · 0.85
ParsePatternFunction · 0.70

Tested by

no test coverage detected