-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff_resolution.py
More file actions
164 lines (142 loc) · 6.91 KB
/
Copy pathdiff_resolution.py
File metadata and controls
164 lines (142 loc) · 6.91 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
###########################################################################
File: diff_resolution.py
Author:
Description: Module to classify each of the lines in a diff results set.
Marks each line as added, changed, or same.
Copyright (C) PyMerge Team 2019
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
###########################################################################
"""
import changeset
import longest_common_subseq
import pymerge_enums
def get_next_idx_match(match_list: list or set, curr_idx: int) -> list:
"""
Gets the next matching index set from a padded LCS output list
:param match_list: padded LCS output list
:param curr_idx: current index that the calling function is at
:return: list containing the next index match pair
"""
for n in range(curr_idx, len(match_list[0])):
try:
if match_list[0][n] != -1 and match_list[1][n] != -1:
return [match_list[0][n], match_list[1][n]]
except IndexError:
return [-1, -1]
def diff_set(
file_a,
file_b,
file_a_path,
file_b_path,
change_set_a: changeset.ChangeSet,
change_set_b: changeset.ChangeSet,
):
"""
This function gets the diff between two files and adds each line to a change set.
Flags are set for each lines depending on if the lines were changes, added, or are the same.
:param file_a: left hand file to compare
:param file_b: right hand file to compare
:param change_set_a: change set object for the left file
:param change_set_b: change set object for the right file
:return: pmEnums.CHANGED value indicating if operation was successful
"""
file_a_lines: list = file_a.read().splitlines()
file_b_lines: list = file_b.read().splitlines()
last_vals: list = [0, 0] # Last found match indices
# Get the raw, padded LCS output
file_a_lines.append(
"$"
) # Append a token on the end to make sure last 'lines' always match
file_b_lines.append("$")
raw_diff: list = longest_common_subseq.padded_lcs(
file_a_lines, file_b_lines, max(len(file_a_lines), len(file_b_lines))
)
for n in range(len(raw_diff[0])):
if raw_diff[0][n] != -1 and raw_diff[1][n] != -1:
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.SAME, file_a_lines[raw_diff[0][n]]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.SAME, file_b_lines[raw_diff[1][n]]
)
last_vals = [raw_diff[0][n], raw_diff[1][n]]
else:
# If the delta between the next match indices and the previous match indices is equal, just set to diff
if (
(0 < n < len(raw_diff[0]) - 1)
and ((raw_diff[0][n - 1] + 2) == raw_diff[0][n + 1])
or ((raw_diff[1][n - 1] + 2) == raw_diff[1][n + 1])
):
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[raw_diff[0][n - 1] + 1]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[raw_diff[1][n - 1] + 1]
)
else:
# Get the next matching indices
next_vals = get_next_idx_match(raw_diff, n)
if next_vals == [-1, -1]:
return pymerge_enums.RESULT.ERROR
idx_delta = [next_vals[0] - last_vals[0], next_vals[1] - last_vals[1]]
# If the match index deltas are equal the line flags can default to CHANGED
if idx_delta[0] == idx_delta[1]:
last_vals = [x + 1 for x in last_vals]
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[last_vals[0]]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[last_vals[1]]
)
# If the delta is greater on the left side, that means lines were inserted in the left file
elif idx_delta[0] > idx_delta[1]:
# Check if the last index matches are getting close to to the next index matches
if last_vals[1] < (next_vals[1] - 1):
last_vals = [x + 1 for x in last_vals]
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[last_vals[0]]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[last_vals[1]]
)
else:
last_vals = [x + 1 for x in last_vals]
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[last_vals[0]]
)
change_set_b.add_change(n, pymerge_enums.CHANGEDENUM.ADDED, "")
# if the delta is greater on the right side, that means lines were inserted in the right file
elif idx_delta[0] < idx_delta[1]:
if last_vals[0] < (next_vals[0] - 1):
last_vals = [x + 1 for x in last_vals]
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[last_vals[0]]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[last_vals[1]]
)
else:
last_vals = [x + 1 for x in last_vals]
change_set_a.add_change(n, pymerge_enums.CHANGEDENUM.ADDED, "")
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[last_vals[1]]
)
else:
# The default flag is CHANGED
change_set_a.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_a_lines[raw_diff[0][n]]
)
change_set_b.add_change(
n, pymerge_enums.CHANGEDENUM.CHANGED, file_b_lines[raw_diff[1][n]]
)
return pymerge_enums.RESULT.GOOD