forked from FabriceSalvaire/CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiff.py
More file actions
342 lines (195 loc) · 9.88 KB
/
Diff.py
File metadata and controls
342 lines (195 loc) · 9.88 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#####################################################################################################
from bzrlib.patiencediff import unified_diff_files, PatienceSequenceMatcher
#####################################################################################################
class HunkAbc(object):
###############################################
def __init__(self, hunk_slice):
self.slice = hunk_slice
self.length = hunk_slice.stop - hunk_slice.start
###############################################
def __bool__(self):
return self.length == 0
###############################################
def __len__(self):
return self.length
#####################################################################################################
class HunksAbc(list):
###############################################
def __init__(self, diff_buffer):
self.diff_buffer = diff_buffer
###############################################
def add_hunk(self, hunk_slice):
hunk = self.new_hunk(hunk_slice)
self.append(hunk)
return hunk
#####################################################################################################
class DiffBufferAbc(object):
###############################################
def __init__(self, buffer_obj, name, hunks_class):
self.buffer = buffer_obj
self.name = name
self.hunks = hunks_class(self)
###############################################
def __getitem__(self, hunk):
return self.buffer[hunk.slice]
###############################################
def add_hunk(self, hunk_slice):
return self.hunks.add_hunk(hunk_slice)
#####################################################################################################
class LinearHunk(HunkAbc):
###############################################
def __init__(self, text, hunk_slice):
super(LinearHunk, self).__init__(hunk_slice)
self._text = text
###############################################
def text(self):
return self._text[self.slice]
#####################################################################################################
class LinearHunks(HunksAbc):
###############################################
def new_hunk(self, hunk_slice):
return LinearHunk(self.diff_buffer, hunk_slice)
#####################################################################################################
class LinearDiffBuffer(DiffBufferAbc):
###############################################
def __init__(self, text, name):
super(LinearDiffBuffer, self).__init__(text, name, LinearHunks)
#####################################################################################################
class LinearTwoWayHunk(object):
###############################################
def __init__(self, slice_a, slice_b):
self.slice_a, self.slice_b = slice_a, slice_b
#####################################################################################################
class LinearTwoWayHunkEqual(LinearTwoWayHunk):
repr_string = 'equal'
#####################################################################################################
class LinearTwoWayHunkDelete(LinearTwoWayHunk):
repr_string = 'delete'
#####################################################################################################
class LinearTwoWayHunkInsert(LinearTwoWayHunk):
repr_string = 'insert'
#####################################################################################################
class LinearTwoWayHunkReplace(LinearTwoWayHunk):
repr_string = 'replace'
#####################################################################################################
class Hunk(HunkAbc):
###############################################
def __init__(self, diff_buffer, hunk_slice):
super(Hunk, self).__init__(hunk_slice)
self.diff_buffer = diff_buffer
###############################################
def __repr__(self):
if not self:
return "Hunk %s empty\n" % (self.diff_buffer.name)
string_template = 'Hunk %s [%u, %u]\n'
s = string_template % (self.diff_buffer.name,
self.slice.start, self.slice.stop -1)
s += ' '.join(self.lines())
return s
###############################################
def __str__(self):
return ''.join(self.lines())
###############################################
def lines(self):
for line in self.diff_buffer[self]:
yield line
#####################################################################################################
class Hunks(HunksAbc):
###############################################
def __str__(self):
return '\n'.join([str(x) for x in self])
###############################################
def new_hunk(self, hunk_slice):
return Hunk(self.diff_buffer, hunk_slice)
#####################################################################################################
class FileDiffBuffer(DiffBufferAbc):
###############################################
def __init__(self, file_handler, name):
super(FileDiffBuffer, self).__init__(file_handler, name, Hunks)
#####################################################################################################
class TwoWayHunk(object):
###############################################
def __init__(self, hunk_a, hunk_b):
self.hunk_a, self.hunk_b = hunk_a, hunk_b
###############################################
def __str__(self):
s = '-'*50 + '\n' + self.repr_string + '\n'
for hunk in self.hunk_a, self.hunk_b:
s += repr(hunk)
return s
#####################################################################################################
class TwoWayHunkEqual(TwoWayHunk):
repr_string = 'equal'
#####################################################################################################
class TwoWayHunkDelete(TwoWayHunk):
repr_string = 'delete'
#####################################################################################################
class TwoWayHunkInsert(TwoWayHunk):
repr_string = 'insert'
#####################################################################################################
class TwoWayHunkReplace(TwoWayHunk):
repr_string = 'replace'
###############################################
def __init__(self, hunk_a, hunk_b):
super(TwoWayHunkReplace, self).__init__(hunk_a, hunk_b)
self.diff_buffer_a = LinearDiffBuffer(str(hunk_a), 'A')
self.diff_buffer_b = LinearDiffBuffer(str(hunk_b), 'B')
sequence_matcher_class = PatienceSequenceMatcher
sequence_matcher = sequence_matcher_class(None,
self.diff_buffer_a.buffer,
self.diff_buffer_b.buffer)
sequence_matcher_groups = sequence_matcher.get_opcodes()
self.hunks = hunks = TwoWayHunks()
for tag, lower_a, upper_a, lower_b, upper_b in sequence_matcher_groups:
hunk_a = self.diff_buffer_a.add_hunk(slice(lower_a, upper_a))
hunk_b = self.diff_buffer_b.add_hunk(slice(lower_b, upper_b))
if tag == 'equal':
hunk_diff = LinearTwoWayHunkEqual(hunk_a, hunk_b)
elif tag == 'delete':
hunk_diff = LinearTwoWayHunkDelete(hunk_a, hunk_b)
elif tag == 'insert':
hunk_diff = LinearTwoWayHunkInsert(hunk_a, hunk_b)
elif tag == 'replace':
hunk_diff = LinearTwoWayHunkReplace(hunk_a, hunk_b)
hunks.append(hunk_diff)
###############################################
def __str__(self):
s = super(TwoWayHunkReplace, self).__str__()
s += str(self.hunks)
return s
#####################################################################################################
class TwoWayHunks(list):
###############################################
def __str__(self):
return '\n'.join([str(x) for x in self])
#####################################################################################################
class Diff2Way(object):
###############################################
def __init__(self, diff_buffer_a, diff_buffer_b):
self.diff_buffer_a = diff_buffer_a
self.diff_buffer_b = diff_buffer_b
sequence_matcher_class = PatienceSequenceMatcher
sequence_matcher = sequence_matcher_class(None,
self.diff_buffer_a.buffer,
self.diff_buffer_b.buffer)
number_of_lines_of_context = 3
sequence_matcher_groups = sequence_matcher.get_grouped_opcodes(number_of_lines_of_context)
self.hunks = hunks = TwoWayHunks()
for group in sequence_matcher_groups:
for tag, lower_a, upper_a, lower_b, upper_b in group:
hunk_a = self.diff_buffer_a.add_hunk(slice(lower_a, upper_a))
hunk_b = self.diff_buffer_b.add_hunk(slice(lower_b, upper_b))
if tag == 'equal':
hunk_diff = TwoWayHunkEqual(hunk_a, hunk_b)
elif tag == 'delete':
hunk_diff = TwoWayHunkDelete(hunk_a, hunk_b)
elif tag == 'insert':
hunk_diff = TwoWayHunkInsert(hunk_a, hunk_b)
elif tag == 'replace':
hunk_diff = TwoWayHunkReplace(hunk_a, hunk_b)
hunks.append(hunk_diff)
#####################################################################################################
#
# End
#
#####################################################################################################