39
34
from bzrlib.osutils import getcwd, pathjoin
35
from bzrlib import progress
40
36
from bzrlib.transform import TreeTransform
41
37
from bzrlib.tests import TestCaseWithTransport, TestSkipped
42
38
from bzrlib.workingtree import WorkingTree
45
41
class MergeBuilder(object):
47
42
def __init__(self, dir=None):
48
43
self.dir = osutils.mkdtemp(prefix="merge-test", dir=dir)
49
44
self.tree_root = generate_ids.gen_root_id()
51
46
path = pathjoin(self.dir, name)
53
wt = controldir.ControlDir.create_standalone_workingtree(path)
48
wt = bzrlib.bzrdir.BzrDir.create_standalone_workingtree(path)
54
49
# the tests perform pulls, so need a branch that is writeable.
56
51
wt.set_root_id(self.tree_root)
495
490
wtb = d_b.open_workingtree()
496
491
wtb.commit('this revision', allow_pointless=False)
497
492
self.assertEqual(1, wtb.merge_from_branch(wta.branch))
498
self.assertPathExists('b/file.THIS')
499
self.assertPathExists('b/file.BASE')
500
self.assertPathExists('b/file.OTHER')
493
self.failUnlessExists('b/file.THIS')
494
self.failUnlessExists('b/file.BASE')
495
self.failUnlessExists('b/file.OTHER')
502
497
self.assertEqual(1, wtb.merge_from_branch(wta.branch,
503
498
merge_type=WeaveMerger))
504
self.assertPathExists('b/file')
505
self.assertPathExists('b/file.THIS')
506
self.assertPathExists('b/file.BASE')
507
self.assertPathExists('b/file.OTHER')
499
self.failUnlessExists('b/file')
500
self.failUnlessExists('b/file.THIS')
501
self.failUnlessExists('b/file.BASE')
502
self.failUnlessExists('b/file.OTHER')
509
504
def test_weave_conflicts_not_in_base(self):
510
505
builder = self.make_branch_builder('source')
534
529
self.assertEqual(1, tree.merge_from_branch(tree.branch,
535
530
to_revision='D-id',
536
531
merge_type=WeaveMerger))
537
self.assertPathExists('tree/foo.THIS')
538
self.assertPathExists('tree/foo.OTHER')
532
self.failUnlessExists('tree/foo.THIS')
533
self.failUnlessExists('tree/foo.OTHER')
539
534
self.expectFailure('fail to create .BASE in some criss-cross merges',
540
self.assertPathExists, 'tree/foo.BASE')
541
self.assertPathExists('tree/foo.BASE')
535
self.failUnlessExists, 'tree/foo.BASE')
536
self.failUnlessExists('tree/foo.BASE')
543
538
def test_merge_unrelated(self):
544
539
"""Sucessfully merges unrelated branches with no common names"""
545
540
wta = self.make_branch_and_tree('a')
547
with file('a/a_file', 'wb') as f: f.write('contents\n')
542
file('a/a_file', 'wb').write('contents\n')
548
543
wta.add('a_file')
549
544
wta.commit('a_revision', allow_pointless=False)
550
545
wtb = self.make_branch_and_tree('b')
552
with file('b/b_file', 'wb') as f: f.write('contents\n')
547
file('b/b_file', 'wb').write('contents\n')
553
548
wtb.add('b_file')
554
549
b_rev = wtb.commit('b_revision', allow_pointless=False)
555
550
wta.merge_from_branch(wtb.branch, b_rev, 'null:')
556
self.assertTrue(os.path.lexists('a/b_file'))
551
self.assert_(os.path.lexists('a/b_file'))
557
552
self.assertEqual([b_rev], wta.get_parent_ids()[1:])
559
554
def test_merge_unrelated_conflicting(self):
560
555
"""Sucessfully merges unrelated branches with common names"""
561
556
wta = self.make_branch_and_tree('a')
563
with file('a/file', 'wb') as f: f.write('contents\n')
558
file('a/file', 'wb').write('contents\n')
565
560
wta.commit('a_revision', allow_pointless=False)
566
561
wtb = self.make_branch_and_tree('b')
568
with file('b/file', 'wb') as f: f.write('contents\n')
563
file('b/file', 'wb').write('contents\n')
570
565
b_rev = wtb.commit('b_revision', allow_pointless=False)
571
566
wta.merge_from_branch(wtb.branch, b_rev, 'null:')
572
self.assertTrue(os.path.lexists('a/file'))
573
self.assertTrue(os.path.lexists('a/file.moved'))
567
self.assert_(os.path.lexists('a/file'))
568
self.assert_(os.path.lexists('a/file.moved'))
574
569
self.assertEqual([b_rev], wta.get_parent_ids()[1:])
576
571
def test_merge_deleted_conflicts(self):
577
572
wta = self.make_branch_and_tree('a')
578
with file('a/file', 'wb') as f: f.write('contents\n')
573
file('a/file', 'wb').write('contents\n')
580
575
wta.commit('a_revision', allow_pointless=False)
581
576
self.run_bzr('branch a b')
582
577
os.remove('a/file')
583
578
wta.commit('removed file', allow_pointless=False)
584
with file('b/file', 'wb') as f: f.write('changed contents\n')
579
file('b/file', 'wb').write('changed contents\n')
585
580
wtb = WorkingTree.open('b')
586
581
wtb.commit('changed file', allow_pointless=False)
587
582
wtb.merge_from_branch(wta.branch, wta.branch.last_revision(),
588
583
wta.branch.get_rev_id(1))
589
self.assertFalse(os.path.lexists('b/file'))
584
self.failIf(os.path.lexists('b/file'))
591
586
def test_merge_metadata_vs_deletion(self):
592
587
"""Conflict deletion vs metadata change"""
593
588
a_wt = self.make_branch_and_tree('a')
594
with file('a/file', 'wb') as f: f.write('contents\n')
589
file('a/file', 'wb').write('contents\n')
596
591
a_wt.commit('r0')
597
592
self.run_bzr('branch a b')
603
598
self.assertFalse(os.path.exists('a/file'))
604
599
b_wt.commit('exec a')
605
600
a_wt.merge_from_branch(b_wt.branch, b_wt.last_revision(), 'null:')
606
self.assertTrue(os.path.exists('a/file'))
601
self.assert_(os.path.exists('a/file'))
608
603
def test_merge_swapping_renames(self):
609
604
a_wt = self.make_branch_and_tree('a')
610
with file('a/un','wb') as f: f.write('UN')
611
with file('a/deux','wb') as f: f.write('DEUX')
605
file('a/un','wb').write('UN')
606
file('a/deux','wb').write('DEUX')
612
607
a_wt.add('un', 'un-id')
613
608
a_wt.add('deux', 'deux-id')
614
609
a_wt.commit('r0', rev_id='r0')
620
615
b_wt.commit('r1', rev_id='r1')
621
616
self.assertEqual(0, a_wt.merge_from_branch(b_wt.branch,
622
617
b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1)))
623
self.assertPathExists('a/un')
624
self.assertTrue('a/deux')
618
self.failUnlessExists('a/un')
619
self.failUnless('a/deux')
625
620
self.assertFalse(os.path.exists('a/tmp'))
626
621
self.assertEqual(file('a/un').read(),'DEUX')
627
622
self.assertEqual(file('a/deux').read(),'UN')
629
624
def test_merge_delete_and_add_same(self):
630
625
a_wt = self.make_branch_and_tree('a')
631
with file('a/file', 'wb') as f: f.write('THIS')
626
file('a/file', 'wb').write('THIS')
633
628
a_wt.commit('r0')
634
629
self.run_bzr('branch a b')
635
630
b_wt = WorkingTree.open('b')
636
631
os.remove('b/file')
637
632
b_wt.commit('r1')
638
with file('b/file', 'wb') as f: f.write('THAT')
633
file('b/file', 'wb').write('THAT')
640
635
b_wt.commit('r2')
641
636
a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
642
637
b_wt.branch.get_rev_id(1))
643
self.assertTrue(os.path.exists('a/file'))
638
self.assert_(os.path.exists('a/file'))
644
639
self.assertEqual(file('a/file').read(),'THAT')
646
641
def test_merge_rename_before_create(self):
660
655
a_wt = self.make_branch_and_tree('a')
661
with file('a/foo', 'wb') as f: f.write('A/FOO')
656
file('a/foo', 'wb').write('A/FOO')
663
658
a_wt.commit('added foo')
664
659
self.run_bzr('branch a b')
665
660
b_wt = WorkingTree.open('b')
666
661
b_wt.rename_one('foo', 'bar')
667
with file('b/foo', 'wb') as f: f.write('B/FOO')
662
file('b/foo', 'wb').write('B/FOO')
669
664
b_wt.commit('moved foo to bar, added new foo')
670
665
a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),