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

Function ParsePatchHeader

pkg/gitdiff/patch_header.go:175–211  ·  view source on GitHub ↗

ParsePatchHeader parses the preamble string returned by [Parse] into a PatchHeader. Due to the variety of header formats, some fields of the parsed PatchHeader may be unset after parsing. Supported formats are the short, medium, full, fuller, and email pretty formats used by `git diff`, `git log`,

(header string, options ...PatchHeaderOption)

Source from the content-addressed store, hash-verified

173// returning errors, but will return errors if well-identified content like
174// dates or identies uses unknown or invalid formats.
175func ParsePatchHeader(header string, options ...PatchHeaderOption) (*PatchHeader, error) {
176 opts := patchHeaderOptions{
177 subjectCleanMode: SubjectCleanAll, // match git defaults
178 }
179 for _, optFn := range options {
180 optFn(&opts)
181 }
182
183 header = strings.TrimSpace(header)
184 if header == "" {
185 return &PatchHeader{}, nil
186 }
187
188 var firstLine, rest string
189 if idx := strings.IndexByte(header, '\n'); idx >= 0 {
190 firstLine = header[:idx]
191 rest = header[idx+1:]
192 } else {
193 firstLine = header
194 rest = ""
195 }
196
197 switch {
198 case strings.HasPrefix(firstLine, mailHeaderPrefix):
199 return parseHeaderMail(firstLine, strings.NewReader(rest), opts)
200
201 case strings.HasPrefix(firstLine, mailMinimumHeaderPrefix):
202 // With a minimum header, the first line is part of the actual mail
203 // content and needs to be parsed as part of the "rest"
204 return parseHeaderMail("", strings.NewReader(header), opts)
205
206 case strings.HasPrefix(firstLine, prettyHeaderPrefix):
207 return parseHeaderPretty(firstLine, strings.NewReader(rest))
208 }
209
210 return nil, errors.New("unrecognized patch header format")
211}
212
213func parseHeaderPretty(prettyLine string, r io.Reader) (*PatchHeader, error) {
214 const (

Callers 1

TestParsePatchHeaderFunction · 0.85

Calls 3

parseHeaderMailFunction · 0.85
parseHeaderPrettyFunction · 0.85
NewMethod · 0.80

Tested by 1

TestParsePatchHeaderFunction · 0.68