~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: 2010-02-04 16:06:36 UTC
  • mfrom: (5007 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5023.
  • Revision ID: john@arbash-meinel.com-20100204160636-xqeuwz8bwt8bbts4
Merge bzr.dev 5007, resolve conflict, update NEWS

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
 
    features,
41
37
    TestCaseWithMemoryTransport,
42
38
    TestCaseWithTransport,
43
39
    test_merge_core,
91
87
        os.chdir('branch2')
92
88
        self.run_bzr('merge ../branch1/baz', retcode=3)
93
89
        self.run_bzr('merge ../branch1/foo')
94
 
        self.assertPathExists('foo')
95
 
        self.assertPathDoesNotExist('bar')
 
90
        self.failUnlessExists('foo')
 
91
        self.failIfExists('bar')
96
92
        wt2 = WorkingTree.open('.') # opens branch2
97
93
        self.assertEqual([tip], wt2.get_parent_ids())
98
94
 
122
118
        finally:
123
119
            wt1.unlock()
124
120
 
125
 
    def test_merge_into_null_tree(self):
126
 
        wt = self.make_branch_and_tree('tree')
127
 
        null_tree = wt.basis_tree()
128
 
        self.build_tree(['tree/file'])
129
 
        wt.add('file')
130
 
        wt.commit('tree with root')
131
 
        merger = _mod_merge.Merge3Merger(null_tree, null_tree, null_tree, wt,
132
 
                                         this_branch=wt.branch,
133
 
                                         do_merge=False)
134
 
        with merger.make_preview_transform() as tt:
135
 
            self.assertEqual([], tt.find_conflicts())
136
 
            preview = tt.get_preview_tree()
137
 
            self.assertEqual(wt.get_root_id(), preview.get_root_id())
138
 
 
139
 
    def test_merge_unrelated_retains_root(self):
140
 
        wt = self.make_branch_and_tree('tree')
141
 
        other_tree = self.make_branch_and_tree('other')
142
 
        self.addCleanup(other_tree.lock_read().unlock)
143
 
        merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
144
 
                                         this_branch=wt.branch,
145
 
                                         do_merge=False)
146
 
        with transform.TransformPreview(wt) as merger.tt:
147
 
            merger._compute_transform()
148
 
            new_root_id = merger.tt.final_file_id(merger.tt.root)
149
 
            self.assertEqual(wt.get_root_id(), new_root_id)
150
 
 
151
121
    def test_create_rename(self):
152
122
        """Rename an inventory entry while creating the file"""
153
123
        tree =self.make_branch_and_tree('.')
184
154
        log = StringIO()
185
155
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
186
156
                    this_tree=tree_b, ignore_zero=True)
187
 
        self.assertTrue('All changes applied successfully.\n' not in
 
157
        self.failUnless('All changes applied successfully.\n' not in
188
158
            self.get_log())
189
159
        tree_b.revert()
190
160
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
191
161
                    this_tree=tree_b, ignore_zero=False)
192
 
        self.assertTrue('All changes applied successfully.\n' in self.get_log())
 
162
        self.failUnless('All changes applied successfully.\n' in self.get_log())
193
163
 
194
164
    def test_merge_inner_conflicts(self):
195
165
        tree_a = self.make_branch_and_tree('a')
325
295
        tree_a.commit('commit 2')
326
296
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
327
297
        tree_b.rename_one('file_1', 'renamed')
328
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
 
298
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
 
299
                                                    progress.DummyProgress())
329
300
        merger.merge_type = _mod_merge.Merge3Merger
330
301
        merger.do_merge()
331
302
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
339
310
        tree_a.commit('commit 2')
340
311
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
341
312
        tree_b.rename_one('file_1', 'renamed')
342
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
 
313
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
 
314
                                                    progress.DummyProgress())
343
315
        merger.merge_type = _mod_merge.WeaveMerger
344
316
        merger.do_merge()
345
317
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
346
318
 
 
319
    def test_Merger_defaults_to_DummyProgress(self):
 
320
        branch = self.make_branch('branch')
 
321
        merger = _mod_merge.Merger(branch, pb=None)
 
322
        self.assertIsInstance(merger._pb, progress.DummyProgress)
 
