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