/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: John Arbash Meinel
  • Date: 2011-08-18 13:27:18 UTC
  • mto: (6015.9.14 2.4)
  • mto: This revision was merged to the branch mainline in revision 6090.
  • Revision ID: john@arbash-meinel.com-20110818132718-b4rrujubsaa3evuz
Add a __repr__ to _LazyListJoin to make it easier to debug.

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,
21
23
    conflicts,
22
24
    errors,
 
25
    inventory,
23
26
    knit,
24
27
    memorytree,
25
28
    merge as _mod_merge,
26
29
    option,
27
 
    progress,
 
30
    revision as _mod_revision,
28
31
    tests,
29
32
    transform,
30
33
    versionedfile,
32
35
from bzrlib.conflicts import ConflictList, TextConflict
33
36
from bzrlib.errors import UnrelatedBranches, NoCommits
34
37
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
35
 
from bzrlib.osutils import pathjoin, file_kind
 
38
from bzrlib.osutils import basename, pathjoin, file_kind
36
39
from bzrlib.tests import (
37
40
    TestCaseWithMemoryTransport,
38
41
    TestCaseWithTransport,
87
90
        os.chdir('branch2')
88
91
        self.run_bzr('merge ../branch1/baz', retcode=3)
89
92
        self.run_bzr('merge ../branch1/foo')
90
 
        self.failUnlessExists('foo')
91
 
        self.failIfExists('bar')
 
93
        self.assertPathExists('foo')
 
94
        self.assertPathDoesNotExist('bar')
92
95
        wt2 = WorkingTree.open('.') # opens branch2
93
96
        self.assertEqual([tip], wt2.get_parent_ids())
94
97
 
118
121
        finally:
119
122
            wt1.unlock()
120
123
 
 
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
 
121
161
    def test_create_rename(self):
122
162
        """Rename an inventory entry while creating the file"""
123
163
        tree =self.make_branch_and_tree('.')
154
194
        log = StringIO()
155
195
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
156
196
                    this_tree=tree_b, ignore_zero=True)
157
 
        self.failUnless('All changes applied successfully.\n' not in
 
197
        self.assertTrue('All changes applied successfully.\n' not in
158
198
            self.get_log())
159
199
        tree_b.revert()
160
200
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
161
201
                    this_tree=tree_b, ignore_zero=False)
162
 
        self.failUnless('All changes applied successfully.\n' in self.get_log())
 
202
        self.assertTrue('All changes applied successfully.\n' in self.get_log())
163
203
 
164
204
    def test_merge_inner_conflicts(self):
165
205
        tree_a = self.make_branch_and_tree('a')
384
424
                             '>>>>>>> MERGE-SOURCE\n',
385
425
                             'this/file')
386
426
 
 
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
 
387
447
    def test_make_merger(self):
388
448
        this_tree = self.make_branch_and_tree('this')
389
449
        this_tree.commit('rev1', rev_id='rev1')
453
513
        finally:
454
514
            tree_file.close()
455
515
 
 
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
 
456
534
    def test_merge_add_into_deleted_root(self):
457
535
        # Yes, people actually do this.  And report bugs if it breaks.
458
536
        source = self.make_branch_and_tree('source', format='rich-root-pack')
1267
1345
        self.assertEqual(['B-id', 'C-id', 'F-id'],
1268
1346
                         [t.get_revision_id() for t in merger._lca_trees])
1269
1347
 
 
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
 
1270
1368
    def test_no_criss_cross_passed_to_merge_type(self):
1271
1369
        class LCATreesMerger(LoggingMerger):
1272
1370
            supports_lca_trees = True
1804
1902
        builder.build_snapshot('C-id', ['A-id'], [])
1805
1903
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1806
1904
            [('unversion', 'a-id'),
 
1905
             ('flush', None),
1807
1906
             ('add', (u'a', 'a-id', 'directory', None))])
1808
1907
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1809
1908
        merge_obj = self.make_merge_obj(builder, 'E-id')
1827
1926
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1828
1927
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1829
1928
            [('unversion', 'a-id'),
 
1929
             ('flush', None),
1830
1930
             ('add', (u'a', 'a-id', 'directory', None))])
1831
1931
        merge_obj = self.make_merge_obj(builder, 'E-id')
1832
1932
        entries = list(merge_obj._entries_lca())
2840
2940
 
2841
2941
    def get_merger_factory(self):
2842
2942
        # Allows  the inner methods to access the test attributes
2843
 
        test = self
 
2943
        calls = self.calls
2844
2944
 
2845
2945
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2846
2946
            name_prefix = "foo"
2847
2947
            default_files = ['bar']
2848
2948
 
2849
2949
            def merge_text(self, params):
2850
 
                test.calls.append('merge_text')
 
2950
                calls.append('merge_text')
2851
2951
                return ('not_applicable', None)
2852
2952
 
2853
2953
        def factory(merger):
2917
3017
        conflicts = builder.merge()
2918
3018
        # The hook should not call the merge_text() method
2919
3019
        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)