~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-11-17 03:20:35 UTC
  • mfrom: (4792.4.3 456036)
  • Revision ID: pqm@pqm.ubuntu.com-20091117032035-s3sgtlixj1lrminn
(Gordon Tyler) Fix IndexError during 'bzr ignore /' (#456036)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    transform,
30
30
    versionedfile,
31
31
    )
 
32
from bzrlib.branch import Branch
32
33
from bzrlib.conflicts import ConflictList, TextConflict
33
 
from bzrlib.errors import UnrelatedBranches, NoCommits
 
34
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
34
35
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
35
36
from bzrlib.osutils import pathjoin, file_kind
36
37
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
37
38
from bzrlib.workingtree import WorkingTree
38
 
 
 
39
from bzrlib.transform import TreeTransform
39
40
 
40
41
class TestMerge(TestCaseWithTransport):
41
42
    """Test appending more than one revision"""
150
151
        log = StringIO()
151
152
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
152
153
                    this_tree=tree_b, ignore_zero=True)
153
 
        self.failUnless('All changes applied successfully.\n' not in
154
 
            self.get_log())
 
154
        log = self._get_log(keep_log_file=True)
 
155
        self.failUnless('All changes applied successfully.\n' not in log)
155
156
        tree_b.revert()
156
157
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
157
158
                    this_tree=tree_b, ignore_zero=False)
158
 
        self.failUnless('All changes applied successfully.\n' in self.get_log())
 
159
        log = self._get_log(keep_log_file=True)
 
160
        self.failUnless('All changes applied successfully.\n' in log)
159
161
 
160
162
    def test_merge_inner_conflicts(self):
161
163
        tree_a = self.make_branch_and_tree('a')
522
524
        self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
523
525
        return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
524
526
 
525
 
    def test_base_from_plan(self):
526
 
        self.setup_plan_merge()
527
 
        plan = self.plan_merge_vf.plan_merge('B', 'C')
528
 
        pwm = versionedfile.PlanWeaveMerge(plan)
529
 
        self.assertEqual(['a\n', 'b\n', 'c\n'], pwm.base_from_plan())
530
 
 
531
527
    def test_unique_lines(self):
532
528
        plan = self.setup_plan_merge()
533
529
        self.assertEqual(plan._unique_lines(
831
827
                          ('unchanged', 'f\n'),
832
828
                          ('unchanged', 'g\n')],
833
829
                         list(plan))
834
 
        plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
835
 
        # This is one of the main differences between plan_merge and
836
 
        # plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
837
 
        # because 'x' was not present in one of the bases. However, in this
838
 
        # case it is spurious because 'x' does not exist in the global base A.
839
 
        self.assertEqual([
840
 
                          ('unchanged', 'h\n'),
841
 
                          ('unchanged', 'a\n'),
842
 
                          ('conflicted-a', 'x\n'),
843
 
                          ('new-b', 'z\n'),
844
 
                          ('unchanged', 'c\n'),
845
 
                          ('unchanged', 'd\n'),
846
 
                          ('unchanged', 'y\n'),
847
 
                          ('unchanged', 'f\n'),
848
 
                          ('unchanged', 'g\n')],
849
 
                         list(plan))
850
 
 
851
 
    def test_criss_cross_flip_flop(self):
852
 
        # This is specificly trying to trigger problems when using limited
853
 
        # ancestry and weaves. The ancestry graph looks like:
854
 
        #       XX      unused ancestor, should not show up in the weave
855
 
        #       |
856
 
        #       A       Unique LCA
857
 
        #      / \  
858
 
        #     B   C     B & C both introduce a new line
859
 
        #     |\ /|  
860
 
        #     | X |  
861
 
        #     |/ \| 
862
 
        #     D   E     B & C are both merged, so both are common ancestors
863
 
        #               In the process of merging, both sides order the new
864
 
        #               lines differently
865
 
        #
866
 
        self.add_rev('root', 'XX', [], 'qrs')
867
 
        self.add_rev('root', 'A', ['XX'], 'abcdef')
868
 
        self.add_rev('root', 'B', ['A'], 'abcdgef')
869
 
        self.add_rev('root', 'C', ['A'], 'abcdhef')
870
 
        self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
871
 
        self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
872
 
        plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
873
 
        self.assertEqual([
874
 
                          ('unchanged', 'a\n'),
875
 
                          ('unchanged', 'b\n'),
876
 
                          ('unchanged', 'c\n'),
877
 
                          ('unchanged', 'd\n'),
878
 
                          ('new-b', 'h\n'),
879
 
                          ('unchanged', 'g\n'),
880
 
                          ('killed-b', 'h\n'),
881
 
                          ('unchanged', 'e\n'),
882
 
                          ('unchanged', 'f\n'),
883
 
                         ], plan)
884
 
        pwm = versionedfile.PlanWeaveMerge(plan)
885
 
        self.assertEqualDiff('\n'.join('abcdghef') + '\n',
886
 
                             ''.join(pwm.base_from_plan()))
