changeset 48564:c2537aec3bb6

simplemerge: change _minimize() to minimize a single conflict `_minimize()` is weird in that it changes `a_lines` and `b_lines` but leaves `base_lines` unchanged, which means they'll be inconsistent. That was fine because the caller never used `base_lines`. With the recent refactorings of this code, we can now make this function cleaner by having it minimize a single conflict and not care about `base_lines`. This also makes the code simpler and makes the code for each `render_*()` function more similar. Differential Revision: https://phab.mercurial-scm.org/D11981
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 11 Jan 2022 22:13:56 -0800
parents ad0c6bf6f02e
children 7ed4c4753891
files mercurial/simplemerge.py
diffstat 1 files changed, 23 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/simplemerge.py	Tue Jan 11 22:03:55 2022 -0800
+++ b/mercurial/simplemerge.py	Tue Jan 11 22:13:56 2022 -0800
@@ -306,47 +306,33 @@
     return b'\n'
 
 
-def _minimize(merge_groups):
+def _minimize(a_lines, b_lines):
     """Trim conflict regions of lines where A and B sides match.
 
     Lines where both A and B have made the same changes at the beginning
     or the end of each merge region are eliminated from the conflict
     region and are instead considered the same.
     """
-    for what, lines in merge_groups:
-        if what != b"conflict":
-            yield what, lines
-            continue
-        base_lines, a_lines, b_lines = lines
-        alen = len(a_lines)
-        blen = len(b_lines)
+    alen = len(a_lines)
+    blen = len(b_lines)
 
-        # find matches at the front
-        ii = 0
-        while ii < alen and ii < blen and a_lines[ii] == b_lines[ii]:
-            ii += 1
-        startmatches = ii
+    # find matches at the front
+    ii = 0
+    while ii < alen and ii < blen and a_lines[ii] == b_lines[ii]:
+        ii += 1
+    startmatches = ii
 
-        # find matches at the end
-        ii = 0
-        while ii < alen and ii < blen and a_lines[-ii - 1] == b_lines[-ii - 1]:
-            ii += 1
-        endmatches = ii
-
-        if startmatches > 0:
-            yield b'same', a_lines[:startmatches]
+    # find matches at the end
+    ii = 0
+    while ii < alen and ii < blen and a_lines[-ii - 1] == b_lines[-ii - 1]:
+        ii += 1
+    endmatches = ii
 
-        yield (
-            b'conflict',
-            (
-                base_lines,
-                a_lines[startmatches : alen - endmatches],
-                b_lines[startmatches : blen - endmatches],
-            ),
-        )
-
-        if endmatches > 0:
-            yield b'same', a_lines[alen - endmatches :]
+    lines_before = a_lines[:startmatches]
+    new_a_lines = a_lines[startmatches : alen - endmatches]
+    new_b_lines = b_lines[startmatches : blen - endmatches]
+    lines_after = a_lines[alen - endmatches :]
+    return lines_before, new_a_lines, new_b_lines, lines_after
 
 
 def render_minimized(
@@ -365,17 +351,20 @@
     if name_b:
         end_marker = end_marker + b' ' + name_b
     merge_groups = m3.merge_groups()
-    merge_groups = _minimize(merge_groups)
     lines = []
     for what, group_lines in merge_groups:
         if what == b'conflict':
+            conflicts = True
             base_lines, a_lines, b_lines = group_lines
-            conflicts = True
+            minimized = _minimize(a_lines, b_lines)
+            lines_before, a_lines, b_lines, lines_after = minimized
+            lines.extend(lines_before)
             lines.append(start_marker + newline)
             lines.extend(a_lines)
             lines.append(mid_marker + newline)
             lines.extend(b_lines)
             lines.append(end_marker + newline)
+            lines.extend(lines_after)
         else:
             lines.extend(group_lines)
     return lines, conflicts