@@ -303,12 +303,25 @@ const parse_multiple_raw_patches = (all_lines: string[]): Diff[] => {
303303 )
304304
305305 if ( contains_main_header && contains_plus_plus_plus ) {
306- const patch_info = process_collected_patch_lines ( current_patch_lines )
307- if ( patch_info ) {
308- patches . push ( patch_info )
306+ let should_split = false
307+ if ( line . startsWith ( 'diff --git ' ) ) {
308+ should_split = true
309+ } else if ( line . startsWith ( '--- ' ) ) {
310+ const contains_hunk = current_patch_lines . some ( ( l ) =>
311+ l . startsWith ( '@@' )
312+ )
313+ if ( contains_hunk ) {
314+ should_split = true
315+ }
316+ }
317+ if ( should_split ) {
318+ const patch_info = process_collected_patch_lines ( current_patch_lines )
319+ if ( patch_info ) {
320+ patches . push ( patch_info )
321+ }
322+ current_patch_lines = [ line ]
323+ continue
309324 }
310- current_patch_lines = [ line ]
311- continue
312325 }
313326 }
314327 current_patch_lines . push ( line )
@@ -399,13 +412,42 @@ const build_patch_content = (
399412 let patch_content : string
400413
401414 if ( patch_start_index >= 0 ) {
402- const patch_lines = lines . slice ( patch_start_index ) . map ( ( line ) => {
403- if ( line . startsWith ( '--- ' ) || line . startsWith ( '+++ ' ) ) {
404- return normalize_header_line ( line )
415+ let patch_lines = lines . slice ( patch_start_index )
416+
417+ const hunk_start_idx = patch_lines . findIndex ( ( line ) =>
418+ line . startsWith ( '@@' )
419+ )
420+
421+ if ( hunk_start_idx > 0 ) {
422+ const header_lines = patch_lines . slice ( 0 , hunk_start_idx )
423+ const body_lines = patch_lines . slice ( hunk_start_idx )
424+
425+ let from_line : string | undefined
426+ let to_line : string | undefined
427+ let diff_git_line : string | undefined
428+
429+ for ( const line of header_lines ) {
430+ if ( line . startsWith ( '--- ' ) ) from_line = line
431+ if ( line . startsWith ( '+++ ' ) ) to_line = line
432+ if ( line . startsWith ( 'diff --git ' ) ) diff_git_line = line
405433 }
406- return line
407- } )
408- patch_content = patch_lines . join ( '\n' )
434+
435+ const final_header : string [ ] = [ ]
436+ if ( diff_git_line ) final_header . push ( diff_git_line )
437+ if ( from_line ) final_header . push ( from_line )
438+ if ( to_line ) final_header . push ( to_line )
439+
440+ patch_lines = [ ...final_header , ...body_lines ]
441+ }
442+
443+ patch_content = patch_lines
444+ . map ( ( line ) => {
445+ if ( line . startsWith ( '--- ' ) || line . startsWith ( '+++ ' ) ) {
446+ return normalize_header_line ( line )
447+ }
448+ return line
449+ } )
450+ . join ( '\n' )
409451 } else {
410452 const content_start_index = lines . findIndex ( ( line ) => line . startsWith ( '@@' ) )
411453
0 commit comments