@@ -8,6 +8,42 @@ import { create_safe_path } from '@/utils/path-sanitizer'
88import { apply_diff } from '../utils/edit-formats/diffs'
99import { remove_directory_if_empty } from '../utils/file-operations'
1010
11+ export const sanitize_patch_content = (
12+ patch_content : string ,
13+ workspace_name ?: string
14+ ) : string => {
15+ if ( ! workspace_name ) return patch_content
16+
17+ const lines = patch_content . split ( '\n' )
18+ return lines . map ( line => {
19+ if ( line . startsWith ( '--- a/' ) ) {
20+ const match = line . match ( / ^ - - - a \/ ( .+ ) $ / )
21+ if ( match && match [ 1 ] . startsWith ( `${ workspace_name } /` ) ) {
22+ return `--- a/${ match [ 1 ] . substring ( workspace_name . length + 1 ) } `
23+ }
24+ } else if ( line . startsWith ( '+++ b/' ) ) {
25+ const match = line . match ( / ^ \+ \+ \+ b \/ ( .+ ) $ / )
26+ if ( match && match [ 1 ] . startsWith ( `${ workspace_name } /` ) ) {
27+ return `+++ b/${ match [ 1 ] . substring ( workspace_name . length + 1 ) } `
28+ }
29+ } else if ( line . startsWith ( 'diff --git ' ) ) {
30+ const match = line . match ( / ^ d i f f - - g i t a \/ ( .+ ) b \/ ( .+ ) $ / )
31+ if ( match ) {
32+ let path_a = match [ 1 ]
33+ let path_b = match [ 2 ]
34+ if ( path_a . startsWith ( `${ workspace_name } /` ) ) {
35+ path_a = path_a . substring ( workspace_name . length + 1 )
36+ }
37+ if ( path_b . startsWith ( `${ workspace_name } /` ) ) {
38+ path_b = path_b . substring ( workspace_name . length + 1 )
39+ }
40+ return `diff --git a/${ path_a } b/${ path_b } `
41+ }
42+ }
43+ return line
44+ } ) . join ( '\n' )
45+ }
46+
1147const INDENTATION_BASED_EXTENSIONS = new Set ( [ '.py' , '.yaml' , '.yml' ] )
1248
1349const is_indentation_based_file = ( file_path : string ) : boolean => {
@@ -23,39 +59,6 @@ interface PatchFileInfo {
2359 is_renaming ?: boolean
2460}
2561
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-
5962const parse_patch_header = (
6063 patch_content : string ,
6164 workspace_path ?: string
@@ -75,7 +78,7 @@ const parse_patch_header = (
7578 if ( line == '--- /dev/null' ) {
7679 is_new = true
7780 } else {
78- const from_match = line . match ( / ^ - - - (?: a \/ ) ? ( .+ ) $ / )
81+ const from_match = line . match ( / ^ - - - a \/ ( .+ ) $ / )
7982 if ( from_match && from_match [ 1 ] ) {
8083 from_path = from_match [ 1 ]
8184 }
@@ -85,7 +88,7 @@ const parse_patch_header = (
8588 if ( line == '+++ /dev/null' ) {
8689 is_deleted = true
8790 } else {
88- const match = line . match ( / ^ \+ \+ \+ (?: b \/ ) ? ( .+ ) $ / )
91+ const match = line . match ( / ^ \+ \+ \+ b \/ ( .+ ) $ / )
8992 if ( match && match [ 1 ] ) {
9093 to_path = match [ 1 ]
9194 }
@@ -483,7 +486,6 @@ export const apply_git_patch = async (
483486 original_states ?: OriginalFileState [ ]
484487 diff_application_method ?: 'recount' | 'search_and_replace'
485488} > => {
486- patch_content = normalize_patch_paths ( patch_content , workspace_path )
487489 const patch_info = parse_patch_header ( patch_content , workspace_path )
488490
489491 if ( patch_info . is_new ) {
0 commit comments