# HG changeset patch # User Martin von Zweigbergk # Date 2022-01-08 02:42:31 # Node ID ce8c82a5cd65edae97661bf580e9c0eb83a35660 # Parent c91418480cb08491d1bcb5e5e3a369417f7e72c9 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 diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py --- a/mercurial/debugcommands.py +++ b/mercurial/debugcommands.py @@ -272,7 +272,7 @@ def debugbuilddag( 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") diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py --- a/mercurial/simplemerge.py +++ b/mercurial/simplemerge.py @@ -99,7 +99,7 @@ class Merge3Text(object): 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 @@ class Merge3Text(object): 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 @@ def simplemerge(ui, localctx, basectx, o 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/tests/test-simplemerge.py b/tests/test-simplemerge.py --- a/tests/test-simplemerge.py +++ b/tests/test-simplemerge.py @@ -179,7 +179,7 @@ class TestMerge3(TestCase): 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 @@ class TestMerge3(TestCase): [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 @@ class TestMerge3(TestCase): [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 @@ class TestMerge3(TestCase): [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 @@ class TestMerge3(TestCase): [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 @@ class TestMerge3(TestCase): [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 @@ class TestMerge3(TestCase): ], ) - ml = m3.merge_lines( + ml, conflicts = m3.merge_lines( name_a=b'a', name_b=b'b', start_marker=b'<<', @@ -338,7 +338,7 @@ bbb 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 @@ bbb 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 @@ bbb 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, )