/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Martin
  • Date: 2010-05-25 17:27:52 UTC
  • mfrom: (5254 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5257.
  • Revision ID: gzlist@googlemail.com-20100525172752-amm089xcikv968sw
Merge bzr.dev to unite with similar changes already made

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_lines
 
21
from bzrlib import merge, merge3
 
22
 
 
23
 
 
24
class NewsMerger(merge.ConfigurableFileMerger):
 
25
    """Merge bzr NEWS files."""
 
26
 
 
27
    name_prefix = "news"
 
28
 
 
29
    def merge_text(self, params):
 
30
        """Perform a simple 3-way merge of a bzr NEWS file.
 
31
        
 
32
        Each section of a bzr NEWS file is essentially an ordered set of bullet
 
33
        points, so we can simply take a set of bullet points, determine which
 
34
        bullets to add and which to remove, sort, and reserialize.
 
35
        """
 
36
        # Transform the different versions of the NEWS file into a bunch of
 
37
        # text lines where each line matches one part of the overall
 
38
        # 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 = []
 
45
        for group in m3.merge_groups():
 
46
            if group[0] == 'conflict':
 
47
                _, base, a, b = group
 
48
                # Are all the conflicting lines bullets?  If so, we can merge
 
49
                # this.
 
50
                for line_set in [base, a, b]:
 
51
                    for line in line_set:
 
52
                        if line[0] != 'bullet':
 
53
                            # Something else :(
 
54
                            # Maybe the default merge can cope.
 
55
                            return 'not_applicable', None
 
56
                # Calculate additions and deletions.
 
57
                new_in_a = set(a).difference(base)
 
58
                new_in_b = set(b).difference(base)
 
59
                all_new = new_in_a.union(new_in_b)
 
60
                deleted_in_a = set(base).difference(a)
 
61
                deleted_in_b = set(base).difference(b)
 
62
                # Combine into the final set of bullet points.
 
63
                final = all_new.difference(deleted_in_a).difference(
 
64
                    deleted_in_b)
 
65
                # Sort, and emit.
 
66
                final = sorted(final, key=sort_key)
 
67
                result_chunks.extend(final)
 
68
            else:
 
69
                result_chunks.extend(group[1])
 
70
        # 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()