1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2012, 2016 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 (
40
41
TestCaseWithMemoryTransport,
41
42
TestCaseWithTransport,
82
83
tip = wt1.commit('empty commit')
83
84
wt2 = self.make_branch_and_tree('branch2')
84
85
wt2.pull(wt1.branch)
85
file('branch1/foo', 'wb').write('foo')
86
file('branch1/bar', 'wb').write('bar')
86
with file('branch1/foo', 'wb') as f:
88
with file('branch1/bar', 'wb') as f:
89
92
wt1.commit('add foobar')
91
self.run_bzr('merge ../branch1/baz', retcode=3)
92
self.run_bzr('merge ../branch1/foo')
93
self.assertPathExists('foo')
94
self.assertPathDoesNotExist('bar')
95
wt2 = WorkingTree.open('.') # opens branch2
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')
96
98
self.assertEqual([tip], wt2.get_parent_ids())
98
100
def test_pending_with_null(self):
138
140
def test_merge_unrelated_retains_root(self):
139
141
wt = self.make_branch_and_tree('tree')
140
root_id_before_merge = wt.get_root_id()
141
other_tree = self.make_branch_and_tree('other')
142
# Do a commit so there is something to merge
143
other_tree.commit('commit other')
144
self.assertNotEquals(root_id_before_merge, other_tree.get_root_id())
145
wt.merge_from_branch(other_tree.branch,
146
from_revision=_mod_revision.NULL_REVISION)
147
self.assertEqual(root_id_before_merge, wt.get_root_id())
149
def test_merge_preview_unrelated_retains_root(self):
150
wt = self.make_branch_and_tree('tree')
151
other_tree = self.make_branch_and_tree('other')
152
# Do a commit so there is something to merge
153
other_tree.commit('commit other')
142
other_tree = self.make_branch_and_tree('other')
143
self.addCleanup(other_tree.lock_read().unlock)
154
144
merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
155
145
this_branch=wt.branch,
157
with merger.make_preview_transform() as tt:
158
preview = tt.get_preview_tree()
159
self.assertEqual(wt.get_root_id(), preview.get_root_id())
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)
161
152
def test_create_rename(self):
162
153
"""Rename an inventory entry while creating the file"""
163
154
tree =self.make_branch_and_tree('.')
164
file('name1', 'wb').write('Hello')
155
with file('name1', 'wb') as f: f.write('Hello')
165
156
tree.add('name1')
166
157
tree.commit(message="hello")
167
158
tree.rename_one('name1', 'name2')
174
165
os.mkdir('dirname1')
175
166
tree.add('dirname1')
176
167
filename = pathjoin('dirname1', 'name1')
177
file(filename, 'wb').write('Hello')
168
with file(filename, 'wb') as f: f.write('Hello')
178
169
tree.add(filename)
179
170
tree.commit(message="hello")
180
171
filename2 = pathjoin('dirname1', 'name2')
237
228
def test_nested_merge(self):
238
229
tree = self.make_branch_and_tree('tree',
239
format='dirstate-with-subtree')
230
format='development-subtree')
240
231
sub_tree = self.make_branch_and_tree('tree/sub-tree',
241
format='dirstate-with-subtree')
232
format='development-subtree')
242
233
sub_tree.set_root_id('sub-tree-root')
243
234
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
244
235
sub_tree.add('file')
430
421
self.addCleanup(tree.unlock)
431
422
self.build_tree(['a'])
433
tree.commit("added a")
434
first_rev = tree.branch.revision_history()[0]
424
first_rev = tree.commit("added a")
435
425
merger = _mod_merge.Merger.from_revision_ids(None, tree,
436
426
_mod_revision.NULL_REVISION,
457
447
merger.merge_type = _mod_merge.Merge3Merger
458
448
tree_merger = merger.make_merger()
459
449
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
460
self.assertEqual('rev2b', tree_merger.other_tree.get_revision_id())
461
self.assertEqual('rev1', tree_merger.base_tree.get_revision_id())
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)
463
456
def test_make_preview_transform(self):
464
457
this_tree = self.make_branch_and_tree('this')
519
512
self.addCleanup(tree.unlock)
520
513
self.build_tree(['a'])
522
tree.commit("added a")
515
first_rev = tree.commit("added a")
523
516
old_root_id = tree.get_root_id()
524
first_rev = tree.branch.revision_history()[0]
525
517
merger = _mod_merge.Merger.from_revision_ids(None, tree,
526
518
_mod_revision.NULL_REVISION,
528
520
merger.merge_type = _mod_merge.Merge3Merger
529
521
conflict_count = merger.do_merge()
530
522
self.assertEqual(0, conflict_count)
531
self.assertEquals(set([old_root_id]), tree.all_file_ids())
523
self.assertEqual(set([old_root_id]), tree.all_file_ids())
532
524
tree.set_parent_ids([])
534
526
def test_merge_add_into_deleted_root(self):
567
559
class TestPlanMerge(TestCaseWithMemoryTransport):
570
TestCaseWithMemoryTransport.setUp(self)
562
super(TestPlanMerge, self).setUp()
571
563
mapper = versionedfile.PrefixMapper()
572
564
factory = knit.make_file_factory(True, mapper)
573
565
self.vf = factory(self.get_transport())
2260
2252
wt.get_file_text('foo-id'))
2262
2254
def test_modified_symlink(self):
2263
self.requireFeature(tests.SymlinkFeature)
2255
self.requireFeature(features.SymlinkFeature)
2264
2256
# A Create symlink foo => bar
2266
2258
# B C B relinks foo => baz
2305
2297
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2307
2299
def test_renamed_symlink(self):
2308
self.requireFeature(tests.SymlinkFeature)
2300
self.requireFeature(features.SymlinkFeature)
2309
2301
# A Create symlink foo => bar
2311
2303
# B C B renames foo => barry
2361
2353
self.assertEqual('blah', wt.id2path('foo-id'))
2363
2355
def test_symlink_no_content_change(self):
2364
self.requireFeature(tests.SymlinkFeature)
2356
self.requireFeature(features.SymlinkFeature)
2365
2357
# A Create symlink foo => bar
2367
2359
# B C B relinks foo => baz
2412
2404
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2414
2406
def test_symlink_this_changed_kind(self):
2415
self.requireFeature(tests.SymlinkFeature)
2407
self.requireFeature(features.SymlinkFeature)
2418
2410
# B C B creates symlink foo => bar
2466
2458
def test_symlink_all_wt(self):
2467
2459
"""Check behavior if all trees are Working Trees."""
2468
self.requireFeature(tests.SymlinkFeature)
2460
self.requireFeature(features.SymlinkFeature)
2469
2461
# The big issue is that entry.symlink_target is None for WorkingTrees.
2470
2462
# So we need to make sure we handle that case correctly.
2642
2634
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2643
2635
# 'tree-reference'
2644
2636
wt = self.make_branch_and_tree('tree',
2645
format='dirstate-with-subtree')
2637
format='development-subtree')
2646
2638
wt.lock_write()
2647
2639
self.addCleanup(wt.unlock)
2648
2640
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2649
format='dirstate-with-subtree')
2641
format='development-subtree')
2650
2642
wt.set_root_id('a-root-id')
2651
2643
sub_tree.set_root_id('sub-tree-root')
2652
2644
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2677
2669
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2678
2670
# 'tree-reference'
2679
2671
wt = self.make_branch_and_tree('tree',
2680
format='dirstate-with-subtree')
2672
format='development-subtree')
2681
2673
wt.lock_write()
2682
2674
self.addCleanup(wt.unlock)
2683
2675
sub_tree = self.make_branch_and_tree('tree/sub',
2684
format='dirstate-with-subtree')
2676
format='development-subtree')
2685
2677
wt.set_root_id('a-root-id')
2686
2678
sub_tree.set_root_id('sub-tree-root')
2687
2679
self.build_tree_contents([('tree/sub/file', 'text1')])
2716
2708
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2717
2709
# 'tree-reference'
2718
2710
wt = self.make_branch_and_tree('tree',
2719
format='dirstate-with-subtree')
2711
format='development-subtree')
2720
2712
wt.lock_write()
2721
2713
self.addCleanup(wt.unlock)
2722
2714
sub_tree = self.make_branch_and_tree('tree/sub',
2723
format='dirstate-with-subtree')
2715
format='development-subtree')
2724
2716
wt.set_root_id('a-root-id')
2725
2717
sub_tree.set_root_id('sub-tree-root')
2726
2718
self.build_tree_contents([('tree/sub/file', 'text1')])
2759
2751
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2760
2752
# 'tree-reference'
2761
2753
wt = self.make_branch_and_tree('tree',
2762
format='dirstate-with-subtree')
2754
format='development-subtree')
2763
2755
wt.lock_write()
2764
2756
self.addCleanup(wt.unlock)
2765
2757
sub_tree = self.make_branch_and_tree('tree/sub',
2766
format='dirstate-with-subtree')
2758
format='development-subtree')
2767
2759
wt.set_root_id('a-root-id')
2768
2760
sub_tree.set_root_id('sub-tree-root')
2769
2761
self.build_tree_contents([('tree/sub/file', 'text1')])
3254
3246
# The dest tree is unmodified.
3255
3247
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3256
3248
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)