~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge.py

(gz) Backslash escape selftest output when printing to non-unicode consoles
 (Martin [gz])

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,
295
298
        tree_a.commit('commit 2')
296
299
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
297
300
        tree_b.rename_one('file_1', 'renamed')
298
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
299
 
                                                    progress.DummyProgress())
 
301
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
300
302
        merger.merge_type = _mod_merge.Merge3Merger
301
303
        merger.do_merge()
302
304
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
310
312
        tree_a.commit('commit 2')
311
313
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
312
314
        tree_b.rename_one('file_1', 'renamed')
313
 
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
314
 
                                                    progress.DummyProgress())
 
315
        merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
315
316
        merger.merge_type = _mod_merge.WeaveMerger
316
317
        merger.do_merge()
317
318
        self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
318
319
 
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
 
 
324
320
    def prepare_cherrypick(self):
325
321
        """Prepare a pair of trees for cherrypicking tests.
326
322
 
347
343
 
348
344
    def test_weave_cherrypick(self):
349
345
        this_tree, other_tree = self.prepare_cherrypick()
350
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
346
        merger = _mod_merge.Merger.from_revision_ids(None,
351
347
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
352
348
        merger.merge_type = _mod_merge.WeaveMerger
353
349
        merger.do_merge()
355
351
 
356
352
    def test_weave_cannot_reverse_cherrypick(self):
357
353
        this_tree, other_tree = self.prepare_cherrypick()
358
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
354
        merger = _mod_merge.Merger.from_revision_ids(None,
359
355
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
360
356
        merger.merge_type = _mod_merge.WeaveMerger
361
357
        self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
362
358
 
363
359
    def test_merge3_can_reverse_cherrypick(self):
364
360
        this_tree, other_tree = self.prepare_cherrypick()
365
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
361
        merger = _mod_merge.Merger.from_revision_ids(None,
366
362
            this_tree, 'rev2b', 'rev3b', other_tree.branch)
367
363
        merger.merge_type = _mod_merge.Merge3Merger
368
364
        merger.do_merge()
380
376
        this_tree.lock_write()
381
377
        self.addCleanup(this_tree.unlock)
382
378
 
383
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
379
        merger = _mod_merge.Merger.from_revision_ids(None,
384
380
            this_tree, 'rev3b', 'rev2b', other_tree.branch)
385
381
        merger.merge_type = _mod_merge.Merge3Merger
386
382
        merger.do_merge()
399
395
        other_tree.commit('rev2', rev_id='rev2b')
400
396
        this_tree.lock_write()
401
397
        self.addCleanup(this_tree.unlock)
402
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress,
 
398
        merger = _mod_merge.Merger.from_revision_ids(None,
403
399
            this_tree, 'rev2b', other_branch=other_tree.branch)
404
400
        merger.merge_type = _mod_merge.Merge3Merger
405
401
        tree_merger = merger.make_merger()
419
415
        other_tree.commit('rev2', rev_id='rev2b')
420
416
        this_tree.lock_write()
421
417
        self.addCleanup(this_tree.unlock)
422
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
418
        merger = _mod_merge.Merger.from_revision_ids(None,
423
419
            this_tree, 'rev2b', other_branch=other_tree.branch)
424
420
        merger.merge_type = _mod_merge.Merge3Merger
425
421
        tree_merger = merger.make_merger()
449
445
        other_tree.commit('rev2', rev_id='rev2b')
450
446
        this_tree.lock_write()
451
447
        self.addCleanup(this_tree.unlock)
452
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
448
        merger = _mod_merge.Merger.from_revision_ids(None,
453
449
            this_tree, 'rev2b', other_branch=other_tree.branch)
454
450
        merger.merge_type = _mod_merge.Merge3Merger
455
451
        tree_merger = merger.make_merger()
1205
1201
        mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1206
1202
        mem_tree.lock_write()
1207
1203
        self.addCleanup(mem_tree.unlock)
1208
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
1204
        merger = _mod_merge.Merger.from_revision_ids(None,
1209
1205
            mem_tree, other_revision_id)
1210
1206
        merger.set_interesting_files(interesting_files)
1211
1207
        # It seems there is no matching function for set_interesting_ids
1274
1270
        self.assertEqual(['B-id', 'C-id', 'F-id'],
1275
1271
                         [t.get_revision_id() for t in merger._lca_trees])
1276
1272
 
 
1273
    def test_find_base_new_root_criss_cross(self):
 
1274
        # A   B
 
1275
        # |\ /|
 
1276
        # | X |
 
1277
        # |/ \|
 
1278
        # C   D
 
1279
        
 
1280
        builder = self.get_builder()
 
1281
        builder.build_snapshot('A-id', None,
 
1282
            [('add', ('', None, 'directory', None))])
 
1283
        builder.build_snapshot('B-id', [],
 
1284
            [('add', ('', None, 'directory', None))])
 
1285
        builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
 
1286
        builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
 
1287
        merger = self.make_Merger(builder, 'D-id')
 
1288
        self.assertEqual('A-id', merger.base_rev_id)
 
1289
        self.assertTrue(merger._is_criss_cross)
 
1290
        self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
 
1291
                                            for t in merger._lca_trees])
 
1292
 
1277
1293
    def test_no_criss_cross_passed_to_merge_type(self):
1278
1294
        class LCATreesMerger(LoggingMerger):
1279
1295
            supports_lca_trees = True
1985
2001
 
1986
2002
    def do_merge(self, builder, other_revision_id):
1987
2003
        wt = self.get_wt_from_builder(builder)
1988
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2004
        merger = _mod_merge.Merger.from_revision_ids(None,
1989
2005
            wt, other_revision_id)
1990
2006
        merger.merge_type = _mod_merge.Merge3Merger
1991
2007
        return wt, merger.do_merge()
2251
2267
        wt.commit('D merges B & C', rev_id='D-id')
2252
2268
        self.assertEqual('barry', wt.id2path('foo-id'))
2253
2269
        # Check the output of the Merger object directly
2254
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2270
        merger = _mod_merge.Merger.from_revision_ids(None,
2255
2271
            wt, 'F-id')
2256
2272
        merger.merge_type = _mod_merge.Merge3Merger
2257
2273
        merge_obj = merger.make_merger()
2307
2323
        wt.commit('F foo => bing', rev_id='F-id')
2308
2324
 
2309
2325
        # Check the output of the Merger object directly
2310
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2326
        merger = _mod_merge.Merger.from_revision_ids(None,
2311
2327
            wt, 'E-id')
2312
2328
        merger.merge_type = _mod_merge.Merge3Merger
2313
2329
        merge_obj = merger.make_merger()
2358
2374
        list(wt.iter_changes(wt.basis_tree()))
2359
2375
        wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2360
2376
 
2361
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2377
        merger = _mod_merge.Merger.from_revision_ids(None,
2362
2378
            wt, 'E-id')
2363
2379
        merger.merge_type = _mod_merge.Merge3Merger
2364
2380
        merge_obj = merger.make_merger()
2573
2589
        wt.branch.set_last_revision_info(2, 'B-id')
2574
2590
        wt.commit('D', rev_id='D-id', recursive=None)
2575
2591
 
2576
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2592
        merger = _mod_merge.Merger.from_revision_ids(None,
2577
2593
            wt, 'E-id')
2578
2594
        merger.merge_type = _mod_merge.Merge3Merger
2579
2595
        merge_obj = merger.make_merger()
2610
2626
        wt.branch.set_last_revision_info(2, 'B-id')
2611
2627
        wt.commit('D', rev_id='D-id', recursive=None)
2612
2628
 
2613
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2629
        merger = _mod_merge.Merger.from_revision_ids(None,
2614
2630
            wt, 'E-id')
2615
2631
        merger.merge_type = _mod_merge.Merge3Merger
2616
2632
        merge_obj = merger.make_merger()
2650
2666
        wt.branch.set_last_revision_info(2, 'B-id')
2651
2667
        wt.commit('D', rev_id='D-id', recursive=None)
2652
2668
 
2653
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2669
        merger = _mod_merge.Merger.from_revision_ids(None,
2654
2670
            wt, 'E-id')
2655
2671
        merger.merge_type = _mod_merge.Merge3Merger
2656
2672
        merge_obj = merger.make_merger()
2695
2711
        wt.branch.set_last_revision_info(2, 'B-id')
2696
2712
        wt.commit('D', rev_id='D-id', recursive=None)
2697
2713
 
2698
 
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
2714
        merger = _mod_merge.Merger.from_revision_ids(None,
2699
2715
            wt, 'E-id')
2700
2716
        merger.merge_type = _mod_merge.Merge3Merger
2701
2717
        merge_obj = merger.make_merger()
2841
2857
 
2842
2858
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2843
2859
 
2844
 
    def test_affected_files_cached(self):
2845
 
        """Ensures that the config variable is cached"""
2846
 
        class SimplePlan(_mod_merge.ConfigurableFileMerger):
 
2860
    def setUp(self):
 
2861
        super(TestConfigurableFileMerger, self).setUp()
 
2862
        self.calls = []
 
2863
 
 
2864
    def get_merger_factory(self):
 
2865
        # Allows  the inner methods to access the test attributes
 
2866
        test = self
 
2867
 
 
2868
        class FooMerger(_mod_merge.ConfigurableFileMerger):
2847
2869
            name_prefix = "foo"
2848
 
            default_files = ["my default"]
 
2870
            default_files = ['bar']
 
2871
 
2849
2872
            def merge_text(self, params):
2850
 
                return ('not applicable', None)
 
2873
                test.calls.append('merge_text')
 
2874
                return ('not_applicable', None)
 
2875
 
2851
2876
        def factory(merger):
2852
 
            result = SimplePlan(merger)
 
2877
            result = FooMerger(merger)
 
2878
            # Make sure we start with a clean slate
2853
2879
            self.assertEqual(None, result.affected_files)
 
2880
            # Track the original merger
2854
2881
            self.merger = result
2855
2882
            return result
 
2883
 
 
2884
        return factory
 
2885
 
 
2886
    def _install_hook(self, factory):
2856
2887
        _mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2857
 
            factory, 'test factory')
 
2888
                                                   factory, 'test factory')
 
2889
 
 
2890
    def make_builder(self):
2858
2891
        builder = test_merge_core.MergeBuilder(self.test_base_dir)
2859
2892
        self.addCleanup(builder.cleanup)
2860
 
        builder.add_file('NEWS', builder.tree_root, 'name1', 'text1', True)
2861
 
        builder.change_contents('NEWS', other='text4', this='text3')
 
2893
        return builder
 
2894
 
 
2895
    def make_text_conflict(self, file_name='bar'):
 
2896
        factory = self.get_merger_factory()
 
2897
        self._install_hook(factory)
 
2898
        builder = self.make_builder()
 
2899
        builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
 
2900
        builder.change_contents('bar-id', other='text4', this='text3')
 
2901
        return builder
 
2902
 
 
2903
    def make_kind_change(self):
 
2904
        factory = self.get_merger_factory()
 
2905
        self._install_hook(factory)
 
2906
        builder = self.make_builder()
 
2907
        builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
 
2908
                         this=False)
 
2909
        builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
 
2910
                        base=False, other=False)
 
2911
        return builder
 
2912
 
 
2913
    def test_uses_this_branch(self):
 
2914
        builder = self.make_text_conflict()
 
2915
        tt = builder.make_preview_transform()
 
2916
        self.addCleanup(tt.finalize)
 
2917
 
 
2918
    def test_affected_files_cached(self):
 
2919
        """Ensures that the config variable is cached"""
 
2920
        builder = self.make_text_conflict()
2862
2921
        conflicts = builder.merge()
2863
2922
        # The hook should set the variable
2864
 
        self.assertEqual(["my default"], self.merger.affected_files)
 
2923
        self.assertEqual(['bar'], self.merger.affected_files)
 
2924
        self.assertEqual(1, len(conflicts))
 
2925
 
 
2926
    def test_hook_called_for_text_conflicts(self):
 
2927
        builder = self.make_text_conflict()
 
2928
        conflicts = builder.merge()
 
2929
        # The hook should call the merge_text() method
 
2930
        self.assertEqual(['merge_text'], self.calls)
 
2931
 
 
2932
    def test_hook_not_called_for_kind_change(self):
 
2933
        builder = self.make_kind_change()
 
2934
        conflicts = builder.merge()
 
2935
        # The hook should not call the merge_text() method
 
2936
        self.assertEqual([], self.calls)
 
2937
 
 
2938
    def test_hook_not_called_for_other_files(self):
 
2939
        builder = self.make_text_conflict('foobar')
 
2940
        conflicts = builder.merge()
 
2941
        # The hook should not call the merge_text() method
 
2942
        self.assertEqual([], self.calls)
 
2943
 
 
2944
 
 
2945
class TestMergeIntoBase(tests.TestCaseWithTransport):
 
2946
 
 
2947
    def setup_simple_branch(self, relpath, shape=None, root_id=None):
 
2948
        """One commit, containing tree specified by optional shape.
 
