2940
2917
conflicts = builder.merge()
2941
2918
# The hook should not call the merge_text() method
2942
2919
self.assertEqual([], self.calls)
2945
class TestMergeIntoBase(tests.TestCaseWithTransport):
2947
def setup_simple_branch(self, relpath, shape=None, root_id=None):
2948
"""One commit, containing tree specified by optional shape.
2950
Default is empty tree (just root entry).
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('/')))
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(''))
2967
def setup_two_branches(self, custom_root_ids=True):
2968
"""Setup 2 branches, one will be a library, the other a project."""
2972
root_id = inventory.ROOT_ID
2973
project_wt = self.setup_simple_branch(
2974
'project', ['README', 'dir/', 'dir/file.c'],
2976
lib_wt = self.setup_simple_branch(
2977
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
2979
return project_wt, lib_wt
2981
def do_merge_into(self, location, merge_as):
2982
"""Helper for using MergeIntoMerger.
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'.
2989
operation = cleanup.OperationWithCleanups(self._merge_into)
2990
return operation.run(location, merge_as)
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(
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)
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()
3010
def assertTreeEntriesEqual(self, expected_entries, tree):
3011
"""Assert that 'tree' contains the expected inventory entries.
3013
:param expected_entries: sequence of (path, file-id) pairs.
3015
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3016
self.assertEqual(expected_entries, files)
3019
class TestMergeInto(TestMergeIntoBase):
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'),
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'),
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.
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(
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'),
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.
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)
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'),
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.
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
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)
3120
def test_only_subdir(self):
3121
"""When the location points to just part of a tree, merge just that
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')
3129
self.addCleanup(dest_wt.unlock)
3130
# The r1-lib1 revision should NOT be merged into this one (this is a
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'),
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')
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')],
3153
def test_no_such_source_path(self):
3154
"""PathNotInTree is raised if the specified path in the source tree
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')
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)
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.
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')
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)