835
827
('unchanged', 'f\n'),
836
828
('unchanged', 'g\n')],
838
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
839
# This is one of the main differences between plan_merge and
840
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
841
# because 'x' was not present in one of the bases. However, in this
842
# case it is spurious because 'x' does not exist in the global base A.
844
('unchanged', 'h\n'),
845
('unchanged', 'a\n'),
846
('conflicted-a', 'x\n'),
848
('unchanged', 'c\n'),
849
('unchanged', 'd\n'),
850
('unchanged', 'y\n'),
851
('unchanged', 'f\n'),
852
('unchanged', 'g\n')],
855
def test_criss_cross_flip_flop(self):
856
# This is specificly trying to trigger problems when using limited
857
# ancestry and weaves. The ancestry graph looks like:
858
# XX unused ancestor, should not show up in the weave
862
# B C B & C both introduce a new line
866
# D E B & C are both merged, so both are common ancestors
867
# In the process of merging, both sides order the new
870
self.add_rev('root', 'XX', [], 'qrs')
871
self.add_rev('root', 'A', ['XX'], 'abcdef')
872
self.add_rev('root', 'B', ['A'], 'abcdgef')
873
self.add_rev('root', 'C', ['A'], 'abcdhef')
874
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
875
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
876
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
878
('unchanged', 'a\n'),
879
('unchanged', 'b\n'),
880
('unchanged', 'c\n'),
881
('unchanged', 'd\n'),
883
('unchanged', 'g\n'),
885
('unchanged', 'e\n'),
886
('unchanged', 'f\n'),
888
pwm = versionedfile.PlanWeaveMerge(plan)
889
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
890
''.join(pwm.base_from_plan()))
891
# Reversing the order reverses the merge plan, and final order of 'hg'
893
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
895
('unchanged', 'a\n'),
896
('unchanged', 'b\n'),
897
('unchanged', 'c\n'),
898
('unchanged', 'd\n'),
900
('unchanged', 'h\n'),
902
('unchanged', 'e\n'),
903
('unchanged', 'f\n'),
905
pwm = versionedfile.PlanWeaveMerge(plan)
906
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
907
''.join(pwm.base_from_plan()))
908
# This is where lca differs, in that it (fairly correctly) determines
909
# that there is a conflict because both sides resolved the merge
911
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
913
('unchanged', 'a\n'),
914
('unchanged', 'b\n'),
915
('unchanged', 'c\n'),
916
('unchanged', 'd\n'),
917
('conflicted-b', 'h\n'),
918
('unchanged', 'g\n'),
919
('conflicted-a', 'h\n'),
920
('unchanged', 'e\n'),
921
('unchanged', 'f\n'),
923
pwm = versionedfile.PlanWeaveMerge(plan)
924
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
925
''.join(pwm.base_from_plan()))
926
# Reversing it changes what line is doubled, but still gives a
928
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
930
('unchanged', 'a\n'),
931
('unchanged', 'b\n'),
932
('unchanged', 'c\n'),
933
('unchanged', 'd\n'),
934
('conflicted-b', 'g\n'),
935
('unchanged', 'h\n'),
936
('conflicted-a', 'g\n'),
937
('unchanged', 'e\n'),
938
('unchanged', 'f\n'),
940
pwm = versionedfile.PlanWeaveMerge(plan)
941
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
942
''.join(pwm.base_from_plan()))
944
831
def assertRemoveExternalReferences(self, filtered_parent_map,
945
832
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)
1148
1187
class LoggingMerger(object):
1149
1188
# These seem to be the required attributes
1150
1189
requires_base = False
2837
2876
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2838
2877
self.assertLCAMultiWay('conflict',
2839
2878
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2842
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2844
def test_affected_files_cached(self):
2845
"""Ensures that the config variable is cached"""
2846
class SimplePlan(_mod_merge.ConfigurableFileMerger):
2848
default_files = ["my default"]
2849
def merge_text(self, params):
2850
return ('not applicable', None)
2851
def factory(merger):
2852
result = SimplePlan(merger)
2853
self.assertEqual(None, result.affected_files)
2854
self.merger = result
2856
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2857
factory, 'test factory')
2858
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2859
self.addCleanup(builder.cleanup)
2860
builder.add_file('NEWS', builder.tree_root, 'name1', 'text1', True)
2861
builder.change_contents('NEWS', other='text4', this='text3')
2862
conflicts = builder.merge()
2863
# The hook should set the variable
2864
self.assertEqual(["my default"], self.merger.affected_files)