/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Merge logic for news_merge plugin."""
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .parser import simple_parse_lines
20
from ... 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.
21
22
4797.5.2 by Robert Collins
Refactor NewsMerger into a reusable base class merge.ConfigurableFileMerger.
23
class NewsMerger(merge.ConfigurableFileMerger):
24
    """Merge bzr NEWS files."""
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
25
4797.5.3 by Robert Collins
Tweak ConfigurableFileMerger to use class variables rather than requiring __init__ wrapping as future proofing for helper functions.
26
    name_prefix = "news"
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
27
4797.5.2 by Robert Collins
Refactor NewsMerger into a reusable base class merge.ConfigurableFileMerger.
28
    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.
29
        """Perform a simple 3-way merge of a bzr NEWS file.
6391.1.2 by Benjamin Peterson
kill trailing whitespace
30
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
31
        Each section of a bzr NEWS file is essentially an ordered set of bullet
32
        points, so we can simply take a set of bullet points, determine which
33
        bullets to add and which to remove, sort, and reserialize.
34
        """
35
        # Transform the different versions of the NEWS file into a bunch of
36
        # text lines where each line matches one part of the overall
37
        # structure, e.g. a heading or bullet.
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
38
        this_lines = list(simple_parse_lines(params.this_lines))
39
        other_lines = list(simple_parse_lines(params.other_lines))
40
        base_lines = list(simple_parse_lines(params.base_lines))
41
        m3 = merge3.Merge3(base_lines, this_lines, other_lines,
7143.15.2 by Jelmer Vernooij
Run autopep8.
42
                           allow_objects=True)
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
43
        result_chunks = []
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
44
        for group in m3.merge_groups():
45
            if group[0] == 'conflict':
46
                _, base, a, b = group
47
                # Are all the conflicting lines bullets?  If so, we can merge
48
                # this.
49
                for line_set in [base, a, b]:
50
                    for line in line_set:
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
51
                        if line[0] != 'bullet':
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
52
                            # Something else :(
53
                            # Maybe the default merge can cope.
54
                            return 'not_applicable', None
55
                # Calculate additions and deletions.
56
                new_in_a = set(a).difference(base)
57
                new_in_b = set(b).difference(base)
58
                all_new = new_in_a.union(new_in_b)
59
                deleted_in_a = set(base).difference(a)
60
                deleted_in_b = set(base).difference(b)
61
                # Combine into the final set of bullet points.
62
                final = all_new.difference(deleted_in_a).difference(
63
                    deleted_in_b)
64
                # Sort, and emit.
65
                final = sorted(final, key=sort_key)
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
66
                result_chunks.extend(final)
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
67
            else:
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
68
                result_chunks.extend(group[1])
4797.5.1 by Robert Collins
Support state on per-file merging to permit more efficient use of configuration data.
69
        # Transform the merged elements back into real blocks of lines.
5158.5.1 by Andrew Bennetts
Allow Merge3 to work with objects, not just lists of strs. This removes a lot of cruft from news_merge.
70
        result_lines = '\n\n'.join(chunk[1] for chunk in result_chunks)
71
        return 'success', result_lines
72
73
74
def sort_key(chunk):
75
    return chunk[1].replace('`', '').lower()