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