~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: Martin Pool
  • Date: 2010-02-11 04:20:19 UTC
  • mto: This revision was merged to the branch mainline in revision 5029.
  • Revision ID: mbp@sourcefrog.net-20100211042019-o7e4sgd2z5b82mw3
Add vim modeline

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from StringIO import StringIO
19
19
 
20
20
from bzrlib import (
21
 
    branch as _mod_branch,
22
 
    cleanup,
23
21
    conflicts,
24
22
    errors,
25
 
    inventory,
26
23
    knit,
27
24
    memorytree,
28
25
    merge as _mod_merge,
29
26
    option,
30
 
    revision as _mod_revision,
 
27
    progress,
31
28
    tests,
32
29
    transform,
33
30
    versionedfile,
35
32
from bzrlib.conflicts import ConflictList, TextConflict
36
33
from bzrlib.errors import UnrelatedBranches, NoCommits
37
34
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
38
 
from bzrlib.osutils import basename, pathjoin, file_kind
 
35
from bzrlib.osutils import pathjoin, file_kind
39
36
from bzrlib.tests import (
40
37
    TestCaseWithMemoryTransport,
41
38
    TestCaseWithTransport,
90
87
        os.chdir('branch2')
91
88
        self.run_bzr('merge ../branch1/baz', retcode=3)
92
89
        self.run_bzr('merge ../branch1/foo')
93
 
        self.assertPathExists('foo')
94
 
        self.assertPathDoesNotExist('bar')
 
90
        self.failUnlessExists('foo')
 
91
        self.failIfExists('bar')
95
92
        wt2 = WorkingTree.open('.') # opens branch2
96
93
        self.assertEqual([tip], wt2.get_parent_ids())
97
94
 
121
118
        finally:
122
119
            wt1.unlock()
123
120
 
124
 
    def test_merge_into_null_tree(self):
125
 
        wt = self.make_branch_and_tree('tree')
126
 
        null_tree = wt.basis_tree()
127
 
        self.build_tree(['tree/file'])
128
 
        wt.add('file')
129
 
        wt.commit('tree with root')
130
 
        merger = _mod_merge.Merge3Merger(null_tree, null_tree, null_tree, wt,
131
 
                                         this_branch=wt.branch,
132
 
                                         do_merge=False)
133
 
        with merger.make_preview_transform() as tt:
134
 
            self.assertEqual([], tt.find_conflicts())
135
 
            preview = tt.get_preview_tree()
136
 
            self.assertEqual(wt.get_root_id(), preview.get_root_id())
137
 
 
138
 
    def test_merge_unrelated_retains_root(self):
139
 
        wt = self.make_branch_and_tree('tree')
140
 
        root_id_before_merge = wt.get_root_id()
141
 
        other_tree = self.make_branch_and_tree('other')
142
 
        # Do a commit so there is something to merge
143
 
        other_tree.commit('commit other')
144
 
        self.assertNotEquals(root_id_before_merge, other_tree.get_root_id())
145
 
        wt.merge_from_branch(other_tree.branch,
146
 
                             from_revision=_mod_revision.NULL_REVISION)
147
 
        self.assertEqual(root_id_before_merge, wt.get_root_id())
148
 
 
149
 
    def test_merge_preview_unrelated_retains_root(self):
150
 
        wt = self.make_branch_and_tree('tree')
151
 
        other_tree = self.make_branch_and_tree('other')
152
 
        # Do a commit so there is something to merge
153
 
        other_tree.commit('commit other')
154
 
        merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
155
 
                                         this_branch=wt.branch,
156
 
                                         do_merge=False)
157
 
        with merger.make_preview_transform() as tt:
158
 
            preview = tt.get_preview_tree()
159
 
            self.assertEqual(wt.get_root_id(), preview.get_root_id())
160
 
 
161
121
    def test_create_rename(self):
162
122
        """Rename an inventory entry while creating the file"""
163
123
        tree =self.make_branch_and_tree('.')
194
154
        log = StringIO()
195
155
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
196
156
                    this_tree=tree_b, ignore_zero=True)
197
 
        self.assertTrue('All changes applied successfully.\n' not in
 
157
        self.failUnless('All changes applied successfully.\n' not in
198
158
            self.get_log())
199
159
        tree_b.revert()
200
160
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
201
161
                    this_tree=tree_b, ignore_zero=False)