2949
        
 
2950
        Default is empty tree (just root entry).
 
2951
        """
 
2952
        if root_id is None:
 
2953
            root_id = '%s-root-id' % (relpath,)
 
2954
        wt = self.make_branch_and_tree(relpath)
 
2955
        wt.set_root_id(root_id)
 
2956
        if shape is not None:
 
2957
            adjusted_shape = [relpath + '/' + elem for elem in shape]
 
2958
            self.build_tree(adjusted_shape)
 
2959
            ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
 
2960
                   for elem in shape]
 
2961
            wt.add(shape, ids=ids)
 
2962
        rev_id = 'r1-%s' % (relpath,)
 
2963
        wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
 
2964
        self.assertEqual(root_id, wt.path2id(''))
 
2965
        return wt
 
2966
 
 
2967
    def setup_two_branches(self, custom_root_ids=True):
 
2968
        """Setup 2 branches, one will be a library, the other a project."""
 
2969
        if custom_root_ids:
 
2970
            root_id = None
 
2971
        else:
 
2972
            root_id = inventory.ROOT_ID
 
2973
        project_wt = self.setup_simple_branch(
 
2974
            'project', ['README', 'dir/', 'dir/file.c'],
 
2975
            root_id)
 
2976
        lib_wt = self.setup_simple_branch(
 
2977
            'lib1', ['README', 'Makefile', 'foo.c'], root_id)
 
2978
 
 
2979
        return project_wt, lib_wt
 
2980
 
 
2981
    def do_merge_into(self, location, merge_as):
 
2982
        """Helper for using MergeIntoMerger.
 
