fix: diff calculations

This commit is contained in:
Tomasz Stefaniak 2025-08-14 10:05:10 -07:00
parent 833443cacf
commit d6ff7dae2d
2 changed files with 86 additions and 18 deletions

View file

@ -59,22 +59,8 @@ jobs:
# Get PR diff with context
gh pr diff $PR_NUMBER --color=never > pr_diff.txt
# Annotate diff with position numbers for easy reference
awk '
/^@@/ {
pos = 0
print
next
}
/^diff --git/ || /^index / || /^--- / || /^\+\+\+ / {
print
next
}
{
pos++
printf "[POS:%d] %s\n", pos, $0
}
' pr_diff.txt > pr_diff_annotated.txt
# Annotate diff with GitHub API positions
node ${{ github.workspace }}/pr-review-action/scripts/annotate-diff.js pr_diff.txt > pr_diff_annotated.txt
# Create review prompt for inline comments
cat > inline_review_prompt.txt << 'EOF'
@ -172,7 +158,7 @@ jobs:
echo "Invalid JSON output from Continue CLI"
echo "{\"review_summary\": \"Failed to generate review\", \"comments\": []}" > inline_review.json
else
# Use the output directly since positions are already correct
# Positions are already correct from our annotation
cp inline_review_raw.json inline_review.json
echo "Review output:"
cat inline_review.json
@ -302,7 +288,6 @@ jobs:
inline_review_raw.json
inline_review.json
inline_review_prompt.txt
pr_files.json
pr_diff.txt
pr_diff_annotated.txt
retention-days: 7

View file

@ -0,0 +1,83 @@
#!/usr/bin/env node
/**
* Annotates a git diff with GitHub API positions
*
* Takes a raw diff and adds [POS:N] markers with the correct
* GitHub API positions (cumulative, with +1 between hunks)
*/
const fs = require('fs');
function annotateDiff(diffContent) {
const lines = diffContent.split('\n');
const output = [];
let githubPos = 0;
let inHunk = false;
for (const line of lines) {
// Pass through git headers unchanged
if (line.startsWith('diff --git') ||
line.startsWith('index ') ||
line.startsWith('---') ||
line.startsWith('+++') ||
line.startsWith('rename ') ||
line.startsWith('similarity ') ||
line.startsWith('new file') ||
line.startsWith('deleted file') ||
line.startsWith('old mode') ||
line.startsWith('new mode')) {
output.push(line);
continue;
}
// Handle hunk headers
if (line.startsWith('@@')) {
if (inHunk) {
githubPos++; // GitHub's +1 between hunks
}
inHunk = true;
output.push(line);
continue;
}
// Skip "no newline" marker
if (line.startsWith('\\ No newline')) {
output.push(line);
continue;
}
// Annotate actual diff lines with GitHub positions
if (inHunk) {
githubPos++;
output.push(`[POS:${githubPos}] ${line}`);
} else {
output.push(line);
}
}
return output.join('\n');
}
// Main
function main() {
// Read diff from stdin or file
let input = '';
if (process.argv.length > 2) {
// Read from file
input = fs.readFileSync(process.argv[2], 'utf8');
} else {
// Read from stdin
input = fs.readFileSync(0, 'utf8');
}
const annotated = annotateDiff(input);
console.log(annotated);
}
if (require.main === module) {
main();
}
module.exports = { annotateDiff };