202
 
        self.assertTrue('All changes applied successfully.\n' in self.get_log())
 
162
        self.failUnless('All changes applied successfully.\n' in self.get_log())
203
163
 
204
164
    def test_merge_inner_conflicts(self):
205
165
        tree_a = self.make_branch_and_tree('a')
424
384
                             '>>>>>>> MERGE-SOURCE\n',
425
385
                             'this/file')
426
386
 
427
 
    def test_merge_reverse_revision_range(self):
428
 
        tree = self.make_branch_and_tree(".")
429
 
        tree.lock_write()
430
 
        self.addCleanup(tree.unlock)
431
 
        self.build_tree(['a'])
432
 
        tree.add('a')
433
 
        tree.commit("added a")
434
 
        first_rev = tree.branch.revision_history()[0]
435
 
        merger = _mod_merge.Merger.from_revision_ids(None, tree,
436
 
                                          _mod_revision.NULL_REVISION,
437
 
                                          first_rev)
438
 
        merger.merge_type = _mod_merge.Merge3Merger
439
 
        merger.interesting_files = 'a'
440
 
        conflict_count = merger.do_merge()
441
 
        self.assertEqual(0, conflict_count)
442
 
 
443
 
        self.assertPathDoesNotExist("a")
444
 
        tree.revert()
445
 
        self.assertPathExists("a")
446
 
 
447
387
    def test_make_merger(self):
448
388
        this_tree = self.make_branch_and_tree('this')
449
389
        this_tree.commit('rev1', rev_id='rev1')
513
453
        finally:
514
454
            tree_file.close()
515
455
 
516
 
    def test_merge_require_tree_root(self):
517
 
        tree = self.make_branch_and_tree(".")
518
 
        tree.lock_write()
519
 
        self.addCleanup(tree.unlock)
520
 
        self.build_tree(['a'])
521
 
        tree.add('a')
522
 
        tree.commit("added a")
523
 
        old_root_id = tree.get_root_id()
524
 
        first_rev = tree.branch.revision_history()[0]
525
 
        merger = _mod_merge.Merger.from_revision_ids(None, tree,
526
 
                                          _mod_revision.NULL_REVISION,
527
 
                                          first_rev)
528
 
        merger.merge_type = _mod_merge.Merge3Merger
529
 
        conflict_count = merger.do_merge()
530
 
        self.assertEqual(0, conflict_count)
531
 
        self.assertEquals(set([old_root_id]), tree.all_file_ids())
532
 
        tree.set_parent_ids([])
533
 
 
534
456
    def test_merge_add_into_deleted_root(self):
535
457
        # Yes, people actually do this.  And report bugs if it breaks.
536
458
        source = self.make_branch_and_tree('source', format='rich-root-pack')
1345
1267
        self.assertEqual(['B-id', 'C-id', 'F-id'],
1346
1268
                         [t.get_revision_id() for t in merger._lca_trees])
1347
1269
 
1348
 
    def test_find_base_new_root_criss_cross(self):
1349
 
        # A   B
1350
 
        # |\ /|
1351
 
        # | X |
1352
 
        # |/ \|
1353
 
        # C   D
1354
 
        
1355
 
        builder = self.get_builder()
1356
 
        builder.build_snapshot('A-id', None,
1357
 
            [('add', ('', None, 'directory', None))])
1358
 
        builder.build_snapshot('B-id', [],
1359
 
            [('add', ('', None, 'directory', None))])
1360
 
        builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1361
 
        builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1362
 
        merger = self.make_Merger(builder, 'D-id')
1363
 
        self.assertEqual('A-id', merger.base_rev_id)
1364
 
        self.assertTrue(merger._is_criss_cross)
1365
 
        self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1366
 
                                            for t in merger._lca_trees])
1367
 
 
1368
1270
    def test_no_criss_cross_passed_to_merge_type(self):
1369
1271
        class LCATreesMerger(LoggingMerger):
1370
1272
            supports_lca_trees = True
1902
1804
        builder.build_snapshot('C-id', ['A-id'], [])
1903
1805
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1904
1806
            [('unversion', 'a-id'),
1905
 
             ('flush', None),
1906
1807
             ('add', (u'a', 'a-id', 'directory', None))])
1907
1808
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1908
1809
        merge_obj = self.make_merge_obj(builder, 'E-id')