2983
        
 
2984
        :param location: location of directory to merge from, either the
 
2985
            location of a branch or of a path inside a branch.
 
2986
        :param merge_as: the path in a tree to add the new directory as.
 
2987
        :returns: the conflicts from 'do_merge'.
 
2988
        """
 
2989
        operation = cleanup.OperationWithCleanups(self._merge_into)
 
2990
        return operation.run(location, merge_as)
 
2991
 
 
2992
    def _merge_into(self, op, location, merge_as):
 
2993
        # Open and lock the various tree and branch objects
 
2994
        wt, subdir_relpath = WorkingTree.open_containing(merge_as)
 
2995
        op.add_cleanup(wt.lock_write().unlock)
 
2996
        branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
 
2997
            location)
 
2998
        op.add_cleanup(branch_to_merge.lock_read().unlock)
 
2999
        other_tree = branch_to_merge.basis_tree()
 
3000
        op.add_cleanup(other_tree.lock_read().unlock)
 
3001
        # Perform the merge
 
3002
        merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
 
3003
            other_branch=branch_to_merge, target_subdir=subdir_relpath,
 
3004
            source_subpath=subdir_to_merge)
 
3005
        merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
 
3006
        conflicts = merger.do_merge()
 
3007
        merger.set_pending()
 
3008
        return conflicts
 
3009
 
 
3010
    def assertTreeEntriesEqual(self, expected_entries, tree):
 
3011
        """Assert that 'tree' contains the expected inventory entries.
 
