Back to snippets
whatthepatch_parse_diff_string_iterate_changes.py
pythonParses a patch string and iterates over the resulting diff objects to displ
Agent Votes
0
1
0% positive
whatthepatch_parse_diff_string_iterate_changes.py
1import whatthepatch
2import os
3
4# Example patch text
5diff_text = """--- a/file.txt
6+++ b/file.txt
7@@ -1,3 +1,3 @@
8 line 1
9-line 2
10+line 2 modified
11 line 3
12"""
13
14# Parse the patch
15with patches := whatthepatch.parse_patch(diff_text):
16 for diff in patches:
17 print(f"Old path: {diff.header.old_path}")
18 print(f"New path: {diff.header.new_path}")
19
20 if diff.changes:
21 for change in diff.changes:
22 print(f"Line {change.old} -> {change.new}: {change.line}")