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)
309
307
tree_a.commit('commit 2')
310
308
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
311
309
tree_b.rename_one('file_1', 'renamed')
312
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
310
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
311
progress.DummyProgress())
313
312
merger.merge_type = _mod_merge.WeaveMerger
314
313
merger.do_merge()
315
314
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
316
def test_Merger_defaults_to_DummyProgress(self):
317
branch = self.make_branch('branch')
318
merger = _mod_merge.Merger(branch, pb=None)
319
self.assertIsInstance(merger._pb, progress.DummyProgress)
317
321
def prepare_cherrypick(self):
318
322
"""Prepare a pair of trees for cherrypicking tests.
349
353
def test_weave_cannot_reverse_cherrypick(self):
350
354
this_tree, other_tree = self.prepare_cherrypick()
351
merger = _mod_merge.Merger.from_revision_ids(None,
355
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
352
356
this_tree, 'rev2b', 'rev3b', other_tree.branch)
353
357
merger.merge_type = _mod_merge.WeaveMerger
354
358
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
356
360
def test_merge3_can_reverse_cherrypick(self):
357
361
this_tree, other_tree = self.prepare_cherrypick()
358
merger = _mod_merge.Merger.from_revision_ids(None,
362
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
359
363
this_tree, 'rev2b', 'rev3b', other_tree.branch)
360
364
merger.merge_type = _mod_merge.Merge3Merger
361
365
merger.do_merge()
519
523
self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
520
524
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
526
def test_unique_lines(self):
529
527
plan = self.setup_plan_merge()
530
528
self.assertEqual(plan._unique_lines(
828
826
('unchanged', 'f\n'),
829
827
('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
830
def assertRemoveExternalReferences(self, filtered_parent_map,
938
831
child_map, tails, parent_map):
1034
class TestMergeImplementation(object):
1036
def do_merge(self, target_tree, source_tree, **kwargs):
1037
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
1038
target_tree, source_tree.last_revision(),
1039
other_branch=source_tree.branch)
1040
merger.merge_type=self.merge_type
1041
for name, value in kwargs.items():
1042
setattr(merger, name, value)
1045
def test_merge_specific_file(self):
1046
this_tree = self.make_branch_and_tree('this')
1047
this_tree.lock_write()
1048
self.addCleanup(this_tree.unlock)
1049
self.build_tree_contents([
1050
('this/file1', 'a\nb\n'),
1051
('this/file2', 'a\nb\n')
1053
this_tree.add(['file1', 'file2'])
1054
this_tree.commit('Added files')
1055
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1056
self.build_tree_contents([
1057
('other/file1', 'a\nb\nc\n'),
1058
('other/file2', 'a\nb\nc\n')
1060
other_tree.commit('modified both')
1061
self.build_tree_contents([
1062
('this/file1', 'd\na\nb\n'),
1063
('this/file2', 'd\na\nb\n')
1065
this_tree.commit('modified both')
1066
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
1067
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
1068
self.assertFileEqual('d\na\nb\n', 'this/file2')
1070
def test_merge_move_and_change(self):
1071
this_tree = self.make_branch_and_tree('this')
1072
this_tree.lock_write()
1073
self.addCleanup(this_tree.unlock)
1074
self.build_tree_contents([
1075
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
1077
this_tree.add('file1',)
1078
this_tree.commit('Added file')
1079
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
1080
self.build_tree_contents([
1081
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
1083
other_tree.commit('Changed 2 to 2.1')
1084
self.build_tree_contents([
1085
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
1087
this_tree.commit('Swapped 2 & 3')
1088
self.do_merge(this_tree, other_tree)
1089
self.assertFileEqual('line 1\n'
1096
'>>>>>>> MERGE-SOURCE\n'
1097
'line 4\n', 'this/file1')
1100
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
1102
merge_type = _mod_merge.Merge3Merger
1105
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
1107
merge_type = _mod_merge.WeaveMerger
1110
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
1112
merge_type = _mod_merge.LCAMerger
1114
def test_merge_move_and_change(self):
1115
self.expectFailure("lca merge doesn't conflict for move and change",
1116
super(TestLCAMerge, self).test_merge_move_and_change)
1141
1119
class LoggingMerger(object):
1142
1120
# These seem to be the required attributes
1143
1121
requires_base = False
1210
1188
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
1190
def test_find_base(self):
1234
1191
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1235
1192
self.assertEqual('A-id', merger.base_rev_id)
2830
2787
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2831
2788
self.assertLCAMultiWay('conflict',
2832
2789
'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_uses_this_branch(self):
2891
builder = self.make_text_conflict()
2892
tt = builder.make_preview_transform()
2893
self.addCleanup(tt.finalize)
2895
def test_affected_files_cached(self):
2896
"""Ensures that the config variable is cached"""
2897
builder = self.make_text_conflict()
2898
conflicts = builder.merge()
2899
# The hook should set the variable
2900
self.assertEqual(['bar'], self.merger.affected_files)
2901
self.assertEqual(1, len(conflicts))
2903
def test_hook_called_for_text_conflicts(self):
2904
builder = self.make_text_conflict()
2905
conflicts = builder.merge()
2906
# The hook should call the merge_text() method
2907
self.assertEqual(['merge_text'], self.calls)
2909
def test_hook_not_called_for_kind_change(self):
2910
builder = self.make_kind_change()
2911
conflicts = builder.merge()
2912
# The hook should not call the merge_text() method
2913
self.assertEqual([], self.calls)
2915
def test_hook_not_called_for_other_files(self):
2916
builder = self.make_text_conflict('foobar')
2917
conflicts = builder.merge()
2918
# The hook should not call the merge_text() method
2919
self.assertEqual([], self.calls)