(raw: string | null | undefined)
| 18 | } |
| 19 | |
| 20 | export function parseDiff(raw: string | null | undefined): DiffFile[] { |
| 21 | if (!raw || typeof raw !== 'string' || raw.length === 0) { |
| 22 | return []; |
| 23 | } |
| 24 | |
| 25 | const files: DiffFile[] = []; |
| 26 | let currentFile: DiffFile | null = null; |
| 27 | let awaitingNewFilePath = false; |
| 28 | |
| 29 | const finalizeCurrentFile = () => { |
| 30 | if (currentFile && hasRenderableHunks(currentFile)) { |
| 31 | files.push(currentFile); |
| 32 | } |
| 33 | currentFile = null; |
| 34 | awaitingNewFilePath = false; |
| 35 | }; |
| 36 | |
| 37 | for (const line of raw.split('\n')) { |
| 38 | if (line.startsWith('--- a/')) { |
| 39 | finalizeCurrentFile(); |
| 40 | currentFile = { path: line.slice(6), hunks: [] }; |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | if (line === '--- /dev/null') { |
| 45 | finalizeCurrentFile(); |
| 46 | awaitingNewFilePath = true; |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if (line.startsWith('+++ b/')) { |
| 51 | if (awaitingNewFilePath || !currentFile) { |
| 52 | currentFile = { path: line.slice(6), hunks: [] }; |
| 53 | } |
| 54 | awaitingNewFilePath = false; |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | if (line === '+++ /dev/null') { |
| 59 | awaitingNewFilePath = false; |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | if (line.startsWith('@@') && currentFile) { |
| 64 | currentFile.hunks.push({ header: line, lines: [] }); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (!currentFile || currentFile.hunks.length === 0) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | const hunk = currentFile.hunks[currentFile.hunks.length - 1]; |
| 73 | |
| 74 | if (line.startsWith('+')) { |
| 75 | hunk.lines.push({ type: 'add', text: line.slice(1) }); |
| 76 | } else if (line.startsWith('-')) { |
| 77 | hunk.lines.push({ type: 'del', text: line.slice(1) }); |
no test coverage detected