323
 
347
324
    def prepare_cherrypick(self):
348
325
        """Prepare a pair of trees for cherrypicking tests.
349
326
 
370
347
 
371
348
    def test_weave_cherrypick(self):
372
349
        this_tree, other_tree = self.prepare_cherrypick()
373
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
350
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
374
351
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
375
352
        merger.merge_type = _mod_merge.WeaveMerger
376
353
        merger.do_merge()
378
355
 
379
356
    def test_weave_cannot_reverse_cherrypick(self):
380
357
        this_tree, other_tree = self.prepare_cherrypick()
381
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
358
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
382
359
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
383
360
        merger.merge_type = _mod_merge.WeaveMerger
384
361
        self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
385
362
 
386
363
    def test_merge3_can_reverse_cherrypick(self):
387
364
        this_tree, other_tree = self.prepare_cherrypick()
388
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
365
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
389
366
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
390
367
        merger.merge_type = _mod_merge.Merge3Merger
391
368
        merger.do_merge()
403
380
        this_tree.lock_write()
404
381
        self.addCleanup(this_tree.unlock)
405
382
 
406
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
383
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
407
384
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
408
385
        merger.merge_type = _mod_merge.Merge3Merger
409
386
        merger.do_merge()
414
391
                             '>>>>>>> MERGE-SOURCE\n',
415
392
                             'this/file')
416
393
 
417
 
    def test_merge_reverse_revision_range(self):
418
 
        tree = self.make_branch_and_tree(".")
419
 
        tree.lock_write()
420
 
        self.addCleanup(tree.unlock)
421
 
        self.build_tree(['a'])
422
 
        tree.add('a')
423
 
        tree.commit("added a")
424
 
        first_rev = tree.branch.revision_history()[0]
425
 
        merger = _mod_merge.Merger.from_revision_ids(None, tree,
426
 
                                          _mod_revision.NULL_REVISION,
427
 
                                          first_rev)
428
 
        merger.merge_type = _mod_merge.Merge3Merger
429
 
        merger.interesting_files = 'a'
430
 
        conflict_count = merger.do_merge()
431
 
        self.assertEqual(0, conflict_count)
432
 
 
433
 
        self.assertPathDoesNotExist("a")
434
 
        tree.revert()
435
 
        self.assertPathExists("a")
436
 
 
437
394
    def test_make_merger(self):
438
395
        this_tree = self.make_branch_and_tree('this')
439
396
        this_tree.commit('rev1', rev_id='rev1')
442
399
        other_tree.commit('rev2', rev_id='rev2b')
443
400
        this_tree.lock_write()
444
401
        self.addCleanup(this_tree.unlock)
445
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
402
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress,
446
403
            this_tree, 'rev2b', other_branch=other_tree.branch)
447
404
        merger.merge_type = _mod_merge.Merge3Merger
448
405
        tree_merger = merger.make_merger()
462
419
        other_tree.commit('rev2', rev_id='rev2b')
463
420
        this_tree.lock_write()
464
421
        self.addCleanup(this_tree.unlock)
465
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
422
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
466
423
            this_tree, 'rev2b', other_branch=other_tree.branch)
467
424
        merger.merge_type = _mod_merge.Merge3Merger
468
425
        tree_merger = merger.make_merger()
492
449
        other_tree.commit('rev2', rev_id='rev2b')
493
450
        this_tree.lock_write()
494
451
        self.addCleanup(this_tree.unlock)
495
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
452
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
496
453
            this_tree, 'rev2b', other_branch=other_tree.branch)
497
454
        merger.merge_type = _mod_merge.Merge3Merger
498
455
        tree_merger = merger.make_merger()
503
460
        finally:
504
461
            tree_file.close()
505
462
 
506
 
    def test_merge_require_tree_root(self):
507
 
        tree = self.make_branch_and_tree(".")
508
 
        tree.lock_write()
509
 
        self.addCleanup(tree.unlock)
510
 
        self.build_tree(['a'])
511
 
        tree.add('a')
512
 
        tree.commit("added a")
513
 
        old_root_id = tree.get_root_id()
514
 
        first_rev = tree.branch.revision_history()[0]
515
 
        merger = _mod_merge.Merger.from_revision_ids(None, tree,
516
 
                                          _mod_revision.NULL_REVISION,
517
 
                                          first_rev)
518
 
        merger.merge_type = _mod_merge.Merge3Merger
519
 
        conflict_count = merger.do_merge()
520
 
        self.assertEqual(0, conflict_count)
521
 
        self.assertEquals(set([old_root_id]), tree.all_file_ids())
522
 
        tree.set_parent_ids([])
523
 
 
524
463
    def test_merge_add_into_deleted_root(self):
525
464
        # Yes, people actually do this.  And report bugs if it breaks.
526
465
        source = self.make_branch_and_tree('source', format='rich-root-pack')
1266
1205
        mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1267
1206
        mem_tree.lock_write()
1268
1207
        self.addCleanup(mem_tree.unlock)
1269
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
1208
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1270
1209
            mem_tree, other_revision_id)
1271
1210
        merger.set_interesting_files(interesting_files)
1272
1211
        # It seems there is no matching function for set_interesting_ids
1335
1274
        self.assertEqual(['B-id', 'C-id', 'F-id'],
1336
1275
                         [t.get_revision_id() for t in merger._lca_trees])
1337
1276
 
1338
 
    def test_find_base_new_root_criss_cross(self):
1339
 
        # A   B
1340
 
        # |\ /|
1341
 
        # | X |
1342
 
        # |/ \|
1343
 
        # C   D
1344
 
        
1345
 
        builder = self.get_builder()
1346
 
        builder.build_snapshot('A-id', None,
1347
 
            [('add', ('', None, 'directory', None))])
1348
 
        builder.build_snapshot('B-id', [],
1349
 
            [('add', ('', None, 'directory', None))])
1350
 
        builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1351
 
        builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1352
 
        merger = self.make_Merger(builder, 'D-id')
1353
 
        self.assertEqual('A-id', merger.base_rev_id)
1354
 
        self.assertTrue(merger._is_criss_cross)
1355
 
        self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1356
 
                                            for t in merger._lca_trees])
1357
 
 
1358
1277
    def test_no_criss_cross_passed_to_merge_type(self):
1359
1278
        class LCATreesMerger(LoggingMerger):
1360
1279
            supports_lca_trees = True
1892
1811
        builder.build_snapshot('C-id', ['A-id'], [])
1893
1812
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1894
1813
            [('unversion', 'a-id'),
1895
 
             ('flush', None),
1896
1814
             ('add', (u'a', 'a-id', 'directory', None))])
1897
1815
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1898
1816
        merge_obj = self.make_merge_obj(builder, 'E-id')
1916
1834
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1917
1835
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1918
1836
            [('unversion', 'a-id'),
1919
 
             ('flush', None),
1920
1837
             ('add', (u'a', 'a-id', 'directory', None))])
1921
1838
        merge_obj = self.make_merge_obj(builder, 'E-id')
1922
1839
        entries = list(merge_obj._entries_lca())
2068
1985
 
2069
1986
    def do_merge(self, builder, other_revision_id):
2070
1987
        wt = self.get_wt_from_builder(builder)
2071
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
1988
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2072
1989
            wt, other_revision_id)
2073
1990
        merger.merge_type = _mod_merge.Merge3Merger
2074
1991
        return wt, merger.do_merge()
2185
2102
        self.assertTrue(wt.is_executable('foo-id'))
2186
2103
 
2187
2104
    def test_create_symlink(self):
2188
 
        self.requireFeature(features.SymlinkFeature)
 
2105
        self.requireFeature(tests.SymlinkFeature)
2189
2106
        #   A
2190
2107
        #  / \
2191
2108
        # B   C
2250
2167
                             wt.get_file_text('foo-id'))
2251
2168
 
2252
2169
    def test_modified_symlink(self):
2253
 
        self.requireFeature(features.SymlinkFeature)
 
2170
        self.requireFeature(tests.SymlinkFeature)
2254
2171
        #   A       Create symlink foo => bar
2255
2172
        #  / \
2256
2173
        # B   C     B relinks foo => baz
2295
2212
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2296
2213
 
2297
2214
    def test_renamed_symlink(self):
2298
 
        self.requireFeature(features.SymlinkFeature)
 
2215
        self.requireFeature(tests.SymlinkFeature)
2299
2216
        #   A       Create symlink foo => bar
2300
2217
        #  / \
2301
2218
        # B   C     B renames foo => barry
2334
2251
        wt.commit('D merges B & C', rev_id='D-id')
2335
2252
        self.assertEqual('barry', wt.id2path('foo-id'))
2336
2253
        # Check the output of the Merger object directly
2337
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2254
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2338
2255
            wt, 'F-id')
2339
2256
        merger.merge_type = _mod_merge.Merge3Merger
2340
2257
        merge_obj = merger.make_merger()
2351
2268
        self.assertEqual('blah', wt.id2path('foo-id'))
2352
2269
 
2353
2270
    def test_symlink_no_content_change(self):
2354
 
        self.requireFeature(features.SymlinkFeature)
 
2271
        self.requireFeature(tests.SymlinkFeature)
2355
2272
        #   A       Create symlink foo => bar
2356
2273
        #  / \
2357
2274
        # B   C     B relinks foo => baz
2390
2307
        wt.commit('F foo => bing', rev_id='F-id')
2391
2308
 
2392
2309
        # Check the output of the Merger object directly
2393
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2310
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2394
2311
            wt, 'E-id')
2395
2312
        merger.merge_type = _mod_merge.Merge3Merger
2396
2313
        merge_obj = merger.make_merger()
2402
2319
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2403
2320
 
2404
2321
    def test_symlink_this_changed_kind(self):
2405
 
        self.requireFeature(features.SymlinkFeature)
 
2322
        self.requireFeature(tests.SymlinkFeature)
2406
2323
        #   A       Nothing
2407
2324
        #  / \
2408
2325
        # B   C     B creates symlink foo => bar
2441
2358
        list(wt.iter_changes(wt.basis_tree()))
2442
2359
        wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2443
2360
 
2444
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2361
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2445
2362
            wt, 'E-id')
2446
2363
        merger.merge_type = _mod_merge.Merge3Merger
2447
2364
        merge_obj = merger.make_merger()
2455
2372
 
2456
2373
    def test_symlink_all_wt(self):
2457
2374
        """Check behavior if all trees are Working Trees."""
2458
 
        self.requireFeature(features.SymlinkFeature)
 
2375
        self.requireFeature(tests.SymlinkFeature)
2459
2376
        # The big issue is that entry.symlink_target is None for WorkingTrees.
2460
2377
        # So we need to make sure we handle that case correctly.
2461
2378
        #   A   foo => bar
2656
2573
        wt.branch.set_last_revision_info(2, 'B-id')
2657
2574
        wt.commit('D', rev_id='D-id', recursive=None)
2658
2575
 
2659
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2576
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2660
2577
            wt, 'E-id')
2661
2578
        merger.merge_type = _mod_merge.Merge3Merger
2662
2579
        merge_obj = merger.make_merger()
2693
2610
        wt.branch.set_last_revision_info(2, 'B-id')
2694
2611
        wt.commit('D', rev_id='D-id', recursive=None)
2695
2612
 
2696
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2613
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2697
2614
            wt, 'E-id')
2698
2615
        merger.merge_type = _mod_merge.Merge3Merger
2699
2616
        merge_obj = merger.make_merger()
2733
2650
        wt.branch.set_last_revision_info(2, 'B-id')
2734
2651
        wt.commit('D', rev_id='D-id', recursive=None)
2735
2652
 
2736
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2653
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2737
2654
            wt, 'E-id')
2738
2655
        merger.merge_type = _mod_merge.Merge3Merger
2739
2656
        merge_obj = merger.make_merger()
2778
2695
        wt.branch.set_last_revision_info(2, 'B-id')
2779
2696
        wt.commit('D', rev_id='D-id', recursive=None)
2780
2697
 
2781
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2698
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2782
2699
            wt, 'E-id')
2783
2700
        merger.merge_type = _mod_merge.Merge3Merger
2784
2701
        merge_obj = merger.make_merger()
2930
2847
 
2931
2848
    def get_merger_factory(self):
2932
2849
        # Allows  the inner methods to access the test attributes
2933
 
        calls = self.calls
 
2850
        test = self
2934
2851
 
2935
2852
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2936
2853
            name_prefix = "foo"
2937
2854
            default_files = ['bar']
2938
2855
 
2939
2856
            def merge_text(self, params):
2940
 
                calls.append('merge_text')
 
2857
                test.calls.append('merge_text')
2941
2858
                return ('not_applicable', None)
2942
2859
 
2943
2860
        def factory(merger):
2977
2894
                        base=False, other=False)
2978
2895
        return builder
2979
2896
 
2980
 
    def test_uses_this_branch(self):
2981
 
        builder = self.make_text_conflict()
2982
 
        tt = builder.make_preview_transform()
2983
 
        self.addCleanup(tt.finalize)
2984
 
 
2985
2897
    def test_affected_files_cached(self):
2986
2898
        """Ensures that the config variable is cached"""
2987
2899
        builder = self.make_text_conflict()
3007
2919
        conflicts = builder.merge()
3008
2920
        # The hook should not call the merge_text() method
3009
2921
        self.assertEqual([], self.calls)
3010
 
 
3011
 
 
3012
 
class TestMergeIntoBase(tests.TestCaseWithTransport):
3013
 
 
3014
 
    def setup_simple_branch(self, relpath, shape=None, root_id=None):
3015
 
        """One commit, containing tree specified by optional shape.
