1
# Copyright (C) 2005-2011, 2016 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
22
from ..errors import BinaryFile
23
from ..sixish import (
29
return BytesIO(t).readlines()
31
############################################################
32
# test case data from the gnu diffutils manual
34
TZU = split_lines(b""" The Nameless is the origin of Heaven and Earth;
35
The named is the mother of all things.
37
Therefore let there always be non-being,
38
so we may see their subtlety,
39
And let there always be being,
40
so we may see their outcome.
42
But after they are produced,
43
they have different names.
44
They both may be called deep and profound.
45
Deeper and more profound,
46
The door of all subtleties!
49
LAO = split_lines(b""" The Way that can be told of is not the eternal Way;
50
The name that can be named is not the eternal name.
51
The Nameless is the origin of Heaven and Earth;
52
The Named is the mother of all things.
53
Therefore let there always be non-being,
54
so we may see their subtlety,
55
And let there always be being,
56
so we may see their outcome.
58
But after they are produced,
59
they have different names.
63
TAO = split_lines(b""" The Way that can be told of is not the eternal Way;
64
The name that can be named is not the eternal name.
65
The Nameless is the origin of Heaven and Earth;
66
The named is the mother of all things.
68
Therefore let there always be non-being,
69
so we may see their subtlety,
70
And let there always be being,
71
so we may see their result.
73
But after they are produced,
74
they have different names.
76
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
80
MERGED_RESULT = split_lines(b""" The Way that can be told of is not the eternal Way;
81
The name that can be named is not the eternal name.
82
The Nameless is the origin of Heaven and Earth;
83
The Named is the mother of all things.
84
Therefore let there always be non-being,
85
so we may see their subtlety,
86
And let there always be being,
87
so we may see their result.
89
But after they are produced,
90
they have different names.
94
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
99
class TestMerge3(tests.TestCase):
101
def test_no_changes(self):
102
"""No conflicts because nothing changed"""
103
m3 = merge3.Merge3([b'aaa', b'bbb'],
107
self.assertEqual(m3.find_unconflicted(),
110
self.assertEqual(list(m3.find_sync_regions()),
116
self.assertEqual(list(m3.merge_regions()),
117
[('unchanged', 0, 2)])
119
self.assertEqual(list(m3.merge_groups()),
120
[('unchanged', [b'aaa', b'bbb'])])
122
def test_front_insert(self):
123
m3 = merge3.Merge3([b'zz'],
124
[b'aaa', b'bbb', b'zz'],
127
# todo: should use a sentinal at end as from get_matching_blocks
128
# to match without zz
129
self.assertEqual(list(m3.find_sync_regions()),
131
(1, 1, 3, 3, 1, 1),])
133
self.assertEqual(list(m3.merge_regions()),
135
('unchanged', 0, 1)])
137
self.assertEqual(list(m3.merge_groups()),
138
[('a', [b'aaa', b'bbb']),
139
('unchanged', [b'zz'])])
141
def test_null_insert(self):
142
m3 = merge3.Merge3([],
145
# todo: should use a sentinal at end as from get_matching_blocks
146
# to match without zz
147
self.assertEqual(list(m3.find_sync_regions()),
148
[(0, 0, 2, 2, 0, 0)])
150
self.assertEqual(list(m3.merge_regions()),
153
self.assertEqual(list(m3.merge_lines()),
156
def test_no_conflicts(self):
157
"""No conflicts because only one side changed"""
158
m3 = merge3.Merge3([b'aaa', b'bbb'],
159
[b'aaa', b'111', b'bbb'],
162
self.assertEqual(m3.find_unconflicted(),
165
self.assertEqual(list(m3.find_sync_regions()),
168
(2, 2, 3, 3, 2, 2),])
170
self.assertEqual(list(m3.merge_regions()),
171
[('unchanged', 0, 1),
173
('unchanged', 1, 2),])
175
def test_append_a(self):
176
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
177
[b'aaa\n', b'bbb\n', b'222\n'],
178
[b'aaa\n', b'bbb\n'])
180
self.assertEqual(b''.join(m3.merge_lines()),
183
def test_append_b(self):
184
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
185
[b'aaa\n', b'bbb\n'],
186
[b'aaa\n', b'bbb\n', b'222\n'])
188
self.assertEqual(b''.join(m3.merge_lines()),
191
def test_append_agreement(self):
192
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
193
[b'aaa\n', b'bbb\n', b'222\n'],
194
[b'aaa\n', b'bbb\n', b'222\n'])
196
self.assertEqual(b''.join(m3.merge_lines()),
199
def test_append_clash(self):
200
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
201
[b'aaa\n', b'bbb\n', b'222\n'],
202
[b'aaa\n', b'bbb\n', b'333\n'])
204
ml = m3.merge_lines(name_a=b'a',
209
self.assertEqual(b''.join(ml),
220
def test_insert_agreement(self):
221
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
222
[b'aaa\n', b'222\n', b'bbb\n'],
223
[b'aaa\n', b'222\n', b'bbb\n'])
225
ml = m3.merge_lines(name_a=b'a',
230
self.assertEqual(b''.join(ml), b'aaa\n222\nbbb\n')
233
def test_insert_clash(self):
234
"""Both try to insert lines in the same place."""
235
m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
236
[b'aaa\n', b'111\n', b'bbb\n'],
237
[b'aaa\n', b'222\n', b'bbb\n'])
239
self.assertEqual(m3.find_unconflicted(),
242
self.assertEqual(list(m3.find_sync_regions()),
245
(2, 2, 3, 3, 3, 3),])
247
self.assertEqual(list(m3.merge_regions()),
248
[('unchanged', 0, 1),
249
('conflict', 1, 1, 1, 2, 1, 2),
250
('unchanged', 1, 2)])
252
self.assertEqual(list(m3.merge_groups()),
253
[('unchanged', [b'aaa\n']),
254
('conflict', [], [b'111\n'], [b'222\n']),
255
('unchanged', [b'bbb\n']),
258
ml = m3.merge_lines(name_a=b'a',
263
self.assertEqual(b''.join(ml),
273
def test_replace_clash(self):
274
"""Both try to insert lines in the same place."""
275
m3 = merge3.Merge3([b'aaa', b'000', b'bbb'],
276
[b'aaa', b'111', b'bbb'],
277
[b'aaa', b'222', b'bbb'])
279
self.assertEqual(m3.find_unconflicted(),
282
self.assertEqual(list(m3.find_sync_regions()),
285
(3, 3, 3, 3, 3, 3),])
287
def test_replace_multi(self):
288
"""Replacement with regions of different size."""
289
m3 = merge3.Merge3([b'aaa', b'000', b'000', b'bbb'],
290
[b'aaa', b'111', b'111', b'111', b'bbb'],
291
[b'aaa', b'222', b'222', b'222', b'222', b'bbb'])
293
self.assertEqual(m3.find_unconflicted(),
297
self.assertEqual(list(m3.find_sync_regions()),
300
(4, 4, 5, 5, 6, 6),])
302
def test_merge_poem(self):
303
"""Test case from diff3 manual"""
304
m3 = merge3.Merge3(TZU, LAO, TAO)
305
ml = list(m3.merge_lines(b'LAO', b'TAO'))
306
self.log('merge result:')
307
self.log(b''.join(ml))
308
self.assertEqual(ml, MERGED_RESULT)
310
def test_minimal_conflicts_common(self):
312
base_text = (b"a\n" * 20).splitlines(True)
313
this_text = (b"a\n"*10+b"b\n" * 10).splitlines(True)
314
other_text = (b"a\n"*10+b"c\n"+b"b\n" * 8 + b"c\n").splitlines(True)
315
m3 = merge3.Merge3(base_text, other_text, this_text)
316
m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
317
merged_text = b"".join(list(m_lines))
318
optimal_text = (b"a\n" * 10 + b"<<<<<<< OTHER\nc\n"
319
+ 8* b"b\n" + b"c\n=======\n"
320
+ 10*b"b\n" + b">>>>>>> THIS\n")
321
self.assertEqualDiff(optimal_text, merged_text)
323
def test_minimal_conflicts_unique(self):
325
"""Add a newline to each entry in the string"""
326
return [(int2byte(x)+b'\n') for x in bytearray(s)]
328
base_text = add_newline(b"abcdefghijklm")
329
this_text = add_newline(b"abcdefghijklmNOPQRSTUVWXYZ")
330
other_text = add_newline(b"abcdefghijklm1OPQRSTUVWXY2")
331
m3 = merge3.Merge3(base_text, other_text, this_text)
332
m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
333
merged_text = b"".join(list(m_lines))
334
optimal_text = b''.join(add_newline(b"abcdefghijklm")
335
+ [b"<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"]
336
+ add_newline(b'OPQRSTUVWXY')
337
+ [b"<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"]
339
self.assertEqualDiff(optimal_text, merged_text)
341
def test_minimal_conflicts_nonunique(self):
343
"""Add a newline to each entry in the string"""
344
return [(int2byte(x)+b'\n') for x in bytearray(s)]
346
base_text = add_newline(b"abacddefgghij")
347
this_text = add_newline(b"abacddefgghijkalmontfprz")
348
other_text = add_newline(b"abacddefgghijknlmontfprd")
349
m3 = merge3.Merge3(base_text, other_text, this_text)
350
m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
351
merged_text = b"".join(list(m_lines))
352
optimal_text = b''.join(add_newline(b"abacddefgghijk")
353
+ [b"<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"]
354
+ add_newline(b'lmontfpr')
355
+ [b"<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"]
357
self.assertEqualDiff(optimal_text, merged_text)
359
def test_reprocess_and_base(self):
360
"""Reprocessing and showing base breaks correctly"""
361
base_text = (b"a\n" * 20).splitlines(True)
362
this_text = (b"a\n"*10+b"b\n" * 10).splitlines(True)
363
other_text = (b"a\n"*10+b"c\n"+b"b\n" * 8 + b"c\n").splitlines(True)
364
m3 = merge3.Merge3(base_text, other_text, this_text)
365
m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True,
366
base_marker=b'|||||||')
367
self.assertRaises(merge3.CantReprocessAndShowBase, list, m_lines)
369
def test_binary(self):
370
self.assertRaises(BinaryFile, merge3.Merge3, [b'\x00'], [b'a'], [b'b'])
372
def test_dos_text(self):
375
other_text = b'c\r\n'
376
m3 = merge3.Merge3(base_text.splitlines(True),
377
other_text.splitlines(True),
378
this_text.splitlines(True))
379
m_lines = m3.merge_lines(b'OTHER', b'THIS')
380
self.assertEqual(b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
381
b'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
383
def test_mac_text(self):
387
m3 = merge3.Merge3(base_text.splitlines(True),
388
other_text.splitlines(True),
389
this_text.splitlines(True))
390
m_lines = m3.merge_lines(b'OTHER', b'THIS')
391
self.assertEqual(b'<<<<<<< OTHER\rc\r=======\rb\r'
392
b'>>>>>>> THIS\r'.splitlines(True), list(m_lines))
394
def test_merge3_cherrypick(self):
395
base_text = b"a\nb\n"
397
other_text = b"a\nb\nc\n"
398
# When cherrypicking, lines in base are not part of the conflict
399
m3 = merge3.Merge3(base_text.splitlines(True),
400
this_text.splitlines(True),
401
other_text.splitlines(True), is_cherrypick=True)
402
m_lines = m3.merge_lines()
403
self.assertEqualDiff(b'a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
406
# This is not symmetric
407
m3 = merge3.Merge3(base_text.splitlines(True),
408
other_text.splitlines(True),
409
this_text.splitlines(True), is_cherrypick=True)
410
m_lines = m3.merge_lines()
411
self.assertEqualDiff(b'a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
414
def test_merge3_cherrypick_w_mixed(self):
415
base_text = b'a\nb\nc\nd\ne\n'
416
this_text = b'a\nb\nq\n'
417
other_text = b'a\nb\nc\nd\nf\ne\ng\n'
418
# When cherrypicking, lines in base are not part of the conflict
419
m3 = merge3.Merge3(base_text.splitlines(True),
420
this_text.splitlines(True),
421
other_text.splitlines(True), is_cherrypick=True)
422
m_lines = m3.merge_lines()
423
self.assertEqualDiff(b'a\n'
436
def test_allow_objects(self):
437
"""Objects other than strs may be used with Merge3 when
440
merge_groups and merge_regions work with non-str input. Methods that
441
return lines like merge_lines fail.
443
base = [(x, x) for x in 'abcde']
444
a = [(x, x) for x in 'abcdef']
445
b = [(x, x) for x in 'Zabcde']
446
m3 = merge3.Merge3(base, a, b, allow_objects=True)
451
list(m3.merge_regions()))
453
[('b', [('Z', 'Z')]),
454
('unchanged', [(x, x) for x in 'abcde']),
455
('a', [('f', 'f')])],
456
list(m3.merge_groups()))