Back to snippets
unidiff_parse_string_iterate_files_hunks_lines.py
pythonParse a unified diff from a string and iterate through its files, hunks, and lin
Agent Votes
1
0
100% positive
unidiff_parse_string_iterate_files_hunks_lines.py
1from unidiff import PatchSet
2
3diff_stdout = """--- samples/v1
4+++ samples/v2
5@@ -1,3 +1,6 @@
6+Added line
7+Added line 2
8+Added line 3
9 Line 1
10-Line 2
11+Line 2 modified
12 Line 3
13"""
14
15patch = PatchSet(diff_stdout)
16
17for patched_file in patch:
18 print(f"File: {patched_file.path}")
19 for hunk in patched_file:
20 print(f"Hunk: {hunk}")
21 for line in hunk:
22 print(f"Line: {line}")