32
from bzrlib.branch import Branch
32
33
from bzrlib.conflicts import ConflictList, TextConflict
33
from bzrlib.errors import UnrelatedBranches, NoCommits
34
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
34
35
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
35
36
from bzrlib.osutils import pathjoin, file_kind
36
from bzrlib.tests import (
37
TestCaseWithMemoryTransport,
38
TestCaseWithTransport,
37
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
38
from bzrlib.trace import (enable_test_log, disable_test_log)
41
39
from bzrlib.workingtree import WorkingTree
155
153
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
156
154
this_tree=tree_b, ignore_zero=True)
157
self.failUnless('All changes applied successfully.\n' not in
155
log = self._get_log(keep_log_file=True)
156
self.failUnless('All changes applied successfully.\n' not in log)
160
158
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
161
159
this_tree=tree_b, ignore_zero=False)
162
self.failUnless('All changes applied successfully.\n' in self.get_log())
160
log = self._get_log(keep_log_file=True)
161
self.failUnless('All changes applied successfully.\n' in log)
164
163
def test_merge_inner_conflicts(self):
165
164
tree_a = self.make_branch_and_tree('a')
220
219
tree_a.add('file')
221
220
tree_a.commit('commit base')
222
221
# basis_tree() is only guaranteed to be valid as long as it is actually
223
# the basis tree. This test commits to the tree after grabbing basis,
224
# so we go to the repository.
222
# the basis tree. This mutates the tree after grabbing basis, so go to
225
224
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
226
225
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
227
226
self.build_tree_contents([('tree_a/file', 'content_2')])
228
227
tree_a.commit('commit other')
229
228
other_tree = tree_a.basis_tree()
230
# 'file' is now missing but isn't altered in any commit in b so no
231
# change should be applied.
232
229
os.unlink('tree_b/file')
233
230
merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
349
348
def test_weave_cannot_reverse_cherrypick(self):
350
349
this_tree, other_tree = self.prepare_cherrypick()
351
merger = _mod_merge.Merger.from_revision_ids(None,
350
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
352
351
this_tree, 'rev2b', 'rev3b', other_tree.branch)
353
352
merger.merge_type = _mod_merge.WeaveMerger
354
353
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
356
355
def test_merge3_can_reverse_cherrypick(self):
357
356
this_tree, other_tree = self.prepare_cherrypick()
358
merger = _mod_merge.Merger.from_revision_ids(None,
357
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
359
358
this_tree, 'rev2b', 'rev3b', other_tree.branch)
360
359
merger.merge_type = _mod_merge.Merge3Merger
361
360
merger.do_merge()
519
518
self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
520
519
return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
522
def test_base_from_plan(self):
523
self.setup_plan_merge()
524
plan = self.plan_merge_vf.plan_merge('B', 'C')
525
pwm = versionedfile.PlanWeaveMerge(plan)
526
self.assertEqual(['a\n', 'b\n', 'c\n'], pwm.base_from_plan())
528
521
def test_unique_lines(self):
529
522
plan = self.setup_plan_merge()
530
523
self.assertEqual(plan._unique_lines(
828
821
('unchanged', 'f\n'),
829
822
('unchanged', 'g\n')],
831
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
832
# This is one of the main differences between plan_merge and
833
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
834
# because 'x' was not present in one of the bases. However, in this
835
# case it is spurious because 'x' does not exist in the global base A.
837
('unchanged', 'h\n'),
838
('unchanged', 'a\n'),
839
('conflicted-a', 'x\n'),
841
('unchanged', 'c\n'),
842
('unchanged', 'd\n'),
843
('unchanged', 'y\n'),
844
('unchanged', 'f\n'),
845
('unchanged', 'g\n')],
848
def test_criss_cross_flip_flop(self):
849
# This is specificly trying to trigger problems when using limited
850
# ancestry and weaves. The ancestry graph looks like:
851
# XX unused ancestor, should not show up in the weave
855
# B C B & C both introduce a new line
859
# D E B & C are both merged, so both are common ancestors
860
# In the process of merging, both sides order the new
863
self.add_rev('root', 'XX', [], 'qrs')
864
self.add_rev('root', 'A', ['XX'], 'abcdef')
865
self.add_rev('root', 'B', ['A'], 'abcdgef')
866
self.add_rev('root', 'C', ['A'], 'abcdhef')
867
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
868
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
869
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
871
('unchanged', 'a\n'),
872
('unchanged', 'b\n'),
873
('unchanged', 'c\n'),
874
('unchanged', 'd\n'),
876
('unchanged', 'g\n'),
878
('unchanged', 'e\n'),
879
('unchanged', 'f\n'),
881
pwm = versionedfile.PlanWeaveMerge(plan)
882
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
883
''.join(pwm.base_from_plan()))
884
# Reversing the order reverses the merge plan, and final order of 'hg'
886
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
888
('unchanged', 'a\n'),
889
('unchanged', 'b\n'),
890
('unchanged', 'c\n'),
891
('unchanged', 'd\n'),
893
('unchanged', 'h\n'),
895
('unchanged', 'e\n'),
896
('unchanged', 'f\n'),
898
pwm = versionedfile.PlanWeaveMerge(plan)
899
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
900
''.join(pwm.base_from_plan()))
901
# This is where lca differs, in that it (fairly correctly) determines
902
# that there is a conflict because both sides resolved the merge
904
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
906
('unchanged', 'a\n'),
907
('unchanged', 'b\n'),
908
('unchanged', 'c\n'),
909
('unchanged', 'd\n'),
910
('conflicted-b', 'h\n'),
911
('unchanged', 'g\n'),
912
('conflicted-a', 'h\n'),
913
('unchanged', 'e\n'),
914
('unchanged', 'f\n'),
916
pwm = versionedfile.PlanWeaveMerge(plan)
917
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
918
''.join(pwm.base_from_plan()))
919
# Reversing it changes what line is doubled, but still gives a
921
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
923
('unchanged', 'a\n'),
924
('unchanged', 'b\n'),
925
('unchanged', 'c\n'),
926
('unchanged', 'd\n'),
927
('conflicted-b', 'g\n'),
928
('unchanged', 'h\n'),
929
('conflicted-a', 'g\n'),
930
('unchanged', 'e\n'),
931
('unchanged', 'f\n'),
933
pwm = versionedfile.PlanWeaveMerge(plan)
934
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
935
''.join(pwm.base_from_plan()))
937
825
def assertRemoveExternalReferences(self, filtered_parent_map,
938
826
child_map, tails, parent_map):
1029
class TestMergeImplementation(object):
1031
def do_merge(self, target_tree, source_tree, **kwargs):
1032
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1033
target_tree, source_tree.last_revision(),
1034
other_branch=source_tree.branch)
1035
merger.merge_type=self.merge_type
1036
for name, value in kwargs.items():
1037
setattr(merger, name, value)
1040
def test_merge_specific_file(self):
1041
this_tree = self.make_branch_and_tree('this')
1042
this_tree.lock_write()
1043
self.addCleanup(this_tree.unlock)
1044
self.build_tree_contents([
1045
('this/file1', 'a\nb\n'),
1046
('this/file2', 'a\nb\n')
1048
this_tree.add(['file1', 'file2'])
1049
this_tree.commit('Added files')
1050
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1051
self.build_tree_contents([
1052
('other/file1', 'a\nb\nc\n'),
1053
('other/file2', 'a\nb\nc\n')
1055
other_tree.commit('modified both')
1056
self.build_tree_contents([
1057
('this/file1', 'd\na\nb\n'),
1058
('this/file2', 'd\na\nb\n')
1060
this_tree.commit('modified both')
1061
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
1062
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
1063
self.assertFileEqual('d\na\nb\n', 'this/file2')
1065
def test_merge_move_and_change(self):
1066
this_tree = self.make_branch_and_tree('this')
1067
this_tree.lock_write()
1068
self.addCleanup(this_tree.unlock)
1069
self.build_tree_contents([
1070
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
1072
this_tree.add('file1',)
1073
this_tree.commit('Added file')
1074
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1075
self.build_tree_contents([
1076
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
1078
other_tree.commit('Changed 2 to 2.1')
1079
self.build_tree_contents([
1080
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
1082
this_tree.commit('Swapped 2 & 3')
1083
self.do_merge(this_tree, other_tree)
1084
self.assertFileEqual('line 1\n'
1091
'>>>>>>> MERGE-SOURCE\n'
1092
'line 4\n', 'this/file1')
1095
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
1097
merge_type = _mod_merge.Merge3Merger
1100
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
1102
merge_type = _mod_merge.WeaveMerger
1105
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
1107
merge_type = _mod_merge.LCAMerger
1109
def test_merge_move_and_change(self):
1110
self.expectFailure("lca merge doesn't conflict for move and change",
1111
super(TestLCAMerge, self).test_merge_move_and_change)
1141
1114
class LoggingMerger(object):
1142
1115
# These seem to be the required attributes
1143
1116
requires_base = False
1210
1183
class TestMergerInMemory(TestMergerBase):
1212
def test_cache_trees_with_revision_ids_None(self):
1213
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1214
original_cache = dict(merger._cached_trees)
1215
merger.cache_trees_with_revision_ids([None])
1216
self.assertEqual(original_cache, merger._cached_trees)
1218
def test_cache_trees_with_revision_ids_no_revision_id(self):
1219
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1220
original_cache = dict(merger._cached_trees)
1221
tree = self.make_branch_and_memory_tree('tree')
1222
merger.cache_trees_with_revision_ids([tree])
1223
self.assertEqual(original_cache, merger._cached_trees)
1225
def test_cache_trees_with_revision_ids_having_revision_id(self):
1226
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1227
original_cache = dict(merger._cached_trees)
1228
tree = merger.this_branch.repository.revision_tree('B-id')
1229
original_cache['B-id'] = tree
1230
merger.cache_trees_with_revision_ids([tree])
1231
self.assertEqual(original_cache, merger._cached_trees)
1233
1185
def test_find_base(self):
1234
1186
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1235
1187
self.assertEqual('A-id', merger.base_rev_id)
2830
2782
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2831
2783
self.assertLCAMultiWay('conflict',
2832
2784
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2835
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2838
super(TestConfigurableFileMerger, self).setUp()
2841
def get_merger_factory(self):
2842
# Allows the inner methods to access the test attributes
2845
class FooMerger(_mod_merge.ConfigurableFileMerger):
2847
default_files = ['bar']
2849
def merge_text(self, params):
2850
test.calls.append('merge_text')
2851
return ('not_applicable', None)
2853
def factory(merger):
2854
result = FooMerger(merger)
2855
# Make sure we start with a clean slate
2856
self.assertEqual(None, result.affected_files)
2857
# Track the original merger
2858
self.merger = result
2863
def _install_hook(self, factory):
2864
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2865
factory, 'test factory')
2867
def make_builder(self):
2868
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2869
self.addCleanup(builder.cleanup)
2872
def make_text_conflict(self, file_name='bar'):
2873
factory = self.get_merger_factory()
2874
self._install_hook(factory)
2875
builder = self.make_builder()
2876
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2877
builder.change_contents('bar-id', other='text4', this='text3')
2880
def make_kind_change(self):
2881
factory = self.get_merger_factory()
2882
self._install_hook(factory)
2883
builder = self.make_builder()
2884
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2886
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2887
base=False, other=False)
2890
def test_affected_files_cached(self):
2891
"""Ensures that the config variable is cached"""
2892
builder = self.make_text_conflict()
2893
conflicts = builder.merge()
2894
# The hook should set the variable
2895
self.assertEqual(['bar'], self.merger.affected_files)
2896
self.assertEqual(1, len(conflicts))
2898
def test_hook_called_for_text_conflicts(self):
2899
builder = self.make_text_conflict()
2900
conflicts = builder.merge()
2901
# The hook should call the merge_text() method
2902
self.assertEqual(['merge_text'], self.calls)
2904
def test_hook_not_called_for_kind_change(self):
2905
builder = self.make_kind_change()
2906
conflicts = builder.merge()
2907
# The hook should not call the merge_text() method
2908
self.assertEqual([], self.calls)
2910
def test_hook_not_called_for_other_files(self):
2911
builder = self.make_text_conflict('foobar')
2912
conflicts = builder.merge()
2913
# The hook should not call the merge_text() method
2914
self.assertEqual([], self.calls)