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

Method ParseNextFileHeader

pkg/gitdiff/file_header.go:19–61  ·  view source on GitHub ↗

ParseNextFileHeader finds and parses the next file header in the stream. If a header is found, it returns a file and all input before the header. It returns nil if no headers are found before the end of the input.

()

Source from the content-addressed store, hash-verified

17// a header is found, it returns a file and all input before the header. It
18// returns nil if no headers are found before the end of the input.
19func (p *parser) ParseNextFileHeader() (*File, string, error) {
20 var preamble strings.Builder
21 var file *File
22 for {
23 // check for disconnected fragment headers (corrupt patch)
24 frag, err := p.ParseTextFragmentHeader()
25 if err != nil {
26 // not a valid header, nothing to worry about
27 goto NextLine
28 }
29 if frag != nil {
30 return nil, "", p.Errorf(-1, "patch fragment without file header: %s", frag.Header())
31 }
32
33 // check for a git-generated patch
34 file, err = p.ParseGitFileHeader()
35 if err != nil {
36 return nil, "", err
37 }
38 if file != nil {
39 return file, preamble.String(), nil
40 }
41
42 // check for a "traditional" patch
43 file, err = p.ParseTraditionalFileHeader()
44 if err != nil {
45 return nil, "", err
46 }
47 if file != nil {
48 return file, preamble.String(), nil
49 }
50
51 NextLine:
52 preamble.WriteString(p.Line(0))
53 if err := p.Next(); err != nil {
54 if err == io.EOF {
55 break
56 }
57 return nil, "", err
58 }
59 }
60 return nil, preamble.String(), nil
61}
62
63func (p *parser) ParseGitFileHeader() (*File, error) {
64 const prefix = "diff --git "

Callers 3

TestParseNextFileHeaderFunction · 0.80
ParseFunction · 0.80

Calls 9

ErrorfMethod · 0.95
ParseGitFileHeaderMethod · 0.95
LineMethod · 0.95
NextMethod · 0.95
HeaderMethod · 0.80
WriteStringMethod · 0.80
StringMethod · 0.45

Tested by 2

TestParseNextFileHeaderFunction · 0.64