896
825
('unchanged', 'f\n'),
897
826
('unchanged', 'g\n')],
899
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
900
# This is one of the main differences between plan_merge and
901
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
902
# because 'x' was not present in one of the bases. However, in this
903
# case it is spurious because 'x' does not exist in the global base A.
905
('unchanged', 'h\n'),
906
('unchanged', 'a\n'),
907
('conflicted-a', 'x\n'),
909
('unchanged', 'c\n'),
910
('unchanged', 'd\n'),
911
('unchanged', 'y\n'),
912
('unchanged', 'f\n'),
913
('unchanged', 'g\n')],
916
def test_criss_cross_flip_flop(self):
917
# This is specificly trying to trigger problems when using limited
918
# ancestry and weaves. The ancestry graph looks like:
919
# XX unused ancestor, should not show up in the weave
923
# B C B & C both introduce a new line
927
# D E B & C are both merged, so both are common ancestors
928
# In the process of merging, both sides order the new
931
self.add_rev('root', 'XX', [], 'qrs')
932
self.add_rev('root', 'A', ['XX'], 'abcdef')
933
self.add_rev('root', 'B', ['A'], 'abcdgef')
934
self.add_rev('root', 'C', ['A'], 'abcdhef')
935
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
936
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
937
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
939
('unchanged', 'a\n'),
940
('unchanged', 'b\n'),
941
('unchanged', 'c\n'),
942
('unchanged', 'd\n'),
944
('unchanged', 'g\n'),
946
('unchanged', 'e\n'),
947
('unchanged', 'f\n'),
949
pwm = versionedfile.PlanWeaveMerge(plan)
950
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
951
''.join(pwm.base_from_plan()))
952
# Reversing the order reverses the merge plan, and final order of 'hg'
954
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
956
('unchanged', 'a\n'),
957
('unchanged', 'b\n'),
958
('unchanged', 'c\n'),
959
('unchanged', 'd\n'),
961
('unchanged', 'h\n'),
963
('unchanged', 'e\n'),
964
('unchanged', 'f\n'),
966
pwm = versionedfile.PlanWeaveMerge(plan)
967
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
968
''.join(pwm.base_from_plan()))
969
# This is where lca differs, in that it (fairly correctly) determines
970
# that there is a conflict because both sides resolved the merge
972
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
974
('unchanged', 'a\n'),
975
('unchanged', 'b\n'),
976
('unchanged', 'c\n'),
977
('unchanged', 'd\n'),
978
('conflicted-b', 'h\n'),
979
('unchanged', 'g\n'),
980
('conflicted-a', 'h\n'),
981
('unchanged', 'e\n'),
982
('unchanged', 'f\n'),
984
pwm = versionedfile.PlanWeaveMerge(plan)
985
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
986
''.join(pwm.base_from_plan()))
987
# Reversing it changes what line is doubled, but still gives a
989
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
991
('unchanged', 'a\n'),
992
('unchanged', 'b\n'),
993
('unchanged', 'c\n'),
994
('unchanged', 'd\n'),
995
('conflicted-b', 'g\n'),
996
('unchanged', 'h\n'),
997
('conflicted-a', 'g\n'),
998
('unchanged', 'e\n'),
999
('unchanged', 'f\n'),
1001
pwm = versionedfile.PlanWeaveMerge(plan)
1002
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
1003
''.join(pwm.base_from_plan()))
1005
829
def assertRemoveExternalReferences(self, filtered_parent_map,
1006
830
child_map, tails, parent_map):
1033
class TestMergeImplementation(object):
1035
def do_merge(self, target_tree, source_tree, **kwargs):
1036
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1037
target_tree, source_tree.last_revision(),
1038
other_branch=source_tree.branch)
1039
merger.merge_type=self.merge_type
1040
for name, value in kwargs.items():
1041
setattr(merger, name, value)
1044
def test_merge_specific_file(self):
1045
this_tree = self.make_branch_and_tree('this')
1046
this_tree.lock_write()
1047
self.addCleanup(this_tree.unlock)
1048
self.build_tree_contents([
1049
('this/file1', 'a\nb\n'),
1050
('this/file2', 'a\nb\n')
1052
this_tree.add(['file1', 'file2'])
1053
this_tree.commit('Added files')
1054
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1055
self.build_tree_contents([
1056
('other/file1', 'a\nb\nc\n'),
1057
('other/file2', 'a\nb\nc\n')
1059
other_tree.commit('modified both')
1060
self.build_tree_contents([
1061
('this/file1', 'd\na\nb\n'),
1062
('this/file2', 'd\na\nb\n')
1064
this_tree.commit('modified both')
1065
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
1066
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
1067
self.assertFileEqual('d\na\nb\n', 'this/file2')
1069
def test_merge_move_and_change(self):
1070
this_tree = self.make_branch_and_tree('this')
1071
this_tree.lock_write()
1072
self.addCleanup(this_tree.unlock)
1073
self.build_tree_contents([
1074
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
1076
this_tree.add('file1',)
1077
this_tree.commit('Added file')
1078
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1079
self.build_tree_contents([
1080
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
1082
other_tree.commit('Changed 2 to 2.1')
1083
self.build_tree_contents([
1084
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
1086
this_tree.commit('Swapped 2 & 3')
1087
self.do_merge(this_tree, other_tree)
1088
self.assertFileEqual('line 1\n'
1095
'>>>>>>> MERGE-SOURCE\n'
1096
'line 4\n', 'this/file1')
1098
def test_modify_conflicts_with_delete(self):
1099
# If one side deletes a line, and the other modifies that line, then
1100
# the modification should be considered a conflict
1101
builder = self.make_branch_builder('test')
1102
builder.start_series()
1103
builder.build_snapshot('BASE-id', None,
1104
[('add', ('', None, 'directory', None)),
1105
('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
1108
builder.build_snapshot('OTHER-id', ['BASE-id'],
1109
[('modify', ('foo-id', 'a\nc\nd\ne\n'))])
1110
# Modify 'b\n', add 'X\n'
1111
builder.build_snapshot('THIS-id', ['BASE-id'],
1112
[('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
1113
builder.finish_series()
1114
branch = builder.get_branch()
1115
this_tree = branch.bzrdir.create_workingtree()
1116
this_tree.lock_write()
1117
self.addCleanup(this_tree.unlock)
1118
other_tree = this_tree.bzrdir.sprout('other', 'OTHER-id').open_workingtree()
1119
self.do_merge(this_tree, other_tree)
1120
if self.merge_type is _mod_merge.LCAMerger:
1121
self.expectFailure("lca merge doesn't track deleted lines",
1122
self.assertFileEqual,
1127
'>>>>>>> MERGE-SOURCE\n'
1133
self.assertFileEqual(
1138
'>>>>>>> MERGE-SOURCE\n'
1145
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
1147
merge_type = _mod_merge.Merge3Merger
1150
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
1152
merge_type = _mod_merge.WeaveMerger
1155
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
1157
merge_type = _mod_merge.LCAMerger
1159
def test_merge_move_and_change(self):
1160
self.expectFailure("lca merge doesn't conflict for move and change",
1161
super(TestLCAMerge, self).test_merge_move_and_change)
1209
1164
class LoggingMerger(object):
1210
1165
# These seem to be the required attributes
1211
1166
requires_base = False
2920
2832
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2921
2833
self.assertLCAMultiWay('conflict',
2922
2834
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2925
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2928
super(TestConfigurableFileMerger, self).setUp()
2931
def get_merger_factory(self):
2932
# Allows the inner methods to access the test attributes
2935
class FooMerger(_mod_merge.ConfigurableFileMerger):
2937
default_files = ['bar']
2939
def merge_text(self, params):
2940
calls.append('merge_text')
2941
return ('not_applicable', None)
2943
def factory(merger):
2944
result = FooMerger(merger)
2945
# Make sure we start with a clean slate
2946
self.assertEqual(None, result.affected_files)
2947
# Track the original merger
2948
self.merger = result
2953
def _install_hook(self, factory):
2954
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2955
factory, 'test factory')
2957
def make_builder(self):
2958
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2959
self.addCleanup(builder.cleanup)
2962
def make_text_conflict(self, file_name='bar'):
2963
factory = self.get_merger_factory()
2964
self._install_hook(factory)
2965
builder = self.make_builder()
2966
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2967
builder.change_contents('bar-id', other='text4', this='text3')
2970
def make_kind_change(self):
2971
factory = self.get_merger_factory()
2972
self._install_hook(factory)
2973
builder = self.make_builder()
2974
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2976
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2977
base=False, other=False)
2980
def test_uses_this_branch(self):
2981
builder = self.make_text_conflict()
2982
tt = builder.make_preview_transform()
2983
self.addCleanup(tt.finalize)
2985
def test_affected_files_cached(self):
2986
"""Ensures that the config variable is cached"""
2987
builder = self.make_text_conflict()
2988
conflicts = builder.merge()
2989
# The hook should set the variable
2990
self.assertEqual(['bar'], self.merger.affected_files)
2991
self.assertEqual(1, len(conflicts))
2993
def test_hook_called_for_text_conflicts(self):
2994
builder = self.make_text_conflict()
2995
conflicts = builder.merge()
2996
# The hook should call the merge_text() method
2997
self.assertEqual(['merge_text'], self.calls)
2999
def test_hook_not_called_for_kind_change(self):
3000
builder = self.make_kind_change()
3001
conflicts = builder.merge()
3002
# The hook should not call the merge_text() method
3003
self.assertEqual([], self.calls)
3005
def test_hook_not_called_for_other_files(self):
3006
builder = self.make_text_conflict('foobar')
3007
conflicts = builder.merge()
3008
# The hook should not call the merge_text() method
3009
self.assertEqual([], self.calls)
3012
class TestMergeIntoBase(tests.TestCaseWithTransport):
3014
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3015
"""One commit, containing tree specified by optional shape.
3017
Default is empty tree (just root entry).
3020
root_id = '%s-root-id' % (relpath,)
3021
wt = self.make_branch_and_tree(relpath)
3022
wt.set_root_id(root_id)
3023
if shape is not None:
3024
adjusted_shape = [relpath + '/' + elem for elem in shape]
3025
self.build_tree(adjusted_shape)
3026
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3028
wt.add(shape, ids=ids)
3029
rev_id = 'r1-%s' % (relpath,)
3030
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3031
self.assertEqual(root_id, wt.path2id(''))
3034
def setup_two_branches(self, custom_root_ids=True):
3035
"""Setup 2 branches, one will be a library, the other a project."""
3039
root_id = inventory.ROOT_ID
3040
project_wt = self.setup_simple_branch(
3041
'project', ['README', 'dir/', 'dir/file.c'],
3043
lib_wt = self.setup_simple_branch(
3044
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3046
return project_wt, lib_wt
3048
def do_merge_into(self, location, merge_as):
3049
"""Helper for using MergeIntoMerger.
3051
:param location: location of directory to merge from, either the
3052
location of a branch or of a path inside a branch.
3053
:param merge_as: the path in a tree to add the new directory as.
3054
:returns: the conflicts from 'do_merge'.
3056
operation = cleanup.OperationWithCleanups(self._merge_into)
3057
return operation.run(location, merge_as)
3059
def _merge_into(self, op, location, merge_as):
3060
# Open and lock the various tree and branch objects
3061
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3062
op.add_cleanup(wt.lock_write().unlock)
3063
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3065
op.add_cleanup(branch_to_merge.lock_read().unlock)
3066
other_tree = branch_to_merge.basis_tree()
3067
op.add_cleanup(other_tree.lock_read().unlock)
3069
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3070
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3071
source_subpath=subdir_to_merge)
3072
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3073
conflicts = merger.do_merge()
3074
merger.set_pending()
3077
def assertTreeEntriesEqual(self, expected_entries, tree):
3078
"""Assert that 'tree' contains the expected inventory entries.
3080
:param expected_entries: sequence of (path, file-id) pairs.
3082
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3083
self.assertEqual(expected_entries, files)
3086
class TestMergeInto(TestMergeIntoBase):
3088
def test_newdir_with_unique_roots(self):
3089
"""Merge a branch with a unique root into a new directory."""
3090
project_wt, lib_wt = self.setup_two_branches()
3091
self.do_merge_into('lib1', 'project/lib1')
3092
project_wt.lock_read()
3093
self.addCleanup(project_wt.unlock)
3094
# The r1-lib1 revision should be merged into this one
3095
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3096
self.assertTreeEntriesEqual(
3097
[('', 'project-root-id'),
3098
('README', 'project-README-id'),
3099
('dir', 'project-dir-id'),
3100
('lib1', 'lib1-root-id'),
3101
('dir/file.c', 'project-file.c-id'),
3102
('lib1/Makefile', 'lib1-Makefile-id'),
3103
('lib1/README', 'lib1-README-id'),
3104
('lib1/foo.c', 'lib1-foo.c-id'),
3107
def test_subdir(self):
3108
"""Merge a branch into a subdirectory of an existing directory."""
3109
project_wt, lib_wt = self.setup_two_branches()
3110
self.do_merge_into('lib1', 'project/dir/lib1')
3111
project_wt.lock_read()
3112
self.addCleanup(project_wt.unlock)
3113
# The r1-lib1 revision should be merged into this one
3114
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3115
self.assertTreeEntriesEqual(
3116
[('', 'project-root-id'),
3117
('README', 'project-README-id'),
3118
('dir', 'project-dir-id'),
3119
('dir/file.c', 'project-file.c-id'),
3120
('dir/lib1', 'lib1-root-id'),
3121
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3122
('dir/lib1/README', 'lib1-README-id'),
3123
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3126
def test_newdir_with_repeat_roots(self):
3127
"""If the file-id of the dir to be merged already exists a new ID will
3128
be allocated to let the merge happen.
3130
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3131
root_id = project_wt.path2id('')
3132
self.do_merge_into('lib1', 'project/lib1')
3133
project_wt.lock_read()
3134
self.addCleanup(project_wt.unlock)
3135
# The r1-lib1 revision should be merged into this one
3136
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3137
new_lib1_id = project_wt.path2id('lib1')
3138
self.assertNotEqual(None, new_lib1_id)
3139
self.assertTreeEntriesEqual(
3141
('README', 'project-README-id'),
3142
('dir', 'project-dir-id'),
3143
('lib1', new_lib1_id),
3144
('dir/file.c', 'project-file.c-id'),
3145
('lib1/Makefile', 'lib1-Makefile-id'),
3146
('lib1/README', 'lib1-README-id'),
3147
('lib1/foo.c', 'lib1-foo.c-id'),
3150
def test_name_conflict(self):
3151
"""When the target directory name already exists a conflict is
3152
generated and the original directory is renamed to foo.moved.
3154
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3155
src_wt = self.setup_simple_branch('src', ['README'])
3156
conflicts = self.do_merge_into('src', 'dest/dir')
3157
self.assertEqual(1, conflicts)
3159
self.addCleanup(dest_wt.unlock)
3160
# The r1-lib1 revision should be merged into this one
3161
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3162
self.assertTreeEntriesEqual(
3163
[('', 'dest-root-id'),
3164
('dir', 'src-root-id'),
3165
('dir.moved', 'dest-dir-id'),
3166
('dir/README', 'src-README-id'),
3167
('dir.moved/file.txt', 'dest-file.txt-id'),
3170
def test_file_id_conflict(self):
3171
"""A conflict is generated if the merge-into adds a file (or other
3172
inventory entry) with a file-id that already exists in the target tree.
3174
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3175
# Make a second tree with a file-id that will clash with file.txt in
3177
src_wt = self.make_branch_and_tree('src')
3178
self.build_tree(['src/README'])
3179
src_wt.add(['README'], ids=['dest-file.txt-id'])
3180
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3181
conflicts = self.do_merge_into('src', 'dest/dir')
3182
# This is an edge case that shouldn't happen to users very often. So
3183
# we don't care really about the exact presentation of the conflict,
3184
# just that there is one.
3185
self.assertEqual(1, conflicts)
3187
def test_only_subdir(self):
3188
"""When the location points to just part of a tree, merge just that
3191
dest_wt = self.setup_simple_branch('dest')
3192
src_wt = self.setup_simple_branch(
3193
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3194
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3196
self.addCleanup(dest_wt.unlock)
3197
# The r1-lib1 revision should NOT be merged into this one (this is a
3199
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3200
self.assertTreeEntriesEqual(
3201
[('', 'dest-root-id'),
3202
('dir', 'src-dir-id'),
3203
('dir/foo.c', 'src-foo.c-id'),
3206
def test_only_file(self):
3207
"""An edge case: merge just one file, not a whole dir."""
3208
dest_wt = self.setup_simple_branch('dest')
3209
two_file_wt = self.setup_simple_branch(
3210
'two-file', ['file1.txt', 'file2.txt'])
3211
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3213
self.addCleanup(dest_wt.unlock)
3214
# The r1-lib1 revision should NOT be merged into this one
3215
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3216
self.assertTreeEntriesEqual(
3217
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3220
def test_no_such_source_path(self):
3221
"""PathNotInTree is raised if the specified path in the source tree
3224
dest_wt = self.setup_simple_branch('dest')
3225
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3226
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3227
'src/no-such-dir', 'dest/foo')
3229
self.addCleanup(dest_wt.unlock)
3230
# The dest tree is unmodified.
3231
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3232
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3234
def test_no_such_target_path(self):
3235
"""PathNotInTree is also raised if the specified path in the target
3236
tree does not exist.
3238
dest_wt = self.setup_simple_branch('dest')
3239
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3240
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3241
'src', 'dest/no-such-dir/foo')
3243
self.addCleanup(dest_wt.unlock)
3244
# The dest tree is unmodified.
3245
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3246
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)