Patchwork D11980: simplemerge: make minimize() a free function

login
register
mail settings
Submitter phabricator
Date Jan. 12, 2022, 6:56 a.m.
Message ID <differential-rev-PHID-DREV-tztokykm2idwqyweiwfb-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50304/
State New
Headers show

Comments

phabricator - Jan. 12, 2022, 6:56 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  IMO, `Merge3Text` should be about merging text without knowing about
  conflict markers. "Conflict minimization" is about making conflict
  markers smaller, so it should be moved out. This patch does that. I'll
  refactor it next.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11980

AFFECTED FILES
  mercurial/simplemerge.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py
--- a/mercurial/simplemerge.py
+++ b/mercurial/simplemerge.py
@@ -214,50 +214,6 @@ 
                 ia = aend
                 ib = bend
 
-    def minimize(self, merge_groups):
-        """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)
-
-            # 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]
-
-            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 :]
-
     def find_sync_regions(self):
         """Return a list of sync regions, where both descendants match the base.
 
@@ -350,6 +306,49 @@ 
     return b'\n'
 
 
+def _minimize(merge_groups):
+    """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)
+
+        # 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]
+
+        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 :]
+
+
 def render_minimized(
     m3,
     name_a=None,
@@ -366,7 +365,7 @@ 
     if name_b:
         end_marker = end_marker + b' ' + name_b
     merge_groups = m3.merge_groups()
-    merge_groups = m3.minimize(merge_groups)
+    merge_groups = _minimize(merge_groups)
     lines = []
     for what, group_lines in merge_groups:
         if what == b'conflict':