~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-01-20 18:55:04 UTC
  • mfrom: (4971.2.2 505762)
  • Revision ID: pqm@pqm.ubuntu.com-20100120185504-es1x5ntwauunwxvp
(nmb) Explain bound branches in "branches" help topic

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
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
39
 
from bzrlib.tests import (
40
 
    features,
41
 
    TestCaseWithMemoryTransport,
42
 
    TestCaseWithTransport,
43
 
    test_merge_core,
44
 
    )
 
35
from bzrlib.osutils import pathjoin, file_kind
 
36
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
45
37
from bzrlib.workingtree import WorkingTree
46
38
 
47
39
 
91
83
        os.chdir('branch2')
92
84
        self.run_bzr('merge ../branch1/baz', retcode=3)
93
85
        self.run_bzr('merge ../branch1/foo')
94
 
        self.assertPathExists('foo')
95
 
        self.assertPathDoesNotExist('bar')
 
86
        self.failUnlessExists('foo')
 
87
        self.failIfExists('bar')
96
88
        wt2 = WorkingTree.open('.') # opens branch2
97
89
        self.assertEqual([tip], wt2.get_parent_ids())
98
90
 
122
114
        finally:
123
115
            wt1.unlock()
124
116
 
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
117
    def test_create_rename(self):
152
118
        """Rename an inventory entry while creating the file"""
153
119
        tree =self.make_branch_and_tree('.')
184
150
        log = StringIO()
185
151
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
186
152
                    this_tree=tree_b, ignore_zero=True)
187
 
        self.assertTrue('All changes applied successfully.\n' not in
 
153
        self.failUnless('All changes applied successfully.\n' not in
188
154
            self.get_log())
189
155
        tree_b.revert()
190
156
        merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
191
157
                    this_tree=tree_b, ignore_zero=False)
192
 
        self.assertTrue('All changes applied successfully.\n' in self.get_log())
 
158
        self.failUnless('All changes applied successfully.\n' in self.get_log())
193
159
 
194
160
    def test_merge_inner_conflicts(self):
195
161
        tree_a = self.make_branch_and_tree('a')
325
291
        tree_a.commit('commit 2')
326
292
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
327
293
        tree_b.rename_one('file_1', 'renamed')
328
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
 
294
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
 
295
                                                    progress.DummyProgress())
329
296
        merger.merge_type = _mod_merge.Merge3Merger
330
297
        merger.do_merge()
331
298
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
339
306
        tree_a.commit('commit 2')
340
307
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
341
308
        tree_b.rename_one('file_1', 'renamed')
342
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
 
309
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
 
310
                                                    progress.DummyProgress())
343
311
        merger.merge_type = _mod_merge.WeaveMerger
344
312
        merger.do_merge()
345
313
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
346
314
 
 
315
    def test_Merger_defaults_to_DummyProgress(self):
 
316
        branch = self.make_branch('branch')
 
317
        merger = _mod_merge.Merger(branch, pb=None)
 
318
        self.assertIsInstance(merger._pb, progress.DummyProgress)
 
319
 
347
320
    def prepare_cherrypick(self):
