@@ -391,25 +391,75 @@ const extract_all_code_block_patches = (params: {
391391 const block = code_blocks [ i ]
392392 if ( block . type == 'diff' || block . type == 'patch' ) {
393393 let file_path_hint : string | undefined
394+ let is_new_file_from_hint = false
394395 if ( i > 0 ) {
395396 const prev_block_lines = lines . slice (
396397 code_blocks [ i - 1 ] . start + 1 ,
397398 code_blocks [ i - 1 ] . end
398399 )
399- const xml_path_regex = / < f i l e \s + p a t h = [ " ' ] ( [ ^ " ' ] + ) [ " ' ] /
400+ const xml_path_regex = / < f i l e \s + p a t h = [ " ' ] ( [ ^ " ' ] + ) [ " ' ] ( [ ^ > ] * ) > /
400401 const non_empty_lines = prev_block_lines . filter ( ( l ) => l . trim ( ) != '' )
401402 if ( non_empty_lines . length === 1 ) {
402403 const match = non_empty_lines [ 0 ] . match ( xml_path_regex )
403404 if ( match && match [ 1 ] ) {
404405 file_path_hint = match [ 1 ]
406+ const attributes = match [ 2 ] || ''
407+ if ( / \s n e w \b / . test ( attributes ) ) {
408+ is_new_file_from_hint = true
409+ }
410+ }
411+ }
412+ }
413+
414+ if ( ! file_path_hint && block . start > 0 ) {
415+ for ( let j = block . start - 1 ; j >= Math . max ( 0 , block . start - 5 ) ; j -- ) {
416+ const prev_line = lines [ j ] . trim ( )
417+ if ( ! prev_line ) continue
418+
419+ let extracted = extract_path_from_line_of_code ( prev_line )
420+
421+ if ( ! extracted ) {
422+ let potential_path = prev_line
423+ if ( potential_path . endsWith ( ':' ) ) {
424+ potential_path = potential_path . slice ( 0 , - 1 ) . trim ( )
425+ }
426+ if (
427+ potential_path &&
428+ ( potential_path . includes ( '/' ) ||
429+ potential_path . includes ( '\\' ) ||
430+ potential_path . includes ( '.' ) ) &&
431+ ! potential_path . endsWith ( '.' ) &&
432+ / ^ [ a - z A - Z 0 - 9 _ . / @ - ] + $ / . test ( potential_path )
433+ ) {
434+ extracted = potential_path
435+ }
436+ }
437+
438+ if ( extracted ) {
439+ let all_intermediate_lines_empty = true
440+ for ( let k = j + 1 ; k < block . start ; k ++ ) {
441+ if ( lines [ k ] . trim ( ) !== '' ) {
442+ all_intermediate_lines_empty = false
443+ break
444+ }
445+ }
446+ if ( all_intermediate_lines_empty ) {
447+ file_path_hint = extracted
448+ break
449+ }
405450 }
406451 }
407452 }
408453 const block_lines = lines . slice ( block . start + 1 , block . end )
409454 if ( file_path_hint ) {
410455 const { from_path, to_path } = extract_paths_from_lines ( block_lines )
411456 if ( ! from_path && ! to_path ) {
412- block_lines . unshift ( `--- /dev/null` , `+++ b/${ file_path_hint } ` )
457+ block_lines . unshift (
458+ is_new_file_from_hint
459+ ? '--- /dev/null'
460+ : `--- a/${ file_path_hint } ` ,
461+ `+++ b/${ file_path_hint } `
462+ )
413463 }
414464 }
415465 const parsed_patches = parse_multiple_raw_patches ( {
@@ -456,7 +506,24 @@ const extract_all_code_block_patches = (params: {
456506 }
457507
458508 if ( block . type == 'diff' || block . type == 'patch' ) {
459- items . push ( ...( diff_block_patches . get ( block . start ) || [ ] ) )
509+ const patches = diff_block_patches . get ( block . start ) || [ ]
510+ if ( patches . length > 0 ) {
511+ const last_item = items . length > 0 ? items [ items . length - 1 ] : undefined
512+ if ( last_item && last_item . type == 'text' ) {
513+ let potential_path = last_item . content . trim ( )
514+ if ( potential_path . endsWith ( ':' ) ) {
515+ potential_path = potential_path . slice ( 0 , - 1 ) . trim ( )
516+ }
517+ const { relative_path } = extract_workspace_and_path ( {
518+ raw_file_path : potential_path ,
519+ is_single_root_folder_workspace : params . is_single_root
520+ } )
521+ if ( relative_path == patches [ 0 ] . file_path ) {
522+ items . pop ( )
523+ }
524+ }
525+ }
526+ items . push ( ...patches )
460527 } else {
461528 // Check if this block was a hint for the next block.
462529 if ( i < code_blocks . length - 1 ) {
0 commit comments