Skip to content

Instantly share code, notes, and snippets.

@eiginn
Last active May 1, 2020 03:13
Show Gist options
  • Save eiginn/0fcc596bf72973eef955aa2361cc0e73 to your computer and use it in GitHub Desktop.
Save eiginn/0fcc596bf72973eef955aa2361cc0e73 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# GistID: 0fcc596bf72973eef955aa2361cc0e73
import sys
import os
import tempfile
from subprocess import call
import unidiff
if len(sys.argv) != 2:
print("must specify unified diff file")
sys.exit(1)
diff = unidiff.PatchSet.from_filename(sys.argv[1])
for filename in diff:
print("File:", filename.source_file)
old_parts = tempfile.NamedTemporaryFile(suffix='{}_old'.format(os.path.basename(filename.source_file)),
delete=False,
mode='w+t')
new_parts = tempfile.NamedTemporaryFile(suffix='{}_new'.format(os.path.basename(filename.target_file)),
delete=False,
mode='w+t')
for hunk in filename:
for l in hunk.source:
if l.startswith(' '):
old_parts.file.write(l[1:])
new_parts.file.write(l[1:])
continue
if l.startswith('-'):
old_parts.file.write(l[1:])
continue
if l.startswith('+'):
new_parts.file.write(l[1:])
continue
old_parts.file.flush()
new_parts.file.flush()
print("old: {}".format(old_parts.name))
print("new: {}".format(new_parts.name))
call(['vimdiff', old_parts.name, new_parts.name])
old_parts.close()
new_parts.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment