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