Skip to content

Commit

Permalink
add script to parse gitdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Nov 7, 2024
1 parent 5450d47 commit f1828f7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/parseDiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const fs = require('fs/promises')

function parseFilePath(filePathLine) {
return filePathLine.split(' ')[2].split('/').slice(1).join('/')
}

function parseRange(rangeLine) {
const [_fromRange, toRange] = rangeLine.split(' ').slice(1, 3)
const [startLine, numLines] = toRange.slice(1).split(',').map(Number)
const range = [startLine, startLine + numLines]
return range
}

async function parseChanges(diffPath) {
const diff = await fs.readFileSync(diffPath, 'utf8')
const lines = diff.split('\n')
let currentFile = null
let currentRange = null
let changes = []

for (const line of lines) {
if (line.startsWith('diff')) {
currentFile = parseFilePath(line)
currentRange = null
}
if (line.startsWith('@@')) {
currentRange = parseRange(line)
changes.push({ file: currentFile, range: currentRange })
}
}

return changes
}

function formatChanges(changes) {
return changes.map((change) => `${change.file}#L${change.range[0]}-L${change.range[1]}`).join('\n')
}

async function main() {
const rawDiffPath = process.argv[2]
console.log('Recieved diff path: %s', rawDiffPath)
const changes = await parseChanges(rawDiffPath)
const output = formatChanges(changes)

await fs.writeFile('diff-parsed.txt', output)
}

void main()
3 changes: 3 additions & 0 deletions .github/workflows/parseJscpd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
async function main() {}

void main()

0 comments on commit f1828f7

Please sign in to comment.