Skip to content

Commit

Permalink
Merge pull request #136 from ramya-rao-a/parse-multiple-files
Browse files Browse the repository at this point in the history
Parse multiple files in the absence of the Index line to fix #135 issue.
  • Loading branch information
kpdecker authored Oct 1, 2016
2 parents 9e1542f + f4d1579 commit 74e327d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/patch/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '\\') {
Expand Down
53 changes: 53 additions & 0 deletions test/patch/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@
Expand Down

0 comments on commit 74e327d

Please sign in to comment.