1147
class TestMergeImplementation(object):
1149
def do_merge(self, target_tree, source_tree, **kwargs):
1150
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1151
target_tree, source_tree.last_revision(),
1152
other_branch=source_tree.branch)
1153
merger.merge_type=self.merge_type
1154
for name, value in kwargs.items():
1155
setattr(merger, name, value)
1158
def test_merge_specific_file(self):
1159
this_tree = self.make_branch_and_tree('this')
1160
this_tree.lock_write()
1161
self.addCleanup(this_tree.unlock)
1162
self.build_tree_contents([
1163
('this/file1', 'a\nb\n'),
1164
('this/file2', 'a\nb\n')
1166
this_tree.add(['file1', 'file2'])
1167
this_tree.commit('Added files')
1168
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1169
self.build_tree_contents([
1170
('other/file1', 'a\nb\nc\n'),
1171
('other/file2', 'a\nb\nc\n')
1173
other_tree.commit('modified both')
1174
self.build_tree_contents([
1175
('this/file1', 'd\na\nb\n'),
1176
('this/file2', 'd\na\nb\n')
1178
this_tree.commit('modified both')
1179
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
1180
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
1181
self.assertFileEqual('d\na\nb\n', 'this/file2')
1183
def test_merge_move_and_change(self):
1184
this_tree = self.make_branch_and_tree('this')
1185
this_tree.lock_write()
1186
self.addCleanup(this_tree.unlock)
1187
self.build_tree_contents([
1188
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
1190
this_tree.add('file1',)
1191
this_tree.commit('Added file')
1192
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1193
self.build_tree_contents([
1194
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
1196
other_tree.commit('Changed 2 to 2.1')
1197
self.build_tree_contents([
1198
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
1200
this_tree.commit('Swapped 2 & 3')
1201
self.do_merge(this_tree, other_tree)
1202
self.assertFileEqual('line 1\n'
1209
'>>>>>>> MERGE-SOURCE\n'
1210
'line 4\n', 'this/file1')
1212
def test_modify_conflicts_with_delete(self):
1213
# If one side deletes a line, and the other modifies that line, then
1214
# the modification should be considered a conflict
1215
builder = self.make_branch_builder('test')
1216
builder.start_series()
1217
builder.build_snapshot('BASE-id', None,
1218
[('add', ('', None, 'directory', None)),
1219
('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
1222
builder.build_snapshot('OTHER-id', ['BASE-id'],
1223
[('modify', ('foo-id', 'a\nc\nd\ne\n'))])
1224
# Modify 'b\n', add 'X\n'
1225
builder.build_snapshot('THIS-id', ['BASE-id'],
1226
[('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
1227
builder.finish_series()
1228
branch = builder.get_branch()
1229
this_tree = branch.bzrdir.create_workingtree()
1230
this_tree.lock_write()
1231
self.addCleanup(this_tree.unlock)
1232
other_tree = this_tree.bzrdir.sprout('other', 'OTHER-id').open_workingtree()
1233
self.do_merge(this_tree, other_tree)
1234
if self.merge_type is _mod_merge.LCAMerger:
1235
self.expectFailure("lca merge doesn't track deleted lines",
1236
self.assertFileEqual,
1241
'>>>>>>> MERGE-SOURCE\n'
1247
self.assertFileEqual(
1252
'>>>>>>> MERGE-SOURCE\n'
1258
def get_limbodir_deletiondir(self, wt):
1259
transform = TreeTransform(wt)
1260
limbodir = transform._limbodir
1261
deletiondir = transform._deletiondir
1262
transform.finalize()
1263
return (limbodir, deletiondir)
1265
def test_merge_with_existing_limbo(self):
1266
wt = self.make_branch_and_tree('this')
1267
(limbodir, deletiondir) = self.get_limbodir_deletiondir(wt)
1269
self.assertRaises(errors.ExistingLimbo, self.do_merge, wt, wt)
1270
self.assertRaises(errors.LockError, wt.unlock)
1272
def test_merge_with_pending_deletion(self):
1273
wt = self.make_branch_and_tree('this')
1274
(limbodir, deletiondir) = self.get_limbodir_deletiondir(wt)
1275
os.mkdir(deletiondir)
1276
self.assertRaises(errors.ExistingPendingDeletion, self.do_merge, wt, wt)
1277
self.assertRaises(errors.LockError, wt.unlock)
1280
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
1282
merge_type = _mod_merge.Merge3Merger
1285
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
1287
merge_type = _mod_merge.WeaveMerger
1290
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
1292
merge_type = _mod_merge.LCAMerger
1294
def test_merge_move_and_change(self):
1295
self.expectFailure("lca merge doesn't conflict for move and change",
1296
super(TestLCAMerge, self).test_merge_move_and_change)
1144
1299
class LoggingMerger(object):
1145
1300
# These seem to be the required attributes
1146
1301
requires_base = False
2833
2988
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2834
2989
self.assertLCAMultiWay('conflict',
2835
2990
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2838
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2841
super(TestConfigurableFileMerger, self).setUp()
2844
def get_merger_factory(self):
2845
# Allows the inner methods to access the test attributes
2848
class FooMerger(_mod_merge.ConfigurableFileMerger):
2850
default_files = ['bar']
2852
def merge_text(self, params):
2853
test.calls.append('merge_text')
2854
return ('not_applicable', None)
2856
def factory(merger):
2857
result = FooMerger(merger)
2858
# Make sure we start with a clean slate
2859
self.assertEqual(None, result.affected_files)
2860
# Track the original merger
2861
self.merger = result
2866
def _install_hook(self, factory):
2867
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2868
factory, 'test factory')
2870
def make_builder(self):
2871
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2872
self.addCleanup(builder.cleanup)
2875
def make_text_conflict(self, file_name='bar'):
2876
factory = self.get_merger_factory()
2877
self._install_hook(factory)
2878
builder = self.make_builder()
2879
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2880
builder.change_contents('bar-id', other='text4', this='text3')
2883
def make_kind_change(self):
2884
factory = self.get_merger_factory()
2885
self._install_hook(factory)
2886
builder = self.make_builder()
2887
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2889
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2890
base=False, other=False)
2893
def test_uses_this_branch(self):
2894
builder = self.make_text_conflict()
2895
tt = builder.make_preview_transform()
2896
self.addCleanup(tt.finalize)
2898
def test_affected_files_cached(self):
2899
"""Ensures that the config variable is cached"""
2900
builder = self.make_text_conflict()
2901
conflicts = builder.merge()
2902
# The hook should set the variable
2903
self.assertEqual(['bar'], self.merger.affected_files)
2904
self.assertEqual(1, len(conflicts))
2906
def test_hook_called_for_text_conflicts(self):
2907
builder = self.make_text_conflict()
2908
conflicts = builder.merge()
2909
# The hook should call the merge_text() method
2910
self.assertEqual(['merge_text'], self.calls)
2912
def test_hook_not_called_for_kind_change(self):
2913
builder = self.make_kind_change()
2914
conflicts = builder.merge()
2915
# The hook should not call the merge_text() method
2916
self.assertEqual([], self.calls)
2918
def test_hook_not_called_for_other_files(self):
2919
builder = self.make_text_conflict('foobar')
2920
conflicts = builder.merge()
2921
# The hook should not call the merge_text() method
2922
self.assertEqual([], self.calls)
2925
class TestMergeIntoBase(tests.TestCaseWithTransport):
2927
def setup_simple_branch(self, relpath, shape=None, root_id=None):
2928
"""One commit, containing tree specified by optional shape.
2930
Default is empty tree (just root entry).
2933
root_id = '%s-root-id' % (relpath,)
2934
wt = self.make_branch_and_tree(relpath)
2935
wt.set_root_id(root_id)
2936
if shape is not None:
2937
adjusted_shape = [relpath + '/' + elem for elem in shape]
2938
self.build_tree(adjusted_shape)
2939
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
2941
wt.add(shape, ids=ids)
2942
rev_id = 'r1-%s' % (relpath,)
2943
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
2944
self.assertEqual(root_id, wt.path2id(''))
2947
def setup_two_branches(self, custom_root_ids=True):
2948
"""Setup 2 branches, one will be a library, the other a project."""
2952
root_id = inventory.ROOT_ID
2953
project_wt = self.setup_simple_branch(
2954
'project', ['README', 'dir/', 'dir/file.c'],
2956
lib_wt = self.setup_simple_branch(
2957
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
2959
return project_wt, lib_wt
2961
def do_merge_into(self, location, merge_as):
2962
"""Helper for using MergeIntoMerger.
2964
:param location: location of directory to merge from, either the
2965
location of a branch or of a path inside a branch.
2966
:param merge_as: the path in a tree to add the new directory as.
2967
:returns: the conflicts from 'do_merge'.
2969
operation = cleanup.OperationWithCleanups(self._merge_into)
2970
return operation.run(location, merge_as)
2972
def _merge_into(self, op, location, merge_as):
2973
# Open and lock the various tree and branch objects
2974
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
2975
op.add_cleanup(wt.lock_write().unlock)
2976
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
2978
op.add_cleanup(branch_to_merge.lock_read().unlock)
2979
other_tree = branch_to_merge.basis_tree()
2980
op.add_cleanup(other_tree.lock_read().unlock)
2982
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
2983
other_branch=branch_to_merge, target_subdir=subdir_relpath,
2984
source_subpath=subdir_to_merge)
2985
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
2986
conflicts = merger.do_merge()
2987
merger.set_pending()
2990
def assertTreeEntriesEqual(self, expected_entries, tree):
2991
"""Assert that 'tree' contains the expected inventory entries.
2993
:param expected_entries: sequence of (path, file-id) pairs.
2995
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
2996
self.assertEqual(expected_entries, files)
2999
class TestMergeInto(TestMergeIntoBase):
3001
def test_newdir_with_unique_roots(self):
3002
"""Merge a branch with a unique root into a new directory."""
3003
project_wt, lib_wt = self.setup_two_branches()
3004
self.do_merge_into('lib1', 'project/lib1')
3005
project_wt.lock_read()
3006
self.addCleanup(project_wt.unlock)
3007
# The r1-lib1 revision should be merged into this one
3008
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3009
self.assertTreeEntriesEqual(
3010
[('', 'project-root-id'),
3011
('README', 'project-README-id'),
3012
('dir', 'project-dir-id'),
3013
('lib1', 'lib1-root-id'),
3014
('dir/file.c', 'project-file.c-id'),
3015
('lib1/Makefile', 'lib1-Makefile-id'),
3016
('lib1/README', 'lib1-README-id'),
3017
('lib1/foo.c', 'lib1-foo.c-id'),
3020
def test_subdir(self):
3021
"""Merge a branch into a subdirectory of an existing directory."""
3022
project_wt, lib_wt = self.setup_two_branches()
3023
self.do_merge_into('lib1', 'project/dir/lib1')
3024
project_wt.lock_read()
3025
self.addCleanup(project_wt.unlock)
3026
# The r1-lib1 revision should be merged into this one
3027
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3028
self.assertTreeEntriesEqual(
3029
[('', 'project-root-id'),
3030
('README', 'project-README-id'),
3031
('dir', 'project-dir-id'),
3032
('dir/file.c', 'project-file.c-id'),
3033
('dir/lib1', 'lib1-root-id'),
3034
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3035
('dir/lib1/README', 'lib1-README-id'),
3036
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3039
def test_newdir_with_repeat_roots(self):
3040
"""If the file-id of the dir to be merged already exists a new ID will
3041
be allocated to let the merge happen.
3043
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3044
root_id = project_wt.path2id('')
3045
self.do_merge_into('lib1', 'project/lib1')
3046
project_wt.lock_read()
3047
self.addCleanup(project_wt.unlock)
3048
# The r1-lib1 revision should be merged into this one
3049
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3050
new_lib1_id = project_wt.path2id('lib1')
3051
self.assertNotEqual(None, new_lib1_id)
3052
self.assertTreeEntriesEqual(
3054
('README', 'project-README-id'),
3055
('dir', 'project-dir-id'),
3056
('lib1', new_lib1_id),
3057
('dir/file.c', 'project-file.c-id'),
3058
('lib1/Makefile', 'lib1-Makefile-id'),
3059
('lib1/README', 'lib1-README-id'),
3060
('lib1/foo.c', 'lib1-foo.c-id'),
3063
def test_name_conflict(self):
3064
"""When the target directory name already exists a conflict is
3065
generated and the original directory is renamed to foo.moved.
3067
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3068
src_wt = self.setup_simple_branch('src', ['README'])
3069
conflicts = self.do_merge_into('src', 'dest/dir')
3070
self.assertEqual(1, conflicts)
3072
self.addCleanup(dest_wt.unlock)
3073
# The r1-lib1 revision should be merged into this one
3074
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3075
self.assertTreeEntriesEqual(
3076
[('', 'dest-root-id'),
3077
('dir', 'src-root-id'),
3078
('dir.moved', 'dest-dir-id'),
3079
('dir/README', 'src-README-id'),
3080
('dir.moved/file.txt', 'dest-file.txt-id'),
3083
def test_file_id_conflict(self):
3084
"""A conflict is generated if the merge-into adds a file (or other
3085
inventory entry) with a file-id that already exists in the target tree.
3087
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3088
# Make a second tree with a file-id that will clash with file.txt in
3090
src_wt = self.make_branch_and_tree('src')
3091
self.build_tree(['src/README'])
3092
src_wt.add(['README'], ids=['dest-file.txt-id'])
3093
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3094
conflicts = self.do_merge_into('src', 'dest/dir')
3095
# This is an edge case that shouldn't happen to users very often. So
3096
# we don't care really about the exact presentation of the conflict,
3097
# just that there is one.
3098
self.assertEqual(1, conflicts)
3100
def test_only_subdir(self):
3101
"""When the location points to just part of a tree, merge just that
3104
dest_wt = self.setup_simple_branch('dest')
3105
src_wt = self.setup_simple_branch(
3106
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3107
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3109
self.addCleanup(dest_wt.unlock)
3110
# The r1-lib1 revision should NOT be merged into this one (this is a
3112
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3113
self.assertTreeEntriesEqual(
3114
[('', 'dest-root-id'),
3115
('dir', 'src-dir-id'),
3116
('dir/foo.c', 'src-foo.c-id'),
3119
def test_only_file(self):
3120
"""An edge case: merge just one file, not a whole dir."""
3121
dest_wt = self.setup_simple_branch('dest')
3122
two_file_wt = self.setup_simple_branch(
3123
'two-file', ['file1.txt', 'file2.txt'])
3124
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3126
self.addCleanup(dest_wt.unlock)
3127
# The r1-lib1 revision should NOT be merged into this one
3128
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3129
self.assertTreeEntriesEqual(
3130
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3133
def test_no_such_source_path(self):
3134
"""PathNotInTree is raised if the specified path in the source tree
3137
dest_wt = self.setup_simple_branch('dest')
3138
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3139
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3140
'src/no-such-dir', 'dest/foo')
3142
self.addCleanup(dest_wt.unlock)
3143
# The dest tree is unmodified.
3144
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3145
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3147
def test_no_such_target_path(self):
3148
"""PathNotInTree is also raised if the specified path in the target
3149
tree does not exist.
3151
dest_wt = self.setup_simple_branch('dest')
3152
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3153
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3154
'src', 'dest/no-such-dir/foo')
3156
self.addCleanup(dest_wt.unlock)
3157
# The dest tree is unmodified.
3158
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3159
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)