-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgd2h.py
80 lines (68 loc) · 3.79 KB
/
gd2h.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import print_function
import os
import subprocess
import argparse
import codecs
def exists(command_name):
if os.name == 'nt':
return os.system("where /Q " + command_name) == 0
else:
return os.system("which -s " + command_name) == 0
def run(cmd):
if os.name == 'nt':
cmd = cmd.replace('^','^^') # the caret is an escape characater in Windows cmd.exe
result = os.system(cmd)
if result != 0:
print("error: ", cmd)
return not result
def gitdiff2html(args):
changed_files = [fp for fp in subprocess.check_output("git diff {commit} --name-only".format(commit=args.commit).split()).decode("utf-8").split("\n") if fp.strip() != '']
output_file_temp = args.output_file + '.tmp'
all_dependancies_are_installed = True
if args.auto:
if args.output_file == 'diff.html':
args.output_file = changed_files[0].replace('.txt', '') + '.html'
if args.title == 'Git Diff':
args.title = 'Correction'
args.line_through = True
args.drop_header = True
for dependency in ["git", "aha"]:
if not exists(dependency):
all_dependancies_are_installed = False
print('Command not found: "' + dependency + '". Please install this dependency.')
if all_dependancies_are_installed:
command_string = "git diff --patience --color-words {commit} | aha -s -w --title '{title}' > {output_file_temp}".format(commit=args.commit, output_file_temp=output_file_temp, title=args.title)
if args.print:
print(command_string)
else:
if run(command_string): # execute command
with codecs.open(output_file_temp, "r", "utf-8") as output_file_temp_handle:
with codecs.open(args.output_file, "w", "utf-8") as output_file_handle:
for line in output_file_temp_handle:
if line == ".green {color: green;}" + os.linesep:
output_file_handle.write(".green {color: #02A602; background-color: #D9FED9;}" + os.linesep)
elif args.line_through and line == ".red {color: red;}" + os.linesep:
output_file_handle.write(".red {color: red; text-decoration: line-through;}" + os.linesep)
elif args.drop_header and len(line) > len('<span class="bold ">') \
and line[:len('<span class="bold ">')] in ['<span class="bold ">', '<span class="cyan ">']:
pass
else:
output_file_handle.write(line)
try:
os.remove(output_file_temp)
except:
print("error: could'nt delete ", output_file_temp)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--commit', default='HEAD^ HEAD', help="commits to diff, defaults to 'HEAD^ HEAD'")
parser.add_argument('-o', '--output-file', default='diff.html', help="output file, defaults to 'diff.html'")
parser.add_argument('-t', '--title', default='Git Diff', help="output file title, defaults to 'Git Diff'")
parser.add_argument('-p', '--print', action='store_true', help="print output command instead of running it'")
parser.add_argument('-l', '--line-through', action='store_true', help="line through deleted words'")
parser.add_argument('-d', '--drop-header', action='store_true', help="drop git diff header")
parser.add_argument('-a', '--auto', action='store_true', help="correction mode: set default flags for pretty file correction layout")
parser.set_defaults(func=gitdiff2html)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()