MCPcopy Create free account
hub / github.com/antonmedv/gitmal / parseGitHeaderName

Function parseGitHeaderName

pkg/gitdiff/file_header.go:174–257  ·  view source on GitHub ↗

parseGitHeaderName extracts a default file name from the Git file header line. This is required for mode-only changes and creation/deletion of empty files. Other types of patch include the file name(s) in the header data. If the names in the header do not match because the patch is a rename, return

(header string)

Source from the content-addressed store, hash-verified

172// If the names in the header do not match because the patch is a rename,
173// return an empty default name.
174func parseGitHeaderName(header string) (string, error) {
175 header = strings.TrimSuffix(header, "\n")
176 if len(header) == 0 {
177 return "", nil
178 }
179
180 var err error
181 var first, second string
182
183 // there are 4 cases to account for:
184 //
185 // 1) unquoted unquoted
186 // 2) unquoted "quoted"
187 // 3) "quoted" unquoted
188 // 4) "quoted" "quoted"
189 //
190 quote := strings.IndexByte(header, '"')
191 switch {
192 case quote < 0:
193 // case 1
194 first = header
195
196 case quote > 0:
197 // case 2
198 first = header[:quote-1]
199 if !isSpace(header[quote-1]) {
200 return "", fmt.Errorf("missing separator")
201 }
202
203 second, _, err = parseQuotedName(header[quote:])
204 if err != nil {
205 return "", err
206 }
207
208 case quote == 0:
209 // case 3 or case 4
210 var n int
211 first, n, err = parseQuotedName(header)
212 if err != nil {
213 return "", err
214 }
215
216 // git accepts multiple spaces after a quoted name, but not after an
217 // unquoted name, since the name might end with one or more spaces
218 for n < len(header) && isSpace(header[n]) {
219 n++
220 }
221 if n == len(header) {
222 return "", nil
223 }
224
225 if header[n] == '"' {
226 second, _, err = parseQuotedName(header[n:])
227 if err != nil {
228 return "", err
229 }
230 } else {
231 second = header[n:]

Callers 2

TestParseGitHeaderNameFunction · 0.85
ParseGitFileHeaderMethod · 0.85

Calls 4

isSpaceFunction · 0.85
parseQuotedNameFunction · 0.85
trimTreePrefixFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestParseGitHeaderNameFunction · 0.68