|
1
|
|
|
import difflib |
|
2
|
|
|
|
|
3
|
|
|
from pyprint.ConsolePrinter import ConsolePrinter |
|
4
|
|
|
|
|
5
|
|
|
from coalib.results.Diff import ConflictError |
|
6
|
|
|
from coalib.results.Result import Result |
|
7
|
|
|
from coalib.results.result_actions.ResultAction import ResultAction |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def format_line(line, real_nr="", sign="|", mod_nr="", symbol="", ): |
|
11
|
|
|
return "|{:>4}{}{:>4}|{:1}{}".format(real_nr, |
|
12
|
|
|
sign, |
|
13
|
|
|
mod_nr, |
|
14
|
|
|
symbol, |
|
15
|
|
|
line.rstrip("\n")) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def print_beautified_diff(difflines, printer): |
|
19
|
|
|
current_line_added = None |
|
20
|
|
|
current_line_subtracted = None |
|
21
|
|
|
for line in difflines: |
|
22
|
|
|
if line.startswith("@@"): |
|
23
|
|
|
values = line[line.find("-"):line.rfind(" ")] |
|
24
|
|
|
subtracted, added = tuple(values.split(" ")) |
|
25
|
|
|
current_line_added = int(added.split(",")[0][1:]) |
|
26
|
|
|
current_line_subtracted = int(subtracted.split(",")[0][1:]) |
|
27
|
|
|
elif line.startswith("---"): |
|
28
|
|
|
printer.print(format_line(line[4:], real_nr="----"), color="red") |
|
29
|
|
|
elif line.startswith("+++"): |
|
30
|
|
|
printer.print(format_line(line[4:], mod_nr="++++"), |
|
31
|
|
|
color="green") |
|
32
|
|
|
elif line.startswith("+"): |
|
33
|
|
|
printer.print(format_line(line[1:], |
|
34
|
|
|
mod_nr=current_line_added, |
|
35
|
|
|
symbol="+"), |
|
36
|
|
|
color="green") |
|
37
|
|
|
current_line_added += 1 |
|
38
|
|
|
elif line.startswith("-"): |
|
39
|
|
|
printer.print(format_line(line[1:], |
|
40
|
|
|
real_nr=current_line_subtracted, |
|
41
|
|
|
symbol="-"), |
|
42
|
|
|
color="red") |
|
43
|
|
|
current_line_subtracted += 1 |
|
44
|
|
|
else: |
|
45
|
|
|
printer.print(format_line(line[1:], |
|
46
|
|
|
real_nr=current_line_subtracted, |
|
47
|
|
|
mod_nr=current_line_added, |
|
48
|
|
|
symbol=" ")) |
|
49
|
|
|
current_line_subtracted += 1 |
|
50
|
|
|
current_line_added += 1 |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
class ShowPatchAction(ResultAction): |
|
54
|
|
|
|
|
55
|
|
|
success_message = "Displayed patch successfully." |
|
56
|
|
|
|
|
57
|
|
|
@staticmethod |
|
58
|
|
|
def is_applicable(result, original_file_dict, file_diff_dict): |
|
59
|
|
|
if not isinstance(result, Result) or not result.diffs: |
|
60
|
|
|
return False |
|
61
|
|
|
|
|
62
|
|
|
try: |
|
63
|
|
|
for filename in set(result.diffs) & set(file_diff_dict): |
|
64
|
|
|
result.diffs[filename].__add__(file_diff_dict[filename]) |
|
65
|
|
|
return True |
|
66
|
|
|
except ConflictError: |
|
67
|
|
|
return False |
|
68
|
|
|
|
|
69
|
|
|
def apply(self, |
|
70
|
|
|
result, |
|
71
|
|
|
original_file_dict, |
|
72
|
|
|
file_diff_dict, |
|
73
|
|
|
colored: bool=True): |
|
74
|
|
|
''' |
|
75
|
|
|
Print a diff of the patch that would be applied. |
|
76
|
|
|
|
|
77
|
|
|
:param colored: Wether or not to use colored output. |
|
78
|
|
|
''' |
|
79
|
|
|
printer = ConsolePrinter(colored) |
|
80
|
|
|
|
|
81
|
|
|
for filename, this_diff in sorted(result.diffs.items()): |
|
82
|
|
|
original_file = original_file_dict[filename] |
|
83
|
|
|
try: |
|
84
|
|
|
current_file = file_diff_dict[filename].modified |
|
85
|
|
|
new_file = (file_diff_dict[filename] + this_diff).modified |
|
86
|
|
|
except KeyError: |
|
87
|
|
|
current_file = original_file |
|
88
|
|
|
new_file = this_diff.modified |
|
89
|
|
|
|
|
90
|
|
|
print_beautified_diff(difflib.unified_diff(current_file, |
|
91
|
|
|
new_file, |
|
92
|
|
|
fromfile=filename, |
|
93
|
|
|
tofile=filename), |
|
94
|
|
|
printer) |
|
95
|
|
|
|
|
96
|
|
|
return file_diff_dict |
|
97
|
|
|
|