Skip to content

Commit f4d1579

Browse files
committed
Parse multiple files in the absence of the Index line to fix #135 issue.
1 parent 19860aa commit f4d1579

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: src/patch/parse.js

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export function parsePatch(uniDiff, options = {}) {
8181
let addCount = 0,
8282
removeCount = 0;
8383
for (; i < diffstr.length; i++) {
84+
// Lines starting with '---' could be mistaken for the "remove line" operation
85+
// But they could be the header for the next file. Therefore prune such cases out.
86+
if (diffstr[i].indexOf('--- ') === 0
87+
&& (i + 2 < diffstr.length)
88+
&& diffstr[i + 1].indexOf('+++ ') === 0
89+
&& diffstr[i + 2].indexOf('@@') === 0) {
90+
break;
91+
}
8492
let operation = diffstr[i][0];
8593

8694
if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') {

Diff for: test/patch/parse.js

+53
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,59 @@ Index: test2
171171
}]);
172172
});
173173

174+
it('should parse multiple files without the Index line', function() {
175+
expect(parsePatch(
176+
`--- from\theader1
177+
+++ to\theader2
178+
@@ -1,3 +1,4 @@
179+
line2
180+
line3
181+
+line4
182+
line5
183+
--- from\theader1
184+
+++ to\theader2
185+
@@ -1,3 +1,4 @@
186+
line2
187+
line3
188+
+line4
189+
line5`))
190+
.to.eql([{
191+
oldFileName: 'from',
192+
oldHeader: 'header1',
193+
newFileName: 'to',
194+
newHeader: 'header2',
195+
hunks: [
196+
{
197+
oldStart: 1, oldLines: 3,
198+
newStart: 1, newLines: 4,
199+
lines: [
200+
' line2',
201+
' line3',
202+
'+line4',
203+
' line5'
204+
]
205+
}
206+
]
207+
}, {
208+
oldFileName: 'from',
209+
oldHeader: 'header1',
210+
newFileName: 'to',
211+
newHeader: 'header2',
212+
hunks: [
213+
{
214+
oldStart: 1, oldLines: 3,
215+
newStart: 1, newLines: 4,
216+
lines: [
217+
' line2',
218+
' line3',
219+
'+line4',
220+
' line5'
221+
]
222+
}
223+
]
224+
}]);
225+
});
226+
174227
it('should note added EOFNL', function() {
175228
expect(parsePatch(
176229
`@@ -1,3 +1,4 @@

0 commit comments

Comments
 (0)