2917
3008
conflicts = builder.merge()
2918
3009
# The hook should not call the merge_text() method
2919
3010
self.assertEqual([], self.calls)
3013
class TestMergeIntoBase(tests.TestCaseWithTransport):
3015
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3016
"""One commit, containing tree specified by optional shape.
3018
Default is empty tree (just root entry).
3021
root_id = '%s-root-id' % (relpath,)
3022
wt = self.make_branch_and_tree(relpath)
3023
wt.set_root_id(root_id)
3024
if shape is not None:
3025
adjusted_shape = [relpath + '/' + elem for elem in shape]
3026
self.build_tree(adjusted_shape)
3027
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3029
wt.add(shape, ids=ids)
3030
rev_id = 'r1-%s' % (relpath,)
3031
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3032
self.assertEqual(root_id, wt.path2id(''))
3035
def setup_two_branches(self, custom_root_ids=True):
3036
"""Setup 2 branches, one will be a library, the other a project."""
3040
root_id = inventory.ROOT_ID
3041
project_wt = self.setup_simple_branch(
3042
'project', ['README', 'dir/', 'dir/file.c'],
3044
lib_wt = self.setup_simple_branch(
3045
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3047
return project_wt, lib_wt
3049
def do_merge_into(self, location, merge_as):
3050
"""Helper for using MergeIntoMerger.
3052
:param location: location of directory to merge from, either the
3053
location of a branch or of a path inside a branch.
3054
:param merge_as: the path in a tree to add the new directory as.
3055
:returns: the conflicts from 'do_merge'.
3057
operation = cleanup.OperationWithCleanups(self._merge_into)
3058
return operation.run(location, merge_as)
3060
def _merge_into(self, op, location, merge_as):
3061
# Open and lock the various tree and branch objects
3062
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3063
op.add_cleanup(wt.lock_write().unlock)
3064
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3066
op.add_cleanup(branch_to_merge.lock_read().unlock)
3067
other_tree = branch_to_merge.basis_tree()
3068
op.add_cleanup(other_tree.lock_read().unlock)
3070
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3071
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3072
source_subpath=subdir_to_merge)
3073
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3074
conflicts = merger.do_merge()
3075
merger.set_pending()
3078
def assertTreeEntriesEqual(self, expected_entries, tree):
3079
"""Assert that 'tree' contains the expected inventory entries.
3081
:param expected_entries: sequence of (path, file-id) pairs.
3083
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3084
self.assertEqual(expected_entries, files)
3087
class TestMergeInto(TestMergeIntoBase):
3089
def test_newdir_with_unique_roots(self):
3090
"""Merge a branch with a unique root into a new directory."""
3091
project_wt, lib_wt = self.setup_two_branches()
3092
self.do_merge_into('lib1', 'project/lib1')
3093
project_wt.lock_read()
3094
self.addCleanup(project_wt.unlock)
3095
# The r1-lib1 revision should be merged into this one
3096
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3097
self.assertTreeEntriesEqual(
3098
[('', 'project-root-id'),
3099
('README', 'project-README-id'),
3100
('dir', 'project-dir-id'),
3101
('lib1', 'lib1-root-id'),
3102
('dir/file.c', 'project-file.c-id'),
3103
('lib1/Makefile', 'lib1-Makefile-id'),
3104
('lib1/README', 'lib1-README-id'),
3105
('lib1/foo.c', 'lib1-foo.c-id'),
3108
def test_subdir(self):
3109
"""Merge a branch into a subdirectory of an existing directory."""
3110
project_wt, lib_wt = self.setup_two_branches()
3111
self.do_merge_into('lib1', 'project/dir/lib1')
3112
project_wt.lock_read()
3113
self.addCleanup(project_wt.unlock)
3114
# The r1-lib1 revision should be merged into this one
3115
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3116
self.assertTreeEntriesEqual(
3117
[('', 'project-root-id'),
3118
('README', 'project-README-id'),
3119
('dir', 'project-dir-id'),
3120
('dir/file.c', 'project-file.c-id'),
3121
('dir/lib1', 'lib1-root-id'),
3122
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3123
('dir/lib1/README', 'lib1-README-id'),
3124
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3127
def test_newdir_with_repeat_roots(self):
3128
"""If the file-id of the dir to be merged already exists a new ID will
3129
be allocated to let the merge happen.
3131
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3132
root_id = project_wt.path2id('')
3133
self.do_merge_into('lib1', 'project/lib1')
3134
project_wt.lock_read()
3135
self.addCleanup(project_wt.unlock)
3136
# The r1-lib1 revision should be merged into this one
3137
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3138
new_lib1_id = project_wt.path2id('lib1')
3139
self.assertNotEqual(None, new_lib1_id)
3140
self.assertTreeEntriesEqual(
3142
('README', 'project-README-id'),
3143
('dir', 'project-dir-id'),
3144
('lib1', new_lib1_id),
3145
('dir/file.c', 'project-file.c-id'),
3146
('lib1/Makefile', 'lib1-Makefile-id'),
3147
('lib1/README', 'lib1-README-id'),
3148
('lib1/foo.c', 'lib1-foo.c-id'),
3151
def test_name_conflict(self):
3152
"""When the target directory name already exists a conflict is
3153
generated and the original directory is renamed to foo.moved.
3155
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3156
src_wt = self.setup_simple_branch('src', ['README'])
3157
conflicts = self.do_merge_into('src', 'dest/dir')
3158
self.assertEqual(1, conflicts)
3160
self.addCleanup(dest_wt.unlock)
3161
# The r1-lib1 revision should be merged into this one
3162
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3163
self.assertTreeEntriesEqual(
3164
[('', 'dest-root-id'),
3165
('dir', 'src-root-id'),
3166
('dir.moved', 'dest-dir-id'),
3167
('dir/README', 'src-README-id'),
3168
('dir.moved/file.txt', 'dest-file.txt-id'),
3171
def test_file_id_conflict(self):
3172
"""A conflict is generated if the merge-into adds a file (or other
3173
inventory entry) with a file-id that already exists in the target tree.
3175
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3176
# Make a second tree with a file-id that will clash with file.txt in
3178
src_wt = self.make_branch_and_tree('src')
3179
self.build_tree(['src/README'])
3180
src_wt.add(['README'], ids=['dest-file.txt-id'])
3181
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3182
conflicts = self.do_merge_into('src', 'dest/dir')
3183
# This is an edge case that shouldn't happen to users very often. So
3184
# we don't care really about the exact presentation of the conflict,
3185
# just that there is one.
3186
self.assertEqual(1, conflicts)
3188
def test_only_subdir(self):
3189
"""When the location points to just part of a tree, merge just that
3192
dest_wt = self.setup_simple_branch('dest')
3193
src_wt = self.setup_simple_branch(
3194
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3195
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3197
self.addCleanup(dest_wt.unlock)
3198
# The r1-lib1 revision should NOT be merged into this one (this is a
3200
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3201
self.assertTreeEntriesEqual(
3202
[('', 'dest-root-id'),
3203
('dir', 'src-dir-id'),
3204
('dir/foo.c', 'src-foo.c-id'),
3207
def test_only_file(self):
3208
"""An edge case: merge just one file, not a whole dir."""
3209
dest_wt = self.setup_simple_branch('dest')
3210
two_file_wt = self.setup_simple_branch(
3211
'two-file', ['file1.txt', 'file2.txt'])
3212
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3214
self.addCleanup(dest_wt.unlock)
3215
# The r1-lib1 revision should NOT be merged into this one
3216
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3217
self.assertTreeEntriesEqual(
3218
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3221
def test_no_such_source_path(self):
3222
"""PathNotInTree is raised if the specified path in the source tree
3225
dest_wt = self.setup_simple_branch('dest')
3226
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3227
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3228
'src/no-such-dir', 'dest/foo')
3230
self.addCleanup(dest_wt.unlock)
3231
# The dest tree is unmodified.
3232
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3233
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3235
def test_no_such_target_path(self):
3236
"""PathNotInTree is also raised if the specified path in the target
3237
tree does not exist.
3239
dest_wt = self.setup_simple_branch('dest')
3240
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3241
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3242
'src', 'dest/no-such-dir/foo')
3244
self.addCleanup(dest_wt.unlock)
3245
# The dest tree is unmodified.
3246
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3247
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3250
class TestMergeHooks(TestCaseWithTransport):
3253
super(TestMergeHooks, self).setUp()
3254
self.tree_a = self.make_branch_and_tree('tree_a')
3255
self.build_tree_contents([('tree_a/file', 'content_1')])
3256
self.tree_a.add('file', 'file-id')
3257
self.tree_a.commit('added file')
3259
self.tree_b = self.tree_a.bzrdir.sprout('tree_b').open_workingtree()
3260
self.build_tree_contents([('tree_b/file', 'content_2')])
3261
self.tree_b.commit('modify file')
3263
def test_pre_merge_hook_inject_different_tree(self):
3264
tree_c = self.tree_b.bzrdir.sprout('tree_c').open_workingtree()
3265
self.build_tree_contents([('tree_c/file', 'content_3')])
3266
tree_c.commit("more content")
3268
def factory(merger):
3269
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3270
merger.other_tree = tree_c
3271
calls.append(merger)
3272
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3273
factory, 'test factory')
3274
self.tree_a.merge_from_branch(self.tree_b.branch)
3276
self.assertFileEqual("content_3", 'tree_a/file')
3277
self.assertLength(1, calls)
3279
def test_post_merge_hook_called(self):
3281
def factory(merger):
3282
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3283
calls.append(merger)
3284
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3285
factory, 'test factory')
3287
self.tree_a.merge_from_branch(self.tree_b.branch)
3289
self.assertFileEqual("content_2", 'tree_a/file')
3290
self.assertLength(1, calls)