~bzr-pqm/bzr/bzr.dev

4869.3.27 by Andrew Bennetts
Move news_merge plugin from contrib to bzrlib/plugins, change it to be enabled via a 'news_merge_files' config option, move more code out of the __init__ to minimise overhead, and add lots of docstrings, add NEWS entry.
1
# Copyright (C) 2010 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Merge logic for news_merge plugin."""
18
19
20
from bzrlib.plugins.news_merge.parser import simple_parse
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
21
from bzrlib import merge, merge3
4869.3.27 by Andrew Bennetts
Move news_merge plugin from contrib to bzrlib/plugins, change it to be enabled via a 'news_merge_files' config option, move more code out of the __init__ to minimise overhead, and add lots of docstrings, add NEWS entry.
22
23
24
magic_marker = '|NEWS-MERGE-MAGIC-MARKER|'
25
26
4797.5.2 by Robert Collins
Refactor NewsMerger into a reusable base class merge.ConfigurableFileMerger.
27
class NewsMerger(merge.ConfigurableFileMerger):
28
    """Merge bzr NEWS files."""
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
29
4797.5.3 by Robert Collins
Tweak ConfigurableFileMerger to use class variables rather than requiring __init__ wrapping as future proofing for helper functions.
30
    name_prefix = "news"
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
31
4797.5.2 by Robert Collins
Refactor NewsMerger into a reusable base class merge.ConfigurableFileMerger.
32
    def merge_text(self, params):
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
33
        """Perform a simple 3-way merge of a bzr NEWS file.
34
        
35
        Each section of a bzr NEWS file is essentially an ordered set of bullet
36
        points, so we can simply take a set of bullet points, determine which
37
        bullets to add and which to remove, sort, and reserialize.
38
        """
39
        # Transform the different versions of the NEWS file into a bunch of
40
        # text lines where each line matches one part of the overall
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 = []
49
        for group in m3.merge_groups():
50
            if group[0] == 'conflict':
51
                _, base, a, b = group
52
                # Are all the conflicting lines bullets?  If so, we can merge
53
                # this.
54
                for line_set in [base, a, b]:
55
                    for line in line_set:
56
                        if not line.startswith('bullet'):
57
                            # Something else :(
58
                            # Maybe the default merge can cope.
59
                            return 'not_applicable', None
60
                # Calculate additions and deletions.
61
                new_in_a = set(a).difference(base)
62
                new_in_b = set(b).difference(base)
63
                all_new = new_in_a.union(new_in_b)
64
                deleted_in_a = set(base).difference(a)
65
                deleted_in_b = set(base).difference(b)
66
                # Combine into the final set of bullet points.
67
                final = all_new.difference(deleted_in_a).difference(
68
                    deleted_in_b)
69
                # Sort, and emit.
70
                final = sorted(final, key=sort_key)
71
                result_lines.extend(final)
72
            else:
73
                result_lines.extend(group[1])
74
        # Transform the merged elements back into real blocks of lines.
75
        return 'success', list(fakelines_to_blocks(result_lines))
76
4869.3.27 by Andrew Bennetts
Move news_merge plugin from contrib to bzrlib/plugins, change it to be enabled via a 'news_merge_files' config option, move more code out of the __init__ to minimise overhead, and add lots of docstrings, add NEWS entry.
77
78
def blocks_to_fakelines(blocks):
79
    for kind, text in blocks:
80
        yield '%s%s%s' % (kind, magic_marker, text)
81
82
4869.3.29 by Andrew Bennetts
Stop news_merge from adding extra newlines at the end of the file.
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:]:
4869.3.27 by Andrew Bennetts
Move news_merge plugin from contrib to bzrlib/plugins, change it to be enabled via a 'news_merge_files' config option, move more code out of the __init__ to minimise overhead, and add lots of docstrings, add NEWS entry.
90
        yield fakeline.split(magic_marker, 1)[1]
91
92
93
def sort_key(s):
94
    return s.replace('`', '').lower()