@@ -23,6 +23,39 @@ interface PatchFileInfo {
2323 is_renaming ?: boolean
2424}
2525
26+ export const normalize_patch_paths = (
27+ patch_content : string ,
28+ workspace_path : string
29+ ) : string => {
30+ const workspace_name = path . basename ( workspace_path )
31+ const lines = patch_content . split ( '\n' )
32+ const normalized_lines = lines . map ( ( line ) => {
33+ if ( line . startsWith ( '--- ' ) && line != '--- /dev/null' ) {
34+ const match = line . match ( / ^ - - - ( a \/ ) ? ( .+ ) $ / )
35+ if ( match ) {
36+ const prefix = match [ 1 ] || 'a/'
37+ let path_part = match [ 2 ]
38+ if ( path_part . startsWith ( `${ workspace_name } /` ) ) {
39+ path_part = path_part . substring ( workspace_name . length + 1 )
40+ }
41+ return `--- ${ prefix } ${ path_part } `
42+ }
43+ } else if ( line . startsWith ( '+++ ' ) && line != '+++ /dev/null' ) {
44+ const match = line . match ( / ^ \+ \+ \+ ( b \/ ) ? ( .+ ) $ / )
45+ if ( match ) {
46+ const prefix = match [ 1 ] || 'b/'
47+ let path_part = match [ 2 ]
48+ if ( path_part . startsWith ( `${ workspace_name } /` ) ) {
49+ path_part = path_part . substring ( workspace_name . length + 1 )
50+ }
51+ return `+++ ${ prefix } ${ path_part } `
52+ }
53+ }
54+ return line
55+ } )
56+ return normalized_lines . join ( '\n' )
57+ }
58+
2659const parse_patch_header = (
2760 patch_content : string ,
2861 workspace_path ?: string
@@ -42,7 +75,7 @@ const parse_patch_header = (
4275 if ( line == '--- /dev/null' ) {
4376 is_new = true
4477 } else {
45- const from_match = line . match ( / ^ - - - a \/ ( .+ ) $ / )
78+ const from_match = line . match ( / ^ - - - (?: a \/ ) ? ( .+ ) $ / )
4679 if ( from_match && from_match [ 1 ] ) {
4780 from_path = from_match [ 1 ]
4881 }
@@ -52,7 +85,7 @@ const parse_patch_header = (
5285 if ( line == '+++ /dev/null' ) {
5386 is_deleted = true
5487 } else {
55- const match = line . match ( / ^ \+ \+ \+ b \/ ( .+ ) $ / )
88+ const match = line . match ( / ^ \+ \+ \+ (?: b \/ ) ? ( .+ ) $ / )
5689 if ( match && match [ 1 ] ) {
5790 to_path = match [ 1 ]
5891 }
@@ -450,6 +483,7 @@ export const apply_git_patch = async (
450483 original_states ?: OriginalFileState [ ]
451484 diff_application_method ?: 'recount' | 'search_and_replace'
452485} > => {
486+ patch_content = normalize_patch_paths ( patch_content , workspace_path )
453487 const patch_info = parse_patch_header ( patch_content , workspace_path )
454488
455489 if ( patch_info . is_new ) {
0 commit comments