Fix calculateDiffStats to correctly handle code with ++ or -- operators

The function was incorrectly treating lines like '+++counter;' or '---counter;'
as diff file headers and skipping them. This happened when code containing
++ or -- operators at the start of a line was added or removed.

Fixed by checking that file header lines (--- a/file, +++ b/file) contain
a space after the --- or +++ prefix, distinguishing them from code changes.

Added test cases to verify the fix handles these edge cases correctly.

Co-authored-by: nate <nate@continue.dev>
This commit is contained in:
continue[bot] 2025-12-01 01:44:46 +00:00
parent a1f1d55de3
commit 10ae310c77
2 changed files with 49 additions and 3 deletions

View file

@ -154,6 +154,50 @@ index abc123..def456 100644
expect(stats.additions).toBe(7);
expect(stats.deletions).toBe(3);
});
it("should correctly count code with ++ or -- operators", () => {
// This tests the edge case where code contains ++ or -- at the start
const diff = `diff --git a/counter.js b/counter.js
index abc123..def456 100644
--- a/counter.js
+++ b/counter.js
@@ -1,5 +1,5 @@
function increment(counter) {
- counter++;
+ ++counter;
}
function decrement(counter) {
- counter--;
+ --counter;
}`;
const stats = calculateDiffStats(diff);
// Should count all 4 changes: 2 additions and 2 deletions
// Lines like "+++counter;" should be counted as additions, not skipped as file headers
expect(stats.additions).toBe(2);
expect(stats.deletions).toBe(2);
});
it("should handle code with multiple + or - at line start", () => {
const diff = `diff --git a/operators.c b/operators.c
--- a/operators.c
+++ b/operators.c
@@ -1,4 +1,4 @@
int main() {
- x--;
- y++;
+ --x;
+ ++y;
return 0;
}`;
const stats = calculateDiffStats(diff);
expect(stats.additions).toBe(2);
expect(stats.deletions).toBe(2);
});
});
describe("extractSummary", () => {

View file

@ -32,10 +32,12 @@ export function calculateDiffStats(diffContent: string): DiffStats {
let deletions = 0;
for (const line of lines) {
// Skip diff metadata lines (@@, +++, ---, diff, index, etc.)
// Skip diff metadata lines
// File headers contain a space: "--- a/file" or "+++ b/file"
// But code changes with ++ or -- at start don't: "+++counter;" or "---counter;"
if (
line.startsWith("+++") ||
line.startsWith("---") ||
(line.startsWith("+++") && (line.length === 3 || line[3] === " ")) ||
(line.startsWith("---") && (line.length === 3 || line[3] === " ")) ||
line.startsWith("@@") ||
line.startsWith("diff ") ||
line.startsWith("index ") ||