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
2 changes: 1 addition & 1 deletion src/patch/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export function formatPatch(patch: StructuredPatch | StructuredPatch[], headerOp
}
} else {
if (headerOptions.includeIndex && patch.oldFileName == patch.newFileName && patch.oldFileName !== undefined) {
ret.push('Index: ' + patch.oldFileName);
ret.push('Index: ' + quoteFileNameIfNeeded(patch.oldFileName));
}
if (headerOptions.includeUnderline) {
ret.push('===================================================================');
Expand Down
3 changes: 2 additions & 1 deletion src/patch/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ export function parsePatch(uniDiff: string): StructuredPatch[] {
// -- ExplodingCabbage
const headerMatch = (/^(?:Index:|diff(?: -r \w+)+)\s+/).exec(line);
if (headerMatch) {
index.index = line.substring(headerMatch[0].length).trim();
// Inverse of the quoteFileNameIfNeeded applied to the `Index:` line by formatPatch
index.index = unquoteIfQuoted(line.substring(headerMatch[0].length).trim());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve raw Index filenames from older jsdiff output

For a literal filename such as "report", previous versions of formatPatch emitted Index: "report" while quoting the ---/+++ names as "\"report\"". This unconditional decoding now parses such an older jsdiff patch with index === 'report' but oldFileName === '"report"', so consumers that use index to select the target file can load the wrong file. Restrict unquoting to representations that could have been produced by the new quoting behavior, or reconcile the index with the parsed file headers.

Useful? React with 👍 / 👎.

}
}

Expand Down
27 changes: 27 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,33 @@ describe('patch/create', function() {
const diffResult = createTwoFilesPatch('testFileName', 'testFileName', 'foo\n', 'bar\n', undefined, undefined, {headerOptions: OMIT_HEADERS});
expect(diffResult).to.equal(expectedResult);
});

it('should quote the Index line for file names that require quoting, like the --- and +++ lines', function() {
const fileName = 'x\n--- evil';
const patch = createPatch(fileName, 'foo\n', 'bar\n');
expect(patch).to.equal(
'Index: "x\\n--- evil"\n'
+ '===================================================================\n'
+ '--- "x\\n--- evil"\n'
+ '+++ "x\\n--- evil"\n'
+ '@@ -1,1 +1,1 @@\n'
+ '-foo\n'
+ '+bar\n'
);
expect(parsePatch(patch)[0].oldFileName).to.equal(fileName);
});

it('should unquote a quoted Index line when parsing', function() {
const patch =
'Index: "x\\n--- evil"\n'
+ '===================================================================\n'
+ '--- "x\\n--- evil"\n'
+ '+++ "x\\n--- evil"\n'
+ '@@ -1,1 +1,1 @@\n'
+ '-foo\n'
+ '+bar\n';
expect(parsePatch(patch)[0].index).to.equal('x\n--- evil');
});
});

it('should respect maxEditLength', function() {
Expand Down