MCPcopy
hub / github.com/harness/harness / parseFileHeader

Method parseFileHeader

git/diff/diff.go:240–356  ·  view source on GitHub ↗

nolint:gocognit

()

Source from the content-addressed store, hash-verified

238
239//nolint:gocognit
240func (p *Parser) parseFileHeader() (*File, error) {
241 p.Patch.Reset()
242 submoduleMode := " 160000"
243 if p.IncludePatch && len(p.buffer) > 0 {
244 p.Patch.Write(p.buffer)
245 p.Patch.Write([]byte{'\n'})
246 }
247 line := string(p.buffer)
248 p.buffer = nil
249
250 // NOTE: In case file name is surrounded by double quotes (it happens only in
251 // git-shell). e.g. diff --git "a/xxx" "b/xxx"
252 hasQuote := line[len(diffHead)] == '"' || line[len(line)-1] == '"'
253 middle := strings.Index(line, ` b/`)
254 if hasQuote {
255 middle = strings.Index(line, ` "b/`)
256 }
257
258 beg := len(diffHead)
259 a := line[beg+2 : middle]
260 b := line[middle+3:]
261 if hasQuote {
262 a = string(UnescapeChars([]byte(a[1 : len(a)-1])))
263 b = string(UnescapeChars([]byte(b[1 : len(b)-1])))
264 }
265
266 file := &File{
267 Path: a,
268 OldPath: b,
269 Type: FileChange,
270 }
271
272checkType:
273 for !p.isEOF {
274 newLine, err := p.readLine()
275 if err != nil {
276 return nil, err
277 }
278
279 if p.IncludePatch && len(p.buffer) > 0 {
280 p.Patch.Write(p.buffer)
281 if newLine {
282 p.Patch.Write([]byte{'\n'})
283 }
284 }
285
286 subLine := string(p.buffer)
287 p.buffer = nil
288
289 if len(subLine) == 0 {
290 continue
291 }
292
293 switch {
294 case strings.HasPrefix(subLine, enum.DiffExtHeaderNewFileMode):
295 file.Type = FileAdd
296 file.IsSubmodule = strings.HasSuffix(subLine, submoduleMode)
297 fields := strings.Fields(subLine)

Callers 1

ParseMethod · 0.95

Calls 6

readLineMethod · 0.95
EntryModeTypeAlias · 0.92
UnescapeCharsFunction · 0.85
SplitMethod · 0.80
WriteMethod · 0.65
IndexMethod · 0.65

Tested by

no test coverage detected