3012
 
 
3013
        :param expected_entries: sequence of (path, file-id) pairs.
 
3014
        """
 
3015
        files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
 
3016
        self.assertEqual(expected_entries, files)
 
3017
 
 
3018
 
 
3019
class TestMergeInto(TestMergeIntoBase):
 
3020
 
 
3021
    def test_newdir_with_unique_roots(self):
 
3022
        """Merge a branch with a unique root into a new directory."""
 
3023
        project_wt, lib_wt = self.setup_two_branches()
 
3024
        self.do_merge_into('lib1', 'project/lib1')
 
3025
        project_wt.lock_read()
 
3026
        self.addCleanup(project_wt.unlock)
 
3027
        # The r1-lib1 revision should be merged into this one
 
3028
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
 
3029
        self.assertTreeEntriesEqual(
 
3030
            [('', 'project-root-id'),
 
3031
             ('README', 'project-README-id'),
 
3032
             ('dir', 'project-dir-id'),
 
3033
             ('lib1', 'lib1-root-id'),
 
3034
             ('dir/file.c', 'project-file.c-id'),
 
3035
             ('lib1/Makefile', 'lib1-Makefile-id'),
 
3036
             ('lib1/README', 'lib1-README-id'),
 
3037
             ('lib1/foo.c', 'lib1-foo.c-id'),
 
3038
            ], project_wt)
 
3039
 
 
3040
    def test_subdir(self):
 
3041
        """Merge a branch into a subdirectory of an existing directory."""
 
3042
        project_wt, lib_wt = self.setup_two_branches()
 
3043
        self.do_merge_into('lib1', 'project/dir/lib1')
 
3044
        project_wt.lock_read()
 
3045
        self.addCleanup(project_wt.unlock)
 
3046
        # The r1-lib1 revision should be merged into this one
 
3047
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
 
3048
        self.assertTreeEntriesEqual(
 
3049
            [('', 'project-root-id'),
 
3050
             ('README', 'project-README-id'),
 
3051
             ('dir', 'project-dir-id'),
 
3052
             ('dir/file.c', 'project-file.c-id'),
 
3053
             ('dir/lib1', 'lib1-root-id'),
 
3054
             ('dir/lib1/Makefile', 'lib1-Makefile-id'),
 
3055
             ('dir/lib1/README', 'lib1-README-id'),
 
3056
             ('dir/lib1/foo.c', 'lib1-foo.c-id'),
 
3057
            ], project_wt)
 
3058
 
 
3059
    def test_newdir_with_repeat_roots(self):
 
3060
        """If the file-id of the dir to be merged already exists a new ID will
 
