File tree 2 files changed +61
-0
lines changed
2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,14 @@ export function parsePatch(uniDiff, options = {}) {
81
81
let addCount = 0 ,
82
82
removeCount = 0 ;
83
83
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
+ }
84
92
let operation = diffstr [ i ] [ 0 ] ;
85
93
86
94
if ( operation === '+' || operation === '-' || operation === ' ' || operation === '\\' ) {
Original file line number Diff line number Diff line change @@ -171,6 +171,59 @@ Index: test2
171
171
} ] ) ;
172
172
} ) ;
173
173
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
+
174
227
it ( 'should note added EOFNL' , function ( ) {
175
228
expect ( parsePatch (
176
229
`@@ -1,3 +1,4 @@
You can’t perform that action at this time.
0 commit comments