##// END OF EJS Templates
simplemerge: rewrite `merge_lines()` using `merge_groups()`...
Martin von Zweigbergk -
r49380:88a45330 default
parent child Browse files
Show More
@@ -112,38 +112,30 b' class Merge3Text(object):'
112 112 end_marker = end_marker + b' ' + name_b
113 113 if name_base and base_marker:
114 114 base_marker = base_marker + b' ' + name_base
115 merge_regions = self.merge_regions()
115 merge_groups = self.merge_groups()
116 116 if minimize:
117 merge_regions = self.minimize(merge_regions)
118 for t in merge_regions:
119 what = t[0]
120 if what == b'unchanged':
121 for i in range(t[1], t[2]):
122 yield self.base[i]
123 elif what == b'a' or what == b'same':
124 for i in range(t[1], t[2]):
125 yield self.a[i]
126 elif what == b'b':
127 for i in range(t[1], t[2]):
128 yield self.b[i]
129 elif what == b'conflict':
117 merge_groups = self.minimize(merge_groups)
118 for what, lines in merge_groups:
119 if what == b'conflict':
120 base_lines, a_lines, b_lines = lines
130 121 self.conflicts = True
131 122 if start_marker is not None:
132 123 yield start_marker + newline
133 for i in range(t[3], t[4]):
134 yield self.a[i]
124 for line in a_lines:
125 yield line
135 126 if base_marker is not None:
136 127 yield base_marker + newline
137 for i in range(t[1], t[2]):
138 yield self.base[i]
128 for line in base_lines:
129 yield line
139 130 if mid_marker is not None:
140 131 yield mid_marker + newline
141 for i in range(t[5], t[6]):
142 yield self.b[i]
132 for line in b_lines:
133 yield line
143 134 if end_marker is not None:
144 135 yield end_marker + newline
145 136 else:
146 raise ValueError(what)
137 for line in lines:
138 yield line
147 139
148 140 def merge_groups(self):
149 141 """Yield sequence of line groups. Each one is a tuple:
@@ -272,66 +264,49 b' class Merge3Text(object):'
272 264 ia = aend
273 265 ib = bend
274 266
275 def minimize(self, merge_regions):
267 def minimize(self, merge_groups):
276 268 """Trim conflict regions of lines where A and B sides match.
277 269
278 270 Lines where both A and B have made the same changes at the beginning
279 271 or the end of each merge region are eliminated from the conflict
280 272 region and are instead considered the same.
281 273 """
282 for region in merge_regions:
283 if region[0] != b"conflict":
284 yield region
274 for what, lines in merge_groups:
275 if what != b"conflict":
276 yield what, lines
285 277 continue
286 # pytype thinks this tuple contains only 3 things, but
287 # that's clearly not true because this code successfully
288 # executes. It might be wise to rework merge_regions to be
289 # some kind of attrs type.
290 (
291 issue,
292 z1,
293 z2,
294 a1,
295 a2,
296 b1,
297 b2,
298 ) = region # pytype: disable=bad-unpacking
299 alen = a2 - a1
300 blen = b2 - b1
278 base_lines, a_lines, b_lines = lines
279 alen = len(a_lines)
280 blen = len(b_lines)
301 281
302 282 # find matches at the front
303 283 ii = 0
304 while (
305 ii < alen and ii < blen and self.a[a1 + ii] == self.b[b1 + ii]
306 ):
284 while ii < alen and ii < blen and a_lines[ii] == b_lines[ii]:
307 285 ii += 1
308 286 startmatches = ii
309 287
310 288 # find matches at the end
311 289 ii = 0
312 290 while (
313 ii < alen
314 and ii < blen
315 and self.a[a2 - ii - 1] == self.b[b2 - ii - 1]
291 ii < alen and ii < blen and a_lines[-ii - 1] == b_lines[-ii - 1]
316 292 ):
317 293 ii += 1
318 294 endmatches = ii
319 295
320 296 if startmatches > 0:
321 yield b'same', a1, a1 + startmatches
297 yield b'same', a_lines[:startmatches]
322 298
323 299 yield (
324 300 b'conflict',
325 z1,
326 z2,
327 a1 + startmatches,
328 a2 - endmatches,
329 b1 + startmatches,
330 b2 - endmatches,
301 (
302 base_lines,
303 a_lines[startmatches : alen - endmatches],
304 b_lines[startmatches : blen - endmatches],
305 ),
331 306 )
332 307
333 308 if endmatches > 0:
334 yield b'same', a2 - endmatches, a2
309 yield b'same', a_lines[alen - endmatches :]
335 310
336 311 def find_sync_regions(self):
337 312 """Return a list of sync regions, where both descendants match the base.
General Comments 0
You need to be logged in to leave comments. Login now