1926
1827
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1927
1828
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1928
1829
            [('unversion', 'a-id'),
1929
 
             ('flush', None),
1930
1830
             ('add', (u'a', 'a-id', 'directory', None))])
1931
1831
        merge_obj = self.make_merge_obj(builder, 'E-id')
1932
1832
        entries = list(merge_obj._entries_lca())
2940
2840
 
2941
2841
    def get_merger_factory(self):
2942
2842
        # Allows  the inner methods to access the test attributes
2943
 
        calls = self.calls
 
2843
        test = self
2944
2844
 
2945
2845
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2946
2846
            name_prefix = "foo"
2947
2847
            default_files = ['bar']
2948
2848
 
2949
2849
            def merge_text(self, params):
2950
 
                calls.append('merge_text')
 
2850
                test.calls.append('merge_text')
2951
2851
                return ('not_applicable', None)
2952
2852
 
2953
2853
        def factory(merger):
2987
2887
                        base=False, other=False)
2988
2888
        return builder
2989
2889
 
2990
 
    def test_uses_this_branch(self):
2991
 
        builder = self.make_text_conflict()
2992
 
        tt = builder.make_preview_transform()
2993
 
        self.addCleanup(tt.finalize)
2994
 
 
2995
2890
    def test_affected_files_cached(self):
2996
2891
        """Ensures that the config variable is cached"""
2997
2892
        builder = self.make_text_conflict()
3017
2912
        conflicts = builder.merge()
3018
2913
        # The hook should not call the merge_text() method
3019
2914
        self.assertEqual([], self.calls)
3020
 
 
3021
 
 
3022
 
class TestMergeIntoBase(tests.TestCaseWithTransport):
3023
 
 
3024
 
    def setup_simple_branch(self, relpath, shape=None, root_id=None):
3025
 
        """One commit, containing tree specified by optional shape.
3026
 
        
3027
 
        Default is empty tree (just root entry).
3028
 
        """
3029
 
        if root_id is None:
3030
 
            root_id = '%s-root-id' % (relpath,)
3031
 
        wt = self.make_branch_and_tree(relpath)
3032
 
        wt.set_root_id(root_id)
3033
 
        if shape is not None:
3034
 
            adjusted_shape = [relpath + '/' + elem for elem in shape]
3035
 
            self.build_tree(adjusted_shape)
3036
 
            ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3037
 
                   for elem in shape]
3038
 
            wt.add(shape, ids=ids)
3039
 
        rev_id = 'r1-%s' % (relpath,)
3040
 
        wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3041
 
        self.assertEqual(root_id, wt.path2id(''))
3042
 
        return wt
3043
 
 
3044
 
    def setup_two_branches(self, custom_root_ids=True):
3045
 
        """Setup 2 branches, one will be a library, the other a project."""
3046
 
        if custom_root_ids:
3047
 
            root_id = None
3048
 
        else:
3049
 
            root_id = inventory.ROOT_ID
3050
 
        project_wt = self.setup_simple_branch(
3051
 
            'project', ['README', 'dir/', 'dir/file.c'],
3052
 
            root_id)
3053
 
        lib_wt = self.setup_simple_branch(
3054
 
            'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3055
 
 
3056
 
        return project_wt, lib_wt
3057
 
 
3058
 
    def do_merge_into(self, location, merge_as):
3059
 
        """Helper for using MergeIntoMerger.
3060
 
        
3061
 
        :param location: location of directory to merge from, either the
3062
 
            location of a branch or of a path inside a branch.
3063
 
        :param merge_as: the path in a tree to add the new directory as.
3064
 
        :returns: the conflicts from 'do_merge'.
3065
 
        """
3066
 
        operation = cleanup.OperationWithCleanups(self._merge_into)
3067
 
        return operation.run(location, merge_as)
3068
 
 
3069
 
    def _merge_into(self, op, location, merge_as):
3070
 
        # Open and lock the various tree and branch objects
3071
 
        wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3072
 
        op.add_cleanup(wt.lock_write().unlock)
3073
 
        branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3074
 
            location)
3075
 
        op.add_cleanup(branch_to_merge.lock_read().unlock)
3076
 
        other_tree = branch_to_merge.basis_tree()
3077
 
        op.add_cleanup(other_tree.lock_read().unlock)
3078
 
        # Perform the merge
3079
 
        merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3080
 
            other_branch=branch_to_merge, target_subdir=subdir_relpath,
