~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Aaron Bentley
  • Date: 2010-05-10 11:34:20 UTC
  • mfrom: (5218 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5221.
  • Revision ID: aaron@aaronbentley.com-20100510113420-toh2d5yioobb5uq1
Merged bzr.dev into transform-commit-full.

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
 
24
 
magic_marker = '|NEWS-MERGE-MAGIC-MARKER|'
25
 
 
26
 
 
27
24
class NewsMerger(merge.ConfigurableFileMerger):
28
25
    """Merge bzr NEWS files."""
29
26
 
39
36
        # Transform the different versions of the NEWS file into a bunch of
40
37
        # text lines where each line matches one part of the overall
41
38
        # 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 = []
 
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 = []
49
45
        for group in m3.merge_groups():
50
46
            if group[0] == 'conflict':
51
47
                _, base, a, b = group
53
49
                # this.
54
50
                for line_set in [base, a, b]:
55
51
                    for line in line_set:
56
 
                        if not line.startswith('bullet'):
 
52
                        if line[0] != 'bullet':
57
53
                            # Something else :(
58
54
                            # Maybe the default merge can cope.
59
55
                            return 'not_applicable', None
68
64
                    deleted_in_b)
69
65
                # Sort, and emit.
70
66
                final = sorted(final, key=sort_key)
71
 
                result_lines.extend(final)
 
67
                result_chunks.extend(final)
72
68
            else:
73
 
                result_lines.extend(group[1])
 
69
                result_chunks.extend(group[1])
74
70
        # 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()
 
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()