From 86dc14f6261097c98d2ab6c515a53c55883f2908 Mon Sep 17 00:00:00 2001 From: mikkel Date: Sun, 17 Aug 2025 20:50:03 -0600 Subject: [PATCH] Handle Begin Patch diff format --- gptdiff/applydiff.py | 24 ++++++++++++++++++++++++ tests/test_parse_diff_per_file.py | 14 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/gptdiff/applydiff.py b/gptdiff/applydiff.py index 1d5bf62..831fa7e 100644 --- a/gptdiff/applydiff.py +++ b/gptdiff/applydiff.py @@ -198,6 +198,30 @@ def dedup_diffs(diffs): groups[key].append(value) return [[key, "\n".join(values)] for key, values in groups.items()] + # Special case: handle LLM-style patch delimiters + if "*** Begin Patch" in diff_text: + lines = diff_text.splitlines() + diffs = [] + current_lines = [] + current_file = None + in_patch = False + for line in lines: + stripped = line.strip() + if stripped == "*** Begin Patch": + in_patch = True + current_lines = [] + current_file = None + elif stripped == "*** End Patch": + if current_file is not None: + diffs.append((current_file, "\n".join(current_lines))) + in_patch = False + elif in_patch: + if stripped.startswith("*** Update File:"): + current_file = stripped.split(":", 1)[1].strip() + else: + current_lines.append(line) + return dedup_diffs(diffs) + header_re = re.compile(r'^(?:diff --git\s+)?(a/[^ ]+)\s+(b/[^ ]+)\s*$', re.MULTILINE) lines = diff_text.splitlines() diff --git a/tests/test_parse_diff_per_file.py b/tests/test_parse_diff_per_file.py index 3d41305..6785f18 100644 --- a/tests/test_parse_diff_per_file.py +++ b/tests/test_parse_diff_per_file.py @@ -169,5 +169,19 @@ def test_parse_diff_per_file_unconventional_header(): assert "+++ game.js" in patch, "Expected patch to include '+++ game.js'" assert "+let player" in patch, "Expected patch to include added lines" +def test_begin_patch_format(): + diff_text = """*** Begin Patch +*** Update File: services/clerkReportPdf.tsx +@@ +-changes1 ++changes2 +*** End Patch""" + result = parse_diff_per_file(diff_text) + assert len(result) == 1 + file_path, patch = result[0] + assert file_path == "services/clerkReportPdf.tsx" + assert "-changes1" in patch + assert "+changes2" in patch + if __name__ == '__main__': unittest.main()