827
894
('unchanged', 'f\n'),
828
895
('unchanged', 'g\n')],
897
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
898
# This is one of the main differences between plan_merge and
899
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
900
# because 'x' was not present in one of the bases. However, in this
901
# case it is spurious because 'x' does not exist in the global base A.
903
('unchanged', 'h\n'),
904
('unchanged', 'a\n'),
905
('conflicted-a', 'x\n'),
907
('unchanged', 'c\n'),
908
('unchanged', 'd\n'),
909
('unchanged', 'y\n'),
910
('unchanged', 'f\n'),
911
('unchanged', 'g\n')],
914
def test_criss_cross_flip_flop(self):
915
# This is specificly trying to trigger problems when using limited
916
# ancestry and weaves. The ancestry graph looks like:
917
# XX unused ancestor, should not show up in the weave
921
# B C B & C both introduce a new line
925
# D E B & C are both merged, so both are common ancestors
926
# In the process of merging, both sides order the new
929
self.add_rev('root', 'XX', [], 'qrs')
930
self.add_rev('root', 'A', ['XX'], 'abcdef')
931
self.add_rev('root', 'B', ['A'], 'abcdgef')
932
self.add_rev('root', 'C', ['A'], 'abcdhef')
933
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
934
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
935
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
937
('unchanged', 'a\n'),
938
('unchanged', 'b\n'),
939
('unchanged', 'c\n'),
940
('unchanged', 'd\n'),
942
('unchanged', 'g\n'),
944
('unchanged', 'e\n'),
945
('unchanged', 'f\n'),
947
pwm = versionedfile.PlanWeaveMerge(plan)
948
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
949
''.join(pwm.base_from_plan()))
950
# Reversing the order reverses the merge plan, and final order of 'hg'
952
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
954
('unchanged', 'a\n'),
955
('unchanged', 'b\n'),
956
('unchanged', 'c\n'),
957
('unchanged', 'd\n'),
959
('unchanged', 'h\n'),
961
('unchanged', 'e\n'),
962
('unchanged', 'f\n'),
964
pwm = versionedfile.PlanWeaveMerge(plan)
965
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
966
''.join(pwm.base_from_plan()))
967
# This is where lca differs, in that it (fairly correctly) determines
968
# that there is a conflict because both sides resolved the merge
970
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
972
('unchanged', 'a\n'),
973
('unchanged', 'b\n'),
974
('unchanged', 'c\n'),
975
('unchanged', 'd\n'),
976
('conflicted-b', 'h\n'),
977
('unchanged', 'g\n'),
978
('conflicted-a', 'h\n'),
979
('unchanged', 'e\n'),
980
('unchanged', 'f\n'),
982
pwm = versionedfile.PlanWeaveMerge(plan)
983
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
984
''.join(pwm.base_from_plan()))
985
# Reversing it changes what line is doubled, but still gives a
987
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
989
('unchanged', 'a\n'),
990
('unchanged', 'b\n'),
991
('unchanged', 'c\n'),
992
('unchanged', 'd\n'),
993
('conflicted-b', 'g\n'),
994
('unchanged', 'h\n'),
995
('conflicted-a', 'g\n'),
996
('unchanged', 'e\n'),
997
('unchanged', 'f\n'),
999
pwm = versionedfile.PlanWeaveMerge(plan)
1000
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
1001
''.join(pwm.base_from_plan()))
831
1003
def assertRemoveExternalReferences(self, filtered_parent_map,
832
1004
child_map, tails, parent_map):
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)
1187
1207
class LoggingMerger(object):
1188
1208
# These seem to be the required attributes
1189
1209
requires_base = False
2876
2918
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2877
2919
self.assertLCAMultiWay('conflict',
2878
2920
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2923
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2926
super(TestConfigurableFileMerger, self).setUp()
2929
def get_merger_factory(self):
2930
# Allows the inner methods to access the test attributes
2933
class FooMerger(_mod_merge.ConfigurableFileMerger):
2935
default_files = ['bar']
2937
def merge_text(self, params):
2938
calls.append('merge_text')
2939
return ('not_applicable', None)
2941
def factory(merger):
2942
result = FooMerger(merger)
2943
# Make sure we start with a clean slate
2944
self.assertEqual(None, result.affected_files)
2945
# Track the original merger
2946
self.merger = result
2951
def _install_hook(self, factory):
2952
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2953
factory, 'test factory')
2955
def make_builder(self):
2956
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2957
self.addCleanup(builder.cleanup)
2960
def make_text_conflict(self, file_name='bar'):
2961
factory = self.get_merger_factory()
2962
self._install_hook(factory)
2963
builder = self.make_builder()
2964
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2965
builder.change_contents('bar-id', other='text4', this='text3')
2968
def make_kind_change(self):
2969
factory = self.get_merger_factory()
2970
self._install_hook(factory)
2971
builder = self.make_builder()
2972
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2974
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2975
base=False, other=False)
2978
def test_uses_this_branch(self):
2979
builder = self.make_text_conflict()
2980
tt = builder.make_preview_transform()
2981
self.addCleanup(tt.finalize)
2983
def test_affected_files_cached(self):
2984
"""Ensures that the config variable is cached"""
2985
builder = self.make_text_conflict()
2986
conflicts = builder.merge()
2987
# The hook should set the variable
2988
self.assertEqual(['bar'], self.merger.affected_files)
2989
self.assertEqual(1, len(conflicts))
2991
def test_hook_called_for_text_conflicts(self):
2992
builder = self.make_text_conflict()
2993
conflicts = builder.merge()
2994
# The hook should call the merge_text() method
2995
self.assertEqual(['merge_text'], self.calls)
2997
def test_hook_not_called_for_kind_change(self):
2998
builder = self.make_kind_change()
2999
conflicts = builder.merge()
3000
# The hook should not call the merge_text() method
3001
self.assertEqual([], self.calls)
3003
def test_hook_not_called_for_other_files(self):
3004
builder = self.make_text_conflict('foobar')
3005
conflicts = builder.merge()
3006
# The hook should not call the merge_text() method
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)