Skip to content

Commit c579fe4

Browse files
committed
Implement path prefixing for git diffs and file paths in multi-root workspace environments
1 parent af007f0 commit c579fe4

1 file changed

Lines changed: 69 additions & 8 deletions

File tree

‎apps/editor/src/views/panel/backend/utils/symbols/git/replace-git-symbols.ts‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,42 @@ import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provi
77
import { Logger } from '@shared/utils/logger'
88
import { dictionary } from '@shared/constants/dictionary'
99

10+
const patch_diff_paths = (
11+
diff_text: string,
12+
path_prefix: string,
13+
file_path: string,
14+
old_path?: string,
15+
new_path?: string
16+
): string => {
17+
const lines = diff_text.split('\n')
18+
const a_path_orig = old_path || file_path
19+
const b_path_orig = new_path || file_path
20+
21+
for (let i = 0; i < lines.length; i++) {
22+
if (lines[i].startsWith('diff --git ')) {
23+
lines[i] = `diff --git a/${path_prefix}/${a_path_orig} b/${path_prefix}/${b_path_orig}`
24+
} else if (lines[i].startsWith('--- a/')) {
25+
lines[i] = `--- a/${path_prefix}/${a_path_orig}`
26+
} else if (lines[i].startsWith('+++ b/')) {
27+
lines[i] = `+++ b/${path_prefix}/${b_path_orig}`
28+
} else if (lines[i].startsWith('rename from ')) {
29+
lines[i] = `rename from ${path_prefix}/${lines[i].substring('rename from '.length)}`
30+
} else if (lines[i].startsWith('rename to ')) {
31+
lines[i] = `rename to ${path_prefix}/${lines[i].substring('rename to '.length)}`
32+
} else if (lines[i].startsWith('copy from ')) {
33+
lines[i] = `copy from ${path_prefix}/${lines[i].substring('copy from '.length)}`
34+
} else if (lines[i].startsWith('copy to ')) {
35+
lines[i] = `copy to ${path_prefix}/${lines[i].substring('copy to '.length)}`
36+
}
37+
}
38+
return lines.join('\n')
39+
}
40+
1041
const build_changes_xml = (
1142
diff: string,
1243
cwd: string,
13-
diff_base: string
44+
diff_base: string,
45+
path_prefix?: string
1446
): string => {
1547
// Split diff into per-file sections. Each section starts with 'diff --git '.
1648
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
@@ -21,7 +53,7 @@ const build_changes_xml = (
2153
let changes_content = ''
2254

2355
for (const file_diff_content of file_diffs) {
24-
const full_file_diff = 'diff --git ' + file_diff_content
56+
let full_file_diff = 'diff --git ' + file_diff_content
2557
const lines = full_file_diff.split('\n')
2658
const old_path_line = lines.find((l) => l.startsWith('--- a/'))
2759
const new_path_line = lines.find((l) => l.startsWith('+++ b/'))
@@ -46,7 +78,9 @@ const build_changes_xml = (
4678
}
4779

4880
if (file_path) {
49-
changes_content += `<file path="${file_path}">\n`
81+
const display_path = path_prefix ? `${path_prefix}/${file_path}` : file_path
82+
83+
changes_content += `<file path="${display_path}">\n`
5084

5185
let file_content = ''
5286
try {
@@ -67,6 +101,17 @@ const build_changes_xml = (
67101
})
68102
}
69103
}
104+
105+
if (path_prefix) {
106+
full_file_diff = patch_diff_paths(
107+
full_file_diff,
108+
path_prefix,
109+
file_path,
110+
old_path,
111+
new_path
112+
)
113+
}
114+
70115
changes_content += `<![CDATA[\n${full_file_diff}\n]]>\n`
71116
if (file_content) {
72117
changes_content += `<![CDATA[\n${file_content}\n]]>\n`
@@ -168,7 +213,8 @@ export const replace_changes_symbol = async (params: {
168213
const replacement_text = build_changes_xml(
169214
diff,
170215
target_folder.uri.fsPath,
171-
diff_base
216+
diff_base,
217+
workspace_folders.length > 1 ? folder_name : undefined
172218
)
173219
changes_definitions += replacement_text
174220
result_instruction = result_instruction.replace(
@@ -266,7 +312,8 @@ const build_commit_changes_xml = (
266312
diff: string,
267313
cwd: string,
268314
commit_hash: string,
269-
commit_message?: string
315+
commit_message?: string,
316+
path_prefix?: string
270317
): string => {
271318
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
272319

@@ -277,7 +324,7 @@ const build_commit_changes_xml = (
277324
let changes_content = ''
278325

279326
for (const file_diff_content of file_diffs) {
280-
const full_file_diff = 'diff --git ' + file_diff_content
327+
let full_file_diff = 'diff --git ' + file_diff_content
281328
const lines = full_file_diff.split('\n')
282329
const old_path_line = lines.find((l) => l.startsWith('--- a/'))
283330
const new_path_line = lines.find((l) => l.startsWith('+++ b/'))
@@ -318,7 +365,20 @@ const build_commit_changes_xml = (
318365
}
319366
}
320367

321-
changes_content += `<file path="${file_path}">\n`
368+
const display_path = path_prefix ? `${path_prefix}/${file_path}` : file_path
369+
370+
changes_content += `<file path="${display_path}">\n`
371+
372+
if (path_prefix) {
373+
full_file_diff = patch_diff_paths(
374+
full_file_diff,
375+
path_prefix,
376+
file_path,
377+
old_path,
378+
new_path
379+
)
380+
}
381+
322382
changes_content += `<![CDATA[\n${full_file_diff}\n]]>\n`
323383
if (file_content) {
324384
changes_content += `<![CDATA[\n${file_content}\n]]>\n`
@@ -386,7 +446,8 @@ export const replace_commit_symbol = async (params: {
386446
diff,
387447
target_folder.uri.fsPath,
388448
commit_hash,
389-
commit_message
449+
commit_message,
450+
workspace_folders.length > 1 ? folder_name : undefined
390451
)
391452
commit_definitions += replacement_text
392453

0 commit comments

Comments
 (0)