changeset 48584:ce8c82a5cd65

simplemerge: convert `merge_lines()` away from generator 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). Differential Revision: https://phab.mercurial-scm.org/D11973
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 07 Jan 2022 18:42:31 -0800
parents c91418480cb0
children c6649c53073f
files mercurial/debugcommands.py mercurial/simplemerge.py tests/test-simplemerge.py
diffstat 3 files changed, 29 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/debugcommands.py	Tue Dec 14 13:12:56 2021 -0800
+++ b/mercurial/debugcommands.py	Fri Jan 07 18:42:31 2022 -0800
@@ -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")
--- a/mercurial/simplemerge.py	Tue Dec 14 13:12:56 2021 -0800
+++ b/mercurial/simplemerge.py	Fri Jan 07 18:42:31 2022 -0800
@@ -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'):
--- a/tests/test-simplemerge.py	Tue Dec 14 13:12:56 2021 -0800
+++ b/tests/test-simplemerge.py	Fri Jan 07 18:42:31 2022 -0800
@@ -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,
         )