Patchwork D11973: simplemerge: convert `merge_lines()` away from generator

login
register
mail settings
Submitter phabricator
Date Jan. 12, 2022, 6:56 a.m.
Message ID <differential-rev-PHID-DREV-tqs6mwrfzfnsx7tpju7y-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50302/
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
  We always consume all the lines and put them in a list anyway. By
  making the function not a generator, we can later make it return an
  additional value (to indicate if there were conflicts).

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/debugcommands.py
  mercurial/simplemerge.py
  tests/test-simplemerge.py

CHANGE DETAILS




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

Patch

diff --git a/tests/test-simplemerge.py b/tests/test-simplemerge.py
--- a/tests/test-simplemerge.py
+++ b/tests/test-simplemerge.py
@@ -179,7 +179,7 @@ 
 
         self.assertEqual(list(m3.merge_regions()), [(b'a', 0, 2)])
 
-        self.assertEqual(list(m3.merge_lines()), [b'aaa', b'bbb'])
+        self.assertEqual(m3.merge_lines(), ([b'aaa', b'bbb'], False))
 
     def test_no_conflicts(self):
         """No conflicts because only one side changed"""
@@ -204,7 +204,7 @@ 
             [b'aaa\n', b'bbb\n'],
         )
 
-        self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n')
+        self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
 
     def test_append_b(self):
         m3 = Merge3(
@@ -213,7 +213,7 @@ 
             [b'aaa\n', b'bbb\n', b'222\n'],
         )
 
-        self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n')
+        self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
 
     def test_append_agreement(self):
         m3 = Merge3(
@@ -222,7 +222,7 @@ 
             [b'aaa\n', b'bbb\n', b'222\n'],
         )
 
-        self.assertEqual(b''.join(m3.merge_lines()), b'aaa\nbbb\n222\n')
+        self.assertEqual(b''.join(m3.merge_lines()[0]), b'aaa\nbbb\n222\n')
 
     def test_append_clash(self):
         m3 = Merge3(
@@ -231,7 +231,7 @@ 
             [b'aaa\n', b'bbb\n', b'333\n'],
         )
 
-        ml = m3.merge_lines(
+        ml, conflicts = m3.merge_lines(
             name_a=b'a',
             name_b=b'b',
             start_marker=b'<<',
@@ -250,7 +250,7 @@ 
             [b'aaa\n', b'222\n', b'bbb\n'],
         )
 
-        ml = m3.merge_lines(
+        ml, conflicts = m3.merge_lines(
             name_a=b'a',
             name_b=b'b',
             start_marker=b'<<',
@@ -290,7 +290,7 @@ 
             ],
         )
 
-        ml = m3.merge_lines(
+        ml, conflicts = m3.merge_lines(
             name_a=b'a',
             name_b=b'b',
             start_marker=b'<<',
@@ -338,7 +338,7 @@ 
     def test_merge_poem(self):
         """Test case from diff3 manual"""
         m3 = Merge3(TZU, LAO, TAO)
-        ml = list(m3.merge_lines(b'LAO', b'TAO'))
+        ml, conflicts = m3.merge_lines(b'LAO', b'TAO')
         self.log(b'merge result:')
         self.log(b''.join(ml))
         self.assertEqual(ml, MERGED_RESULT)
@@ -356,11 +356,11 @@ 
             other_text.splitlines(True),
             this_text.splitlines(True),
         )
-        m_lines = m3.merge_lines(b'OTHER', b'THIS')
+        m_lines, conflicts = m3.merge_lines(b'OTHER', b'THIS')
         self.assertEqual(
             b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
             b'>>>>>>> THIS\r\n'.splitlines(True),
-            list(m_lines),
+            m_lines,
         )
 
     def test_mac_text(self):
@@ -372,11 +372,11 @@ 
             other_text.splitlines(True),
             this_text.splitlines(True),
         )
-        m_lines = m3.merge_lines(b'OTHER', b'THIS')
+        m_lines, conflicts = m3.merge_lines(b'OTHER', b'THIS')
         self.assertEqual(
             b'<<<<<<< OTHER\rc\r=======\rb\r'
             b'>>>>>>> THIS\r'.splitlines(True),
-            list(m_lines),
+            m_lines,
         )
 
 
diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py
--- a/mercurial/simplemerge.py
+++ b/mercurial/simplemerge.py
@@ -99,7 +99,7 @@ 
         minimize=False,
     ):
         """Return merge in cvs-like form."""
-        self.conflicts = False
+        conflicts = False
         newline = b'\n'
         if len(self.a) > 0:
             if self.a[0].endswith(b'\r\n'):
@@ -115,27 +115,25 @@ 
         merge_groups = self.merge_groups()
         if minimize:
             merge_groups = self.minimize(merge_groups)
-        for what, lines in merge_groups:
+        lines = []
+        for what, group_lines in merge_groups:
             if what == b'conflict':
-                base_lines, a_lines, b_lines = lines
-                self.conflicts = True
+                base_lines, a_lines, b_lines = group_lines
+                conflicts = True
                 if start_marker is not None:
-                    yield start_marker + newline
-                for line in a_lines:
-                    yield line
+                    lines.append(start_marker + newline)
+                lines.extend(a_lines)
                 if base_marker is not None:
-                    yield base_marker + newline
-                    for line in base_lines:
-                        yield line
+                    lines.append(base_marker + newline)
+                    lines.extend(base_lines)
                 if mid_marker is not None:
-                    yield mid_marker + newline
-                for line in b_lines:
-                    yield line
+                    lines.append(mid_marker + newline)
+                lines.extend(b_lines)
                 if end_marker is not None:
-                    yield end_marker + newline
+                    lines.append(end_marker + newline)
             else:
-                for line in lines:
-                    yield line
+                lines.extend(group_lines)
+        return lines, conflicts
 
     def merge_groups(self):
         """Yield sequence of line groups.  Each one is a tuple:
@@ -510,10 +508,9 @@ 
                 extrakwargs['base_marker'] = b'|||||||'
                 extrakwargs['name_base'] = name_base
                 extrakwargs['minimize'] = False
-            lines = list(
-                m3.merge_lines(name_a=name_a, name_b=name_b, **extrakwargs)
+            lines, conflicts = m3.merge_lines(
+                name_a=name_a, name_b=name_b, **extrakwargs
             )
-            conflicts = m3.conflicts
 
     mergedtext = b''.join(lines)
     if opts.get('print'):
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -272,7 +272,7 @@ 
                             x[fn].data() for x in (pa, p1, p2)
                         ]
                         m3 = simplemerge.Merge3Text(base, local, other)
-                        ml = [l.strip() for l in m3.merge_lines()]
+                        ml = [l.strip() for l in m3.merge_lines()[0]]
                         ml.append(b"")
                     elif at > 0:
                         ml = p1[fn].data().split(b"\n")