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