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 hook for bzr's NEWS file.
19
Install this as a plugin, e.g:
21
cp contrib/news-file-merge-hook.py ~/.bazaar/plugins/news_merge.py
24
from .parser import simple_parse
26
from bzrlib.merge import Merger
27
from bzrlib.merge3 import Merge3
30
def news_merge_hook(params):
31
if params.winner == 'other':
32
# OTHER is a straight winner, rely on default merge.
33
return 'not_applicable', None
34
elif params.is_file_merge():
35
# THIS and OTHER are both files. That's a good start.
36
filename = params.merger.this_tree.id2path(params.file_id)
37
if filename != 'NEWS':
38
return 'not_applicable', None
39
return news_merger(params)
41
return 'not_applicable', None
43
magic_marker = '|NEWS-MERGE-MAGIC-MARKER|'
45
def blocks_to_fakelines(blocks):
46
for kind, text in blocks:
47
yield '%s%s%s' % (kind, magic_marker, text)
49
def fakelines_to_lines(fakelines):
50
for fakeline in fakelines:
51
yield fakeline.split(magic_marker, 1)[1] + '\n'
55
return s.replace('`', '').lower()
57
def news_merger(params):
59
return list(blocks_to_fakelines(simple_parse(''.join(lines))))
60
this_lines = munge(params.this_lines)
61
other_lines = munge(params.other_lines)
62
base_lines = munge(params.base_lines)
63
m3 = Merge3(base_lines, this_lines, other_lines)
65
for group in m3.merge_groups():
66
if group[0] == 'conflict':
68
# are all the conflicting lines bullets? If so, we can merge this.
69
for line_set in [base, a, b]:
71
if not line.startswith('bullet'):
73
# Maybe the default merge can cope.
74
return 'not_applicable', None
75
new_in_a = set(a).difference(base)
76
new_in_b = set(b).difference(base)
77
all_new = new_in_a.union(new_in_b)
78
deleted_in_a = set(base).difference(a)
79
deleted_in_b = set(base).difference(b)
80
final = all_new.difference(deleted_in_a).difference(deleted_in_b)
81
final = sorted(final, key=sort_key)
82
result_lines.extend(final)
84
result_lines.extend(group[1])
85
return 'success', list(fakelines_to_lines(result_lines))
88
Merger.hooks.install_named_hook(
89
'merge_file_content', news_merge_hook, 'NEWS file merge')