MCPcopy Create free account
hub / github.com/cwarden/git-add--interactive / parseHunkHeader

Method parseHunkHeader

internal/git/patch.go:219–248  ·  view source on GitHub ↗
(hunk *Hunk)

Source from the content-addressed store, hash-verified

217}
218
219func (r *Repository) parseHunkHeader(hunk *Hunk) error {
220 if len(hunk.Text) == 0 {
221 return fmt.Errorf("empty hunk")
222 }
223
224 hunkHeaderRe := regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@`)
225 matches := hunkHeaderRe.FindStringSubmatch(hunk.Text[0])
226 if len(matches) < 4 {
227 return fmt.Errorf("invalid hunk header: %s", hunk.Text[0])
228 }
229
230 oldLine, _ := strconv.Atoi(matches[1])
231 oldCnt := 1
232 if matches[2] != "" {
233 oldCnt, _ = strconv.Atoi(matches[2])
234 }
235
236 newLine, _ := strconv.Atoi(matches[3])
237 newCnt := 1
238 if matches[4] != "" {
239 newCnt, _ = strconv.Atoi(matches[4])
240 }
241
242 hunk.OldLine = oldLine
243 hunk.OldCnt = oldCnt
244 hunk.NewLine = newLine
245 hunk.NewCnt = newCnt
246
247 return nil
248}
249
250func (r *Repository) ApplyPatch(patch []byte, mode PatchMode) error {
251 cmd := append(mode.ApplyCmd, "--allow-overlap")

Callers 5

TestParseHunkHeaderFunction · 0.95
parseHunksMethod · 0.95

Calls

no outgoing calls

Tested by 4

TestParseHunkHeaderFunction · 0.76