| 10 | } |
| 11 | |
| 12 | export function computeDiffStats(oldContent: string, newContent: string): DiffStats { |
| 13 | const changes = diffLines(oldContent, newContent); |
| 14 | let additions = 0; |
| 15 | let deletions = 0; |
| 16 | for (const change of changes) { |
| 17 | // Use the diff library's own line count. Counting '\n' characters |
| 18 | // under-counts a final line with no trailing newline (a new file or an |
| 19 | // append with no terminating newline), reporting 0 for a real change. |
| 20 | const lineCount = change.count ?? (change.value.match(/\n/g) ?? []).length; |
| 21 | if (change.added) additions += lineCount; |
| 22 | if (change.removed) deletions += lineCount; |
| 23 | } |
| 24 | return { additions, deletions }; |
| 25 | } |
| 26 | |
| 27 | export interface ColoredDiffLine { |
| 28 | type: 'add' | 'del' | 'context' | 'hunk'; |