3081
 
            source_subpath=subdir_to_merge)
3082
 
        merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3083
 
        conflicts = merger.do_merge()
3084
 
        merger.set_pending()
3085
 
        return conflicts
3086
 
 
3087
 
    def assertTreeEntriesEqual(self, expected_entries, tree):
3088
 
        """Assert that 'tree' contains the expected inventory entries.
3089
 
 
3090
 
        :param expected_entries: sequence of (path, file-id) pairs.
3091
 
        """
3092
 
        files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3093
 
        self.assertEqual(expected_entries, files)
3094
 
 
3095
 
 
3096
 
class TestMergeInto(TestMergeIntoBase):
3097
 
 
3098
 
    def test_newdir_with_unique_roots(self):
3099
 
        """Merge a branch with a unique root into a new directory."""
3100
 
        project_wt, lib_wt = self.setup_two_branches()
3101
 
        self.do_merge_into('lib1', 'project/lib1')
3102
 
        project_wt.lock_read()
3103
 
        self.addCleanup(project_wt.unlock)
3104
 
        # The r1-lib1 revision should be merged into this one
3105
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3106
 
        self.assertTreeEntriesEqual(
3107
 
            [('', 'project-root-id'),
3108
 
             ('README', 'project-README-id'),
3109
 
             ('dir', 'project-dir-id'),
3110
 
             ('lib1', 'lib1-root-id'),
3111
 
             ('dir/file.c', 'project-file.c-id'),
3112
 
             ('lib1/Makefile', 'lib1-Makefile-id'),
3113
 
             ('lib1/README', 'lib1-README-id'),
3114
 
             ('lib1/foo.c', 'lib1-foo.c-id'),
3115
 
            ], project_wt)
3116
 
 
3117
 
    def test_subdir(self):
3118
 
        """Merge a branch into a subdirectory of an existing directory."""
3119
 
        project_wt, lib_wt = self.setup_two_branches()
3120
 
        self.do_merge_into('lib1', 'project/dir/lib1')
3121
 
        project_wt.lock_read()
3122
 
        self.addCleanup(project_wt.unlock)
3123
 
        # The r1-lib1 revision should be merged into this one
3124
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3125
 
        self.assertTreeEntriesEqual(
3126
 
            [('', 'project-root-id'),
3127
 
             ('README', 'project-README-id'),
3128
 
             ('dir', 'project-dir-id'),
3129
 
             ('dir/file.c', 'project-file.c-id'),
3130
 
             ('dir/lib1', 'lib1-root-id'),
3131
 
             ('dir/lib1/Makefile', 'lib1-Makefile-id'),
3132
 
             ('dir/lib1/README', 'lib1-README-id'),
3133
 
             ('dir/lib1/foo.c', 'lib1-foo.c-id'),
3134
 
            ], project_wt)
3135
 
 
3136
 
    def test_newdir_with_repeat_roots(self):
3137
 
        """If the file-id of the dir to be merged already exists a new ID will
3138
 
        be allocated to let the merge happen.
3139
 
        """
3140
 
        project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3141
 
        root_id = project_wt.path2id('')
3142
 
        self.do_merge_into('lib1', 'project/lib1')
3143
 
        project_wt.lock_read()
3144
 
        self.addCleanup(project_wt.unlock)
3145
 
        # The r1-lib1 revision should be merged into this one
3146
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3147
 
        new_lib1_id = project_wt.path2id('lib1')
3148
 
        self.assertNotEqual(None, new_lib1_id)
3149
 
        self.assertTreeEntriesEqual(
3150
 
            [('', root_id),
3151
 
             ('README', 'project-README-id'),
3152
 
             ('dir', 'project-dir-id'),
3153
 
             ('lib1', new_lib1_id),
3154
 
             ('dir/file.c', 'project-file.c-id'),
3155
 
             ('lib1/Makefile', 'lib1-Makefile-id'),
3156
 
             ('lib1/README', 'lib1-README-id'),
3157
 
             ('lib1/foo.c', 'lib1-foo.c-id'),
3158
 
            ], project_wt)
3159
 
 
3160
 
    def test_name_conflict(self):
3161
 
        """When the target directory name already exists a conflict is
3162
 
        generated and the original directory is renamed to foo.moved.
3163
 
        """
3164
 
        dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3165
 
        src_wt = self.setup_simple_branch('src', ['README'])
