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