1
# Copyright (C) 2004, 2005 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
68
64
incorporating the changes from both BASE->OTHER and BASE->THIS.
69
65
All three will typically be sequences of lines."""
70
66
def __init__(self, base, a, b):
71
check_text_lines(base)
70
from difflib import SequenceMatcher
71
self.a_ops = SequenceMatcher(None, base, a).get_opcodes()
72
self.b_ops = SequenceMatcher(None, base, b).get_opcodes()
80
76
def merge_lines(self,
84
start_marker='<<<<<<<',
79
start_marker='<<<<<<<<',
80
mid_marker='========',
81
end_marker='>>>>>>>>',
89
83
"""Return merge in cvs-like form.
93
if self.a[0].endswith('\r\n'):
95
elif self.a[0].endswith('\r'):
97
if base_marker and reprocess:
98
raise CantReprocessAndShowBase()
100
86
start_marker = start_marker + ' ' + name_a
102
88
end_marker = end_marker + ' ' + name_b
103
if name_base and base_marker:
104
base_marker = base_marker + ' ' + name_base
105
merge_regions = self.merge_regions()
106
if reprocess is True:
107
merge_regions = self.reprocess_merge_regions(merge_regions)
108
for t in merge_regions:
90
for t in self.merge_regions():
110
92
if what == 'unchanged':
111
93
for i in range(t[1], t[2]):
117
99
for i in range(t[1], t[2]):
119
101
elif what == 'conflict':
120
yield start_marker + newline
102
yield start_marker + '\n'
121
103
for i in range(t[3], t[4]):
123
if base_marker is not None:
124
yield base_marker + newline
125
for i in range(t[1], t[2]):
127
yield mid_marker + newline
105
yield mid_marker + '\n'
128
106
for i in range(t[5], t[6]):
130
yield end_marker + newline
108
yield end_marker + '\n'
132
110
raise ValueError(what)
290
def reprocess_merge_regions(self, merge_regions):
291
"""Where there are conflict regions, remove the agreed lines.
293
Lines where both A and B have made the same changes are
296
for region in merge_regions:
297
if region[0] != "conflict":
300
type, iz, zmatch, ia, amatch, ib, bmatch = region
301
a_region = self.a[ia:amatch]
302
b_region = self.b[ib:bmatch]
303
matches = bzrlib.patiencediff.PatienceSequenceMatcher(
304
None, a_region, b_region).get_matching_blocks()
307
for region_ia, region_ib, region_len in matches[:-1]:
310
reg = self.mismatch_region(next_a, region_ia, next_b,
314
yield 'same', region_ia, region_len+region_ia
315
next_a = region_ia + region_len
316
next_b = region_ib + region_len
317
reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
323
def mismatch_region(next_a, region_ia, next_b, region_ib):
324
if next_a < region_ia or next_b < region_ib:
325
return 'conflict', None, None, next_a, region_ia, next_b, region_ib
328
269
def find_sync_regions(self):
329
270
"""Return a list of sync regions, where both descendents match the base.
331
272
Generates a list of (base1, base2, a1, a2, b1, b2). There is
332
273
always a zero-length sync region at the end of all the files.
275
from difflib import SequenceMatcher
336
amatches = bzrlib.patiencediff.PatienceSequenceMatcher(
337
None, self.base, self.a).get_matching_blocks()
338
bmatches = bzrlib.patiencediff.PatienceSequenceMatcher(
339
None, self.base, self.b).get_matching_blocks()
278
amatches = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
279
bmatches = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
340
280
len_a = len(amatches)
341
281
len_b = len(bmatches)
393
333
def find_unconflicted(self):
394
334
"""Return a list of ranges in base that are not conflicted."""
395
am = bzrlib.patiencediff.PatienceSequenceMatcher(
396
None, self.base, self.a).get_matching_blocks()
397
bm = bzrlib.patiencediff.PatienceSequenceMatcher(
398
None, self.base, self.b).get_matching_blocks()
335
from difflib import SequenceMatcher
339
# don't sync-up on lines containing only blanks or pounds
340
junk_re = re.compile(r'^[ \t#]*$')
342
am = SequenceMatcher(junk_re.match, self.base, self.a).get_matching_blocks()
343
bm = SequenceMatcher(junk_re.match, self.base, self.b).get_matching_blocks()