|
| 1 | +const fs = require('fs/promises') |
| 2 | + |
| 3 | +function parseFilePath(filePathLine) { |
| 4 | + return filePathLine.split(' ')[2].split('/').slice(1).join('/') |
| 5 | +} |
| 6 | + |
| 7 | +function parseRange(rangeLine) { |
| 8 | + const [_fromRange, toRange] = rangeLine.split(' ').slice(1, 3) |
| 9 | + const [startLine, numLines] = toRange.slice(1).split(',').map(Number) |
| 10 | + const range = [startLine, startLine + numLines] |
| 11 | + return range |
| 12 | +} |
| 13 | + |
| 14 | +async function parseChanges(diffPath) { |
| 15 | + const diff = await fs.readFileSync(diffPath, 'utf8') |
| 16 | + const lines = diff.split('\n') |
| 17 | + let currentFile = null |
| 18 | + let currentRange = null |
| 19 | + let changes = [] |
| 20 | + |
| 21 | + for (const line of lines) { |
| 22 | + if (line.startsWith('diff')) { |
| 23 | + currentFile = parseFilePath(line) |
| 24 | + currentRange = null |
| 25 | + } |
| 26 | + if (line.startsWith('@@')) { |
| 27 | + currentRange = parseRange(line) |
| 28 | + changes.push({ file: currentFile, range: currentRange }) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return changes |
| 33 | +} |
| 34 | + |
| 35 | +function formatChanges(changes) { |
| 36 | + return changes.map((change) => `${change.file}#L${change.range[0]}-L${change.range[1]}`).join('\n') |
| 37 | +} |
| 38 | + |
| 39 | +async function main() { |
| 40 | + const rawDiffPath = process.argv[2] |
| 41 | + console.log('Recieved diff path: %s', rawDiffPath) |
| 42 | + const changes = await parseChanges(rawDiffPath) |
| 43 | + const output = formatChanges(changes) |
| 44 | + |
| 45 | + await fs.writeFile('diff-parsed.txt', output) |
| 46 | +} |
| 47 | + |
| 48 | +void main() |
0 commit comments