Skip to content

Commit 4a899c0

Browse files
committed
Merge pull request #118 from chaaz/master
Add a fix for applying 0-length destination patches
2 parents c6ac39a + d20f367 commit 4a899c0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/patch/apply.js

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function applyPatch(source, uniDiff, options = {}) {
8181
for (let i = 0; i < hunks.length; i++) {
8282
let hunk = hunks[i],
8383
toPos = hunk.offset + hunk.newStart - 1;
84+
if (hunk.newLines == 0) { toPos++; }
8485

8586
for (let j = 0; j < hunk.lines.length; j++) {
8687
let line = hunk.lines[j],

test/patch/apply.js

+17
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,23 @@ describe('patch/apply', function() {
416416
+ 'line5\n');
417417
});
418418

419+
it('should erase a file', function() {
420+
expect(applyPatch(
421+
'line1\n'
422+
+ 'line2\n'
423+
+ 'line3\n'
424+
+ 'line4\n',
425+
426+
'--- test\theader1\n'
427+
+ '+++ test\theader2\n'
428+
+ '@@ -1,4 +0,0 @@\n'
429+
+ '-line1\n'
430+
+ '-line2\n'
431+
+ '-line3\n'
432+
+ '-line4\n'))
433+
.to.equal('');
434+
});
435+
419436
it('should allow custom line comparison', function() {
420437
expect(applyPatch(
421438
'line2\n'

0 commit comments

Comments
 (0)