3016
 
        
3017
 
        Default is empty tree (just root entry).
3018
 
        """
3019
 
        if root_id is None:
3020
 
            root_id = '%s-root-id' % (relpath,)
3021
 
        wt = self.make_branch_and_tree(relpath)
3022
 
        wt.set_root_id(root_id)
3023
 
        if shape is not None:
3024
 
            adjusted_shape = [relpath + '/' + elem for elem in shape]
3025
 
            self.build_tree(adjusted_shape)
3026
 
            ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3027
 
                   for elem in shape]
3028
 
            wt.add(shape, ids=ids)
3029
 
        rev_id = 'r1-%s' % (relpath,)
3030
 
        wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3031
 
        self.assertEqual(root_id, wt.path2id(''))
3032
 
        return wt
3033
 
 
3034
 
    def setup_two_branches(self, custom_root_ids=True):
3035
 
        """Setup 2 branches, one will be a library, the other a project."""
3036
 
        if custom_root_ids:
3037
 
            root_id = None
3038
 
        else:
3039
 
            root_id = inventory.ROOT_ID
3040
 
        project_wt = self.setup_simple_branch(
3041
 
            'project', ['README', 'dir/', 'dir/file.c'],
3042
 
            root_id)
3043
 
        lib_wt = self.setup_simple_branch(
3044
 
            'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3045
 
 
3046
 
        return project_wt, lib_wt
3047
 
 
3048
 
    def do_merge_into(self, location, merge_as):
3049
 
        """Helper for using MergeIntoMerger.
