Skip to content

Commit 9de277f

Browse files
committed
Fix diff parsing to correctly handle and normalize broken or incomplete hunk headers following a diff --git line
1 parent 6f10c87 commit 9de277f

5 files changed

Lines changed: 37 additions & 5 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
@@ -1275,5 +1275,21 @@ describe('clipboard-parser', () => {
12751275
content: load_test_case_file(test_case, '1-file.txt')
12761276
})
12771277
})
1278+
1279+
it('parses diff with broken hunk header after git header', () => {
1280+
const test_case = 'diff-git-broken-hunk-header'
1281+
const text = load_test_case_file(test_case, `${test_case}.txt`)
1282+
const result = parse_response({
1283+
response: text,
1284+
is_single_root_folder_workspace: true
1285+
})
1286+
1287+
expect(result).toHaveLength(1)
1288+
expect(result[0]).toMatchObject({
1289+
type: 'diff',
1290+
file_path: 'src/index.ts',
1291+
content: load_test_case_file(test_case, '1-file.txt')
1292+
})
1293+
})
12781294
})
12791295
})

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ export const parse_response = (params: {
9191

9292
const hunk_header_regex = /^(@@\s+-\d+(?:,\d+)?\s+\+\d+(?:,\d+)?\s+@@)/m
9393
const diff_header_regex = /^---\s+.+\n\+\+\+\s+.+/m
94+
const git_diff_header_regex = /^diff --git /m
9495

9596
if (
9697
hunk_header_regex.test(processed_response) ||
97-
diff_header_regex.test(processed_response)
98+
diff_header_regex.test(processed_response) ||
99+
git_diff_header_regex.test(processed_response)
98100
) {
99101
const patches_or_text = extract_diffs({
100102
clipboard_text: processed_response,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,7 @@ const extract_all_code_block_patches = (params: {
455455
const { from_path, to_path } = extract_paths_from_lines(block_lines)
456456
if (!from_path && !to_path) {
457457
block_lines.unshift(
458-
is_new_file_from_hint
459-
? '--- /dev/null'
460-
: `--- a/${file_path_hint}`,
458+
is_new_file_from_hint ? '--- /dev/null' : `--- a/${file_path_hint}`,
461459
`+++ b/${file_path_hint}`
462460
)
463461
}
@@ -859,9 +857,15 @@ const format_hunk_headers = (lines: string[]): string[] => {
859857
if (hunk_match[2].length > 0) {
860858
formatted_lines.push(hunk_match[2])
861859
}
860+
} else if (line.startsWith('@@') && !hunk_match) {
861+
const context = line.substring(2).trim()
862+
formatted_lines.push('@@ -0,0 +0,0 @@')
863+
if (context) {
864+
formatted_lines.push(' ' + context)
865+
}
862866
} else {
863867
formatted_lines.push(line)
864868
}
865869
}
866870
return formatted_lines
867-
}
871+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -0,0 +0,0 @@
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,4 @@
1+
diff --git a/src/index.ts b/src/index.ts
2+
@@ console.log("hello")
3+
-console.log("old message")
4+
+console.log("new message")

0 commit comments

Comments
 (0)