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()
141
tree_a.commit(message="hello again")
143
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
144
this_tree=tree_b, ignore_zero=True)
145
log = self._get_log(keep_log_file=True)
146
self.failUnless('All changes applied successfully.\n' not in log)
148
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
149
this_tree=tree_b, ignore_zero=False)
150
log = self._get_log(keep_log_file=True)
151
self.failUnless('All changes applied successfully.\n' in log)
153
def test_merge_inner_conflicts(self):
154
tree_a = self.make_branch_and_tree('a')
155
tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
156
merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
157
self.assertEqual(1, len(tree_a.conflicts()))
159
def test_rmdir_conflict(self):
160
tree_a = self.make_branch_and_tree('a')
161
self.build_tree(['a/b/'])
162
tree_a.add('b', 'b-id')
163
tree_a.commit('added b')
164
# basis_tree() is only guaranteed to be valid as long as it is actually
165
# the basis tree. This mutates the tree after grabbing basis, so go to
167
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
168
tree_z = tree_a.bzrdir.sprout('z').open_workingtree()
169
self.build_tree(['a/b/c'])
171
tree_a.commit('added c')
173
tree_z.commit('removed b')
174
merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
176
conflicts.MissingParent('Created directory', 'b', 'b-id'),
177
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
179
merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
182
conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
183
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
186
def test_nested_merge(self):
187
tree = self.make_branch_and_tree('tree',
188
format='dirstate-with-subtree')
189
sub_tree = self.make_branch_and_tree('tree/sub-tree',
190
format='dirstate-with-subtree')
191
sub_tree.set_root_id('sub-tree-root')
192
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
194
sub_tree.commit('foo')
195
tree.add_reference(sub_tree)
196
tree.commit('set text to 1')
197
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
198
# modify the file in the subtree
199
self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
200
# and merge the changes from the diverged subtree into the containing
202
tree2.commit('changed file text')
203
tree.merge_from_branch(tree2.branch)
204
self.assertFileEqual('text2', 'tree/sub-tree/file')
206
def test_merge_with_missing(self):
207
tree_a = self.make_branch_and_tree('tree_a')
208
self.build_tree_contents([('tree_a/file', 'content_1')])
210
tree_a.commit('commit base')
211
# basis_tree() is only guaranteed to be valid as long as it is actually
212
# the basis tree. This mutates the tree after grabbing basis, so go to
214
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
215
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
216
self.build_tree_contents([('tree_a/file', 'content_2')])
217
tree_a.commit('commit other')
218
other_tree = tree_a.basis_tree()
219
os.unlink('tree_b/file')
220
merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
222
def test_merge_kind_change(self):
223
tree_a = self.make_branch_and_tree('tree_a')
224
self.build_tree_contents([('tree_a/file', 'content_1')])
225
tree_a.add('file', 'file-id')
226
tree_a.commit('added file')
227
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
228
os.unlink('tree_a/file')
229
self.build_tree(['tree_a/file/'])
230
tree_a.commit('changed file to directory')
231
tree_b.merge_from_branch(tree_a.branch)
232
self.assertEqual('directory', file_kind('tree_b/file'))
234
self.assertEqual('file', file_kind('tree_b/file'))
235
self.build_tree_contents([('tree_b/file', 'content_2')])
236
tree_b.commit('content change')
237
tree_b.merge_from_branch(tree_a.branch)
238
self.assertEqual(tree_b.conflicts(),
239
[conflicts.ContentsConflict('file',
242
def test_merge_type_registry(self):
243
merge_type_option = option.Option.OPTIONS['merge-type']
244
self.assertFalse('merge4' in [x[0] for x in
245
merge_type_option.iter_switches()])
246
registry = _mod_merge.get_merge_type_registry()
247
registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
248
'time-travelling merge')
249
self.assertTrue('merge4' in [x[0] for x in
250
merge_type_option.iter_switches()])
251
registry.remove('merge4')
252
self.assertFalse('merge4' in [x[0] for x in
253
merge_type_option.iter_switches()])
255
def test_merge_other_moves_we_deleted(self):
256
tree_a = self.make_branch_and_tree('A')
258
self.addCleanup(tree_a.unlock)
259
self.build_tree(['A/a'])
261
tree_a.commit('1', rev_id='rev-1')
263
tree_a.rename_one('a', 'b')
265
bzrdir_b = tree_a.bzrdir.sprout('B', revision_id='rev-1')
266
tree_b = bzrdir_b.open_workingtree()
268
self.addCleanup(tree_b.unlock)
272
tree_b.merge_from_branch(tree_a.branch)
273
except AttributeError:
274
self.fail('tried to join a path when name was None')
276
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
277
tree_a = self.make_branch_and_tree('a')
278
self.build_tree(['a/file_1', 'a/file_2'])
279
tree_a.add(['file_1'])
280
tree_a.commit('commit 1')
281
tree_a.add(['file_2'])
282
tree_a.commit('commit 2')
283
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
284
tree_b.rename_one('file_1', 'renamed')
285
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b,
286
progress.DummyProgress())
287
merger.merge_type = _mod_merge.Merge3Merger
289
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
292
class TestPlanMerge(TestCaseWithMemoryTransport):
295
TestCaseWithMemoryTransport.setUp(self)
296
self.vf = knit.KnitVersionedFile('root', self.get_transport(),
298
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root',
301
def add_version(self, version_id, parents, text):
302
self.vf.add_lines(version_id, parents, [c+'\n' for c in text])
304
def add_uncommitted_version(self, version_id, parents, text):
305
self.plan_merge_vf.add_lines(version_id, parents,
306
[c+'\n' for c in text])
308
def setup_plan_merge(self):
309
self.add_version('A', [], 'abc')
310
self.add_version('B', ['A'], 'acehg')
311
self.add_version('C', ['A'], 'fabg')
312
return _PlanMerge('B', 'C', self.plan_merge_vf)
314
def setup_plan_merge_uncommitted(self):
315
self.add_version('A', [], 'abc')
316
self.add_uncommitted_version('B:', ['A'], 'acehg')
317
self.add_uncommitted_version('C:', ['A'], 'fabg')
318
return _PlanMerge('B:', 'C:', self.plan_merge_vf)
320
def test_unique_lines(self):
321
plan = self.setup_plan_merge()
322
self.assertEqual(plan._unique_lines(
323
plan._get_matching_blocks('B', 'C')),
326
def test_find_new(self):
327
plan = self.setup_plan_merge()
328
self.assertEqual(set([2, 3, 4]), plan._find_new('B'))
329
self.assertEqual(set([0, 3]), plan._find_new('C'))
331
def test_find_new2(self):
332
self.add_version('A', [], 'abc')
333
self.add_version('B', ['A'], 'abcde')
334
self.add_version('C', ['A'], 'abcefg')
335
self.add_version('D', ['A', 'B', 'C'], 'abcdegh')
336
my_plan = _PlanMerge('B', 'D', self.plan_merge_vf)
337
self.assertEqual(set([5, 6]), my_plan._find_new('D'))
338
self.assertEqual(set(), my_plan._find_new('A'))
340
def test_find_new_no_ancestors(self):
341
self.add_version('A', [], 'abc')
342
self.add_version('B', [], 'xyz')
343
my_plan = _PlanMerge('A', 'B', self.vf)
344
self.assertEqual(set([0, 1, 2]), my_plan._find_new('A'))
346
def test_plan_merge(self):
347
self.setup_plan_merge()
348
plan = self.plan_merge_vf.plan_merge('B', 'C')
351
('unchanged', 'a\n'),
356
('unchanged', 'g\n')],
359
def test_plan_merge_uncommitted_files(self):
360
self.setup_plan_merge_uncommitted()
361
plan = self.plan_merge_vf.plan_merge('B:', 'C:')
364
('unchanged', 'a\n'),
369
('unchanged', 'g\n')],