Created
February 2, 2022 00:28
-
-
Save chrismwendt/75288dd95b7f2a8dc0b452b9f940c329 to your computer and use it in GitHub Desktop.
Pretty prints git conflicts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import tempfile | |
from colored import stylize, fg, attr | |
import os | |
import sys | |
def main(): | |
for filename in subprocess.check_output( | |
['git', 'diff', '--name-only', | |
'--diff-filter=U']).decode().splitlines(): | |
with open(filename) as f: | |
contents = f.read() | |
lines = contents.splitlines() | |
for i in range(len(lines)): | |
if lines[i].startswith('<<<<<<<'): | |
current_line = i | |
current_title = lines[i][8:] | |
current = [] | |
base = [] | |
incoming = [] | |
while i < len(lines): | |
i += 1 | |
if lines[i].startswith('|||||||'): | |
base_line = i | |
base_title = lines[i][8:] | |
break | |
current.append(lines[i]) | |
while i < len(lines): | |
i += 1 | |
if lines[i].startswith('======='): | |
incoming_line = i | |
break | |
base.append(lines[i]) | |
while i < len(lines): | |
i += 1 | |
if lines[i].startswith('>>>>>>>'): | |
incoming_title = lines[i][8:] | |
break | |
incoming.append(lines[i]) | |
fn = f"{filename}:{current_line+1}" | |
print(stylize(fn, fg('light_yellow'))) | |
print(stylize(40 * "_" + "BASE", fg('light_cyan'))) | |
with tempfile.NamedTemporaryFile() as a: | |
with tempfile.NamedTemporaryFile() as b: | |
a.write(('\n'.join(base) + '\n').encode()) | |
b.write(('\n'.join(current) + '\n').encode()) | |
a.seek(0) | |
b.seek(0) | |
out, err = subprocess.Popen( | |
[ | |
'delta', '--file-style=omit', | |
'--hunk-header-style=omit', a.name, b.name | |
], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE).communicate() | |
print(out.decode().lstrip(), end="") | |
print(stylize(44 * '‾', fg('light_cyan'))) | |
print() | |
print(stylize(40 * '_' + "INCOMING", fg('light_magenta'))) | |
with tempfile.NamedTemporaryFile() as a: | |
with tempfile.NamedTemporaryFile() as b: | |
a.write('\n'.join(base).encode()) | |
b.write('\n'.join(incoming).encode()) | |
a.seek(0) | |
b.seek(0) | |
out, err = subprocess.Popen( | |
[ | |
'delta', '--file-style=omit', | |
'--hunk-header-style=omit', a.name, b.name | |
], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE).communicate() | |
print(out.decode().lstrip(), end="") | |
print(stylize(48 * '‾', fg('light_magenta'))) | |
print() | |
print() | |
print() | |
print() | |
print() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment