1
# Copyright (C) 2004, 2005 by Canonical Ltd
1
# Copyright (C) 2005-2011 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.tests import TestCaseInTempDir, TestCase
19
from bzrlib.merge3 import Merge3
20
from bzrlib.errors import CantReprocessAndShowBase
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
from bzrlib.errors import CantReprocessAndShowBase, BinaryFile
22
24
def split_lines(t):
23
25
from cStringIO import StringIO
168
170
('unchanged', 1, 2),])
170
172
def test_append_a(self):
171
m3 = Merge3(['aaa\n', 'bbb\n'],
172
['aaa\n', 'bbb\n', '222\n'],
173
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
174
['aaa\n', 'bbb\n', '222\n'],
175
177
self.assertEquals(''.join(m3.merge_lines()),
176
178
'aaa\nbbb\n222\n')
178
180
def test_append_b(self):
179
m3 = Merge3(['aaa\n', 'bbb\n'],
181
['aaa\n', 'bbb\n', '222\n'])
181
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
183
['aaa\n', 'bbb\n', '222\n'])
183
185
self.assertEquals(''.join(m3.merge_lines()),
184
186
'aaa\nbbb\n222\n')
186
188
def test_append_agreement(self):
187
m3 = Merge3(['aaa\n', 'bbb\n'],
188
['aaa\n', 'bbb\n', '222\n'],
189
['aaa\n', 'bbb\n', '222\n'])
189
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
190
['aaa\n', 'bbb\n', '222\n'],
191
['aaa\n', 'bbb\n', '222\n'])
191
193
self.assertEquals(''.join(m3.merge_lines()),
192
194
'aaa\nbbb\n222\n')
194
196
def test_append_clash(self):
195
m3 = Merge3(['aaa\n', 'bbb\n'],
196
['aaa\n', 'bbb\n', '222\n'],
197
['aaa\n', 'bbb\n', '333\n'])
197
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
198
['aaa\n', 'bbb\n', '222\n'],
199
['aaa\n', 'bbb\n', '333\n'])
199
201
ml = m3.merge_lines(name_a='a',
297
299
def test_merge_poem(self):
298
300
"""Test case from diff3 manual"""
299
m3 = Merge3(TZU, LAO, TAO)
301
m3 = merge3.Merge3(TZU, LAO, TAO)
300
302
ml = list(m3.merge_lines('LAO', 'TAO'))
301
303
self.log('merge result:')
302
304
self.log(''.join(ml))
303
305
self.assertEquals(ml, MERGED_RESULT)
305
def test_minimal_conflicts(self):
307
def test_minimal_conflicts_common(self):
306
308
"""Reprocessing"""
307
309
base_text = ("a\n" * 20).splitlines(True)
308
310
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
309
311
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
310
m3 = Merge3(base_text, other_text, this_text)
311
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
312
merged_text = "".join(list(m_lines))
313
optimal_text = "a\n" * 10 + "<<<<<<< OTHER\nc\n=======\n>>>>>>> THIS"\
314
+ "\n" + 8* "b\n" + "<<<<<<< OTHER\nc\n=======\nb\nb\n>>>>>>>"\
316
self.assertEqualDiff(merged_text, optimal_text)
312
m3 = merge3.Merge3(base_text, other_text, this_text)
313
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
314
merged_text = "".join(list(m_lines))
315
optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n"
316
+ 8* "b\n" + "c\n=======\n"
317
+ 10*"b\n" + ">>>>>>> THIS\n")
318
self.assertEqualDiff(optimal_text, merged_text)
320
def test_minimal_conflicts_unique(self):
322
"""Add a newline to each entry in the string"""
323
return [(x+'\n') for x in s]
325
base_text = add_newline("abcdefghijklm")
326
this_text = add_newline("abcdefghijklmNOPQRSTUVWXYZ")
327
other_text = add_newline("abcdefghijklm1OPQRSTUVWXY2")
328
m3 = merge3.Merge3(base_text, other_text, this_text)
329
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
330
merged_text = "".join(list(m_lines))
331
optimal_text = ''.join(add_newline("abcdefghijklm")
332
+ ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"]
333
+ add_newline('OPQRSTUVWXY')
334
+ ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"]
336
self.assertEqualDiff(optimal_text, merged_text)
338
def test_minimal_conflicts_nonunique(self):
340
"""Add a newline to each entry in the string"""
341
return [(x+'\n') for x in s]
343
base_text = add_newline("abacddefgghij")
344
this_text = add_newline("abacddefgghijkalmontfprz")
345
other_text = add_newline("abacddefgghijknlmontfprd")
346
m3 = merge3.Merge3(base_text, other_text, this_text)
347
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
348
merged_text = "".join(list(m_lines))
349
optimal_text = ''.join(add_newline("abacddefgghijk")
350
+ ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"]
351
+ add_newline('lmontfpr')
352
+ ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"]
354
self.assertEqualDiff(optimal_text, merged_text)
318
356
def test_reprocess_and_base(self):
319
357
"""Reprocessing and showing base breaks correctly"""
320
358
base_text = ("a\n" * 20).splitlines(True)
321
359
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
322
360
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
323
m3 = Merge3(base_text, other_text, this_text)
324
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
361
m3 = merge3.Merge3(base_text, other_text, this_text)
362
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
325
363
base_marker='|||||||')
326
364
self.assertRaises(CantReprocessAndShowBase, list, m_lines)
366
def test_binary(self):
367
self.assertRaises(BinaryFile, merge3.Merge3, ['\x00'], ['a'], ['b'])
369
def test_dos_text(self):
373
m3 = merge3.Merge3(base_text.splitlines(True),
374
other_text.splitlines(True),
375
this_text.splitlines(True))
376
m_lines = m3.merge_lines('OTHER', 'THIS')
377
self.assertEqual('<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
378
'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
380
def test_mac_text(self):
384
m3 = merge3.Merge3(base_text.splitlines(True),
385
other_text.splitlines(True),
386
this_text.splitlines(True))
387
m_lines = m3.merge_lines('OTHER', 'THIS')
388
self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r'
389
'>>>>>>> THIS\r'.splitlines(True), list(m_lines))
391
def test_merge3_cherrypick(self):
394
other_text = "a\nb\nc\n"
395
# When cherrypicking, lines in base are not part of the conflict
396
m3 = merge3.Merge3(base_text.splitlines(True),
397
this_text.splitlines(True),
398
other_text.splitlines(True), is_cherrypick=True)
399
m_lines = m3.merge_lines()
400
self.assertEqualDiff('a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
403
# This is not symmetric
404
m3 = merge3.Merge3(base_text.splitlines(True),
405
other_text.splitlines(True),
406
this_text.splitlines(True), is_cherrypick=True)
407
m_lines = m3.merge_lines()
408
self.assertEqualDiff('a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
411
def test_merge3_cherrypick_w_mixed(self):
412
base_text = 'a\nb\nc\nd\ne\n'
413
this_text = 'a\nb\nq\n'
414
other_text = 'a\nb\nc\nd\nf\ne\ng\n'
415
# When cherrypicking, lines in base are not part of the conflict
416
m3 = merge3.Merge3(base_text.splitlines(True),
417
this_text.splitlines(True),
418
other_text.splitlines(True), is_cherrypick=True)
419
m_lines = m3.merge_lines()
420
self.assertEqualDiff('a\n'
433
def test_allow_objects(self):
434
"""Objects other than strs may be used with Merge3 when
437
merge_groups and merge_regions work with non-str input. Methods that
438
return lines like merge_lines fail.
440
base = [(x,x) for x in 'abcde']
441
a = [(x,x) for x in 'abcdef']
442
b = [(x,x) for x in 'Zabcde']
443
m3 = merge3.Merge3(base, a, b, allow_objects=True)
448
list(m3.merge_regions()))
450
[('b', [('Z', 'Z')]),
451
('unchanged', [(x,x) for x in 'abcde']),
452
('a', [('f', 'f')])],
453
list(m3.merge_groups()))