3050
 
        
3051
 
        :param location: location of directory to merge from, either the
3052
 
            location of a branch or of a path inside a branch.
3053
 
        :param merge_as: the path in a tree to add the new directory as.
3054
 
        :returns: the conflicts from 'do_merge'.
3055
 
        """
3056
 
        operation = cleanup.OperationWithCleanups(self._merge_into)
3057
 
        return operation.run(location, merge_as)
3058
 
 
3059
 
    def _merge_into(self, op, location, merge_as):
3060
 
        # Open and lock the various tree and branch objects
3061
 
        wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3062
 
        op.add_cleanup(wt.lock_write().unlock)
3063
 
        branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3064
 
            location)
3065
 
        op.add_cleanup(branch_to_merge.lock_read().unlock)
3066
 
        other_tree = branch_to_merge.basis_tree()
3067
 
        op.add_cleanup(other_tree.lock_read().unlock)
3068
 
        # Perform the merge
3069
 
        merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3070
 
            other_branch=branch_to_merge, target_subdir=subdir_relpath,
3071
 
            source_subpath=subdir_to_merge)
3072
 
        merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3073
 
        conflicts = merger.do_merge()
3074
 
        merger.set_pending()
3075
 
        return conflicts
3076
 
 
3077
 
    def assertTreeEntriesEqual(self, expected_entries, tree):
3078
 
        """Assert that 'tree' contains the expected inventory entries.
