Skip to content

Commit 5f6037e

Browse files
committed
Fix: Bad merge when adding new files (kpdecker#189)
1 parent 2a7ae3e commit 5f6037e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/patch/apply.js

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export function applyPatch(source, uniDiff, options = {}) {
8585
toPos = hunk.oldStart + hunk.offset + diffOffset - 1;
8686
diffOffset += hunk.newLines - hunk.oldLines;
8787

88+
if (toPos < 0) { // Creating a new file
89+
toPos = 0;
90+
}
91+
8892
for (let j = 0; j < hunk.lines.length; j++) {
8993
let line = hunk.lines[j],
9094
operation = line[0],

test/patch/apply.js

+17
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ describe('patch/apply', function() {
461461
+ 'line5\n');
462462
});
463463

464+
it('should create a file', function() {
465+
expect(applyPatch('',
466+
467+
'--- test\theader1\n'
468+
+ '+++ test\theader2\n'
469+
+ '@@ -0,0 +1,4 @@\n'
470+
+ '+line1\n'
471+
+ '+line2\n'
472+
+ '+line3\n'
473+
+ '+line4\n'))
474+
.to.equal(
475+
'line1\n'
476+
+ 'line2\n'
477+
+ 'line3\n'
478+
+ 'line4\n');
479+
});
480+
464481
it('should erase a file', function() {
465482
expect(applyPatch(
466483
'line1\n'

0 commit comments

Comments
 (0)