Parse `git diff --numstat` output into a path → {additions, deletions} map.
(output: string)
| 109 | /** Parse `git diff --numstat` output into a path → {additions, deletions} map. */ |
| 110 | function parseNumstat(output: string): Map<string, { additions: number; deletions: number }> { |
| 111 | const map = new Map<string, { additions: number; deletions: number }>(); |
| 112 | if (!output.trim()) return map; |
| 113 | for (const line of output.trim().split('\n')) { |
| 114 | // Format: "12\t4\tpath"; binary files: "-\t-\tpath"; renames: "1\t2\told => new" or "1\t2\ta/{b => c}/d" |
| 115 | const parts = line.split('\t'); |
| 116 | if (parts.length < 3) continue; |
| 117 | const additions = parts[0] === '-' ? 0 : Number(parts[0]) || 0; |
| 118 | const deletions = parts[1] === '-' ? 0 : Number(parts[1]) || 0; |
| 119 | let filePath = parts.slice(2).join('\t'); |
| 120 | if (filePath.includes(' => ')) { |
| 121 | filePath = filePath.includes('{') |
| 122 | ? filePath.replace(/\{[^}]*? => ([^}]*?)\}/g, '$1').replace(/\/{2,}/g, '/') |
| 123 | : filePath.split(' => ').pop()!; |
| 124 | } |
| 125 | map.set(filePath, { additions, deletions }); |
| 126 | } |
| 127 | return map; |
| 128 | } |
| 129 | |
| 130 | /** Merge numstat counts into name-status entries by path. */ |
| 131 | function attachNumstat( |
| 132 | entries: ChangeEntry[], |