887
 
        # Reversing the order reverses the merge plan, and final order of 'hg'
888
 
        # => 'gh'
889
 
        plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
890
 
        self.assertEqual([
891
 
                          ('unchanged', 'a\n'),
892
 
                          ('unchanged', 'b\n'),
893
 
                          ('unchanged', 'c\n'),
894
 
                          ('unchanged', 'd\n'),
895
 
                          ('new-b', 'g\n'),
896
 
                          ('unchanged', 'h\n'),
897
 
                          ('killed-b', 'g\n'),
898
 
                          ('unchanged', 'e\n'),
899
 
                          ('unchanged', 'f\n'),
900
 
                         ], plan)
901
 
        pwm = versionedfile.PlanWeaveMerge(plan)
902
 
        self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
903
 
                             ''.join(pwm.base_from_plan()))
904
 
        # This is where lca differs, in that it (fairly correctly) determines
905
 
        # that there is a conflict because both sides resolved the merge
906
 
        # differently
907
 
        plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
908
 
        self.assertEqual([
909
 
                          ('unchanged', 'a\n'),
910
 
                          ('unchanged', 'b\n'),
911
 
                          ('unchanged', 'c\n'),
912
 
                          ('unchanged', 'd\n'),
913
 
                          ('conflicted-b', 'h\n'),
914
 
                          ('unchanged', 'g\n'),
915
 
                          ('conflicted-a', 'h\n'),
916
 
                          ('unchanged', 'e\n'),
917
 
                          ('unchanged', 'f\n'),
918
 
                         ], plan)
919
 
        pwm = versionedfile.PlanWeaveMerge(plan)
920
 
        self.assertEqualDiff('\n'.join('abcdgef') + '\n',
921
 
                             ''.join(pwm.base_from_plan()))
922
 
        # Reversing it changes what line is doubled, but still gives a
923
 
        # double-conflict
924
 
        plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
925
 
        self.assertEqual([
926
 
                          ('unchanged', 'a\n'),
927
 
                          ('unchanged', 'b\n'),
928
 
                          ('unchanged', 'c\n'),
929
 
                          ('unchanged', 'd\n'),
930
 
                          ('conflicted-b', 'g\n'),
931
 
                          ('unchanged', 'h\n'),
932
 
                          ('conflicted-a', 'g\n'),
933
 
                          ('unchanged', 'e\n'),
934
 
                          ('unchanged', 'f\n'),
935
 
                         ], plan)
936
 
        pwm = versionedfile.PlanWeaveMerge(plan)
937
 
        self.assertEqualDiff('\n'.join('abcdhef') + '\n',
938
 
                             ''.join(pwm.base_from_plan()))
939
830
 
940
831
    def assertRemoveExternalReferences(self, filtered_parent_map,
941
832
                                       child_map, tails, parent_map):
1141
1032
                         ], list(plan))
1142
1033
 
1143
1034
 
 
1035
class TestMergeImplementation(object):
 
1036
 
 
1037
    def do_merge(self, target_tree, source_tree, **kwargs):
 
1038
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
1039
            target_tree, source_tree.last_revision(),
 
1040
            other_branch=source_tree.branch)
 
1041
        merger.merge_type=self.merge_type
 
1042
        for name, value in kwargs.items():
 
1043
            setattr(merger, name, value)
 
1044
        merger.do_merge()
 
1045
 
 
1046
    def test_merge_specific_file(self):
 
1047
        this_tree = self.make_branch_and_tree('this')
 
1048
        this_tree.lock_write()
 
1049
        self.addCleanup(this_tree.unlock)
 
1050
        self.build_tree_contents([
 
1051
            ('this/file1', 'a\nb\n'),
 
1052
            ('this/file2', 'a\nb\n')
 
1053
        ])
 
1054
        this_tree.add(['file1', 'file2'])
 
1055
        this_tree.commit('Added files')
 
1056
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
1057
        self.build_tree_contents([
 
1058
            ('other/file1', 'a\nb\nc\n'),
 
1059
            ('other/file2', 'a\nb\nc\n')
 
1060
        ])
 
1061
        other_tree.commit('modified both')
 
1062
        self.build_tree_contents([
 
1063
            ('this/file1', 'd\na\nb\n'),
 
1064
            ('this/file2', 'd\na\nb\n')
 
1065
        ])
 
1066
        this_tree.commit('modified both')
 
1067
        self.do_merge(this_tree, other_tree, interesting_files=['file1'])
 
1068
        self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
 
1069
        self.assertFileEqual('d\na\nb\n', 'this/file2')
 
1070
 
 
1071
    def test_merge_move_and_change(self):
 
1072
        this_tree = self.make_branch_and_tree('this')
 
1073
        this_tree.lock_write()
 
1074
        self.addCleanup(this_tree.unlock)
 
