Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scripts/utils/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/GitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!file) {
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The "No newline" marker should be ignored, not counted as a line
// 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', () => {
Expand Down
Loading