@@ -124,6 +124,10 @@ class PatchParser {
124124 return this . i >= this . lines . length
125125 }
126126
127+ private get isOneLineLeft ( ) {
128+ return this . i === this . lines . length - 1
129+ }
130+
127131 // tslint:disable member-ordering
128132 public parse ( ) {
129133 while ( ! this . isEOF ) {
@@ -207,6 +211,7 @@ class PatchParser {
207211 case "-" :
208212 blockType = "deletion"
209213 break
214+ case undefined :
210215 case " " :
211216 blockType = "context"
212217 break
@@ -218,7 +223,16 @@ class PatchParser {
218223 do {
219224 lines . push ( this . currentLine . slice ( 1 ) )
220225 this . nextLine ( )
221- } while ( ! this . isEOF && this . currentLine . startsWith ( firstChar ) )
226+ } while (
227+ ! this . isEOF &&
228+ // handle empty last line as not part of the context
229+ ! ( this . isOneLineLeft && this . currentLine === "" ) &&
230+ // while we have contiguous hunk line types
231+ ( this . currentLine [ 0 ] === firstChar ||
232+ // handle mismatching context types
233+ ( firstChar === " " && this . currentLine [ 0 ] === undefined ) ||
234+ ( firstChar === undefined && this . currentLine [ 0 ] === " " ) )
235+ )
222236
223237 let noNewlineAtEndOfFile = false
224238 if (
@@ -235,6 +249,22 @@ class PatchParser {
235249 } as PatchMutationPart
236250 }
237251
252+ private currentLineIsPartOfHunk ( ) : boolean {
253+ if ( this . isEOF ) {
254+ return false
255+ }
256+ switch ( this . currentLine [ 0 ] ) {
257+ case undefined :
258+ case " " :
259+ case "+" :
260+ case "-" :
261+ case "\\" :
262+ return true
263+ default :
264+ return false
265+ }
266+ }
267+
238268 private parseFileModification ( ) {
239269 const startPath = this . currentLine . slice ( "--- " . length )
240270 this . nextLine ( )
@@ -302,7 +332,10 @@ class PatchParser {
302332
303333 this . nextLine ( )
304334
305- while ( ! this . isEOF && this . currentLine . match ( / ^ ( \+ | - | | \\ ) .* / ) ) {
335+ while (
336+ this . currentLineIsPartOfHunk ( ) &&
337+ ! ( this . isOneLineLeft && this . currentLine === "" )
338+ ) {
306339 const mutations = this . parsePatchMutationPart ( )
307340 hunkParts . push ( mutations )
308341 }
0 commit comments