348
321
        """Prepare a pair of trees for cherrypicking tests.
349
322
 
370
343
 
371
344
    def test_weave_cherrypick(self):
372
345
        this_tree, other_tree = self.prepare_cherrypick()
373
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
346
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
374
347
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
375
348
        merger.merge_type = _mod_merge.WeaveMerger
376
349
        merger.do_merge()
378
351
 
379
352
    def test_weave_cannot_reverse_cherrypick(self):
380
353
        this_tree, other_tree = self.prepare_cherrypick()
381
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
354
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
382
355
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
383
356
        merger.merge_type = _mod_merge.WeaveMerger
384
357
        self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
385
358
 
386
359
    def test_merge3_can_reverse_cherrypick(self):
387
360
        this_tree, other_tree = self.prepare_cherrypick()
388
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
361
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
389
362
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
390
363
        merger.merge_type = _mod_merge.Merge3Merger
391
364
        merger.do_merge()
403
376
        this_tree.lock_write()
404
377
        self.addCleanup(this_tree.unlock)
405
378
 
406
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
379
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
407
380
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
408
381
        merger.merge_type = _mod_merge.Merge3Merger
409
382
        merger.do_merge()
414
387
                             '>>>>>>> MERGE-SOURCE\n',
415
388
                             'this/file')
416
389
 
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
390
    def test_make_merger(self):
438
391
        this_tree = self.make_branch_and_tree('this')
439
392
        this_tree.commit('rev1', rev_id='rev1')
442
395
        other_tree.commit('rev2', rev_id='rev2b')
443
396
        this_tree.lock_write()
444
397
        self.addCleanup(this_tree.unlock)
445
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
398
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress,
446
399
            this_tree, 'rev2b', other_branch=other_tree.branch)
447
400
        merger.merge_type = _mod_merge.Merge3Merger
448
401
        tree_merger = merger.make_merger()
462
415
        other_tree.commit('rev2', rev_id='rev2b')
463
416
        this_tree.lock_write()
464
417
        self.addCleanup(this_tree.unlock)
465
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
418
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
466
419
            this_tree, 'rev2b', other_branch=other_tree.branch)
467
420
        merger.merge_type = _mod_merge.Merge3Merger
468
421
        tree_merger = merger.make_merger()
492
445
        other_tree.commit('rev2', rev_id='rev2b')
493
446
        this_tree.lock_write()
494
447
        self.addCleanup(this_tree.unlock)
495
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
448
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
496
449
            this_tree, 'rev2b', other_branch=other_tree.branch)
497
450
        merger.merge_type = _mod_merge.Merge3Merger
498
451
        tree_merger = merger.make_merger()
503
456
        finally:
504
457
            tree_file.close()
505
458
 
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
459
    def test_merge_add_into_deleted_root(self):
525
460
        # Yes, people actually do this.  And report bugs if it breaks.
526
461
        source = self.make_branch_and_tree('source', format='rich-root-pack')
1266
1201
        mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1267
1202
        mem_tree.lock_write()
1268
1203
        self.addCleanup(mem_tree.unlock)
1269
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
1204
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1270
1205
            mem_tree, other_revision_id)
1271
1206
        merger.set_interesting_files(interesting_files)
1272
1207
        # It seems there is no matching function for set_interesting_ids
1335
1270
        self.assertEqual(['B-id', 'C-id', 'F-id'],
1336
1271
                         [t.get_revision_id() for t in merger._lca_trees])
1337
1272
 
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
1273
    def test_no_criss_cross_passed_to_merge_type(self):
1359
1274
        class LCATreesMerger(LoggingMerger):
1360
1275
            supports_lca_trees = True
1892
1807
        builder.build_snapshot('C-id', ['A-id'], [])
1893
1808
        builder.build_snapshot('E-id', ['C-id', 'B-id'],
1894
1809
            [('unversion', 'a-id'),
1895
 
             ('flush', None),
1896
1810
             ('add', (u'a', 'a-id', 'directory', None))])
1897
1811
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1898
1812
        merge_obj = self.make_merge_obj(builder, 'E-id')
1916
1830
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1917
1831
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
1918
1832
            [('unversion', 'a-id'),
1919
 
             ('flush', None),
1920
1833
             ('add', (u'a', 'a-id', 'directory', None))])
1921
1834
        merge_obj = self.make_merge_obj(builder, 'E-id')
1922
1835
        entries = list(merge_obj._entries_lca())
2068
1981
 
2069
1982
    def do_merge(self, builder, other_revision_id):
2070
1983
        wt = self.get_wt_from_builder(builder)
2071
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
1984
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2072
1985
            wt, other_revision_id)
2073
1986
        merger.merge_type = _mod_merge.Merge3Merger
2074
1987
        return wt, merger.do_merge()
2185
2098
        self.assertTrue(wt.is_executable('foo-id'))
2186
2099
 
2187
2100
    def test_create_symlink(self):
2188
 
        self.requireFeature(features.SymlinkFeature)
 
2101
        self.requireFeature(tests.SymlinkFeature)
2189
2102
        #   A
2190
2103
        #  / \
2191
2104
        # B   C
2250
2163
                             wt.get_file_text('foo-id'))
2251
2164
 
2252
2165
    def test_modified_symlink(self):
2253
 
        self.requireFeature(features.SymlinkFeature)
 
2166
        self.requireFeature(tests.SymlinkFeature)
2254
2167
        #   A       Create symlink foo => bar
2255
2168
        #  / \
2256
2169
        # B   C     B relinks foo => baz
2295
2208
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2296
2209
 
2297
2210
    def test_renamed_symlink(self):
2298
 
        self.requireFeature(features.SymlinkFeature)
 
2211
        self.requireFeature(tests.SymlinkFeature)
2299
2212
        #   A       Create symlink foo => bar
2300
2213
        #  / \
2301
2214
        # B   C     B renames foo => barry
2334
2247
        wt.commit('D merges B & C', rev_id='D-id')
2335
2248
        self.assertEqual('barry', wt.id2path('foo-id'))
2336
2249
        # Check the output of the Merger object directly
2337
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2250
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2338
2251
            wt, 'F-id')
2339
2252
        merger.merge_type = _mod_merge.Merge3Merger
2340
2253
        merge_obj = merger.make_merger()
2351
2264
        self.assertEqual('blah', wt.id2path('foo-id'))
2352
2265
 
2353
2266
    def test_symlink_no_content_change(self):
2354
 
        self.requireFeature(features.SymlinkFeature)
 
2267
        self.requireFeature(tests.SymlinkFeature)
2355
2268
        #   A       Create symlink foo => bar
2356
2269
        #  / \
2357
2270
        # B   C     B relinks foo => baz
2390
2303
        wt.commit('F foo => bing', rev_id='F-id')
2391
2304
 
2392
2305
        # Check the output of the Merger object directly
2393
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2306
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2394
2307
            wt, 'E-id')
2395
2308
        merger.merge_type = _mod_merge.Merge3Merger
2396
2309
        merge_obj = merger.make_merger()
2402
2315
        self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2403
2316
 
2404
2317
    def test_symlink_this_changed_kind(self):
2405
 
        self.requireFeature(features.SymlinkFeature)
 
2318
        self.requireFeature(tests.SymlinkFeature)
2406
2319
        #   A       Nothing
2407
2320
        #  / \
2408
2321
        # B   C     B creates symlink foo => bar
2441
2354
        list(wt.iter_changes(wt.basis_tree()))
2442
2355
        wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2443
2356
 
2444
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2357
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2445
2358
            wt, 'E-id')
2446
2359
        merger.merge_type = _mod_merge.Merge3Merger
2447
2360
        merge_obj = merger.make_merger()
2455
2368
 
2456
2369
    def test_symlink_all_wt(self):
2457
2370
        """Check behavior if all trees are Working Trees."""
2458
 
        self.requireFeature(features.SymlinkFeature)
 
2371
        self.requireFeature(tests.SymlinkFeature)
2459
2372
        # The big issue is that entry.symlink_target is None for WorkingTrees.
2460
2373
        # So we need to make sure we handle that case correctly.
2461
2374
        #   A   foo => bar
2656
2569
        wt.branch.set_last_revision_info(2, 'B-id')
2657
2570
        wt.commit('D', rev_id='D-id', recursive=None)
2658
2571
 
2659
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2572
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2660
2573
            wt, 'E-id')
2661
2574
        merger.merge_type = _mod_merge.Merge3Merger
2662
2575
        merge_obj = merger.make_merger()
2693
2606
        wt.branch.set_last_revision_info(2, 'B-id')
2694
2607
        wt.commit('D', rev_id='D-id', recursive=None)
2695
2608
 
2696
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2609
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2697
2610
            wt, 'E-id')
2698
2611
        merger.merge_type = _mod_merge.Merge3Merger
2699
2612
        merge_obj = merger.make_merger()
2733
2646
        wt.branch.set_last_revision_info(2, 'B-id')
2734
2647
        wt.commit('D', rev_id='D-id', recursive=None)
2735
2648
 
2736
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2649
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2737
2650
            wt, 'E-id')
2738
2651
        merger.merge_type = _mod_merge.Merge3Merger
2739
2652
        merge_obj = merger.make_merger()
2778
2691
        wt.branch.set_last_revision_info(2, 'B-id')
2779
2692
        wt.commit('D', rev_id='D-id', recursive=None)
2780
2693
 
2781
 
        merger = _mod_merge.Merger.from_revision_ids(None,
 
2694
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
2782
2695
            wt, 'E-id')
2783
2696
        merger.merge_type = _mod_merge.Merge3Merger
2784
2697
        merge_obj = merger.make_merger()
2920
2833
            'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2921
2834
        self.assertLCAMultiWay('conflict',
2922
2835
            'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2923
 
 
2924
 
 
2925
 
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2926
 
 
2927
 
    def setUp(self):
2928
 
        super(TestConfigurableFileMerger, self).setUp()
2929
 
        self.calls = []
2930
 
 
2931
 
    def get_merger_factory(self):
2932
 
        # Allows  the inner methods to access the test attributes
2933
 
        calls = self.calls
2934
 
 
2935
 
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2936
 
            name_prefix = "foo"
2937
 
            default_files = ['bar']
2938
 
 
2939
 
            def merge_text(self, params):
2940
 
                calls.append('merge_text')
2941
 
                return ('not_applicable', None)
2942
 
 
2943
 
        def factory(merger):
2944
 
            result = FooMerger(merger)
2945
 
            # Make sure we start with a clean slate
2946
 
            self.assertEqual(None, result.affected_files)
2947
 
            # Track the original merger
2948
 
            self.merger = result
2949
 
            return result
2950
 
 
2951
 
        return factory
2952
 
 
2953
 
    def _install_hook(self, factory):
2954
 
        _mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2955
 
                                                   factory, 'test factory')
2956
 
 
2957
 
    def make_builder(self):
2958
 
        builder = test_merge_core.MergeBuilder(self.test_base_dir)
2959
 
        self.addCleanup(builder.cleanup)
2960
 
        return builder
2961
 
 
2962
 
    def make_text_conflict(self, file_name='bar'):
2963
 
        factory = self.get_merger_factory()
2964
 
        self._install_hook(factory)
2965
 
        builder = self.make_builder()
2966
 
        builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2967
 
        builder.change_contents('bar-id', other='text4', this='text3')
2968
 
        return builder
2969
 
 
2970
 
    def make_kind_change(self):
2971
 
        factory = self.get_merger_factory()
2972
 
        self._install_hook(factory)
2973
 
        builder = self.make_builder()
2974
 
        builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2975
 
                         this=False)
2976
 
        builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2977
 
                        base=False, other=False)
2978
 
        return builder
2979
 
 
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
 
    def test_affected_files_cached(self):
2986
 
        """Ensures that the config variable is cached"""
2987
 
        builder = self.make_text_conflict()
2988
 
        conflicts = builder.merge()
2989
 
        # The hook should set the variable
2990
 
        self.assertEqual(['bar'], self.merger.affected_files)
2991
 
        self.assertEqual(1, len(conflicts))
2992
 
 
2993
 
    def test_hook_called_for_text_conflicts(self):
2994
 
        builder = self.make_text_conflict()
2995
 
        conflicts = builder.merge()
2996
 
        # The hook should call the merge_text() method
2997
 
        self.assertEqual(['merge_text'], self.calls)
2998
 
 
2999
 
    def test_hook_not_called_for_kind_change(self):
3000
 
        builder = self.make_kind_change()
3001
 
        conflicts = builder.merge()
3002
 
        # The hook should not call the merge_text() method
3003
 
        self.assertEqual([], self.calls)
3004
 
 
3005
 
    def test_hook_not_called_for_other_files(self):
3006
 
        builder = self.make_text_conflict('foobar')
3007
 
        conflicts = builder.merge()
3008
 
        # The hook should not call the merge_text() method
3009
 
        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)