3061
        be allocated to let the merge happen.
 
3062
        """
 
3063
        project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
 
3064
        root_id = project_wt.path2id('')
 
3065
        self.do_merge_into('lib1', 'project/lib1')
 
3066
        project_wt.lock_read()
 
3067
        self.addCleanup(project_wt.unlock)
 
3068
        # The r1-lib1 revision should be merged into this one
 
3069
        self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
 
3070
        new_lib1_id = project_wt.path2id('lib1')
 
3071
        self.assertNotEqual(None, new_lib1_id)
 
3072
        self.assertTreeEntriesEqual(
 
3073
            [('', root_id),
 
3074
             ('README', 'project-README-id'),
 
3075
             ('dir', 'project-dir-id'),
 
3076
             ('lib1', new_lib1_id),
 
3077
             ('dir/file.c', 'project-file.c-id'),
 
3078
             ('lib1/Makefile', 'lib1-Makefile-id'),
 
3079
             ('lib1/README', 'lib1-README-id'),
 
3080
             ('lib1/foo.c', 'lib1-foo.c-id'),
 
3081
            ], project_wt)
 
3082
 
 
3083
    def test_name_conflict(self):
 
3084
        """When the target directory name already exists a conflict is
 
3085
        generated and the original directory is renamed to foo.moved.
 
3086
        """
 
3087
        dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
 
3088
        src_wt = self.setup_simple_branch('src', ['README'])
 
3089
        conflicts = self.do_merge_into('src', 'dest/dir')
 
3090
        self.assertEqual(1, conflicts)
 
3091
        dest_wt.lock_read()
 
3092
        self.addCleanup(dest_wt.unlock)
 
3093
        # The r1-lib1 revision should be merged into this one
 
3094
        self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
 
3095
        self.assertTreeEntriesEqual(
 
3096
            [('', 'dest-root-id'),
 
3097
             ('dir', 'src-root-id'),
 
3098
             ('dir.moved', 'dest-dir-id'),
 
3099
             ('dir/README', 'src-README-id'),
 
3100
             ('dir.moved/file.txt', 'dest-file.txt-id'),
 
3101
            ], dest_wt)
 
3102
 
 
3103
    def test_file_id_conflict(self):
 
3104
        """A conflict is generated if the merge-into adds a file (or other
 
3105
        inventory entry) with a file-id that already exists in the target tree.
 
