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