ParseDiff parses raw unified diff output into structured lines.
(raw string)
| 38 | |
| 39 | // ParseDiff parses raw unified diff output into structured lines. |
| 40 | func 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 | |
| 64 | func parseDiffLine(line string, oldNum, newNum *int) *DiffLine { |
| 65 | switch { |