2917
3016
conflicts = builder.merge()
2918
3017
# The hook should not call the merge_text() method
2919
3018
self.assertEqual([], self.calls)
3021
class TestMergeIntoBase(tests.TestCaseWithTransport):
3023
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3024
"""One commit, containing tree specified by optional shape.
3026
Default is empty tree (just root entry).
3029
root_id = '%s-root-id' % (relpath,)
3030
wt = self.make_branch_and_tree(relpath)
3031
wt.set_root_id(root_id)
3032
if shape is not None:
3033
adjusted_shape = [relpath + '/' + elem for elem in shape]
3034
self.build_tree(adjusted_shape)
3035
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3037
wt.add(shape, ids=ids)
3038
rev_id = 'r1-%s' % (relpath,)
3039
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3040
self.assertEqual(root_id, wt.path2id(''))
3043
def setup_two_branches(self, custom_root_ids=True):
3044
"""Setup 2 branches, one will be a library, the other a project."""
3048
root_id = inventory.ROOT_ID
3049
project_wt = self.setup_simple_branch(
3050
'project', ['README', 'dir/', 'dir/file.c'],
3052
lib_wt = self.setup_simple_branch(
3053
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3055
return project_wt, lib_wt
3057
def do_merge_into(self, location, merge_as):
3058
"""Helper for using MergeIntoMerger.
3060
:param location: location of directory to merge from, either the
3061
location of a branch or of a path inside a branch.
3062
:param merge_as: the path in a tree to add the new directory as.
3063
:returns: the conflicts from 'do_merge'.
3065
operation = cleanup.OperationWithCleanups(self._merge_into)
3066
return operation.run(location, merge_as)
3068
def _merge_into(self, op, location, merge_as):
3069
# Open and lock the various tree and branch objects
3070
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3071
op.add_cleanup(wt.lock_write().unlock)
3072
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3074
op.add_cleanup(branch_to_merge.lock_read().unlock)
3075
other_tree = branch_to_merge.basis_tree()
3076
op.add_cleanup(other_tree.lock_read().unlock)
3078
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3079
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3080
source_subpath=subdir_to_merge)
3081
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3082
conflicts = merger.do_merge()
3083
merger.set_pending()
3086
def assertTreeEntriesEqual(self, expected_entries, tree):
3087
"""Assert that 'tree' contains the expected inventory entries.
3089
:param expected_entries: sequence of (path, file-id) pairs.
3091
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3092
self.assertEqual(expected_entries, files)
3095
class TestMergeInto(TestMergeIntoBase):
3097
def test_newdir_with_unique_roots(self):
3098
"""Merge a branch with a unique root into a new directory."""
3099
project_wt, lib_wt = self.setup_two_branches()
3100
self.do_merge_into('lib1', 'project/lib1')
3101
project_wt.lock_read()
3102
self.addCleanup(project_wt.unlock)
3103
# The r1-lib1 revision should be merged into this one
3104
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3105
self.assertTreeEntriesEqual(
3106
[('', 'project-root-id'),
3107
('README', 'project-README-id'),
3108
('dir', 'project-dir-id'),
3109
('lib1', 'lib1-root-id'),
3110
('dir/file.c', 'project-file.c-id'),
3111
('lib1/Makefile', 'lib1-Makefile-id'),
3112
('lib1/README', 'lib1-README-id'),
3113
('lib1/foo.c', 'lib1-foo.c-id'),
3116
def test_subdir(self):
3117
"""Merge a branch into a subdirectory of an existing directory."""
3118
project_wt, lib_wt = self.setup_two_branches()
3119
self.do_merge_into('lib1', 'project/dir/lib1')
3120
project_wt.lock_read()
3121
self.addCleanup(project_wt.unlock)
3122
# The r1-lib1 revision should be merged into this one
3123
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3124
self.assertTreeEntriesEqual(
3125
[('', 'project-root-id'),
3126
('README', 'project-README-id'),
3127
('dir', 'project-dir-id'),
3128
('dir/file.c', 'project-file.c-id'),
3129
('dir/lib1', 'lib1-root-id'),
3130
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3131
('dir/lib1/README', 'lib1-README-id'),
3132
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3135
def test_newdir_with_repeat_roots(self):
3136
"""If the file-id of the dir to be merged already exists a new ID will
3137
be allocated to let the merge happen.
3139
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3140
root_id = project_wt.path2id('')
3141
self.do_merge_into('lib1', 'project/lib1')
3142
project_wt.lock_read()
3143
self.addCleanup(project_wt.unlock)
3144
# The r1-lib1 revision should be merged into this one
3145
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3146
new_lib1_id = project_wt.path2id('lib1')
3147
self.assertNotEqual(None, new_lib1_id)
3148
self.assertTreeEntriesEqual(
3150
('README', 'project-README-id'),
3151
('dir', 'project-dir-id'),
3152
('lib1', new_lib1_id),
3153
('dir/file.c', 'project-file.c-id'),
3154
('lib1/Makefile', 'lib1-Makefile-id'),
3155
('lib1/README', 'lib1-README-id'),
3156
('lib1/foo.c', 'lib1-foo.c-id'),
3159
def test_name_conflict(self):
3160
"""When the target directory name already exists a conflict is
3161
generated and the original directory is renamed to foo.moved.
3163
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3164
src_wt = self.setup_simple_branch('src', ['README'])
3165
conflicts = self.do_merge_into('src', 'dest/dir')
3166
self.assertEqual(1, conflicts)
3168
self.addCleanup(dest_wt.unlock)
3169
# The r1-lib1 revision should be merged into this one
3170
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3171
self.assertTreeEntriesEqual(
3172
[('', 'dest-root-id'),
3173
('dir', 'src-root-id'),
3174
('dir.moved', 'dest-dir-id'),
3175
('dir/README', 'src-README-id'),
3176
('dir.moved/file.txt', 'dest-file.txt-id'),
3179
def test_file_id_conflict(self):
3180
"""A conflict is generated if the merge-into adds a file (or other
3181
inventory entry) with a file-id that already exists in the target tree.
3183
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3184
# Make a second tree with a file-id that will clash with file.txt in
3186
src_wt = self.make_branch_and_tree('src')
3187
self.build_tree(['src/README'])
3188
src_wt.add(['README'], ids=['dest-file.txt-id'])
3189
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3190
conflicts = self.do_merge_into('src', 'dest/dir')
3191
# This is an edge case that shouldn't happen to users very often. So
3192
# we don't care really about the exact presentation of the conflict,
3193
# just that there is one.
3194
self.assertEqual(1, conflicts)
3196
def test_only_subdir(self):
3197
"""When the location points to just part of a tree, merge just that
3200
dest_wt = self.setup_simple_branch('dest')
3201
src_wt = self.setup_simple_branch(
3202
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3203
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3205
self.addCleanup(dest_wt.unlock)
3206
# The r1-lib1 revision should NOT be merged into this one (this is a
3208
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3209
self.assertTreeEntriesEqual(
3210
[('', 'dest-root-id'),
3211
('dir', 'src-dir-id'),
3212
('dir/foo.c', 'src-foo.c-id'),
3215
def test_only_file(self):
3216
"""An edge case: merge just one file, not a whole dir."""
3217
dest_wt = self.setup_simple_branch('dest')
3218
two_file_wt = self.setup_simple_branch(
3219
'two-file', ['file1.txt', 'file2.txt'])
3220
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3222
self.addCleanup(dest_wt.unlock)
3223
# The r1-lib1 revision should NOT be merged into this one
3224
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3225
self.assertTreeEntriesEqual(
3226
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3229
def test_no_such_source_path(self):
3230
"""PathNotInTree is raised if the specified path in the source tree
3233
dest_wt = self.setup_simple_branch('dest')
3234
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3235
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3236
'src/no-such-dir', 'dest/foo')
3238
self.addCleanup(dest_wt.unlock)
3239
# The dest tree is unmodified.
3240
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3241
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3243
def test_no_such_target_path(self):
3244
"""PathNotInTree is also raised if the specified path in the target
3245
tree does not exist.
3247
dest_wt = self.setup_simple_branch('dest')
3248
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3249
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3250
'src', 'dest/no-such-dir/foo')
3252
self.addCleanup(dest_wt.unlock)
3253
# The dest tree is unmodified.
3254
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3255
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)