1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
17
"""Merge logic for news_merge plugin."""
19
from __future__ import absolute_import
22
from bzrlib.plugins.news_merge.parser import simple_parse_lines
23
from bzrlib import merge, merge3
26
class NewsMerger(merge.ConfigurableFileMerger):
27
"""Merge bzr NEWS files."""
31
def merge_text(self, params):
32
"""Perform a simple 3-way merge of a bzr NEWS file.
34
Each section of a bzr NEWS file is essentially an ordered set of bullet
35
points, so we can simply take a set of bullet points, determine which
36
bullets to add and which to remove, sort, and reserialize.
38
# Transform the different versions of the NEWS file into a bunch of
39
# text lines where each line matches one part of the overall
40
# structure, e.g. a heading or bullet.
41
this_lines = list(simple_parse_lines(params.this_lines))
42
other_lines = list(simple_parse_lines(params.other_lines))
43
base_lines = list(simple_parse_lines(params.base_lines))
44
m3 = merge3.Merge3(base_lines, this_lines, other_lines,
47
for group in m3.merge_groups():
48
if group[0] == 'conflict':
50
# Are all the conflicting lines bullets? If so, we can merge
52
for line_set in [base, a, b]:
54
if line[0] != 'bullet':
56
# Maybe the default merge can cope.
57
return 'not_applicable', None
58
# Calculate additions and deletions.
59
new_in_a = set(a).difference(base)
60
new_in_b = set(b).difference(base)
61
all_new = new_in_a.union(new_in_b)
62
deleted_in_a = set(base).difference(a)
63
deleted_in_b = set(base).difference(b)
64
# Combine into the final set of bullet points.
65
final = all_new.difference(deleted_in_a).difference(
68
final = sorted(final, key=sort_key)
69
result_chunks.extend(final)
71
result_chunks.extend(group[1])
72
# Transform the merged elements back into real blocks of lines.
73
result_lines = '\n\n'.join(chunk[1] for chunk in result_chunks)
74
return 'success', result_lines
78
return chunk[1].replace('`', '').lower()