2917
3014
conflicts = builder.merge()
2918
3015
# The hook should not call the merge_text() method
2919
3016
self.assertEqual([], self.calls)
3019
class TestMergeIntoBase(tests.TestCaseWithTransport):
3021
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3022
"""One commit, containing tree specified by optional shape.
3024
Default is empty tree (just root entry).
3027
root_id = '%s-root-id' % (relpath,)
3028
wt = self.make_branch_and_tree(relpath)
3029
wt.set_root_id(root_id)
3030
if shape is not None:
3031
adjusted_shape = [relpath + '/' + elem for elem in shape]
3032
self.build_tree(adjusted_shape)
3033
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3035
wt.add(shape, ids=ids)
3036
rev_id = 'r1-%s' % (relpath,)
3037
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3038
self.assertEqual(root_id, wt.path2id(''))
3041
def setup_two_branches(self, custom_root_ids=True):
3042
"""Setup 2 branches, one will be a library, the other a project."""
3046
root_id = inventory.ROOT_ID
3047
project_wt = self.setup_simple_branch(
3048
'project', ['README', 'dir/', 'dir/file.c'],
3050
lib_wt = self.setup_simple_branch(
3051
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3053
return project_wt, lib_wt
3055
def do_merge_into(self, location, merge_as):
3056
"""Helper for using MergeIntoMerger.
3058
:param location: location of directory to merge from, either the
3059
location of a branch or of a path inside a branch.
3060
:param merge_as: the path in a tree to add the new directory as.
3061
:returns: the conflicts from 'do_merge'.
3063
operation = cleanup.OperationWithCleanups(self._merge_into)
3064
return operation.run(location, merge_as)
3066
def _merge_into(self, op, location, merge_as):
3067
# Open and lock the various tree and branch objects
3068
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3069
op.add_cleanup(wt.lock_write().unlock)
3070
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3072
op.add_cleanup(branch_to_merge.lock_read().unlock)
3073
other_tree = branch_to_merge.basis_tree()
3074
op.add_cleanup(other_tree.lock_read().unlock)
3076
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3077
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3078
source_subpath=subdir_to_merge)
3079
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3080
conflicts = merger.do_merge()
3081
merger.set_pending()
3084
def assertTreeEntriesEqual(self, expected_entries, tree):
3085
"""Assert that 'tree' contains the expected inventory entries.
3087
:param expected_entries: sequence of (path, file-id) pairs.
3089
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3090
self.assertEqual(expected_entries, files)
3093
class TestMergeInto(TestMergeIntoBase):
3095
def test_newdir_with_unique_roots(self):
3096
"""Merge a branch with a unique root into a new directory."""
3097
project_wt, lib_wt = self.setup_two_branches()
3098
self.do_merge_into('lib1', 'project/lib1')
3099
project_wt.lock_read()
3100
self.addCleanup(project_wt.unlock)
3101
# The r1-lib1 revision should be merged into this one
3102
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3103
self.assertTreeEntriesEqual(
3104
[('', 'project-root-id'),
3105
('README', 'project-README-id'),
3106
('dir', 'project-dir-id'),
3107
('lib1', 'lib1-root-id'),
3108
('dir/file.c', 'project-file.c-id'),
3109
('lib1/Makefile', 'lib1-Makefile-id'),
3110
('lib1/README', 'lib1-README-id'),
3111
('lib1/foo.c', 'lib1-foo.c-id'),
3114
def test_subdir(self):
3115
"""Merge a branch into a subdirectory of an existing directory."""
3116
project_wt, lib_wt = self.setup_two_branches()
3117
self.do_merge_into('lib1', 'project/dir/lib1')
3118
project_wt.lock_read()
3119
self.addCleanup(project_wt.unlock)
3120
# The r1-lib1 revision should be merged into this one
3121
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3122
self.assertTreeEntriesEqual(
3123
[('', 'project-root-id'),
3124
('README', 'project-README-id'),
3125
('dir', 'project-dir-id'),
3126
('dir/file.c', 'project-file.c-id'),
3127
('dir/lib1', 'lib1-root-id'),
3128
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3129
('dir/lib1/README', 'lib1-README-id'),
3130
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3133
def test_newdir_with_repeat_roots(self):
3134
"""If the file-id of the dir to be merged already exists a new ID will
3135
be allocated to let the merge happen.
3137
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3138
root_id = project_wt.path2id('')
3139
self.do_merge_into('lib1', 'project/lib1')
3140
project_wt.lock_read()
3141
self.addCleanup(project_wt.unlock)
3142
# The r1-lib1 revision should be merged into this one
3143
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3144
new_lib1_id = project_wt.path2id('lib1')
3145
self.assertNotEqual(None, new_lib1_id)
3146
self.assertTreeEntriesEqual(
3148
('README', 'project-README-id'),
3149
('dir', 'project-dir-id'),
3150
('lib1', new_lib1_id),
3151
('dir/file.c', 'project-file.c-id'),
3152
('lib1/Makefile', 'lib1-Makefile-id'),
3153
('lib1/README', 'lib1-README-id'),
3154
('lib1/foo.c', 'lib1-foo.c-id'),
3157
def test_name_conflict(self):
3158
"""When the target directory name already exists a conflict is
3159
generated and the original directory is renamed to foo.moved.
3161
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3162
src_wt = self.setup_simple_branch('src', ['README'])
3163
conflicts = self.do_merge_into('src', 'dest/dir')
3164
self.assertEqual(1, conflicts)
3166
self.addCleanup(dest_wt.unlock)
3167
# The r1-lib1 revision should be merged into this one
3168
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3169
self.assertTreeEntriesEqual(
3170
[('', 'dest-root-id'),
3171
('dir', 'src-root-id'),
3172
('dir.moved', 'dest-dir-id'),
3173
('dir/README', 'src-README-id'),
3174
('dir.moved/file.txt', 'dest-file.txt-id'),
3177
def test_file_id_conflict(self):
3178
"""A conflict is generated if the merge-into adds a file (or other
3179
inventory entry) with a file-id that already exists in the target tree.
3181
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3182
# Make a second tree with a file-id that will clash with file.txt in
3184
src_wt = self.make_branch_and_tree('src')
3185
self.build_tree(['src/README'])
3186
src_wt.add(['README'], ids=['dest-file.txt-id'])
3187
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3188
conflicts = self.do_merge_into('src', 'dest/dir')
3189
# This is an edge case that shouldn't happen to users very often. So
3190
# we don't care really about the exact presentation of the conflict,
3191
# just that there is one.
3192
self.assertEqual(1, conflicts)
3194
def test_only_subdir(self):
3195
"""When the location points to just part of a tree, merge just that
3198
dest_wt = self.setup_simple_branch('dest')
3199
src_wt = self.setup_simple_branch(
3200
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3201
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3203
self.addCleanup(dest_wt.unlock)
3204
# The r1-lib1 revision should NOT be merged into this one (this is a
3206
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3207
self.assertTreeEntriesEqual(
3208
[('', 'dest-root-id'),
3209
('dir', 'src-dir-id'),
3210
('dir/foo.c', 'src-foo.c-id'),
3213
def test_only_file(self):
3214
"""An edge case: merge just one file, not a whole dir."""
3215
dest_wt = self.setup_simple_branch('dest')
3216
two_file_wt = self.setup_simple_branch(
3217
'two-file', ['file1.txt', 'file2.txt'])
3218
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3220
self.addCleanup(dest_wt.unlock)
3221
# The r1-lib1 revision should NOT be merged into this one
3222
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3223
self.assertTreeEntriesEqual(
3224
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3227
def test_no_such_source_path(self):
3228
"""PathNotInTree is raised if the specified path in the source tree
3231
dest_wt = self.setup_simple_branch('dest')
3232
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3233
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3234
'src/no-such-dir', 'dest/foo')
3236
self.addCleanup(dest_wt.unlock)
3237
# The dest tree is unmodified.
3238
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3239
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3241
def test_no_such_target_path(self):
3242
"""PathNotInTree is also raised if the specified path in the target
3243
tree does not exist.
3245
dest_wt = self.setup_simple_branch('dest')
3246
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3247
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3248
'src', 'dest/no-such-dir/foo')
3250
self.addCleanup(dest_wt.unlock)
3251
# The dest tree is unmodified.
3252
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3253
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)