~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

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_lines
 
20
from bzrlib.plugins.news_merge.parser import simple_parse
21
21
from bzrlib import merge, merge3
22
22
 
23
23
 
 
24
magic_marker = '|NEWS-MERGE-MAGIC-MARKER|'
 
25
 
 
26
 
24
27
class NewsMerger(merge.ConfigurableFileMerger):
25
28
    """Merge bzr NEWS files."""
26
29
 
36
39
        # Transform the different versions of the NEWS file into a bunch of
37
40
        # text lines where each line matches one part of the overall
38
41
        # structure, e.g. a heading or bullet.
39
 
        this_lines = list(simple_parse_lines(params.this_lines))
40
 
        other_lines = list(simple_parse_lines(params.other_lines))
41
 
        base_lines = list(simple_parse_lines(params.base_lines))
42
 
        m3 = merge3.Merge3(base_lines, this_lines, other_lines,
43
 
            allow_objects=True)
44
 
        result_chunks = []
 
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 = []
45
49
        for group in m3.merge_groups():
46
50
            if group[0] == 'conflict':
47
51
                _, base, a, b = group
49
53
                # this.
50
54
                for line_set in [base, a, b]:
51
55
                    for line in line_set:
52
 
                        if line[0] != 'bullet':
 
56
                        if not line.startswith('bullet'):
53
57
                            # Something else :(
54
58
                            # Maybe the default merge can cope.
55
59
                            return 'not_applicable', None
64
68
                    deleted_in_b)
65
69
                # Sort, and emit.
66
70
                final = sorted(final, key=sort_key)
67
 
                result_chunks.extend(final)
 
71
                result_lines.extend(final)
68
72
            else:
69
 
                result_chunks.extend(group[1])
 
73
                result_lines.extend(group[1])
70
74
        # Transform the merged elements back into real blocks of lines.
71
 
        result_lines = '\n\n'.join(chunk[1] for chunk in result_chunks)
72
 
        return 'success', result_lines
73
 
 
74
 
 
75
 
def sort_key(chunk):
76
 
    return chunk[1].replace('`', '').lower()
 
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()