MCPcopy
hub / github.com/jesseduffield/lazygit / Parse

Function Parse

pkg/commands/patch/parse.go:12–42  ·  view source on GitHub ↗
(patchStr string)

Source from the content-addressed store, hash-verified

10var hunkHeaderRegexp = regexp.MustCompile(`(?m)^@@ -(\d+)[^\+]+\+(\d+)[^@]+@@(.*)$`)
11
12func Parse(patchStr string) *Patch {
13 // ignore trailing newline.
14 lines := strings.Split(strings.TrimSuffix(patchStr, "\n"), "\n")
15
16 hunks := []*Hunk{}
17 patchHeader := []string{}
18
19 var currentHunk *Hunk
20 for _, line := range lines {
21 if strings.HasPrefix(line, "@@") {
22 oldStart, newStart, headerContext := headerInfo(line)
23
24 currentHunk = &Hunk{
25 oldStart: oldStart,
26 newStart: newStart,
27 headerContext: headerContext,
28 bodyLines: []*PatchLine{},
29 }
30 hunks = append(hunks, currentHunk)
31 } else if currentHunk != nil {
32 currentHunk.bodyLines = append(currentHunk.bodyLines, newHunkLine(line))
33 } else {
34 patchHeader = append(patchHeader, line)
35 }
36 }
37
38 return &Patch{
39 hunks: hunks,
40 header: patchHeader,
41 }
42}
43
44func headerInfo(header string) (int, int, string) {
45 match := hunkHeaderRegexp.FindStringSubmatch(header)

Callers 11

applySelectionMethod · 0.92
editHunkMethod · 0.92
adjustLineNumberMethod · 0.92
NewStateFunction · 0.92
TestTransformFunction · 0.85
TestParseAndFormatPlainFunction · 0.85
TestLineNumberOfLineFunction · 0.85
TestAdjustLineNumberFunction · 0.85
RenderPatchForFileMethod · 0.85

Calls 2

headerInfoFunction · 0.85
newHunkLineFunction · 0.85

Tested by 6

TestTransformFunction · 0.68
TestParseAndFormatPlainFunction · 0.68
TestLineNumberOfLineFunction · 0.68
TestAdjustLineNumberFunction · 0.68