Skip to content

Commit b398731

Browse files
committed
Implement sanitize_patch_content to remove workspace name prefixes from diff paths before applying patches
1 parent 631129d commit b398731

2 files changed

Lines changed: 46 additions & 40 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/handlers/diff-handler.ts‎

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ import { create_safe_path } from '@/utils/path-sanitizer'
88
import { apply_diff } from '../utils/edit-formats/diffs'
99
import { 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(/^diff --git 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+
1147
const INDENTATION_BASED_EXTENSIONS = new Set(['.py', '.yaml', '.yml'])
1248

1349
const 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-
5962
const 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) {

‎apps/editor/src/commands/apply-chat-response-command/response-processor.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { create_safe_path } from '@/utils/path-sanitizer'
1414
import { dictionary } from '@shared/constants/dictionary'
1515
import { Logger } from '@shared/utils/logger'
16-
import { apply_git_patch } from './handlers/diff-handler'
16+
import { apply_git_patch, sanitize_patch_content } from './handlers/diff-handler'
1717
import { apply_file_relocations } from './utils/file-operations'
1818
import { ModelProvidersManager } from '@/services/model-providers-manager'
1919
import { get_intelligent_update_config } from '@/utils/intelligent-update-utils'
@@ -320,13 +320,17 @@ export const process_chat_response = async (params: {
320320
workspace_path = workspace_map.get(patch.workspace_name)!
321321
}
322322

323-
const result = await apply_git_patch(patch.content, workspace_path)
323+
const sanitized_patch_content = sanitize_patch_content(
324+
patch.content,
325+
patch.workspace_name
326+
)
327+
const result = await apply_git_patch(sanitized_patch_content, workspace_path)
324328

325329
if (result.success) {
326330
if (result.diff_application_method && result.original_states) {
327331
for (const state of result.original_states) {
328332
state.diff_application_method = result.diff_application_method
329-
state.ai_content = patch.content
333+
state.ai_content = sanitized_patch_content
330334
}
331335
}
332336
if (result.original_states) {
@@ -346,7 +350,7 @@ export const process_chat_response = async (params: {
346350
if (result.original_states) {
347351
for (const state of result.original_states) {
348352
state.apply_failed = true
349-
state.ai_content = patch.content
353+
state.ai_content = sanitized_patch_content
350354
}
351355
all_original_states = all_original_states.concat(
352356
result.original_states

0 commit comments

Comments
 (0)