From 15fef933f4a8bd540cfa25bfc794e7ca8687d780 Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Wed, 23 Aug 2017 14:09:01 +0100 Subject: [PATCH] Fix: Bad merge when adding new files (#189) --- src/patch/apply.js | 4 ++++ test/patch/apply.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/patch/apply.js b/src/patch/apply.js index e6509260..e830fc11 100644 --- a/src/patch/apply.js +++ b/src/patch/apply.js @@ -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], diff --git a/test/patch/apply.js b/test/patch/apply.js index 96ff4703..67965662 100755 --- a/test/patch/apply.js +++ b/test/patch/apply.js @@ -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'