From 9b71d754cc2d2961bb4b7b8984e87dccacf85d81 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Thu, 11 Dec 2025 12:13:40 +0000 Subject: [PATCH] fix: handle `\ No newline at end of file" markers correctly` lines in diffs --- scripts/utils/Git.ts | 3 +++ tests/unit/GitTest.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/scripts/utils/Git.ts b/scripts/utils/Git.ts index f76f5f2bbbd2..b42217bab587 100644 --- a/scripts/utils/Git.ts +++ b/scripts/utils/Git.ts @@ -254,6 +254,9 @@ class Git { } else if (firstChar === ' ') { // Context line - skip it (we only care about added/removed lines) continue; + } else if (firstChar === '\\') { + // "No newline at end of file" marker - skip it (metadata, not content) + continue; } else { throw new Error(`Unknown line type! First character of line is ${firstChar}`); } diff --git a/tests/unit/GitTest.ts b/tests/unit/GitTest.ts index a6716f238675..42c02ef488bb 100644 --- a/tests/unit/GitTest.ts +++ b/tests/unit/GitTest.ts @@ -508,6 +508,34 @@ describe('Git', () => { expect(file.addedLines.size).toBe(1); expect(file.removedLines.size).toBe(0); }); + + it('handles "No newline at end of file" markers correctly', () => { + const mockDiffOutput = dedent(` + diff --git a/file.ts b/file.ts + index 1234567..abcdefg 100644 + --- a/file.ts + +++ b/file.ts + @@ -1,1 +1,1 @@ + -const old = 'value'; + \\ No newline at end of file + +const new = 'value'; + `); + + mockExecSync.mockReturnValue(mockDiffOutput); + + const result = Git.diff('main'); + const file = result.files.at(0); + expect(file).toBeDefined(); + if (!file) { + return; + } + + expect(file.filePath).toBe('file.ts'); + expect(file.hunks).toHaveLength(1); + expect(file.modifiedLines.size).toBe(1); + // The "No newline" marker should be ignored, not counted as a line + expect(file.hunks.at(0)?.lines.length).toBe(2); // One removed, one added + }); }); describe('fileDiffType', () => {