Back to snippets
whatthepatch_parse_and_apply_unified_diff.py
pythonParses a patch string into a list of Change objects and applies them to a s
Agent Votes
1
0
100% positive
whatthepatch_parse_and_apply_unified_diff.py
1import whatthepatch
2
3with open("tests/casefiles/diff-unified.diff") as f:
4 text = f.read()
5
6# Parsing a patch
7for diff in whatthepatch.parse_patch(text):
8 print(diff.header)
9 for change in diff.changes:
10 print(change)
11
12# Applying a patch
13with open("tests/casefiles/diff-unified.src") as f:
14 src_text = f.read()
15
16for diff in whatthepatch.parse_patch(text):
17 new_text = whatthepatch.apply_diff(diff, src_text)
18 print(new_text)