3106
        """
 
3107
        dest_wt = self.setup_simple_branch('dest', ['file.txt'])
 
3108
        # Make a second tree with a file-id that will clash with file.txt in
 
3109
        # dest.
 
3110
        src_wt = self.make_branch_and_tree('src')
 
3111
        self.build_tree(['src/README'])
 
3112
        src_wt.add(['README'], ids=['dest-file.txt-id'])
 
3113
        src_wt.commit("Rev 1 of src.", rev_id='r1-src')
 
3114
        conflicts = self.do_merge_into('src', 'dest/dir')
 
3115
        # This is an edge case that shouldn't happen to users very often.  So
 
3116
        # we don't care really about the exact presentation of the conflict,
 
3117
        # just that there is one.
 
3118
        self.assertEqual(1, conflicts)
 
3119
 
 
3120
    def test_only_subdir(self):
 
3121
        """When the location points to just part of a tree, merge just that
 
3122
        subtree.
 
3123
        """
 
3124
        dest_wt = self.setup_simple_branch('dest')
 
3125
        src_wt = self.setup_simple_branch(
 
3126
            'src', ['hello.txt', 'dir/', 'dir/foo.c'])
 
3127
        conflicts = self.do_merge_into('src/dir', 'dest/dir')
 
3128
        dest_wt.lock_read()
 
3129
        self.addCleanup(dest_wt.unlock)
 
3130
        # The r1-lib1 revision should NOT be merged into this one (this is a
 
3131
        # partial merge).
 
3132
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
 
3133
        self.assertTreeEntriesEqual(
 
3134
            [('', 'dest-root-id'),
 
3135
             ('dir', 'src-dir-id'),
 
3136
             ('dir/foo.c', 'src-foo.c-id'),
 
3137
            ], dest_wt)
 
3138
 
 
3139
    def test_only_file(self):
 
3140
        """An edge case: merge just one file, not a whole dir."""
 
3141
        dest_wt = self.setup_simple_branch('dest')
 
3142
        two_file_wt = self.setup_simple_branch(
 
3143
            'two-file', ['file1.txt', 'file2.txt'])
 
3144
        conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
 
3145
        dest_wt.lock_read()
 
3146
        self.addCleanup(dest_wt.unlock)
 
3147
        # The r1-lib1 revision should NOT be merged into this one
 
3148
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
 
3149
        self.assertTreeEntriesEqual(
 
3150
            [('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
 
3151
            dest_wt)
 
3152
 
 
3153
    def test_no_such_source_path(self):
 
3154
        """PathNotInTree is raised if the specified path in the source tree
 
3155
        does not exist.
 
3156
        """
 
3157
        dest_wt = self.setup_simple_branch('dest')
 
3158
        two_file_wt = self.setup_simple_branch('src', ['dir/'])
 
3159
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
 
3160
            'src/no-such-dir', 'dest/foo')
 
3161
        dest_wt.lock_read()
 
3162
        self.addCleanup(dest_wt.unlock)
 
3163
        # The dest tree is unmodified.
 
3164
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
 
3165
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
 
3166
 
 
3167
    def test_no_such_target_path(self):
 
3168
        """PathNotInTree is also raised if the specified path in the target
 
3169
        tree does not exist.
 
3170
        """
 
3171
        dest_wt = self.setup_simple_branch('dest')
 
3172
        two_file_wt = self.setup_simple_branch('src', ['file.txt'])
 
3173
        self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
 
3174
            'src', 'dest/no-such-dir/foo')
 
3175
        dest_wt.lock_read()
 
3176
        self.addCleanup(dest_wt.unlock)
 
3177
        # The dest tree is unmodified.
 
3178
        self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
 
3179
        self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)