3166
 
        conflicts = self.do_merge_into('src', 'dest/dir')
3167
 
        self.assertEqual(1, conflicts)
3168
 
        dest_wt.lock_read()
3169
 
        self.addCleanup(dest_wt.unlock)
3170
 
        # The r1-lib1 revision should be merged into this one
3171
 
        self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3172
 
        self.assertTreeEntriesEqual(
3173
 
            [('', 'dest-root-id'),
3174
 
             ('dir', 'src-root-id'),
3175
 
             ('dir.moved', 'dest-dir-id'),
3176
 
             ('dir/README', 'src-README-id'),
3177
 
             ('dir.moved/file.txt', 'dest-file.txt-id'),
3178
 
            ], dest_wt)
3179
 
 
3180
 
    def test_file_id_conflict(self):
3181
 
        """A conflict is generated if the merge-into adds a file (or other
3182
 
        inventory entry) with a file-id that already exists in the target tree.
3183
 
        """
3184
 
        dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3185
 
        # Make a second tree with a file-id that will clash with file.txt in
3186
 
        # dest.
3187
 
        src_wt = self.make_branch_and_tree('src')
3188
 
        self.build_tree(['src/README'])
3189
 
        src_wt.add(['README'], ids=['dest-file.txt-id'])
3190
 
        src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3191
 
        conflicts = self.do_merge_into('src', 'dest/dir')
3192
 
        # This is an edge case that shouldn't happen to users very often.  So
3193
 
        # we don't care really about the exact presentation of the conflict,
3194
 
        # just that there is one.
3195
 
        self.assertEqual(1, conflicts)
3196
 
 
3197
 
    def test_only_subdir(self):
3198
 
        """When the location points to just part of a tree, merge just that
3199
 
        subtree.
3200
 
        """
3201
 
        dest_wt = self.setup_simple_branch('dest')
3202
 
        src_wt = self.setup_simple_branch(
3203
 
            'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3204
 
        conflicts = self.do_merge_into('src/dir', 'dest/dir')
3205
 
        dest_wt.lock_read()
3206
 
        self.addCleanup(dest_wt.unlock)
3207
 
        # The r1-lib1 revision should NOT be merged into this one (this is a
3208
 
        # partial merge).
3209
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3210
 
        self.assertTreeEntriesEqual(
3211
 
            [('', 'dest-root-id'),
3212
 
             ('dir', 'src-dir-id'),
3213
 
             ('dir/foo.c', 'src-foo.c-id'),
3214
 
            ], dest_wt)
3215
 
 
3216
 
    def test_only_file(self):
3217
 
        """An edge case: merge just one file, not a whole dir."""
3218
 
        dest_wt = self.setup_simple_branch('dest')
3219
 
        two_file_wt = self.setup_simple_branch(
3220
 
            'two-file', ['file1.txt', 'file2.txt'])
3221
 
        conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3222
 
        dest_wt.lock_read()
3223
 
        self.addCleanup(dest_wt.unlock)
3224
 
        # The r1-lib1 revision should NOT be merged into this one
3225
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3226
 
        self.assertTreeEntriesEqual(
3227
 
            [('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3228
 
            dest_wt)
3229
 
 
3230
 
    def test_no_such_source_path(self):
3231
 
        """PathNotInTree is raised if the specified path in the source tree
3232
 
        does not exist.
3233
 
        """
3234
 
        dest_wt = self.setup_simple_branch('dest')
3235
 
        two_file_wt = self.setup_simple_branch('src', ['dir/'])
3236
 
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3237
 
            'src/no-such-dir', 'dest/foo')
3238
 
        dest_wt.lock_read()
3239
 
        self.addCleanup(dest_wt.unlock)
3240
 
        # The dest tree is unmodified.
3241
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3242
 
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3243
 
 
3244
 
    def test_no_such_target_path(self):
3245
 
        """PathNotInTree is also raised if the specified path in the target
3246
 
        tree does not exist.
3247
 
        """
3248
 
        dest_wt = self.setup_simple_branch('dest')
3249
 
        two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3250
 
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3251
 
            'src', 'dest/no-such-dir/foo')
3252
 
        dest_wt.lock_read()
3253
 
        self.addCleanup(dest_wt.unlock)
3254
 
        # The dest tree is unmodified.
3255
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3256
 
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)