MCPcopy Create free account
hub / github.com/JanSmrcka/differ / ParseDiff

Function ParseDiff

internal/ui/diff.go:40–62  ·  view source on GitHub ↗

ParseDiff parses raw unified diff output into structured lines.

(raw string)

Source from the content-addressed store, hash-verified

38
39// ParseDiff parses raw unified diff output into structured lines.
40func ParseDiff(raw string) ParsedDiff {
41 if strings.Contains(raw, "Binary files") && strings.Contains(raw, "differ") {
42 return ParsedDiff{Binary: true}
43 }
44
45 var lines []DiffLine
46 oldNum, newNum := 0, 0
47
48 for _, line := range strings.Split(raw, "\n") {
49 if len(lines) >= maxDiffLines {
50 lines = append(lines, DiffLine{
51 Type: LineHunkHeader, Content: fmt.Sprintf("… truncated (%d+ lines)", maxDiffLines),
52 OldNum: -1, NewNum: -1,
53 })
54 break
55 }
56 dl := parseDiffLine(line, &oldNum, &newNum)
57 if dl != nil {
58 lines = append(lines, *dl)
59 }
60 }
61 return ParsedDiff{Lines: lines}
62}
63
64func parseDiffLine(line string, oldNum, newNum *int) *DiffLine {
65 switch {

Callers 8

loadDiffCmdMethod · 0.85
TestParseDiff_SimpleHunkFunction · 0.85
TestParseDiff_BinaryFileFunction · 0.85
TestParseDiff_EmptyFunction · 0.85
TestParseDiff_TruncationFunction · 0.85
renderCommitDiffFunction · 0.85

Calls 1

parseDiffLineFunction · 0.85

Tested by 6

TestParseDiff_SimpleHunkFunction · 0.68
TestParseDiff_BinaryFileFunction · 0.68
TestParseDiff_EmptyFunction · 0.68
TestParseDiff_TruncationFunction · 0.68