~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2010-01-18 07:00:11 UTC
  • mto: (4973.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4975.
  • Revision ID: andrew.bennetts@canonical.com-20100118070011-zu374wvd0lcgai5a
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.

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
 
21
from bzrlib import merge3
 
22
 
 
23
 
 
24
magic_marker = '|NEWS-MERGE-MAGIC-MARKER|'
 
25
 
 
26
 
 
27
def news_merger(params):
 
28
    """Perform a simple 3-way merge of a bzr NEWS file.
 
29
    
 
30
    Each section of a bzr NEWS file is essentially an ordered set of bullet
 
31
    points, so we can simply take a set of bullet points, determine which
 
32
    bullets to add and which to remove, sort, and reserialize.
 
33
    """
 
34
    # Transform the different versions of the NEWS file into a bunch of text
 
35
    # lines where each line matches one part of the overall structure, e.g. a
 
36
    # heading or bullet.
 
37
    def munge(lines):
 
38
        return list(blocks_to_fakelines(simple_parse(''.join(lines))))
 
39
    this_lines = munge(params.this_lines)
 
40
    other_lines = munge(params.other_lines)
 
41
    base_lines = munge(params.base_lines)
 
42
    m3 = merge3.Merge3(base_lines, this_lines, other_lines)
 
43
    result_lines = []
 
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 this.
 
48
            for line_set in [base, a, b]:
 
49
                for line in line_set:
 
50
                    if not line.startswith('bullet'):
 
51
                        # Something else :(
 
52
                        # Maybe the default merge can cope.
 
53
                        return 'not_applicable', None
 
54
            # Calculate additions and deletions.
 
55
            new_in_a = set(a).difference(base)
 
56
            new_in_b = set(b).difference(base)
 
57
            all_new = new_in_a.union(new_in_b)
 
58
            deleted_in_a = set(base).difference(a)
 
59
            deleted_in_b = set(base).difference(b)
 
60
            # Combine into the final set of bullet points.
 
61
            final = all_new.difference(deleted_in_a).difference(deleted_in_b)
 
62
            # Sort, and emit.
 
63
            final = sorted(final, key=sort_key)
 
64
            result_lines.extend(final)
 
65
        else:
 
66
            result_lines.extend(group[1])
 
67
    # Transform the merged elements back into real lines.
 
68
    return 'success', list(fakelines_to_lines(result_lines))
 
69
 
 
70
 
 
71
def blocks_to_fakelines(blocks):
 
72
    for kind, text in blocks:
 
73
        yield '%s%s%s' % (kind, magic_marker, text)
 
74
 
 
75
 
 
76
def _fakelines_to_lines(fakelines):
 
77
    for fakeline in fakelines:
 
78
        yield fakeline.split(magic_marker, 1)[1]
 
79
 
 
80
def fakelines_to_lines(fakelines):
 
81
    return '\n\n'.join(_fakelines_to_lines(fakelines))
 
82
 
 
83
 
 
84
def sort_key(s):
 
85
    return s.replace('`', '').lower()
 
86