Skip to content
Open
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
80 changes: 80 additions & 0 deletions src/__tests__/diff-parser-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2514,5 +2514,85 @@ describe('DiffParser', () => {
]
`);
});

it('should keep "\\ No newline at end of file" text inside content lines', () => {
const diff =
'diff --git a/fix.patch b/fix.patch\n' +
'index 0000001..0ddf2ba 100644\n' +
'--- a/fix.patch\n' +
'+++ b/fix.patch\n' +
'@@ -1,4 +1,4 @@\n' +
' @@ -1 +1 @@\n' +
'-+old\n' +
'-\\ No newline at end of file\n' +
'++new\n' +
'+\\ No newline at end of file\n' +
' \\ No newline at end of file\n' +
'\\ No newline at end of file\n';

const result = parse(diff);
expect(result).toMatchInlineSnapshot(`
[
{
"addedLines": 2,
"blocks": [
{
"header": "@@ -1,4 +1,4 @@",
"lines": [
{
"content": " @@ -1 +1 @@",
"newNumber": 1,
"oldNumber": 1,
"type": "context",
},
{
"content": "-+old",
"newNumber": undefined,
"oldNumber": 2,
"type": "delete",
},
{
"content": "-\\ No newline at end of file",
"newNumber": undefined,
"oldNumber": 3,
"type": "delete",
},
{
"content": "++new",
"newNumber": 2,
"oldNumber": undefined,
"type": "insert",
},
{
"content": "+\\ No newline at end of file",
"newNumber": 3,
"oldNumber": undefined,
"type": "insert",
},
{
"content": " \\ No newline at end of file",
"newNumber": 4,
"oldNumber": 4,
"type": "context",
},
],
"newStartLine": 1,
"oldStartLine": 1,
"oldStartLine2": null,
},
],
"checksumAfter": "0ddf2ba",
"checksumBefore": "0000001",
"deletedLines": 2,
"isCombined": false,
"isGitDiff": true,
"language": "patch",
"mode": "100644",
"newName": "fix.patch",
"oldName": "fix.patch",
},
]
`);
});
});
});
9 changes: 4 additions & 5 deletions src/diff-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
const combinedNewFile = /^new file mode (\d{6})/;
const combinedDeletedFile = /^deleted file mode (\d{6}),(\d{6})/;

const diffLines = diffInput
.replace(/\\ No newline at end of file/g, '')
.replace(/\r\n?/g, '\n')
.split('\n');
const diffLines = diffInput.replace(/\r\n?/g, '\n').split('\n');

/* Add previous block(if exists) before start a new file */
function saveBlock(): void {
Expand Down Expand Up @@ -261,7 +258,9 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
// Unmerged paths, and possibly other non-diffable files
// https://github.com/scottgonzalez/pretty-diff/issues/11
// Also, remove some useless lines
if (!line || line.startsWith('*')) {
// Lines starting with "\ " are markers like "\ No newline at end of file"
// (the message text can be translated, so only the prefix is checked)
if (!line || line.startsWith('*') || line.startsWith('\\ ')) {
return;
}

Expand Down