3079
 
 
3080
 
        :param expected_entries: sequence of (path, file-id) pairs.
3081
 
        """
3082
 
        files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3083
 
        self.assertEqual(expected_entries, files)
3084
 
 
3085
 
 
3086
 
class TestMergeInto(TestMergeIntoBase):
3087
 
 
3088
 
    def test_newdir_with_unique_roots(self):
3089
 
        """Merge a branch with a unique root into a new directory."""
3090
 
        project_wt, lib_wt = self.setup_two_branches()
3091
 
        self.do_merge_into('lib1', 'project/lib1')
3092
 
        project_wt.lock_read()
3093
 
        self.addCleanup(project_wt.unlock)
3094
 
        # The r1-lib1 revision should be merged into this one
3095
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3096
 
        self.assertTreeEntriesEqual(
3097
 
            [('', 'project-root-id'),
3098
 
             ('README', 'project-README-id'),
3099
 
             ('dir', 'project-dir-id'),
3100
 
             ('lib1', 'lib1-root-id'),
3101
 
             ('dir/file.c', 'project-file.c-id'),
3102
 
             ('lib1/Makefile', 'lib1-Makefile-id'),
3103
 
             ('lib1/README', 'lib1-README-id'),
3104
 
             ('lib1/foo.c', 'lib1-foo.c-id'),
3105
 
            ], project_wt)
3106
 
 
3107
 
    def test_subdir(self):
3108
 
        """Merge a branch into a subdirectory of an existing directory."""
3109
 
        project_wt, lib_wt = self.setup_two_branches()
3110
 
        self.do_merge_into('lib1', 'project/dir/lib1')
3111
 
        project_wt.lock_read()
3112
 
        self.addCleanup(project_wt.unlock)
3113
 
        # The r1-lib1 revision should be merged into this one
3114
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3115
 
        self.assertTreeEntriesEqual(
3116
 
            [('', 'project-root-id'),
3117
 
             ('README', 'project-README-id'),
3118
 
             ('dir', 'project-dir-id'),
3119
 
             ('dir/file.c', 'project-file.c-id'),
3120
 
             ('dir/lib1', 'lib1-root-id'),
3121
 
             ('dir/lib1/Makefile', 'lib1-Makefile-id'),
3122
 
             ('dir/lib1/README', 'lib1-README-id'),
3123
 
             ('dir/lib1/foo.c', 'lib1-foo.c-id'),
3124
 
            ], project_wt)
3125
 
 
3126
 
    def test_newdir_with_repeat_roots(self):
3127
 
        """If the file-id of the dir to be merged already exists a new ID will
