Skip to content

Commit cd9df70

Browse files
committed
Refactor diff patch building to correctly handle and prioritize diff --git and ---/+++ headers when both are present
1 parent 910c800 commit cd9df70

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,5 +1243,21 @@ describe('clipboard-parser', () => {
12431243
content: load_test_case_file(test_case, '1-file.txt')
12441244
})
12451245
})
1246+
1247+
it('parses diff with both git header and traditional ---/+++ headers', () => {
1248+
const test_case = 'diff-git-two-header-types'
1249+
const text = load_test_case_file(test_case, `${test_case}.txt`)
1250+
const result = parse_response({
1251+
response: text,
1252+
is_single_root_folder_workspace: true
1253+
})
1254+
1255+
expect(result).toHaveLength(1)
1256+
expect(result[0]).toMatchObject({
1257+
type: 'diff',
1258+
file_path: 'src/index.ts',
1259+
content: load_test_case_file(test_case, '1-file.txt')
1260+
})
1261+
})
12461262
})
12471263
})

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches/extract-diffs.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/extract-diff-patches/extract-diffs.ts
12
import { Logger } from '@shared/utils/logger'
23
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
34
import { extract_workspace_and_path } from '../clipboard-parser'
@@ -709,9 +710,10 @@ const build_patch_content = (params: {
709710
line.startsWith('@@')
710711
)
711712

712-
if (hunk_start_idx > 0) {
713+
if (hunk_start_idx >= 0) {
713714
const header_lines = patch_lines.slice(0, hunk_start_idx)
714715
const body_lines = patch_lines.slice(hunk_start_idx)
716+
const formatted_body_lines = format_hunk_headers(body_lines)
715717

716718
let from_line: string | undefined
717719
let to_line: string | undefined
@@ -728,7 +730,7 @@ const build_patch_content = (params: {
728730
if (from_line) final_header.push(from_line)
729731
if (to_line) final_header.push(to_line)
730732

731-
patch_lines = [...final_header, ...body_lines]
733+
patch_lines = [...final_header, ...formatted_body_lines]
732734
}
733735

734736
patch_content = patch_lines
@@ -795,4 +797,4 @@ const format_hunk_headers = (lines: string[]): string[] => {
795797
}
796798
}
797799
return formatted_lines
798-
}
800+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -1,3 +1,3 @@
4+
console.log("hello")
5+
-console.log("old message")
6+
+console.log("new message")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
diff --git a/src/index.ts b/src/index.ts
2+
index 1234567..abcdef0 100644
3+
--- a/src/index.ts
4+
+++ b/src/index.ts
5+
@@ -1,3 +1,3 @@ console.log("hello")
6+
-console.log("old message")
7+
+console.log("new message")

0 commit comments

Comments
 (0)