274
270
tree_b.merge_from_branch(tree_a.branch)
275
271
except AttributeError:
276
272
self.fail('tried to join a path when name was None')
278
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
279
tree_a = self.make_branch_and_tree('a')
280
self.build_tree(['a/file_1', 'a/file_2'])
281
tree_a.add(['file_1'])
282
tree_a.commit('commit 1')
283
tree_a.add(['file_2'])
284
tree_a.commit('commit 2')
285
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
286
tree_b.rename_one('file_1', 'renamed')
287
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
288
progress.DummyProgress())
289
merger.merge_type = _mod_merge.Merge3Merger
291
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
293
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
294
tree_a = self.make_branch_and_tree('a')
295
self.build_tree(['a/file_1', 'a/file_2'])
296
tree_a.add(['file_1'])
297
tree_a.commit('commit 1')
298
tree_a.add(['file_2'])
299
tree_a.commit('commit 2')
300
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
301
tree_b.rename_one('file_1', 'renamed')
302
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
303
progress.DummyProgress())
304
merger.merge_type = _mod_merge.WeaveMerger
306
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
308
def prepare_cherrypick(self):
309
"""Prepare a pair of trees for cherrypicking tests.
311
Both trees have a file, 'file'.
312
rev1 sets content to 'a'.
315
A full merge of rev2b and rev3b into this_tree would add both 'b' and
316
'c'. A successful cherrypick of rev2b-rev3b into this_tree will add
319
this_tree = self.make_branch_and_tree('this')
320
self.build_tree_contents([('this/file', "a\n")])
321
this_tree.add('file')
322
this_tree.commit('rev1')
323
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
324
self.build_tree_contents([('other/file', "a\nb\n")])
325
other_tree.commit('rev2b', rev_id='rev2b')
326
self.build_tree_contents([('other/file', "c\na\nb\n")])
327
other_tree.commit('rev3b', rev_id='rev3b')
328
this_tree.lock_write()
329
self.addCleanup(this_tree.unlock)
330
return this_tree, other_tree
332
def test_weave_cherrypick(self):
333
this_tree, other_tree = self.prepare_cherrypick()
334
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
335
this_tree, 'rev3b', 'rev2b', other_tree.branch)
336
merger.merge_type = _mod_merge.WeaveMerger
338
self.assertFileEqual('c\na\n', 'this/file')
340
def test_weave_cannot_reverse_cherrypick(self):
341
this_tree, other_tree = self.prepare_cherrypick()
342
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
343
this_tree, 'rev2b', 'rev3b', other_tree.branch)
344
merger.merge_type = _mod_merge.WeaveMerger
345
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
347
def test_merge3_can_reverse_cherrypick(self):
348
this_tree, other_tree = self.prepare_cherrypick()
349
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
350
this_tree, 'rev2b', 'rev3b', other_tree.branch)
351
merger.merge_type = _mod_merge.Merge3Merger
355
class TestPlanMerge(TestCaseWithMemoryTransport):
358
TestCaseWithMemoryTransport.setUp(self)
359
self.vf = knit.KnitVersionedFile('root', self.get_transport(),
361
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root',
364
def add_version(self, version_id, parents, text):
365
self.vf.add_lines(version_id, parents, [c+'\n' for c in text])
367
def add_uncommitted_version(self, version_id, parents, text):
368
self.plan_merge_vf.add_lines(version_id, parents,
369
[c+'\n' for c in text])
371
def setup_plan_merge(self):
372
self.add_version('A', [], 'abc')
373
self.add_version('B', ['A'], 'acehg')
374
self.add_version('C', ['A'], 'fabg')
375
return _PlanMerge('B', 'C', self.plan_merge_vf)
377
def setup_plan_merge_uncommitted(self):
378
self.add_version('A', [], 'abc')
379
self.add_uncommitted_version('B:', ['A'], 'acehg')
380
self.add_uncommitted_version('C:', ['A'], 'fabg')
381
return _PlanMerge('B:', 'C:', self.plan_merge_vf)
383
def test_unique_lines(self):
384
plan = self.setup_plan_merge()
385
self.assertEqual(plan._unique_lines(
386
plan._get_matching_blocks('B', 'C')),
389
def test_find_new(self):
390
plan = self.setup_plan_merge()
391
self.assertEqual(set([2, 3, 4]), plan._find_new('B'))
392
self.assertEqual(set([0, 3]), plan._find_new('C'))
394
def test_find_new2(self):
395
self.add_version('A', [], 'abc')
396
self.add_version('B', ['A'], 'abcde')
397
self.add_version('C', ['A'], 'abcefg')
398
self.add_version('D', ['A', 'B', 'C'], 'abcdegh')
399
my_plan = _PlanMerge('B', 'D', self.plan_merge_vf)
400
self.assertEqual(set([5, 6]), my_plan._find_new('D'))
401
self.assertEqual(set(), my_plan._find_new('A'))
403
def test_find_new_no_ancestors(self):
404
self.add_version('A', [], 'abc')
405
self.add_version('B', [], 'xyz')
406
my_plan = _PlanMerge('A', 'B', self.vf)
407
self.assertEqual(set([0, 1, 2]), my_plan._find_new('A'))
409
def test_plan_merge(self):
410
self.setup_plan_merge()
411
plan = self.plan_merge_vf.plan_merge('B', 'C')
414
('unchanged', 'a\n'),
419
('unchanged', 'g\n')],
422
def test_plan_merge_uncommitted_files(self):
423
self.setup_plan_merge_uncommitted()
424
plan = self.plan_merge_vf.plan_merge('B:', 'C:')
427
('unchanged', 'a\n'),
432
('unchanged', 'g\n')],
435
def test_subtract_plans(self):
437
('unchanged', 'a\n'),
446
('unchanged', 'a\n'),
455
('unchanged', 'a\n'),
459
('unchanged', 'f\n'),
462
self.assertEqual(subtracted_plan,
463
list(_PlanMerge._subtract_plans(old_plan, new_plan)))
465
def setup_merge_with_base(self):
466
self.add_version('COMMON', [], 'abc')
467
self.add_version('THIS', ['COMMON'], 'abcd')
468
self.add_version('BASE', ['COMMON'], 'eabc')
469
self.add_version('OTHER', ['BASE'], 'eafb')
471
def test_plan_merge_with_base(self):
472
self.setup_merge_with_base()
473
plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
474
self.assertEqual([('unchanged', 'a\n'),
476
('unchanged', 'b\n'),
481
def test_plan_lca_merge(self):
482
self.setup_plan_merge()
483
plan = self.plan_merge_vf.plan_lca_merge('B', 'C')
486
('unchanged', 'a\n'),
491
('unchanged', 'g\n')],
494
def test_plan_lca_merge_uncommitted_files(self):
495
self.setup_plan_merge_uncommitted()
496
plan = self.plan_merge_vf.plan_lca_merge('B:', 'C:')
499
('unchanged', 'a\n'),
504
('unchanged', 'g\n')],
507
def test_plan_lca_merge_with_base(self):
508
self.setup_merge_with_base()
509
plan = self.plan_merge_vf.plan_lca_merge('THIS', 'OTHER', 'BASE')
510
self.assertEqual([('unchanged', 'a\n'),
512
('unchanged', 'b\n'),
517
def test_plan_lca_merge_with_criss_cross(self):
518
self.add_version('ROOT', [], 'abc')
519
# each side makes a change
520
self.add_version('REV1', ['ROOT'], 'abcd')
521
self.add_version('REV2', ['ROOT'], 'abce')
522
# both sides merge, discarding others' changes
523
self.add_version('LCA1', ['REV1', 'REV2'], 'abcd')
524
self.add_version('LCA2', ['REV1', 'REV2'], 'abce')
525
plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
526
self.assertEqual([('unchanged', 'a\n'),
527
('unchanged', 'b\n'),
528
('unchanged', 'c\n'),
529
('conflicted-a', 'd\n'),
530
('conflicted-b', 'e\n'),