1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
1
# Copyright (C) 2005-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
37
37
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
38
38
from bzrlib.osutils import basename, pathjoin, file_kind
39
39
from bzrlib.tests import (
41
40
TestCaseWithMemoryTransport,
42
41
TestCaseWithTransport,
83
82
tip = wt1.commit('empty commit')
84
83
wt2 = self.make_branch_and_tree('branch2')
85
84
wt2.pull(wt1.branch)
86
with file('branch1/foo', 'wb') as f:
88
with file('branch1/bar', 'wb') as f:
85
file('branch1/foo', 'wb').write('foo')
86
file('branch1/bar', 'wb').write('bar')
92
89
wt1.commit('add foobar')
93
self.run_bzr('merge ../branch1/baz', retcode=3, working_dir='branch2')
94
self.run_bzr('merge ../branch1/foo', working_dir='branch2')
95
self.assertPathExists('branch2/foo')
96
self.assertPathDoesNotExist('branch2/bar')
97
wt2 = WorkingTree.open('branch2')
91
self.run_bzr('merge ../branch1/baz', retcode=3)
92
self.run_bzr('merge ../branch1/foo')
93
self.failUnlessExists('foo')
94
self.failIfExists('bar')
95
wt2 = WorkingTree.open('.') # opens branch2
98
96
self.assertEqual([tip], wt2.get_parent_ids())
100
98
def test_pending_with_null(self):
126
def test_merge_into_null_tree(self):
127
wt = self.make_branch_and_tree('tree')
128
null_tree = wt.basis_tree()
129
self.build_tree(['tree/file'])
131
wt.commit('tree with root')
132
merger = _mod_merge.Merge3Merger(null_tree, null_tree, null_tree, wt,
133
this_branch=wt.branch,
135
with merger.make_preview_transform() as tt:
136
self.assertEqual([], tt.find_conflicts())
137
preview = tt.get_preview_tree()
138
self.assertEqual(wt.get_root_id(), preview.get_root_id())
140
def test_merge_unrelated_retains_root(self):
141
wt = self.make_branch_and_tree('tree')
142
other_tree = self.make_branch_and_tree('other')
143
self.addCleanup(other_tree.lock_read().unlock)
144
merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
145
this_branch=wt.branch,
147
with transform.TransformPreview(wt) as merger.tt:
148
merger._compute_transform()
149
new_root_id = merger.tt.final_file_id(merger.tt.root)
150
self.assertEqual(wt.get_root_id(), new_root_id)
152
124
def test_create_rename(self):
153
125
"""Rename an inventory entry while creating the file"""
154
126
tree =self.make_branch_and_tree('.')
155
with file('name1', 'wb') as f: f.write('Hello')
127
file('name1', 'wb').write('Hello')
156
128
tree.add('name1')
157
129
tree.commit(message="hello")
158
130
tree.rename_one('name1', 'name2')
165
137
os.mkdir('dirname1')
166
138
tree.add('dirname1')
167
139
filename = pathjoin('dirname1', 'name1')
168
with file(filename, 'wb') as f: f.write('Hello')
140
file(filename, 'wb').write('Hello')
169
141
tree.add(filename)
170
142
tree.commit(message="hello")
171
143
filename2 = pathjoin('dirname1', 'name2')
186
158
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
187
159
this_tree=tree_b, ignore_zero=True)
188
self.assertTrue('All changes applied successfully.\n' not in
160
self.failUnless('All changes applied successfully.\n' not in
191
163
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
192
164
this_tree=tree_b, ignore_zero=False)
193
self.assertTrue('All changes applied successfully.\n' in self.get_log())
165
self.failUnless('All changes applied successfully.\n' in self.get_log())
195
167
def test_merge_inner_conflicts(self):
196
168
tree_a = self.make_branch_and_tree('a')
228
200
def test_nested_merge(self):
229
201
tree = self.make_branch_and_tree('tree',
230
format='development-subtree')
202
format='dirstate-with-subtree')
231
203
sub_tree = self.make_branch_and_tree('tree/sub-tree',
232
format='development-subtree')
204
format='dirstate-with-subtree')
233
205
sub_tree.set_root_id('sub-tree-root')
234
206
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
235
207
sub_tree.add('file')
415
387
'>>>>>>> MERGE-SOURCE\n',
418
def test_merge_reverse_revision_range(self):
419
tree = self.make_branch_and_tree(".")
421
self.addCleanup(tree.unlock)
422
self.build_tree(['a'])
424
first_rev = tree.commit("added a")
425
merger = _mod_merge.Merger.from_revision_ids(None, tree,
426
_mod_revision.NULL_REVISION,
428
merger.merge_type = _mod_merge.Merge3Merger
429
merger.interesting_files = 'a'
430
conflict_count = merger.do_merge()
431
self.assertEqual(0, conflict_count)
433
self.assertPathDoesNotExist("a")
435
self.assertPathExists("a")
437
390
def test_make_merger(self):
438
391
this_tree = self.make_branch_and_tree('this')
439
392
this_tree.commit('rev1', rev_id='rev1')
447
400
merger.merge_type = _mod_merge.Merge3Merger
448
401
tree_merger = merger.make_merger()
449
402
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
450
self.assertEqual('rev2b',
451
tree_merger.other_tree.get_revision_id())
452
self.assertEqual('rev1',
453
tree_merger.base_tree.get_revision_id())
454
self.assertEqual(other_tree.branch, tree_merger.other_branch)
403
self.assertEqual('rev2b', tree_merger.other_tree.get_revision_id())
404
self.assertEqual('rev1', tree_merger.base_tree.get_revision_id())
456
406
def test_make_preview_transform(self):
457
407
this_tree = self.make_branch_and_tree('this')
507
457
tree_file.close()
509
def test_merge_require_tree_root(self):
510
tree = self.make_branch_and_tree(".")
512
self.addCleanup(tree.unlock)
513
self.build_tree(['a'])
515
first_rev = tree.commit("added a")
516
old_root_id = tree.get_root_id()
517
merger = _mod_merge.Merger.from_revision_ids(None, tree,
518
_mod_revision.NULL_REVISION,
520
merger.merge_type = _mod_merge.Merge3Merger
521
conflict_count = merger.do_merge()
522
self.assertEqual(0, conflict_count)
523
self.assertEqual(set([old_root_id]), tree.all_file_ids())
524
tree.set_parent_ids([])
526
459
def test_merge_add_into_deleted_root(self):
527
460
# Yes, people actually do this. And report bugs if it breaks.
528
461
source = self.make_branch_and_tree('source', format='rich-root-pack')
559
492
class TestPlanMerge(TestCaseWithMemoryTransport):
562
super(TestPlanMerge, self).setUp()
495
TestCaseWithMemoryTransport.setUp(self)
563
496
mapper = versionedfile.PrefixMapper()
564
497
factory = knit.make_file_factory(True, mapper)
565
498
self.vf = factory(self.get_transport())
1337
1270
self.assertEqual(['B-id', 'C-id', 'F-id'],
1338
1271
[t.get_revision_id() for t in merger._lca_trees])
1340
def test_find_base_new_root_criss_cross(self):
1347
builder = self.get_builder()
1348
builder.build_snapshot('A-id', None,
1349
[('add', ('', None, 'directory', None))])
1350
builder.build_snapshot('B-id', [],
1351
[('add', ('', None, 'directory', None))])
1352
builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1353
builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1354
merger = self.make_Merger(builder, 'D-id')
1355
self.assertEqual('A-id', merger.base_rev_id)
1356
self.assertTrue(merger._is_criss_cross)
1357
self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1358
for t in merger._lca_trees])
1360
1273
def test_no_criss_cross_passed_to_merge_type(self):
1361
1274
class LCATreesMerger(LoggingMerger):
1362
1275
supports_lca_trees = True
1894
1807
builder.build_snapshot('C-id', ['A-id'], [])
1895
1808
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1896
1809
[('unversion', 'a-id'),
1898
1810
('add', (u'a', 'a-id', 'directory', None))])
1899
1811
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1900
1812
merge_obj = self.make_merge_obj(builder, 'E-id')
1918
1830
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1919
1831
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1920
1832
[('unversion', 'a-id'),
1922
1833
('add', (u'a', 'a-id', 'directory', None))])
1923
1834
merge_obj = self.make_merge_obj(builder, 'E-id')
1924
1835
entries = list(merge_obj._entries_lca())
2252
2163
wt.get_file_text('foo-id'))
2254
2165
def test_modified_symlink(self):
2255
self.requireFeature(features.SymlinkFeature)
2166
self.requireFeature(tests.SymlinkFeature)
2256
2167
# A Create symlink foo => bar
2258
2169
# B C B relinks foo => baz
2297
2208
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2299
2210
def test_renamed_symlink(self):
2300
self.requireFeature(features.SymlinkFeature)
2211
self.requireFeature(tests.SymlinkFeature)
2301
2212
# A Create symlink foo => bar
2303
2214
# B C B renames foo => barry
2353
2264
self.assertEqual('blah', wt.id2path('foo-id'))
2355
2266
def test_symlink_no_content_change(self):
2356
self.requireFeature(features.SymlinkFeature)
2267
self.requireFeature(tests.SymlinkFeature)
2357
2268
# A Create symlink foo => bar
2359
2270
# B C B relinks foo => baz
2404
2315
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2406
2317
def test_symlink_this_changed_kind(self):
2407
self.requireFeature(features.SymlinkFeature)
2318
self.requireFeature(tests.SymlinkFeature)
2410
2321
# B C B creates symlink foo => bar
2458
2369
def test_symlink_all_wt(self):
2459
2370
"""Check behavior if all trees are Working Trees."""
2460
self.requireFeature(features.SymlinkFeature)
2371
self.requireFeature(tests.SymlinkFeature)
2461
2372
# The big issue is that entry.symlink_target is None for WorkingTrees.
2462
2373
# So we need to make sure we handle that case correctly.
2634
2545
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2635
2546
# 'tree-reference'
2636
2547
wt = self.make_branch_and_tree('tree',
2637
format='development-subtree')
2548
format='dirstate-with-subtree')
2638
2549
wt.lock_write()
2639
2550
self.addCleanup(wt.unlock)
2640
2551
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2641
format='development-subtree')
2552
format='dirstate-with-subtree')
2642
2553
wt.set_root_id('a-root-id')
2643
2554
sub_tree.set_root_id('sub-tree-root')
2644
2555
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2669
2580
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2670
2581
# 'tree-reference'
2671
2582
wt = self.make_branch_and_tree('tree',
2672
format='development-subtree')
2583
format='dirstate-with-subtree')
2673
2584
wt.lock_write()
2674
2585
self.addCleanup(wt.unlock)
2675
2586
sub_tree = self.make_branch_and_tree('tree/sub',
2676
format='development-subtree')
2587
format='dirstate-with-subtree')
2677
2588
wt.set_root_id('a-root-id')
2678
2589
sub_tree.set_root_id('sub-tree-root')
2679
2590
self.build_tree_contents([('tree/sub/file', 'text1')])
2708
2619
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2709
2620
# 'tree-reference'
2710
2621
wt = self.make_branch_and_tree('tree',
2711
format='development-subtree')
2622
format='dirstate-with-subtree')
2712
2623
wt.lock_write()
2713
2624
self.addCleanup(wt.unlock)
2714
2625
sub_tree = self.make_branch_and_tree('tree/sub',
2715
format='development-subtree')
2626
format='dirstate-with-subtree')
2716
2627
wt.set_root_id('a-root-id')
2717
2628
sub_tree.set_root_id('sub-tree-root')
2718
2629
self.build_tree_contents([('tree/sub/file', 'text1')])
2751
2662
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2752
2663
# 'tree-reference'
2753
2664
wt = self.make_branch_and_tree('tree',
2754
format='development-subtree')
2665
format='dirstate-with-subtree')
2755
2666
wt.lock_write()
2756
2667
self.addCleanup(wt.unlock)
2757
2668
sub_tree = self.make_branch_and_tree('tree/sub',
2758
format='development-subtree')
2669
format='dirstate-with-subtree')
2759
2670
wt.set_root_id('a-root-id')
2760
2671
sub_tree.set_root_id('sub-tree-root')
2761
2672
self.build_tree_contents([('tree/sub/file', 'text1')])
2933
2844
def get_merger_factory(self):
2934
2845
# Allows the inner methods to access the test attributes
2937
2848
class FooMerger(_mod_merge.ConfigurableFileMerger):
2938
2849
name_prefix = "foo"
2939
2850
default_files = ['bar']
2941
2852
def merge_text(self, params):
2942
calls.append('merge_text')
2853
test.calls.append('merge_text')
2943
2854
return ('not_applicable', None)
2945
2856
def factory(merger):
3246
3157
# The dest tree is unmodified.
3247
3158
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3248
3159
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3251
class TestMergeHooks(TestCaseWithTransport):
3254
super(TestMergeHooks, self).setUp()
3255
self.tree_a = self.make_branch_and_tree('tree_a')
3256
self.build_tree_contents([('tree_a/file', 'content_1')])
3257
self.tree_a.add('file', 'file-id')
3258
self.tree_a.commit('added file')
3260
self.tree_b = self.tree_a.bzrdir.sprout('tree_b').open_workingtree()
3261
self.build_tree_contents([('tree_b/file', 'content_2')])
3262
self.tree_b.commit('modify file')
3264
def test_pre_merge_hook_inject_different_tree(self):
3265
tree_c = self.tree_b.bzrdir.sprout('tree_c').open_workingtree()
3266
self.build_tree_contents([('tree_c/file', 'content_3')])
3267
tree_c.commit("more content")
3269
def factory(merger):
3270
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3271
merger.other_tree = tree_c
3272
calls.append(merger)
3273
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3274
factory, 'test factory')
3275
self.tree_a.merge_from_branch(self.tree_b.branch)
3277
self.assertFileEqual("content_3", 'tree_a/file')
3278
self.assertLength(1, calls)
3280
def test_post_merge_hook_called(self):
3282
def factory(merger):
3283
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3284
calls.append(merger)
3285
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3286
factory, 'test factory')
3288
self.tree_a.merge_from_branch(self.tree_b.branch)
3290
self.assertFileEqual("content_2", 'tree_a/file')
3291
self.assertLength(1, calls)