~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/news_merge/news_merge.py

  • Committer: Andrew Bennetts
  • Date: 2010-04-19 05:41:54 UTC
  • mto: This revision was merged to the branch mainline in revision 5181.
  • Revision ID: andrew.bennetts@canonical.com-20100419054154-k5pq9dlm977cwpal
Allow Merge3 to work with objects, not just lists of strs.  This removes a lot of cruft from news_merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Merge logic for news_merge plugin."""
18
18
 
19
19
 
20
 
from bzrlib.plugins.news_merge.parser import simple_parse
 
20
from bzrlib.plugins.news_merge.parser import simple_parse_lines
21
21
from bzrlib import merge, merge3
22
22
 
23
23
 
39
39
        # Transform the different versions of the NEWS file into a bunch of
40
40
        # text lines where each line matches one part of the overall
41
41
        # structure, e.g. a heading or bullet.
42
 
        def munge(lines):
43
 
            return list(blocks_to_fakelines(simple_parse(''.join(lines))))
44
 
        this_lines = munge(params.this_lines)
45
 
        other_lines = munge(params.other_lines)
46
 
        base_lines = munge(params.base_lines)
47
 
        m3 = merge3.Merge3(base_lines, this_lines, other_lines)
48
 
        result_lines = []
 
42
        this_lines = list(simple_parse_lines(params.this_lines))
 
43
        other_lines = list(simple_parse_lines(params.other_lines))
 
44
        base_lines = list(simple_parse_lines(params.base_lines))
 
45
        m3 = merge3.Merge3(base_lines, this_lines, other_lines,
 
46
            allow_objects=True)
 
47
        result_chunks = []
49
48
        for group in m3.merge_groups():
50
49
            if group[0] == 'conflict':
51
50
                _, base, a, b = group
53
52
                # this.
54
53
                for line_set in [base, a, b]:
55
54
                    for line in line_set:
56
 
                        if not line.startswith('bullet'):
 
55
                        if line[0] != 'bullet':
57
56
                            # Something else :(
58
57
                            # Maybe the default merge can cope.
59
58
                            return 'not_applicable', None
68
67
                    deleted_in_b)
69
68
                # Sort, and emit.
70
69
                final = sorted(final, key=sort_key)
71
 
                result_lines.extend(final)
 
70
                result_chunks.extend(final)
72
71
            else:
73
 
                result_lines.extend(group[1])
 
72
                result_chunks.extend(group[1])
74
73
        # Transform the merged elements back into real blocks of lines.
75
 
        return 'success', list(fakelines_to_blocks(result_lines))
76
 
 
77
 
 
78
 
def blocks_to_fakelines(blocks):
79
 
    for kind, text in blocks:
80
 
        yield '%s%s%s' % (kind, magic_marker, text)
81
 
 
82
 
 
83
 
def fakelines_to_blocks(fakelines):
84
 
    fakelines = list(fakelines)
85
 
    # Strip out the magic_marker, and reinstate the \n\n between blocks
86
 
    for fakeline in fakelines[:-1]:
87
 
        yield fakeline.split(magic_marker, 1)[1] + '\n\n'
88
 
    # The final block doesn't have a trailing \n\n.
89
 
    for fakeline in fakelines[-1:]:
90
 
        yield fakeline.split(magic_marker, 1)[1]
91
 
 
92
 
 
93
 
def sort_key(s):
94
 
    return s.replace('`', '').lower()
 
74
        result_lines = '\n\n'.join(chunk[1] for chunk in result_chunks)
 
75
        return 'success', result_lines
 
76
 
 
77
 
 
78
def sort_key(chunk):
 
79
    return chunk[1].replace('`', '').lower()