~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: John Arbash Meinel
  • Date: 2009-11-13 19:19:11 UTC
  • mto: This revision was merged to the branch mainline in revision 4799.
  • Revision ID: john@arbash-meinel.com-20091113191911-nokhxxx8mllrrl6x
Intern the various strings that are part of the CHKInventory deserialization.

file_ids and revision_ids, in particular, are quite often repeated, so try to keep
a single copy of them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
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
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
 
from bzrlib.tests import (
37
 
    TestCaseWithMemoryTransport,
38
 
    TestCaseWithTransport,
39
 
    test_merge_core,
40
 
    )
 
37
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
41
38
from bzrlib.workingtree import WorkingTree
42
 
 
 
39
from bzrlib.transform import TreeTransform
43
40
 
44
41
class TestMerge(TestCaseWithTransport):
45
42
    """Test appending more than one revision"""
154
151
        log = StringIO()
155
152
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
156
153
                    this_tree=tree_b, ignore_zero=True)
157
 
        self.failUnless('All changes applied successfully.\n' not in
158
 
            self.get_log())
 
154
        log = self._get_log(keep_log_file=True)
 
155
        self.failUnless('All changes applied successfully.\n' not in log)
159
156
        tree_b.revert()
160
157
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
161
158
                    this_tree=tree_b, ignore_zero=False)
162
 
        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)
163
161
 
164
162
    def test_merge_inner_conflicts(self):
165
163
        tree_a = self.make_branch_and_tree('a')
526
524
        self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
527
525
        return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
528
526
 
529
 
    def test_base_from_plan(self):
530
 
        self.setup_plan_merge()
531
 
        plan = self.plan_merge_vf.plan_merge('B', 'C')
532
 
        pwm = versionedfile.PlanWeaveMerge(plan)
533
 
        self.assertEqual(['a\n', 'b\n', 'c\n'], pwm.base_from_plan())
534
 
 
535
527
    def test_unique_lines(self):
536
528
        plan = self.setup_plan_merge()
537
529
        self.assertEqual(plan._unique_lines(
835
827
                          ('unchanged', 'f\n'),
836
828
                          ('unchanged', 'g\n')],
837
829
                         list(plan))
838
 
        plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
839
 
        # This is one of the main differences between plan_merge and
840
 
        # plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
841
 
        # because 'x' was not present in one of the bases. However, in this
842
 
        # case it is spurious because 'x' does not exist in the global base A.
843
 
        self.assertEqual([
844
 
                          ('unchanged', 'h\n'),
845
 
                          ('unchanged', 'a\n'),
846
 
                          ('conflicted-a', 'x\n'),
847
 
                          ('new-b', 'z\n'),
848
 
                          ('unchanged', 'c\n'),
849
 
                          ('unchanged', 'd\n'),
850
 
                          ('unchanged', 'y\n'),
851
 
                          ('unchanged', 'f\n'),
852
 
                          ('unchanged', 'g\n')],
853
 
                         list(plan))
854
 
 
855
 
    def test_criss_cross_flip_flop(self):
856
 
        # This is specificly trying to trigger problems when using limited
857
 
        # ancestry and weaves. The ancestry graph looks like:
858
 
        #       XX      unused ancestor, should not show up in the weave
859
 
        #       |
860
 
        #       A       Unique LCA
861
 
        #      / \  
862
 
        #     B   C     B & C both introduce a new line
863
 
        #     |\ /|  
864
 
        #     | X |  
865
 
        #     |/ \| 
866
 
        #     D   E     B & C are both merged, so both are common ancestors
867
 
        #               In the process of merging, both sides order the new
868
 
        #               lines differently
869
 
        #
870
 
        self.add_rev('root', 'XX', [], 'qrs')
871
 
        self.add_rev('root', 'A', ['XX'], 'abcdef')
872
 
        self.add_rev('root', 'B', ['A'], 'abcdgef')
873
 
        self.add_rev('root', 'C', ['A'], 'abcdhef')
874
 
        self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
875
 
        self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
876
 
        plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
877
 
        self.assertEqual([
878
 
                          ('unchanged', 'a\n'),
879
 
                          ('unchanged', 'b\n'),
880
 
                          ('unchanged', 'c\n'),
881
 
                          ('unchanged', 'd\n'),
882
 
                          ('new-b', 'h\n'),
883
 
                          ('unchanged', 'g\n'),
884
 
                          ('killed-b', 'h\n'),
885
 
                          ('unchanged', 'e\n'),
886
 
                          ('unchanged', 'f\n'),
887
 
                         ], plan)
