1035
class TestMergeImplementation(object):
1037
def do_merge(self, target_tree, source_tree, **kwargs):
1038
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1039
target_tree, source_tree.last_revision(),
1040
other_branch=source_tree.branch)
1041
merger.merge_type=self.merge_type
1042
for name, value in kwargs.items():
1043
setattr(merger, name, value)
1046
def test_merge_specific_file(self):
1047
this_tree = self.make_branch_and_tree('this')
1048
this_tree.lock_write()
1049
self.addCleanup(this_tree.unlock)
1050
self.build_tree_contents([
1051
('this/file1', 'a\nb\n'),
1052
('this/file2', 'a\nb\n')
1054
this_tree.add(['file1', 'file2'])
1055
this_tree.commit('Added files')
1056
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1057
self.build_tree_contents([
1058
('other/file1', 'a\nb\nc\n'),
1059
('other/file2', 'a\nb\nc\n')
1061
other_tree.commit('modified both')
1062
self.build_tree_contents([
1063
('this/file1', 'd\na\nb\n'),
1064
('this/file2', 'd\na\nb\n')
1066
this_tree.commit('modified both')
1067
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
1068
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
1069
self.assertFileEqual('d\na\nb\n', 'this/file2')
1071
def test_merge_move_and_change(self):
1072
this_tree = self.make_branch_and_tree('this')
1073
this_tree.lock_write()
1074
self.addCleanup(this_tree.unlock)
1075
self.build_tree_contents([
1076
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
1078
this_tree.add('file1',)
1079
this_tree.commit('Added file')
1080
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1081
self.build_tree_contents([
1082
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
1084
other_tree.commit('Changed 2 to 2.1')
1085
self.build_tree_contents([
1086
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
1088
this_tree.commit('Swapped 2 & 3')
1089
self.do_merge(this_tree, other_tree)
1090
self.assertFileEqual('line 1\n'
1097
'>>>>>>> MERGE-SOURCE\n'
1098
'line 4\n', 'this/file1')
1100
def test_modify_conflicts_with_delete(self):
1101
# If one side deletes a line, and the other modifies that line, then
1102
# the modification should be considered a conflict
1103
builder = self.make_branch_builder('test')
1104
builder.start_series()
1105
builder.build_snapshot('BASE-id', None,
1106
[('add', ('', None, 'directory', None)),
1107
('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
1110
builder.build_snapshot('OTHER-id', ['BASE-id'],
1111
[('modify', ('foo-id', 'a\nc\nd\ne\n'))])
1112
# Modify 'b\n', add 'X\n'
1113
builder.build_snapshot('THIS-id', ['BASE-id'],
1114
[('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
1115
builder.finish_series()
1116
branch = builder.get_branch()
1117
this_tree = branch.bzrdir.create_workingtree()
1118
this_tree.lock_write()
1119
self.addCleanup(this_tree.unlock)
1120
other_tree = this_tree.bzrdir.sprout('other', 'OTHER-id').open_workingtree()
1121
self.do_merge(this_tree, other_tree)
1122
if self.merge_type is _mod_merge.LCAMerger:
1123
self.expectFailure("lca merge doesn't track deleted lines",
1124
self.assertFileEqual,
1129
'>>>>>>> MERGE-SOURCE\n'
1135
self.assertFileEqual(
1140
'>>>>>>> MERGE-SOURCE\n'
1146
def get_limbodir_deletiondir(self, wt):
1147
transform = TreeTransform(wt)
1148
limbodir = transform._limbodir
1149
deletiondir = transform._deletiondir
1150
transform.finalize()
1151
return (limbodir, deletiondir)
1153
def test_merge_with_existing_limbo(self):
1154
wt = self.make_branch_and_tree('this')
1155
(limbodir, deletiondir) = self.get_limbodir_deletiondir(wt)
1157
self.assertRaises(errors.ExistingLimbo, self.do_merge, wt, wt)
1158
self.assertRaises(errors.LockError, wt.unlock)
1160
def test_merge_with_pending_deletion(self):
1161
wt = self.make_branch_and_tree('this')
1162
(limbodir, deletiondir) = self.get_limbodir_deletiondir(wt)
1163
os.mkdir(deletiondir)
1164
self.assertRaises(errors.ExistingPendingDeletion, self.do_merge, wt, wt)
1165
self.assertRaises(errors.LockError, wt.unlock)
1168
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
1170
merge_type = _mod_merge.Merge3Merger
1173
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
1175
merge_type = _mod_merge.WeaveMerger
1178
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
1180
merge_type = _mod_merge.LCAMerger
1182
def test_merge_move_and_change(self):
1183
self.expectFailure("lca merge doesn't conflict for move and change",
1184
super(TestLCAMerge, self).test_merge_move_and_change)
1211
1187
class LoggingMerger(object):
1212
1188
# These seem to be the required attributes
1213
1189
requires_base = False
2922
2876
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2923
2877
self.assertLCAMultiWay('conflict',
2924
2878
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2927
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2930
super(TestConfigurableFileMerger, self).setUp()
2933
def get_merger_factory(self):
2934
# Allows the inner methods to access the test attributes
2937
class FooMerger(_mod_merge.ConfigurableFileMerger):
2939
default_files = ['bar']
2941
def merge_text(self, params):
2942
calls.append('merge_text')
2943
return ('not_applicable', None)
2945
def factory(merger):
2946
result = FooMerger(merger)
2947
# Make sure we start with a clean slate
2948
self.assertEqual(None, result.affected_files)
2949
# Track the original merger
2950
self.merger = result
2955
def _install_hook(self, factory):
2956
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2957
factory, 'test factory')
2959
def make_builder(self):
2960
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2961
self.addCleanup(builder.cleanup)
2964
def make_text_conflict(self, file_name='bar'):
2965
factory = self.get_merger_factory()
2966
self._install_hook(factory)
2967
builder = self.make_builder()
2968
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2969
builder.change_contents('bar-id', other='text4', this='text3')
2972
def make_kind_change(self):
2973
factory = self.get_merger_factory()
2974
self._install_hook(factory)
2975
builder = self.make_builder()
2976
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2978
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2979
base=False, other=False)
2982
def test_uses_this_branch(self):
2983
builder = self.make_text_conflict()
2984
tt = builder.make_preview_transform()
2985
self.addCleanup(tt.finalize)
2987
def test_affected_files_cached(self):
2988
"""Ensures that the config variable is cached"""
2989
builder = self.make_text_conflict()
2990
conflicts = builder.merge()
2991
# The hook should set the variable
2992
self.assertEqual(['bar'], self.merger.affected_files)
2993
self.assertEqual(1, len(conflicts))
2995
def test_hook_called_for_text_conflicts(self):
2996
builder = self.make_text_conflict()
2997
conflicts = builder.merge()
2998
# The hook should call the merge_text() method
2999
self.assertEqual(['merge_text'], self.calls)
3001
def test_hook_not_called_for_kind_change(self):
3002
builder = self.make_kind_change()
3003
conflicts = builder.merge()
3004
# The hook should not call the merge_text() method
3005
self.assertEqual([], self.calls)
3007
def test_hook_not_called_for_other_files(self):
3008
builder = self.make_text_conflict('foobar')
3009
conflicts = builder.merge()
3010
# The hook should not call the merge_text() method
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)