1075
        self.build_tree_contents([
 
1076
            ('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
 
1077
        ])
 
1078
        this_tree.add('file1',)
 
1079
        this_tree.commit('Added file')
 
1080
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
1081
        self.build_tree_contents([
 
1082
            ('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
 
1083
        ])
 
1084
        other_tree.commit('Changed 2 to 2.1')
 
1085
        self.build_tree_contents([
 
1086
            ('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
 
1087
        ])
 
1088
        this_tree.commit('Swapped 2 & 3')
 
1089
        self.do_merge(this_tree, other_tree)
 
1090
        self.assertFileEqual('line 1\n'
 
1091
            '<<<<<<< TREE\n'
 
1092
            'line 3\n'
 
1093
            'line 2\n'
 
1094
            '=======\n'
 
1095
            'line 2 to 2.1\n'
 
1096
            'line 3\n'
 
1097
            '>>>>>>> MERGE-SOURCE\n'
 
1098
            'line 4\n', 'this/file1')
 
1099
 
 
1100
    def test_modify_conflicts_with_delete(self):
 
1101
        # If one side deletes a line, and the other modifies that line, then
 
1102
        # the modification should be considered a conflict
 
1103
        builder = self.make_branch_builder('test')
 
1104
        builder.start_series()
 
1105
        builder.build_snapshot('BASE-id', None,
 
1106
            [('add', ('', None, 'directory', None)),
 
1107
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
1108
            ])
 
1109
        # Delete 'b\n'
 
1110
        builder.build_snapshot('OTHER-id', ['BASE-id'],
 
1111
            [('modify', ('foo-id', 'a\nc\nd\ne\n'))])
 
1112
        # Modify 'b\n', add 'X\n'
 
1113
        builder.build_snapshot('THIS-id', ['BASE-id'],
 
1114
            [('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
 
1115
        builder.finish_series()
 
1116
        branch = builder.get_branch()
 
1117
        this_tree = branch.bzrdir.create_workingtree()
 
1118
        this_tree.lock_write()
 
1119
        self.addCleanup(this_tree.unlock)
 
1120
        other_tree = this_tree.bzrdir.sprout('other', 'OTHER-id').open_workingtree()
 
1121
        self.do_merge(this_tree, other_tree)
 
1122
        if self.merge_type is _mod_merge.LCAMerger:
 
1123
            self.expectFailure("lca merge doesn't track deleted lines",
 
1124
                self.assertFileEqual,
 
1125
                    'a\n'
 
1126
                    '<<<<<<< TREE\n'
 
1127
                    'b2\n'
 
1128
                    '=======\n'
 
1129
                    '>>>>>>> MERGE-SOURCE\n'
 
1130
                    'c\n'
 
1131
                    'd\n'
 
1132
                    'X\n'
 
1133
                    'e\n', 'test/foo')
 
1134
        else:
 
1135
            self.assertFileEqual(
 
1136
                'a\n'
 
1137
                '<<<<<<< TREE\n'
 
1138
                'b2\n'
 
1139
                '=======\n'
 
1140
                '>>>>>>> MERGE-SOURCE\n'
 
1141
                'c\n'
 
1142
                'd\n'
 
1143
                'X\n'
 
1144
                'e\n', 'test/foo')
 
1145
 
 
1146
    def get_limbodir_deletiondir(self, wt):
 
1147
        transform = TreeTransform(wt)
 
1148
        limbodir = transform._limbodir
 
1149
        deletiondir = transform._deletiondir
 
1150
        transform.finalize()
 
1151
        return (limbodir, deletiondir)
 
1152
    
 
1153
    def test_merge_with_existing_limbo(self):
 
1154
        wt = self.make_branch_and_tree('this')
 
1155
        (limbodir, deletiondir) =  self.get_limbodir_deletiondir(wt)
 
1156
        os.mkdir(limbodir)
 
1157
        self.assertRaises(errors.ExistingLimbo, self.do_merge, wt, wt)
 
1158
        self.assertRaises(errors.LockError, wt.unlock)
 
1159
 
 
1160
    def test_merge_with_pending_deletion(self):
 
1161
        wt = self.make_branch_and_tree('this')
 
1162
        (limbodir, deletiondir) =  self.get_limbodir_deletiondir(wt)
 
1163
        os.mkdir(deletiondir)
 
1164
        self.assertRaises(errors.ExistingPendingDeletion, self.do_merge, wt, wt)
 
1165
        self.assertRaises(errors.LockError, wt.unlock)
 
1166
 
 
1167
 
 
1168
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
 
1169
 
 
1170
    merge_type = _mod_merge.Merge3Merger
 
1171
 
 
1172
 
 
1173
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
 
1174
 
 
1175
    merge_type = _mod_merge.WeaveMerger
 
1176
 
 
1177
 
 
1178
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
 
1179
 
 
1180
    merge_type = _mod_merge.LCAMerger
 
1181
 
 
1182
    def test_merge_move_and_change(self):
 
1183
        self.expectFailure("lca merge doesn't conflict for move and change",
 
1184
            super(TestLCAMerge, self).test_merge_move_and_change)
 
1185
 
 
1186
 
1144
1187
class LoggingMerger(object):
1145
1188
    # These seem to be the required attributes
1146
1189
    requires_base = False