Skip to content

Commit

Permalink
Fix: Bad merge when adding new files (kpdecker#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyomitch committed Aug 23, 2017
1 parent 2a7ae3e commit 5f6037e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/patch/apply.js
Expand Up @@ -85,6 +85,10 @@ export function applyPatch(source, uniDiff, options = {}) {
toPos = hunk.oldStart + hunk.offset + diffOffset - 1;
diffOffset += hunk.newLines - hunk.oldLines;

if (toPos < 0) { // Creating a new file
toPos = 0;
}

for (let j = 0; j < hunk.lines.length; j++) {
let line = hunk.lines[j],
operation = line[0],
Expand Down
17 changes: 17 additions & 0 deletions test/patch/apply.js
Expand Up @@ -461,6 +461,23 @@ describe('patch/apply', function() {
+ 'line5\n');
});

it('should create a file', function() {
expect(applyPatch('',

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

it('should erase a file', function() {
expect(applyPatch(
'line1\n'
Expand Down

0 comments on commit 5f6037e

Please sign in to comment.