1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from StringIO import StringIO
29
from bzrlib.branch import Branch
30
from bzrlib.conflicts import ConflictList, TextConflict
31
from bzrlib.errors import UnrelatedBranches, NoCommits, BzrCommandError
32
from bzrlib.merge import transform_tree, merge_inner, _PlanMerge
33
from bzrlib.osutils import pathjoin, file_kind
34
from bzrlib.revision import common_ancestor
35
from bzrlib.tests import TestCaseWithTransport, TestCaseWithMemoryTransport
36
from bzrlib.trace import (enable_test_log, disable_test_log)
37
from bzrlib.workingtree import WorkingTree
40
class TestMerge(TestCaseWithTransport):
41
"""Test appending more than one revision"""
43
def test_pending(self):
44
wt = self.make_branch_and_tree('.')
45
rev_a = wt.commit("lala!")
46
self.assertEqual([rev_a], wt.get_parent_ids())
47
self.assertRaises(errors.PointlessMerge, wt.merge_from_branch,
49
self.assertEqual([rev_a], wt.get_parent_ids())
53
wt = self.make_branch_and_tree('.')
57
wt.merge_from_branch(wt.branch, wt.branch.get_rev_id(2),
58
wt.branch.get_rev_id(1))
60
def test_nocommits(self):
61
wt = self.test_pending()
62
wt2 = self.make_branch_and_tree('branch2')
63
self.assertRaises(NoCommits, wt.merge_from_branch, wt2.branch)
66
def test_unrelated(self):
67
wt, wt2 = self.test_nocommits()
69
self.assertRaises(UnrelatedBranches, wt.merge_from_branch, wt2.branch)
72
def test_merge_one_file(self):
73
"""Do a partial merge of a tree which should not affect tree parents."""
74
wt1 = self.make_branch_and_tree('branch1')
75
tip = wt1.commit('empty commit')
76
wt2 = self.make_branch_and_tree('branch2')
78
file('branch1/foo', 'wb').write('foo')
79
file('branch1/bar', 'wb').write('bar')
82
wt1.commit('add foobar')
84
self.run_bzr('merge ../branch1/baz', retcode=3)
85
self.run_bzr('merge ../branch1/foo')
86
self.failUnlessExists('foo')
87
self.failIfExists('bar')
88
wt2 = WorkingTree.open('.') # opens branch2
89
self.assertEqual([tip], wt2.get_parent_ids())
91
def test_pending_with_null(self):
92
"""When base is forced to revno 0, parent_ids are set"""
93
wt2 = self.test_unrelated()
94
wt1 = WorkingTree.open('.')
97
# merge all of branch 2 into branch 1 even though they
99
wt1.merge_from_branch(wt2.branch, wt2.last_revision(), 'null:')
100
self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
101
wt1.get_parent_ids())
102
return (wt1, wt2.branch)
104
def test_two_roots(self):
105
"""Merge base is sane when two unrelated branches are merged"""
106
wt1, br2 = self.test_pending_with_null()
108
last = wt1.branch.last_revision()
109
self.assertEqual(common_ancestor(last, last, wt1.branch.repository), last)
111
def test_create_rename(self):
112
"""Rename an inventory entry while creating the file"""
113
tree =self.make_branch_and_tree('.')
114
file('name1', 'wb').write('Hello')
116
tree.commit(message="hello")
117
tree.rename_one('name1', 'name2')
119
transform_tree(tree, tree.branch.basis_tree())
121
def test_layered_rename(self):
122
"""Rename both child and parent at same time"""
123
tree =self.make_branch_and_tree('.')
126
filename = pathjoin('dirname1', 'name1')
127
file(filename, 'wb').write('Hello')
129
tree.commit(message="hello")
130
filename2 = pathjoin('dirname1', 'name2')
131
tree.rename_one(filename, filename2)
132
tree.rename_one('dirname1', 'dirname2')
133
transform_tree(tree, tree.branch.basis_tree())
135
def test_ignore_zero_merge_inner(self):
136
# Test that merge_inner's ignore zero parameter is effective
137
tree_a =self.make_branch_and_tree('a')
138
tree_a.commit(message="hello")
139
dir_b = tree_a.bzrdir.sprout('b')
140
tree_b = dir_b.open_workingtree()
142
self.addCleanup(tree_b.unlock)
143
tree_a.commit(message="hello again")
145
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
146
this_tree=tree_b, ignore_zero=True)
147
log = self._get_log(keep_log_file=True)
148
self.failUnless('All changes applied successfully.\n' not in log)
150
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
151
this_tree=tree_b, ignore_zero=False)
152
log = self._get_log(keep_log_file=True)
153
self.failUnless('All changes applied successfully.\n' in log)
155
def test_merge_inner_conflicts(self):
156
tree_a = self.make_branch_and_tree('a')
157
tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
158
merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
159
self.assertEqual(1, len(tree_a.conflicts()))
161
def test_rmdir_conflict(self):
162
tree_a = self.make_branch_and_tree('a')
163
self.build_tree(['a/b/'])
164
tree_a.add('b', 'b-id')
165
tree_a.commit('added b')
166
# basis_tree() is only guaranteed to be valid as long as it is actually
167
# the basis tree. This mutates the tree after grabbing basis, so go to
169
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
170
tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
171
self.build_tree(['a/b/c'])
173
tree_a.commit('added c')
175
tree_z.commit('removed b')
176
merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
178
conflicts.MissingParent('Created directory', 'b', 'b-id'),
179
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
181
merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
184
conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
185
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
188
def test_nested_merge(self):
189
tree = self.make_branch_and_tree('tree',
190
format='dirstate-with-subtree')
191
sub_tree = self.make_branch_and_tree('tree/sub-tree',
192
format='dirstate-with-subtree')
193
sub_tree.set_root_id('sub-tree-root')
194
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
196
sub_tree.commit('foo')
197
tree.add_reference(sub_tree)
198
tree.commit('set text to 1')
199
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
200
# modify the file in the subtree
201
self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
202
# and merge the changes from the diverged subtree into the containing
204
tree2.commit('changed file text')
205
tree.merge_from_branch(tree2.branch)
206
self.assertFileEqual('text2', 'tree/sub-tree/file')
208
def test_merge_with_missing(self):
209
tree_a = self.make_branch_and_tree('tree_a')
210
self.build_tree_contents([('tree_a/file', 'content_1')])
212
tree_a.commit('commit base')
213
# basis_tree() is only guaranteed to be valid as long as it is actually
214
# the basis tree. This mutates the tree after grabbing basis, so go to
216
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
217
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
218
self.build_tree_contents([('tree_a/file', 'content_2')])
219
tree_a.commit('commit other')
220
other_tree = tree_a.basis_tree()
221
os.unlink('tree_b/file')
222
merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
224
def test_merge_kind_change(self):
225
tree_a = self.make_branch_and_tree('tree_a')
226
self.build_tree_contents([('tree_a/file', 'content_1')])
227
tree_a.add('file', 'file-id')
228
tree_a.commit('added file')
229
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
230
os.unlink('tree_a/file')
231
self.build_tree(['tree_a/file/'])
232
tree_a.commit('changed file to directory')
233
tree_b.merge_from_branch(tree_a.branch)
234
self.assertEqual('directory', file_kind('tree_b/file'))
236
self.assertEqual('file', file_kind('tree_b/file'))
237
self.build_tree_contents([('tree_b/file', 'content_2')])
238
tree_b.commit('content change')
239
tree_b.merge_from_branch(tree_a.branch)
240
self.assertEqual(tree_b.conflicts(),
241
[conflicts.ContentsConflict('file',
244
def test_merge_type_registry(self):
245
merge_type_option = option.Option.OPTIONS['merge-type']
246
self.assertFalse('merge4' in [x[0] for x in
247
merge_type_option.iter_switches()])
248
registry = _mod_merge.get_merge_type_registry()
249
registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
250
'time-travelling merge')
251
self.assertTrue('merge4' in [x[0] for x in
252
merge_type_option.iter_switches()])
253
registry.remove('merge4')
254
self.assertFalse('merge4' in [x[0] for x in
255
merge_type_option.iter_switches()])
257
def test_merge_other_moves_we_deleted(self):
258
tree_a = self.make_branch_and_tree('A')
260
self.addCleanup(tree_a.unlock)
261
self.build_tree(['A/a'])
263
tree_a.commit('1', rev_id='rev-1')
265
tree_a.rename_one('a', 'b')
267
bzrdir_b = tree_a.bzrdir.sprout('B', revision_id='rev-1')
268
tree_b = bzrdir_b.open_workingtree()
270
self.addCleanup(tree_b.unlock)
274
tree_b.merge_from_branch(tree_a.branch)
275
except AttributeError:
276
self.fail('tried to join a path when name was None')
278
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
279
tree_a = self.make_branch_and_tree('a')
280
self.build_tree(['a/file_1', 'a/file_2'])
281
tree_a.add(['file_1'])
282
tree_a.commit('commit 1')
283
tree_a.add(['file_2'])
284
tree_a.commit('commit 2')
285
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
286
tree_b.rename_one('file_1', 'renamed')
287
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
288
progress.DummyProgress())
289
merger.merge_type = _mod_merge.Merge3Merger
291
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
293
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
294
tree_a = self.make_branch_and_tree('a')
295
self.build_tree(['a/file_1', 'a/file_2'])
296
tree_a.add(['file_1'])
297
tree_a.commit('commit 1')
298
tree_a.add(['file_2'])
299
tree_a.commit('commit 2')
300
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
301
tree_b.rename_one('file_1', 'renamed')
302
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
303
progress.DummyProgress())
304
merger.merge_type = _mod_merge.WeaveMerger
306
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
308
def prepare_cherrypick(self):
309
"""Prepare a pair of trees for cherrypicking tests.
311
Both trees have a file, 'file'.
312
rev1 sets content to 'a'.
315
A full merge of rev2b and rev3b into this_tree would add both 'b' and
316
'c'. A successful cherrypick of rev2b-rev3b into this_tree will add
319
this_tree = self.make_branch_and_tree('this')
320
self.build_tree_contents([('this/file', "a\n")])
321
this_tree.add('file')
322
this_tree.commit('rev1')
323
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
324
self.build_tree_contents([('other/file', "a\nb\n")])
325
other_tree.commit('rev2b', rev_id='rev2b')
326
self.build_tree_contents([('other/file', "c\na\nb\n")])
327
other_tree.commit('rev3b', rev_id='rev3b')
328
this_tree.lock_write()
329
self.addCleanup(this_tree.unlock)
330
return this_tree, other_tree
332
def test_weave_cherrypick(self):
333
this_tree, other_tree = self.prepare_cherrypick()
334
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
335
this_tree, 'rev3b', 'rev2b', other_tree.branch)
336
merger.merge_type = _mod_merge.WeaveMerger
338
self.assertFileEqual('c\na\n', 'this/file')
340
def test_weave_cannot_reverse_cherrypick(self):
341
this_tree, other_tree = self.prepare_cherrypick()
342
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
343
this_tree, 'rev2b', 'rev3b', other_tree.branch)
344
merger.merge_type = _mod_merge.WeaveMerger
345
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
347
def test_merge3_can_reverse_cherrypick(self):
348
this_tree, other_tree = self.prepare_cherrypick()
349
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
350
this_tree, 'rev2b', 'rev3b', other_tree.branch)
351
merger.merge_type = _mod_merge.Merge3Merger
355
class TestPlanMerge(TestCaseWithMemoryTransport):
358
TestCaseWithMemoryTransport.setUp(self)
359
self.vf = knit.KnitVersionedFile('root', self.get_transport(),
361
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root',
364
def add_version(self, version_id, parents, text):
365
self.vf.add_lines(version_id, parents, [c+'\n' for c in text])
367
def add_uncommitted_version(self, version_id, parents, text):
368
self.plan_merge_vf.add_lines(version_id, parents,
369
[c+'\n' for c in text])
371
def setup_plan_merge(self):
372
self.add_version('A', [], 'abc')
373
self.add_version('B', ['A'], 'acehg')
374
self.add_version('C', ['A'], 'fabg')
375
return _PlanMerge('B', 'C', self.plan_merge_vf)
377
def setup_plan_merge_uncommitted(self):
378
self.add_version('A', [], 'abc')
379
self.add_uncommitted_version('B:', ['A'], 'acehg')
380
self.add_uncommitted_version('C:', ['A'], 'fabg')
381
return _PlanMerge('B:', 'C:', self.plan_merge_vf)
383
def test_unique_lines(self):
384
plan = self.setup_plan_merge()
385
self.assertEqual(plan._unique_lines(
386
plan._get_matching_blocks('B', 'C')),
389
def test_find_new(self):
390
plan = self.setup_plan_merge()
391
self.assertEqual(set([2, 3, 4]), plan._find_new('B'))
392
self.assertEqual(set([0, 3]), plan._find_new('C'))
394
def test_find_new2(self):
395
self.add_version('A', [], 'abc')
396
self.add_version('B', ['A'], 'abcde')
397
self.add_version('C', ['A'], 'abcefg')
398
self.add_version('D', ['A', 'B', 'C'], 'abcdegh')
399
my_plan = _PlanMerge('B', 'D', self.plan_merge_vf)
400
self.assertEqual(set([5, 6]), my_plan._find_new('D'))
401
self.assertEqual(set(), my_plan._find_new('A'))
403
def test_find_new_no_ancestors(self):
404
self.add_version('A', [], 'abc')
405
self.add_version('B', [], 'xyz')
406
my_plan = _PlanMerge('A', 'B', self.vf)
407
self.assertEqual(set([0, 1, 2]), my_plan._find_new('A'))
409
def test_plan_merge(self):
410
self.setup_plan_merge()
411
plan = self.plan_merge_vf.plan_merge('B', 'C')
414
('unchanged', 'a\n'),
419
('unchanged', 'g\n')],
422
def test_plan_merge_uncommitted_files(self):
423
self.setup_plan_merge_uncommitted()
424
plan = self.plan_merge_vf.plan_merge('B:', 'C:')
427
('unchanged', 'a\n'),
432
('unchanged', 'g\n')],
435
def test_subtract_plans(self):
437
('unchanged', 'a\n'),
446
('unchanged', 'a\n'),
455
('unchanged', 'a\n'),
459
('unchanged', 'f\n'),
462
self.assertEqual(subtracted_plan,
463
list(_PlanMerge._subtract_plans(old_plan, new_plan)))
465
def setup_merge_with_base(self):
466
self.add_version('COMMON', [], 'abc')
467
self.add_version('THIS', ['COMMON'], 'abcd')
468
self.add_version('BASE', ['COMMON'], 'eabc')
469
self.add_version('OTHER', ['BASE'], 'eafb')
471
def test_plan_merge_with_base(self):
472
self.setup_merge_with_base()
473
plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
474
self.assertEqual([('unchanged', 'a\n'),
476
('unchanged', 'b\n'),
481
def test_plan_lca_merge(self):
482
self.setup_plan_merge()
483
plan = self.plan_merge_vf.plan_lca_merge('B', 'C')
486
('unchanged', 'a\n'),
491
('unchanged', 'g\n')],
494
def test_plan_lca_merge_uncommitted_files(self):
495
self.setup_plan_merge_uncommitted()
496
plan = self.plan_merge_vf.plan_lca_merge('B:', 'C:')
499
('unchanged', 'a\n'),
504
('unchanged', 'g\n')],
507
def test_plan_lca_merge_with_base(self):
508
self.setup_merge_with_base()
509
plan = self.plan_merge_vf.plan_lca_merge('THIS', 'OTHER', 'BASE')
510
self.assertEqual([('unchanged', 'a\n'),
512
('unchanged', 'b\n'),
517
def test_plan_lca_merge_with_criss_cross(self):
518
self.add_version('ROOT', [], 'abc')
519
# each side makes a change
520
self.add_version('REV1', ['ROOT'], 'abcd')
521
self.add_version('REV2', ['ROOT'], 'abce')
522
# both sides merge, discarding others' changes
523
self.add_version('LCA1', ['REV1', 'REV2'], 'abcd')
524
self.add_version('LCA2', ['REV1', 'REV2'], 'abce')
525
plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
526
self.assertEqual([('unchanged', 'a\n'),
527
('unchanged', 'b\n'),
528
('unchanged', 'c\n'),
529
('conflicted-a', 'd\n'),
530
('conflicted-b', 'e\n'),