3128
 
        be allocated to let the merge happen.
3129
 
        """
3130
 
        project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3131
 
        root_id = project_wt.path2id('')
3132
 
        self.do_merge_into('lib1', 'project/lib1')
3133
 
        project_wt.lock_read()
3134
 
        self.addCleanup(project_wt.unlock)
3135
 
        # The r1-lib1 revision should be merged into this one
3136
 
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3137
 
        new_lib1_id = project_wt.path2id('lib1')
3138
 
        self.assertNotEqual(None, new_lib1_id)
3139
 
        self.assertTreeEntriesEqual(
3140
 
            [('', root_id),
3141
 
             ('README', 'project-README-id'),
3142
 
             ('dir', 'project-dir-id'),
3143
 
             ('lib1', new_lib1_id),
3144
 
             ('dir/file.c', 'project-file.c-id'),
3145
 
             ('lib1/Makefile', 'lib1-Makefile-id'),
3146
 
             ('lib1/README', 'lib1-README-id'),
3147
 
             ('lib1/foo.c', 'lib1-foo.c-id'),
3148
 
            ], project_wt)
3149
 
 
3150
 
    def test_name_conflict(self):
3151
 
        """When the target directory name already exists a conflict is
3152
 
        generated and the original directory is renamed to foo.moved.
