@@ -306,33 +306,46 @@ jobs:
306
306
}
307
307
}
308
308
309
- // Helper function to calculate diff position
309
+ // Helper function to calculate diff position according to GitHub API
310
310
this.calculateDiffPosition = function(patch, targetLine) {
311
311
const lines = patch.split('\n');
312
312
let position = 0;
313
- let currentLine = 0;
313
+ let currentNewLine = 0;
314
+ let inHunk = false;
314
315
315
316
for (const line of lines) {
316
- position++;
317
+ // Skip file headers (---, +++)
318
+ if (line.startsWith('---') || line.startsWith('+++')) {
319
+ continue;
320
+ }
317
321
318
322
if (line.startsWith('@@')) {
319
323
// Parse hunk header: @@ -oldStart,oldCount +newStart,newCount @@
320
324
const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
321
325
if (match) {
322
- currentLine = parseInt(match[1]) - 1; // Start one before the first line
326
+ currentNewLine = parseInt(match[1]) - 1; // Start one before the first line
327
+ inHunk = true;
328
+ position = 0; // Reset position counter for this hunk
323
329
}
324
330
continue;
325
331
}
326
332
333
+ if (!inHunk) continue;
334
+
335
+ // Increment position for every line in the hunk
336
+ position++;
337
+
327
338
if (line.startsWith('+')) {
328
- currentLine++;
329
- if (currentLine === targetLine) {
339
+ // This is an added line
340
+ currentNewLine++;
341
+ if (currentNewLine === targetLine) {
330
342
return position;
331
343
}
332
344
} else if (line.startsWith(' ')) {
333
- currentLine++;
345
+ // This is a context line (unchanged)
346
+ currentNewLine++;
334
347
}
335
- // Lines starting with '-' don't increment currentLine (they're deleted)
348
+ // Lines starting with '-' don't increment currentNewLine (they're deleted)
336
349
}
337
350
338
351
return -1; // Line not found in diff
0 commit comments