| 29 | } |
| 30 | |
| 31 | export function parseDiffToFiles(rawPatch: string): DiffFile[] { |
| 32 | const files: DiffFile[] = []; |
| 33 | |
| 34 | for (const chunk of splitDiffChunks(rawPatch)) { |
| 35 | const lines = chunk.split('\n'); |
| 36 | const fromFileLines = parseDiffFilePathLines(lines); |
| 37 | const fromHeader = parseDiffGitHeader(lines[0] ?? ''); |
| 38 | const oldPath = fromFileLines.oldPath ?? fromFileLines.newPath ?? fromHeader.oldPath; |
| 39 | const newPath = fromFileLines.newPath ?? fromFileLines.oldPath ?? fromHeader.newPath; |
| 40 | if (!oldPath || !newPath) continue; |
| 41 | |
| 42 | let additions = 0; |
| 43 | let deletions = 0; |
| 44 | |
| 45 | for (const line of lines) { |
| 46 | if (line.startsWith('+') && !line.startsWith('+++')) additions += 1; |
| 47 | if (line.startsWith('-') && !line.startsWith('---')) deletions += 1; |
| 48 | } |
| 49 | |
| 50 | files.push({ |
| 51 | path: newPath, |
| 52 | oldPath: oldPath !== newPath ? oldPath : undefined, |
| 53 | patch: chunk, |
| 54 | additions, |
| 55 | deletions, |
| 56 | status: deriveStatus(lines, oldPath, newPath), |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | return files; |
| 61 | } |