3153
 
        """
3154
 
        dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3155
 
        src_wt = self.setup_simple_branch('src', ['README'])
3156
 
        conflicts = self.do_merge_into('src', 'dest/dir')
3157
 
        self.assertEqual(1, conflicts)
3158
 
        dest_wt.lock_read()
3159
 
        self.addCleanup(dest_wt.unlock)
3160
 
        # The r1-lib1 revision should be merged into this one
3161
 
        self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3162
 
        self.assertTreeEntriesEqual(
3163
 
            [('', 'dest-root-id'),
3164
 
             ('dir', 'src-root-id'),
3165
 
             ('dir.moved', 'dest-dir-id'),
3166
 
             ('dir/README', 'src-README-id'),
3167
 
             ('dir.moved/file.txt', 'dest-file.txt-id'),
3168
 
            ], dest_wt)
3169
 
 
3170
 
    def test_file_id_conflict(self):
3171
 
        """A conflict is generated if the merge-into adds a file (or other
3172
 
        inventory entry) with a file-id that already exists in the target tree.
3173
 
        """
3174
 
        dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3175
 
        # Make a second tree with a file-id that will clash with file.txt in
3176
 
        # dest.
3177
 
        src_wt = self.make_branch_and_tree('src')
3178
 
        self.build_tree(['src/README'])
3179
 
        src_wt.add(['README'], ids=['dest-file.txt-id'])
3180
 
        src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3181
 
        conflicts = self.do_merge_into('src', 'dest/dir')
3182
 
        # This is an edge case that shouldn't happen to users very often.  So
3183
 
        # we don't care really about the exact presentation of the conflict,
3184
 
        # just that there is one.
3185
 
        self.assertEqual(1, conflicts)
3186
 
 
3187
 
    def test_only_subdir(self):
3188
 
        """When the location points to just part of a tree, merge just that
3189
 
        subtree.
3190
 
        """
3191
 
        dest_wt = self.setup_simple_branch('dest')
3192
 
        src_wt = self.setup_simple_branch(
3193
 
            'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3194
 
        conflicts = self.do_merge_into('src/dir', 'dest/dir')
3195
 
        dest_wt.lock_read()
3196
 
        self.addCleanup(dest_wt.unlock)
3197
 
        # The r1-lib1 revision should NOT be merged into this one (this is a
3198
 
        # partial merge).
3199
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3200
 
        self.assertTreeEntriesEqual(
3201
 
            [('', 'dest-root-id'),
3202
 
             ('dir', 'src-dir-id'),
3203
 
             ('dir/foo.c', 'src-foo.c-id'),
3204
 
            ], dest_wt)
3205
 
 
3206
 
    def test_only_file(self):
3207
 
        """An edge case: merge just one file, not a whole dir."""
3208
 
        dest_wt = self.setup_simple_branch('dest')
3209
 
        two_file_wt = self.setup_simple_branch(
3210
 
            'two-file', ['file1.txt', 'file2.txt'])
3211
 
        conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3212
 
        dest_wt.lock_read()
3213
 
        self.addCleanup(dest_wt.unlock)
3214
 
        # The r1-lib1 revision should NOT be merged into this one
3215
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3216
 
        self.assertTreeEntriesEqual(
3217
 
            [('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3218
 
            dest_wt)
3219
 
 
3220
 
    def test_no_such_source_path(self):
3221
 
        """PathNotInTree is raised if the specified path in the source tree
3222
 
        does not exist.
3223
 
        """
3224
 
        dest_wt = self.setup_simple_branch('dest')
3225
 
        two_file_wt = self.setup_simple_branch('src', ['dir/'])
3226
 
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3227
 
            'src/no-such-dir', 'dest/foo')
3228
 
        dest_wt.lock_read()
3229
 
        self.addCleanup(dest_wt.unlock)
3230
 
        # The dest tree is unmodified.
3231
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3232
 
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3233
 
 
3234
 
    def test_no_such_target_path(self):
3235
 
        """PathNotInTree is also raised if the specified path in the target
3236
 
        tree does not exist.
3237
 
        """
3238
 
        dest_wt = self.setup_simple_branch('dest')
3239
 
        two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3240
 
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3241
 
            'src', 'dest/no-such-dir/foo')
3242
 
        dest_wt.lock_read()
3243
 
        self.addCleanup(dest_wt.unlock)
3244
 
        # The dest tree is unmodified.
3245
 
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3246
 
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)