883
825
('unchanged', 'f\n'),
884
826
('unchanged', 'g\n')],
886
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
887
# This is one of the main differences between plan_merge and
888
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
889
# because 'x' was not present in one of the bases. However, in this
890
# case it is spurious because 'x' does not exist in the global base A.
892
('unchanged', 'h\n'),
893
('unchanged', 'a\n'),
894
('conflicted-a', 'x\n'),
896
('unchanged', 'c\n'),
897
('unchanged', 'd\n'),
898
('unchanged', 'y\n'),
899
('unchanged', 'f\n'),
900
('unchanged', 'g\n')],
903
def test_criss_cross_flip_flop(self):
904
# This is specificly trying to trigger problems when using limited
905
# ancestry and weaves. The ancestry graph looks like:
906
# XX unused ancestor, should not show up in the weave
910
# B C B & C both introduce a new line
914
# D E B & C are both merged, so both are common ancestors
915
# In the process of merging, both sides order the new
918
self.add_rev('root', 'XX', [], 'qrs')
919
self.add_rev('root', 'A', ['XX'], 'abcdef')
920
self.add_rev('root', 'B', ['A'], 'abcdgef')
921
self.add_rev('root', 'C', ['A'], 'abcdhef')
922
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
923
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
924
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
926
('unchanged', 'a\n'),
927
('unchanged', 'b\n'),
928
('unchanged', 'c\n'),
929
('unchanged', 'd\n'),
931
('unchanged', 'g\n'),
933
('unchanged', 'e\n'),
934
('unchanged', 'f\n'),
936
pwm = versionedfile.PlanWeaveMerge(plan)
937
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
938
''.join(pwm.base_from_plan()))
939
# Reversing the order reverses the merge plan, and final order of 'hg'
941
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
943
('unchanged', 'a\n'),
944
('unchanged', 'b\n'),
945
('unchanged', 'c\n'),
946
('unchanged', 'd\n'),
948
('unchanged', 'h\n'),
950
('unchanged', 'e\n'),
951
('unchanged', 'f\n'),
953
pwm = versionedfile.PlanWeaveMerge(plan)
954
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
955
''.join(pwm.base_from_plan()))
956
# This is where lca differs, in that it (fairly correctly) determines
957
# that there is a conflict because both sides resolved the merge
959
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
961
('unchanged', 'a\n'),
962
('unchanged', 'b\n'),
963
('unchanged', 'c\n'),
964
('unchanged', 'd\n'),
965
('conflicted-b', 'h\n'),
966
('unchanged', 'g\n'),
967
('conflicted-a', 'h\n'),
968
('unchanged', 'e\n'),
969
('unchanged', 'f\n'),
971
pwm = versionedfile.PlanWeaveMerge(plan)
972
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
973
''.join(pwm.base_from_plan()))
974
# Reversing it changes what line is doubled, but still gives a
976
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
978
('unchanged', 'a\n'),
979
('unchanged', 'b\n'),
980
('unchanged', 'c\n'),
981
('unchanged', 'd\n'),
982
('conflicted-b', 'g\n'),
983
('unchanged', 'h\n'),
984
('conflicted-a', 'g\n'),
985
('unchanged', 'e\n'),
986
('unchanged', 'f\n'),
988
pwm = versionedfile.PlanWeaveMerge(plan)
989
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
990
''.join(pwm.base_from_plan()))
992
829
def assertRemoveExternalReferences(self, filtered_parent_map,
993
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)
1196
1164
class LoggingMerger(object):
1197
1165
# These seem to be the required attributes
1198
1166
requires_base = False
2905
2832
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2906
2833
self.assertLCAMultiWay('conflict',
2907
2834
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2910
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2913
super(TestConfigurableFileMerger, self).setUp()
2916
def get_merger_factory(self):
2917
# Allows the inner methods to access the test attributes
2920
class FooMerger(_mod_merge.ConfigurableFileMerger):
2922
default_files = ['bar']
2924
def merge_text(self, params):
2925
calls.append('merge_text')
2926
return ('not_applicable', None)
2928
def factory(merger):
2929
result = FooMerger(merger)
2930
# Make sure we start with a clean slate
2931
self.assertEqual(None, result.affected_files)
2932
# Track the original merger
2933
self.merger = result
2938
def _install_hook(self, factory):
2939
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2940
factory, 'test factory')
2942
def make_builder(self):
2943
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2944
self.addCleanup(builder.cleanup)
2947
def make_text_conflict(self, file_name='bar'):
2948
factory = self.get_merger_factory()
2949
self._install_hook(factory)
2950
builder = self.make_builder()
2951
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2952
builder.change_contents('bar-id', other='text4', this='text3')
2955
def make_kind_change(self):
2956
factory = self.get_merger_factory()
2957
self._install_hook(factory)
2958
builder = self.make_builder()
2959
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2961
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2962
base=False, other=False)
2965
def test_uses_this_branch(self):
2966
builder = self.make_text_conflict()
2967
tt = builder.make_preview_transform()
2968
self.addCleanup(tt.finalize)
2970
def test_affected_files_cached(self):
2971
"""Ensures that the config variable is cached"""
2972
builder = self.make_text_conflict()
2973
conflicts = builder.merge()
2974
# The hook should set the variable
2975
self.assertEqual(['bar'], self.merger.affected_files)
2976
self.assertEqual(1, len(conflicts))
2978
def test_hook_called_for_text_conflicts(self):
2979
builder = self.make_text_conflict()
2980
conflicts = builder.merge()
2981
# The hook should call the merge_text() method
2982
self.assertEqual(['merge_text'], self.calls)
2984
def test_hook_not_called_for_kind_change(self):
2985
builder = self.make_kind_change()
2986
conflicts = builder.merge()
2987
# The hook should not call the merge_text() method
2988
self.assertEqual([], self.calls)
2990
def test_hook_not_called_for_other_files(self):
2991
builder = self.make_text_conflict('foobar')
2992
conflicts = builder.merge()
2993
# The hook should not call the merge_text() method
2994
self.assertEqual([], self.calls)
2997
class TestMergeIntoBase(tests.TestCaseWithTransport):
2999
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3000
"""One commit, containing tree specified by optional shape.
3002
Default is empty tree (just root entry).
3005
root_id = '%s-root-id' % (relpath,)
3006
wt = self.make_branch_and_tree(relpath)
3007
wt.set_root_id(root_id)
3008
if shape is not None:
3009
adjusted_shape = [relpath + '/' + elem for elem in shape]
3010
self.build_tree(adjusted_shape)
3011
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3013
wt.add(shape, ids=ids)
3014
rev_id = 'r1-%s' % (relpath,)
3015
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3016
self.assertEqual(root_id, wt.path2id(''))
3019
def setup_two_branches(self, custom_root_ids=True):
3020
"""Setup 2 branches, one will be a library, the other a project."""
3024
root_id = inventory.ROOT_ID
3025
project_wt = self.setup_simple_branch(
3026
'project', ['README', 'dir/', 'dir/file.c'],
3028
lib_wt = self.setup_simple_branch(
3029
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3031
return project_wt, lib_wt
3033
def do_merge_into(self, location, merge_as):
3034
"""Helper for using MergeIntoMerger.
3036
:param location: location of directory to merge from, either the
3037
location of a branch or of a path inside a branch.
3038
:param merge_as: the path in a tree to add the new directory as.
3039
:returns: the conflicts from 'do_merge'.
3041
operation = cleanup.OperationWithCleanups(self._merge_into)
3042
return operation.run(location, merge_as)
3044
def _merge_into(self, op, location, merge_as):
3045
# Open and lock the various tree and branch objects
3046
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3047
op.add_cleanup(wt.lock_write().unlock)
3048
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3050
op.add_cleanup(branch_to_merge.lock_read().unlock)
3051
other_tree = branch_to_merge.basis_tree()
3052
op.add_cleanup(other_tree.lock_read().unlock)
3054
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3055
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3056
source_subpath=subdir_to_merge)
3057
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3058
conflicts = merger.do_merge()
3059
merger.set_pending()
3062
def assertTreeEntriesEqual(self, expected_entries, tree):
3063
"""Assert that 'tree' contains the expected inventory entries.
3065
:param expected_entries: sequence of (path, file-id) pairs.
3067
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3068
self.assertEqual(expected_entries, files)
3071
class TestMergeInto(TestMergeIntoBase):
3073
def test_newdir_with_unique_roots(self):
3074
"""Merge a branch with a unique root into a new directory."""
3075
project_wt, lib_wt = self.setup_two_branches()
3076
self.do_merge_into('lib1', 'project/lib1')
3077
project_wt.lock_read()
3078
self.addCleanup(project_wt.unlock)
3079
# The r1-lib1 revision should be merged into this one
3080
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3081
self.assertTreeEntriesEqual(
3082
[('', 'project-root-id'),
3083
('README', 'project-README-id'),
3084
('dir', 'project-dir-id'),
3085
('lib1', 'lib1-root-id'),
3086
('dir/file.c', 'project-file.c-id'),
3087
('lib1/Makefile', 'lib1-Makefile-id'),
3088
('lib1/README', 'lib1-README-id'),
3089
('lib1/foo.c', 'lib1-foo.c-id'),
3092
def test_subdir(self):
3093
"""Merge a branch into a subdirectory of an existing directory."""
3094
project_wt, lib_wt = self.setup_two_branches()
3095
self.do_merge_into('lib1', 'project/dir/lib1')
3096
project_wt.lock_read()
3097
self.addCleanup(project_wt.unlock)
3098
# The r1-lib1 revision should be merged into this one
3099
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3100
self.assertTreeEntriesEqual(
3101
[('', 'project-root-id'),
3102
('README', 'project-README-id'),
3103
('dir', 'project-dir-id'),
3104
('dir/file.c', 'project-file.c-id'),
3105
('dir/lib1', 'lib1-root-id'),
3106
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3107
('dir/lib1/README', 'lib1-README-id'),
3108
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3111
def test_newdir_with_repeat_roots(self):
3112
"""If the file-id of the dir to be merged already exists a new ID will
3113
be allocated to let the merge happen.
3115
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3116
root_id = project_wt.path2id('')
3117
self.do_merge_into('lib1', 'project/lib1')
3118
project_wt.lock_read()
3119
self.addCleanup(project_wt.unlock)
3120
# The r1-lib1 revision should be merged into this one
3121
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3122
new_lib1_id = project_wt.path2id('lib1')
3123
self.assertNotEqual(None, new_lib1_id)
3124
self.assertTreeEntriesEqual(
3126
('README', 'project-README-id'),
3127
('dir', 'project-dir-id'),
3128
('lib1', new_lib1_id),
3129
('dir/file.c', 'project-file.c-id'),
3130
('lib1/Makefile', 'lib1-Makefile-id'),
3131
('lib1/README', 'lib1-README-id'),
3132
('lib1/foo.c', 'lib1-foo.c-id'),
3135
def test_name_conflict(self):
3136
"""When the target directory name already exists a conflict is
3137
generated and the original directory is renamed to foo.moved.
3139
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3140
src_wt = self.setup_simple_branch('src', ['README'])
3141
conflicts = self.do_merge_into('src', 'dest/dir')
3142
self.assertEqual(1, conflicts)
3144
self.addCleanup(dest_wt.unlock)
3145
# The r1-lib1 revision should be merged into this one
3146
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3147
self.assertTreeEntriesEqual(
3148
[('', 'dest-root-id'),
3149
('dir', 'src-root-id'),
3150
('dir.moved', 'dest-dir-id'),
3151
('dir/README', 'src-README-id'),
3152
('dir.moved/file.txt', 'dest-file.txt-id'),
3155
def test_file_id_conflict(self):
3156
"""A conflict is generated if the merge-into adds a file (or other
3157
inventory entry) with a file-id that already exists in the target tree.
3159
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3160
# Make a second tree with a file-id that will clash with file.txt in
3162
src_wt = self.make_branch_and_tree('src')
3163
self.build_tree(['src/README'])
3164
src_wt.add(['README'], ids=['dest-file.txt-id'])
3165
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3166
conflicts = self.do_merge_into('src', 'dest/dir')
3167
# This is an edge case that shouldn't happen to users very often. So
3168
# we don't care really about the exact presentation of the conflict,
3169
# just that there is one.
3170
self.assertEqual(1, conflicts)
3172
def test_only_subdir(self):
3173
"""When the location points to just part of a tree, merge just that
3176
dest_wt = self.setup_simple_branch('dest')
3177
src_wt = self.setup_simple_branch(
3178
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3179
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3181
self.addCleanup(dest_wt.unlock)
3182
# The r1-lib1 revision should NOT be merged into this one (this is a
3184
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3185
self.assertTreeEntriesEqual(
3186
[('', 'dest-root-id'),
3187
('dir', 'src-dir-id'),
3188
('dir/foo.c', 'src-foo.c-id'),
3191
def test_only_file(self):
3192
"""An edge case: merge just one file, not a whole dir."""
3193
dest_wt = self.setup_simple_branch('dest')
3194
two_file_wt = self.setup_simple_branch(
3195
'two-file', ['file1.txt', 'file2.txt'])
3196
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3198
self.addCleanup(dest_wt.unlock)
3199
# The r1-lib1 revision should NOT be merged into this one
3200
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3201
self.assertTreeEntriesEqual(
3202
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3205
def test_no_such_source_path(self):
3206
"""PathNotInTree is raised if the specified path in the source tree
3209
dest_wt = self.setup_simple_branch('dest')
3210
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3211
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3212
'src/no-such-dir', 'dest/foo')
3214
self.addCleanup(dest_wt.unlock)
3215
# The dest tree is unmodified.
3216
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3217
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3219
def test_no_such_target_path(self):
3220
"""PathNotInTree is also raised if the specified path in the target
3221
tree does not exist.
3223
dest_wt = self.setup_simple_branch('dest')
3224
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3225
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3226
'src', 'dest/no-such-dir/foo')
3228
self.addCleanup(dest_wt.unlock)
3229
# The dest tree is unmodified.
3230
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3231
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)