Elffuss BraidHub

kikocis/continue public

87f75892af56 · 2.1 KiBRawPermalink
1#!/usr/bin/env node
2
3/**
4 * Annotates a git diff with GitHub API positions
5 *
6 * Takes a raw diff and adds [POS:N] markers with the correct
7 * GitHub API positions (cumulative, with +1 between hunks)
8 */
9
10const fs = require("fs");
11
12function annotateDiff(diffContent) {
13 const lines = diffContent.split("\n");
14 const output = [];
15 let githubPos = 0;
16 let inHunk = false;
17
18 for (const line of lines) {
19 // Handle new file - reset position counter
20 if (line.startsWith("diff --git")) {
21 githubPos = 0;
22 inHunk = false;
23 output.push(line);
24 continue;
25 }
26
27 // Pass through other git headers unchanged
28 if (
29 line.startsWith("index ") ||
30 line.startsWith("---") ||
31 line.startsWith("+++") ||
32 line.startsWith("rename ") ||
33 line.startsWith("similarity ") ||
34 line.startsWith("new file") ||
35 line.startsWith("deleted file") ||
36 line.startsWith("old mode") ||
37 line.startsWith("new mode") ||
38 line.startsWith("Binary files")
39 ) {
40 output.push(line);
41 continue;
42 }
43
44 // Handle hunk headers
45 if (line.startsWith("@@")) {
46 if (inHunk) {
47 githubPos++; // GitHub's +1 between hunks
48 }
49 inHunk = true;
50 output.push(line);
51 continue;
52 }
53
54 // Handle "no newline" marker - it counts as a position but isn't annotated
55 if (line.startsWith("\\ No newline")) {
56 if (inHunk) {
57 githubPos++; // GitHub counts this as a position
58 }
59 output.push(line); // Don't annotate it
60 continue;
61 }
62
63 // Annotate actual diff lines with GitHub positions
64 if (inHunk) {
65 githubPos++;
66 output.push(`[POS:${githubPos}] ${line}`);
67 } else {
68 output.push(line);
69 }
70 }
71
72 return output.join("\n");
73}
74
75// Main
76function main() {
77 // Read diff from stdin or file
78 let input = "";
79
80 if (process.argv.length > 2) {
81 // Read from file
82 input = fs.readFileSync(process.argv[2], "utf8");
83 } else {
84 // Read from stdin
85 input = fs.readFileSync(0, "utf8");
86 }
87
88 const annotated = annotateDiff(input);
89 console.log(annotated);
90}
91
92if (require.main === module) {
93 main();
94}
95
96module.exports = { annotateDiff };