Skip to content

Commit

Permalink
Update to match quirks of unified diff format (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
aray-openai committed Nov 8, 2020
1 parent e47ca0a commit c64d178
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
let oldEOFNewline = ((/\n$/).test(oldStr));
let newEOFNewline = ((/\n$/).test(newStr));
let noNlBeforeAdds = lines.length == 0 && curRange.length > hunk.oldLines;
if (!oldEOFNewline && noNlBeforeAdds) {
if (!oldEOFNewline && noNlBeforeAdds && oldStr.length > 0) {
// special case: old has no eol and no trailing context; no-nl can end up before adds
// however, if the old file is empty, do not output the no-nl line
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
}
if ((!oldEOFNewline && !noNlBeforeAdds) || !newEOFNewline) {
Expand Down Expand Up @@ -110,6 +111,15 @@ export function formatPatch(diff) {

for (let i = 0; i < diff.hunks.length; i++) {
const hunk = diff.hunks[i];
// Unified Diff Format quirk: If the chunk size is 0,
// the first number is one lower than one would expect.
// https://www.artima.com/weblogs/viewpost.jsp?thread=164293
if (hunk.oldLines === 0) {
hunk.oldStart -= 1;
}
if (hunk.newLines === 0) {
hunk.newStart -= 1;
}
ret.push(
'@@ -' + hunk.oldStart + ',' + hunk.oldLines
+ ' +' + hunk.newStart + ',' + hunk.newLines
Expand Down
14 changes: 12 additions & 2 deletions src/patch/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ export function parsePatch(uniDiff, options = {}) {

let hunk = {
oldStart: +chunkHeader[1],
oldLines: +chunkHeader[2] || 1,
oldLines: typeof chunkHeader[2] === 'undefined' ? 1 : +chunkHeader[2],
newStart: +chunkHeader[3],
newLines: +chunkHeader[4] || 1,
newLines: typeof chunkHeader[4] === 'undefined' ? 1 : +chunkHeader[4],
lines: [],
linedelimiters: []
};

// Unified Diff Format quirk: If the chunk size is 0,
// the first number is one lower than one would expect.
// https://www.artima.com/weblogs/viewpost.jsp?thread=164293
if (hunk.oldLines === 0) {
hunk.oldStart += 1;
}
if (hunk.newLines === 0) {
hunk.newStart += 1;
}

let addCount = 0,
removeCount = 0;
for (; i < diffstr.length; i++) {
Expand Down
154 changes: 154 additions & 0 deletions test/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,160 @@ describe('patch/apply', function() {
+ 'line5\n');
});

it('should apply single line patches with zero context and zero removed', function() {
expect(applyPatch(
'line2\n'
+ 'line3\n'
+ 'line5\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -2,0 +3 @@\n'
+ '+line4\n'))
.to.equal(
'line2\n'
+ 'line3\n'
+ 'line4\n'
+ 'line5\n');
});

it('should apply multiline patches with zero context and zero removed', function() {
expect(applyPatch(
'line2\n'
+ 'line3\n'
+ 'line7\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -2,0 +3,3 @@\n'
+ '+line4\n'
+ '+line5\n'
+ '+line6\n'))
.to.equal(
'line2\n'
+ 'line3\n'
+ 'line4\n'
+ 'line5\n'
+ 'line6\n'
+ 'line7\n');
});

it('should apply single line patches with zero context and zero removed at start of file', function() {
expect(applyPatch(
'line2\n'
+ 'line3\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -0,0 +1 @@\n'
+ '+line1\n'))
.to.equal(
'line1\n'
+ 'line2\n'
+ 'line3\n');
});

it('should apply multi line patches with zero context and zero removed at start of file', function() {
expect(applyPatch(
'line3\n'
+ 'line4\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -0,0 +1,2 @@\n'
+ '+line1\n'
+ '+line2\n'))
.to.equal(
'line1\n'
+ 'line2\n'
+ 'line3\n'
+ 'line4\n');
});

it('should apply multi line patches with zero context and zero removed at end of file', function() {
expect(applyPatch(
'line1\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,0 +2 @@\n'
+ '+line2\n'))
.to.equal(
'line1\n'
+ 'line2\n');
});

it('should apply multi line patches with zero context and zero removed at end of file', function() {
expect(applyPatch(
'line1\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,0 +2,2 @@\n'
+ '+line2\n'
+ '+line3\n'))
.to.equal(
'line1\n'
+ 'line2\n'
+ 'line3\n');
});

it('should apply single line patches with zero context and zero added at beginning of file', function() {
expect(applyPatch(
'line1\n'
+ 'line2\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1 +0,0 @@\n'
+ '-line1\n'))
.to.equal(
'line2\n');
});

it('should apply multi line patches with zero context and zero added at beginning of file', function() {
expect(applyPatch(
'line1\n'
+ 'line2\n'
+ 'line3\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,2 +0,0 @@\n'
+ '-line1\n'
+ '-line2\n'))
.to.equal(
'line3\n');
});

it('should apply single line patches with zero context and zero added at end of file', function() {
expect(applyPatch(
'line1\n'
+ 'line2\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -2 +1,0 @@\n'
+ '-line2\n'))
.to.equal(
'line1\n');
});

it('should apply multi line patches with zero context and zero added at end of file', function() {
expect(applyPatch(
'line1\n'
+ 'line2\n'
+ 'line3\n',

'--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -2,2 +1,0 @@\n'
+ '-line2\n'
+ '-line3\n'))
.to.equal(
'line1\n');
});

it('should fail on mismatch', function() {
expect(applyPatch(
'line2\n'
Expand Down
15 changes: 7 additions & 8 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ describe('patch/create', function() {
+ '\\ No newline at end of file\n');
});

it('should output only one "no newline" at end of file message on empty file', function() {
it('should output no "no newline" at end of file message on empty file', function() {
expect(createPatch('test', '', 'line1\nline2\nline3\nline4', 'header1', 'header2')).to.equal(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,0 +1,4 @@\n'
+ '\\ No newline at end of file\n'
+ '@@ -0,0 +1,4 @@\n'
+ '+line1\n'
+ '+line2\n'
+ '+line3\n'
Expand All @@ -139,7 +138,7 @@ describe('patch/create', function() {
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,0 @@\n'
+ '@@ -1,4 +0,0 @@\n'
+ '-line1\n'
+ '-line2\n'
+ '-line3\n'
Expand Down Expand Up @@ -528,7 +527,7 @@ describe('patch/create', function() {
expect(diffResult).to.equal(expectedResult);
});

it('should generatea a patch with context size 0', function() {
it('should generate a patch with context size 0', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
Expand All @@ -538,11 +537,11 @@ describe('patch/create', function() {
+ '-value\n'
+ '+new value\n'
+ '+new value 2\n'
+ '@@ -11,1 +12,0 @@\n'
+ '@@ -11,1 +11,0 @@\n'
+ '-remove value\n'
+ '@@ -21,1 +21,0 @@\n'
+ '@@ -21,1 +20,0 @@\n'
+ '-remove value\n'
+ '@@ -30,0 +29,1 @@\n'
+ '@@ -29,0 +29,1 @@\n'
+ '+add value\n'
+ '@@ -34,1 +34,2 @@\n'
+ '-value\n'
Expand Down

0 comments on commit c64d178

Please sign in to comment.