renderCommitDiff renders a full commit diff (may contain multiple files).
(raw string, styles Styles, t theme.Theme, width int)
| 135 | |
| 136 | // renderCommitDiff renders a full commit diff (may contain multiple files). |
| 137 | func renderCommitDiff(raw string, styles Styles, t theme.Theme, width int) string { |
| 138 | initChromaStyle(t.ChromaStyle) |
| 139 | |
| 140 | var b strings.Builder |
| 141 | // Split by file boundaries and render each section |
| 142 | currentFile := "" |
| 143 | var currentLines []string |
| 144 | |
| 145 | for _, line := range strings.Split(raw, "\n") { |
| 146 | if strings.HasPrefix(line, "diff --git") { |
| 147 | // Flush previous file |
| 148 | if len(currentLines) > 0 { |
| 149 | parsed := ParseDiff(strings.Join(currentLines, "\n")) |
| 150 | b.WriteString(RenderDiff(parsed, currentFile, styles, t, width)) |
| 151 | } |
| 152 | currentFile = extractFilename(line) |
| 153 | // Add file separator |
| 154 | b.WriteString(styles.HeaderBar.Width(width).Render(" " + currentFile)) |
| 155 | b.WriteByte('\n') |
| 156 | currentLines = []string{line} |
| 157 | } else { |
| 158 | currentLines = append(currentLines, line) |
| 159 | } |
| 160 | } |
| 161 | // Flush last file |
| 162 | if len(currentLines) > 0 { |
| 163 | parsed := ParseDiff(strings.Join(currentLines, "\n")) |
| 164 | b.WriteString(RenderDiff(parsed, currentFile, styles, t, width)) |
| 165 | } |
| 166 | return b.String() |
| 167 | } |
| 168 | |
| 169 | // extractFilename pulls the b/ path from "diff --git a/foo b/foo". |
| 170 | func extractFilename(diffHeader string) string { |
no test coverage detected