1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2012 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):
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)
124
152
def test_create_rename(self):
125
153
"""Rename an inventory entry while creating the file"""
126
154
tree =self.make_branch_and_tree('.')
127
file('name1', 'wb').write('Hello')
155
with file('name1', 'wb') as f: f.write('Hello')
128
156
tree.add('name1')
129
157
tree.commit(message="hello")
130
158
tree.rename_one('name1', 'name2')
137
165
os.mkdir('dirname1')
138
166
tree.add('dirname1')
139
167
filename = pathjoin('dirname1', 'name1')
140
file(filename, 'wb').write('Hello')
168
with file(filename, 'wb') as f: f.write('Hello')
141
169
tree.add(filename)
142
170
tree.commit(message="hello")
143
171
filename2 = pathjoin('dirname1', 'name2')
200
228
def test_nested_merge(self):
201
229
tree = self.make_branch_and_tree('tree',
202
format='dirstate-with-subtree')
230
format='development-subtree')
203
231
sub_tree = self.make_branch_and_tree('tree/sub-tree',
204
format='dirstate-with-subtree')
232
format='development-subtree')
205
233
sub_tree.set_root_id('sub-tree-root')
206
234
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
207
235
sub_tree.add('file')
387
415
'>>>>>>> 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")
390
437
def test_make_merger(self):
391
438
this_tree = self.make_branch_and_tree('this')
392
439
this_tree.commit('rev1', rev_id='rev1')
400
447
merger.merge_type = _mod_merge.Merge3Merger
401
448
tree_merger = merger.make_merger()
402
449
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
403
self.assertEqual('rev2b', tree_merger.other_tree.get_revision_id())
404
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)
406
456
def test_make_preview_transform(self):
407
457
this_tree = self.make_branch_and_tree('this')
457
507
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.assertEquals(set([old_root_id]), tree.all_file_ids())
524
tree.set_parent_ids([])
459
526
def test_merge_add_into_deleted_root(self):
460
527
# Yes, people actually do this. And report bugs if it breaks.
461
528
source = self.make_branch_and_tree('source', format='rich-root-pack')
492
559
class TestPlanMerge(TestCaseWithMemoryTransport):
495
TestCaseWithMemoryTransport.setUp(self)
562
super(TestPlanMerge, self).setUp()
496
563
mapper = versionedfile.PrefixMapper()
497
564
factory = knit.make_file_factory(True, mapper)
498
565
self.vf = factory(self.get_transport())
1827
1894
builder.build_snapshot('C-id', ['A-id'], [])
1828
1895
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1829
1896
[('unversion', 'a-id'),
1830
1898
('add', (u'a', 'a-id', 'directory', None))])
1831
1899
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1832
1900
merge_obj = self.make_merge_obj(builder, 'E-id')
1850
1918
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1851
1919
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1852
1920
[('unversion', 'a-id'),
1853
1922
('add', (u'a', 'a-id', 'directory', None))])
1854
1923
merge_obj = self.make_merge_obj(builder, 'E-id')
1855
1924
entries = list(merge_obj._entries_lca())
2183
2252
wt.get_file_text('foo-id'))
2185
2254
def test_modified_symlink(self):
2186
self.requireFeature(tests.SymlinkFeature)
2255
self.requireFeature(features.SymlinkFeature)
2187
2256
# A Create symlink foo => bar
2189
2258
# B C B relinks foo => baz
2228
2297
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2230
2299
def test_renamed_symlink(self):
2231
self.requireFeature(tests.SymlinkFeature)
2300
self.requireFeature(features.SymlinkFeature)
2232
2301
# A Create symlink foo => bar
2234
2303
# B C B renames foo => barry
2284
2353
self.assertEqual('blah', wt.id2path('foo-id'))
2286
2355
def test_symlink_no_content_change(self):
2287
self.requireFeature(tests.SymlinkFeature)
2356
self.requireFeature(features.SymlinkFeature)
2288
2357
# A Create symlink foo => bar
2290
2359
# B C B relinks foo => baz
2335
2404
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2337
2406
def test_symlink_this_changed_kind(self):
2338
self.requireFeature(tests.SymlinkFeature)
2407
self.requireFeature(features.SymlinkFeature)
2341
2410
# B C B creates symlink foo => bar
2389
2458
def test_symlink_all_wt(self):
2390
2459
"""Check behavior if all trees are Working Trees."""
2391
self.requireFeature(tests.SymlinkFeature)
2460
self.requireFeature(features.SymlinkFeature)
2392
2461
# The big issue is that entry.symlink_target is None for WorkingTrees.
2393
2462
# So we need to make sure we handle that case correctly.
2565
2634
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2566
2635
# 'tree-reference'
2567
2636
wt = self.make_branch_and_tree('tree',
2568
format='dirstate-with-subtree')
2637
format='development-subtree')
2569
2638
wt.lock_write()
2570
2639
self.addCleanup(wt.unlock)
2571
2640
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2572
format='dirstate-with-subtree')
2641
format='development-subtree')
2573
2642
wt.set_root_id('a-root-id')
2574
2643
sub_tree.set_root_id('sub-tree-root')
2575
2644
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2600
2669
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2601
2670
# 'tree-reference'
2602
2671
wt = self.make_branch_and_tree('tree',
2603
format='dirstate-with-subtree')
2672
format='development-subtree')
2604
2673
wt.lock_write()
2605
2674
self.addCleanup(wt.unlock)
2606
2675
sub_tree = self.make_branch_and_tree('tree/sub',
2607
format='dirstate-with-subtree')
2676
format='development-subtree')
2608
2677
wt.set_root_id('a-root-id')
2609
2678
sub_tree.set_root_id('sub-tree-root')
2610
2679
self.build_tree_contents([('tree/sub/file', 'text1')])
2639
2708
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2640
2709
# 'tree-reference'
2641
2710
wt = self.make_branch_and_tree('tree',
2642
format='dirstate-with-subtree')
2711
format='development-subtree')
2643
2712
wt.lock_write()
2644
2713
self.addCleanup(wt.unlock)
2645
2714
sub_tree = self.make_branch_and_tree('tree/sub',
2646
format='dirstate-with-subtree')
2715
format='development-subtree')
2647
2716
wt.set_root_id('a-root-id')
2648
2717
sub_tree.set_root_id('sub-tree-root')
2649
2718
self.build_tree_contents([('tree/sub/file', 'text1')])
2682
2751
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2683
2752
# 'tree-reference'
2684
2753
wt = self.make_branch_and_tree('tree',
2685
format='dirstate-with-subtree')
2754
format='development-subtree')
2686
2755
wt.lock_write()
2687
2756
self.addCleanup(wt.unlock)
2688
2757
sub_tree = self.make_branch_and_tree('tree/sub',
2689
format='dirstate-with-subtree')
2758
format='development-subtree')
2690
2759
wt.set_root_id('a-root-id')
2691
2760
sub_tree.set_root_id('sub-tree-root')
2692
2761
self.build_tree_contents([('tree/sub/file', 'text1')])
2864
2933
def get_merger_factory(self):
2865
2934
# Allows the inner methods to access the test attributes
2868
2937
class FooMerger(_mod_merge.ConfigurableFileMerger):
2869
2938
name_prefix = "foo"
2870
2939
default_files = ['bar']
2872
2941
def merge_text(self, params):
2873
test.calls.append('merge_text')
2942
calls.append('merge_text')
2874
2943
return ('not_applicable', None)
2876
2945
def factory(merger):
3177
3246
# The dest tree is unmodified.
3178
3247
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3179
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)