888
 
        pwm = versionedfile.PlanWeaveMerge(plan)
889
 
        self.assertEqualDiff('\n'.join('abcdghef') + '\n',
890
 
                             ''.join(pwm.base_from_plan()))
891
 
        # Reversing the order reverses the merge plan, and final order of 'hg'
892
 
        # => 'gh'
893
 
        plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
894
 
        self.assertEqual([
895
 
                          ('unchanged', 'a\n'),
896
 
                          ('unchanged', 'b\n'),
897
 
                          ('unchanged', 'c\n'),
898
 
                          ('unchanged', 'd\n'),
899
 
                          ('new-b', 'g\n'),
900
 
                          ('unchanged', 'h\n'),
901
 
                          ('killed-b', 'g\n'),
902
 
                          ('unchanged', 'e\n'),
903
 
                          ('unchanged', 'f\n'),
904
 
                         ], plan)
905
 
        pwm = versionedfile.PlanWeaveMerge(plan)
906
 
        self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
907
 
                             ''.join(pwm.base_from_plan()))
908
 
        # This is where lca differs, in that it (fairly correctly) determines
909
 
        # that there is a conflict because both sides resolved the merge
910
 
        # differently
911
 
        plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
912
 
        self.assertEqual([
913
 
                          ('unchanged', 'a\n'),
914
 
                          ('unchanged', 'b\n'),
915
 
                          ('unchanged', 'c\n'),
916
 
                          ('unchanged', 'd\n'),
917
 
                          ('conflicted-b', 'h\n'),
918
 
                          ('unchanged', 'g\n'),
919
 
                          ('conflicted-a', 'h\n'),
920
 
                          ('unchanged', 'e\n'),
921
 
                          ('unchanged', 'f\n'),
922
 
                         ], plan)
923
 
        pwm = versionedfile.PlanWeaveMerge(plan)
924
 
        self.assertEqualDiff('\n'.join('abcdgef') + '\n',
925
 
                             ''.join(pwm.base_from_plan()))
926
 
        # Reversing it changes what line is doubled, but still gives a
927
 
        # double-conflict
928
 
        plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
929
 
        self.assertEqual([
930
 
                          ('unchanged', 'a\n'),
931
 
                          ('unchanged', 'b\n'),
932
 
                          ('unchanged', 'c\n'),
933
 
                          ('unchanged', 'd\n'),
934
 
                          ('conflicted-b', 'g\n'),
935
 
                          ('unchanged', 'h\n'),
936
 
                          ('conflicted-a', 'g\n'),
937
 
                          ('unchanged', 'e\n'),
938
 
                          ('unchanged', 'f\n'),
939
 
                         ], plan)
940
 
        pwm = versionedfile.PlanWeaveMerge(plan)
941
 
        self.assertEqualDiff('\n'.join('abcdhef') + '\n',
942
 
                             ''.join(pwm.base_from_plan()))
943
830
 
944
831
    def assertRemoveExternalReferences(self, filtered_parent_map,
945
832
                                       child_map, tails, parent_map):
1145
1032
                         ], list(plan))
1146
1033
 
1147
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
 
1148
1187
class LoggingMerger(object):
1149
1188
    # These seem to be the required attributes
1150
1189
    requires_base = False
2837
2876
            'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2838
2877
        self.assertLCAMultiWay('conflict',
2839
2878
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2840
 
 
2841
 
 
2842
 
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2843
 
 
2844
 
    def test_affected_files_cached(self):
2845
 
        """Ensures that the config variable is cached"""
2846
 
        class SimplePlan(_mod_merge.ConfigurableFileMerger):
2847
 
            name_prefix = "foo"
2848
 
            default_files = ["my default"]
2849
 
            def merge_text(self, params):
2850
 
                return ('not applicable', None)
2851
 
        def factory(merger):
2852
 
            result = SimplePlan(merger)
2853
 
            self.assertEqual(None, result.affected_files)
2854
 
            self.merger = result
2855
 
            return result
2856
 
        _mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2857
 
            factory, 'test factory')
2858
 
        builder = test_merge_core.MergeBuilder(self.test_base_dir)
2859
 
        self.addCleanup(builder.cleanup)
2860
 
        builder.add_file('NEWS', builder.tree_root, 'name1', 'text1', True)
2861
 
        builder.change_contents('NEWS', other='text4', this='text3')
2862
 
        conflicts = builder.merge()
2863
 
        # The hook should set the variable
2864
 
        self.assertEqual(["my default"], self.merger.affected_files)