Skip to content

Commit 300c19e

Browse files
committed
Add original file path tracking and improved file status labeling for renamed and deleted files in commit prompts
1 parent 9877d43 commit 300c19e

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎packages/vscode/src/views/panel/backend/message-handlers/handle-commit-changes/utils/file-utils.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ignore from 'ignore'
99
export interface FileData {
1010
path: string
1111
relative_path: string
12+
original_relative_path?: string
1213
content: string
1314
estimated_tokens: number
1415
status: number
@@ -35,6 +36,14 @@ export const collect_affected_files_with_metadata = async (params: {
3536
continue
3637
}
3738

39+
let original_relative_path: string | undefined
40+
if (change.status === 4 && change.originalUri) {
41+
original_relative_path = path.relative(
42+
params.repository.rootUri.fsPath,
43+
change.originalUri.fsPath
44+
)
45+
}
46+
3847
let content = ''
3948
let is_large_file = false
4049
try {
@@ -74,6 +83,7 @@ export const collect_affected_files_with_metadata = async (params: {
7483
files_data.push({
7584
path: file_path,
7685
relative_path: relative_path,
86+
original_relative_path,
7787
content: content,
7888
estimated_tokens: estimated_tokens,
7989
status: change.status,

‎packages/vscode/src/views/panel/backend/message-handlers/handle-commit-changes/utils/prompt-utils.ts‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,67 @@ export const build_commit_message_prompt = (
4646
let final_diff_content = full_file_diff
4747

4848
let file_path: string | undefined
49+
let is_deleted = false
4950

5051
if (new_path && new_path != '/dev/null') {
5152
file_path = new_path
5253
} else if (old_path && old_path != '/dev/null') {
5354
file_path = old_path
55+
is_deleted = true
5456
// Shorten diff of a deleted file
5557
const split_diff = full_file_diff.split('+++ /dev/null')
5658
final_diff_content = split_diff[0] + '+++ /dev/null'
59+
} else {
60+
// Fallback: try to identify file from diff header (e.g. binary files)
61+
const diff_header = lines[0]
62+
const matched_file = affected_files_data.find((f) => {
63+
const normalized_path = f.relative_path.replace(/\\/g, '/')
64+
return (
65+
diff_header.includes(`/${normalized_path} `) ||
66+
diff_header.endsWith(`/${normalized_path}`)
67+
)
68+
})
69+
70+
if (matched_file) {
71+
file_path = matched_file.relative_path
72+
}
5773
}
5874

5975
if (file_path) {
6076
const file_data = affected_files_data.find(
6177
(f) => f.relative_path == file_path
6278
)
63-
changes_content += `\n### File: \`${file_path}\`\n\n`
79+
80+
let heading = 'Updated file'
81+
let display_path = `\`${file_path}\``
82+
83+
if (file_data) {
84+
if (file_data.status === 2) {
85+
heading = 'New file'
86+
} else if (file_data.status === 3) {
87+
heading = 'Deleted file'
88+
is_deleted = true
89+
} else if (file_data.status === 4) {
90+
heading = 'Renamed file'
91+
if (file_data.original_relative_path) {
92+
display_path = `\`${file_data.original_relative_path}\` (old) \`${file_path}\` (new)`
93+
}
94+
}
95+
} else {
96+
if (is_deleted) {
97+
heading = 'Deleted file'
98+
} else if (!old_path && new_path) {
99+
heading = 'New file'
100+
}
101+
}
102+
103+
changes_content += `\n### ${heading}: ${display_path}\n\n`
64104
if (file_data) {
65105
changes_content += `The original state of the file for reference:\n\n<![CDATA[\n${file_data.content}\n]]>\n\n`
66106
}
67-
changes_content += `**New changes:**\n\n<![CDATA[\n${final_diff_content}\n]]>\n`
107+
if (!is_deleted) {
108+
changes_content += `**New changes:**\n\n<![CDATA[\n${final_diff_content}\n]]>\n`
109+
}
68110
}
69111
}
70112

0 commit comments

Comments
 (0)