From f4d1579634acd34c84a4932f91d03fc2ad31e66e Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Fri, 16 Sep 2016 11:35:04 -0700 Subject: [PATCH] Parse multiple files in the absence of the Index line to fix #135 issue. --- src/patch/parse.js | 8 +++++++ test/patch/parse.js | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/patch/parse.js b/src/patch/parse.js index cad784f4..6e546d02 100755 --- a/src/patch/parse.js +++ b/src/patch/parse.js @@ -81,6 +81,14 @@ export function parsePatch(uniDiff, options = {}) { let addCount = 0, removeCount = 0; for (; i < diffstr.length; i++) { + // Lines starting with '---' could be mistaken for the "remove line" operation + // But they could be the header for the next file. Therefore prune such cases out. + if (diffstr[i].indexOf('--- ') === 0 + && (i + 2 < diffstr.length) + && diffstr[i + 1].indexOf('+++ ') === 0 + && diffstr[i + 2].indexOf('@@') === 0) { + break; + } let operation = diffstr[i][0]; if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') { diff --git a/test/patch/parse.js b/test/patch/parse.js index fc06afca..b5361634 100644 --- a/test/patch/parse.js +++ b/test/patch/parse.js @@ -171,6 +171,59 @@ Index: test2 }]); }); + it('should parse multiple files without the Index line', function() { + expect(parsePatch( +`--- from\theader1 ++++ to\theader2 +@@ -1,3 +1,4 @@ + line2 + line3 ++line4 + line5 +--- from\theader1 ++++ to\theader2 +@@ -1,3 +1,4 @@ + line2 + line3 ++line4 + line5`)) + .to.eql([{ + oldFileName: 'from', + oldHeader: 'header1', + newFileName: 'to', + newHeader: 'header2', + hunks: [ + { + oldStart: 1, oldLines: 3, + newStart: 1, newLines: 4, + lines: [ + ' line2', + ' line3', + '+line4', + ' line5' + ] + } + ] + }, { + oldFileName: 'from', + oldHeader: 'header1', + newFileName: 'to', + newHeader: 'header2', + hunks: [ + { + oldStart: 1, oldLines: 3, + newStart: 1, newLines: 4, + lines: [ + ' line2', + ' line3', + '+line4', + ' line5' + ] + } + ] + }]); + }); + it('should note added EOFNL', function() { expect(parsePatch( `@@ -1,3 +1,4 @@