1
# Copyright (C) 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
17
# Author: Aaron Bentley <aaron.bentley@utoronto.ca>
19
"""Black-box tests for bzr merge.
24
from bzrlib import merge_directive
25
from bzrlib.branch import Branch
26
from bzrlib.bzrdir import BzrDir
27
from bzrlib.conflicts import ConflictList, ContentsConflict
28
from bzrlib.osutils import abspath, file_kind, pathjoin
29
from bzrlib.tests.blackbox import ExternalBase
30
import bzrlib.urlutils as urlutils
31
from bzrlib.workingtree import WorkingTree
34
class TestMerge(ExternalBase):
36
def example_branch(self, path='.'):
37
tree = self.make_branch_and_tree(path)
38
self.build_tree_contents([
39
(pathjoin(path, 'hello'), 'foo'),
40
(pathjoin(path, 'goodbye'), 'baz')])
42
tree.commit(message='setup')
44
tree.commit(message='setup')
47
def test_merge_reprocess(self):
48
d = BzrDir.create_standalone_workingtree('.')
50
self.run_bzr('merge . --reprocess --merge-type weave')
53
from bzrlib.branch import Branch
55
a_tree = self.example_branch('a')
56
ancestor = a_tree.branch.revno()
57
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
58
self.build_tree_contents([('b/goodbye', 'quux')])
59
b_tree.commit(message="more u's are always good")
61
self.build_tree_contents([('a/hello', 'quuux')])
62
# We can't merge when there are in-tree changes
64
self.run_bzr('merge ../b', retcode=3)
65
a = WorkingTree.open('.')
66
a_tip = a.commit("Like an epidemic of u's")
67
self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
69
self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
70
a_tree.revert(backups=False)
71
self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
72
a_tree.revert(backups=False)
73
self.run_bzr_error(['Show-base is not supported for this merge type'],
74
'merge ../b -r last:1..last:1 --merge-type weave'
76
a_tree.revert(backups=False)
77
self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
78
a_tree.revert(backups=False)
79
self.run_bzr('merge ../b -r last:1')
80
self.check_file_contents('goodbye', 'quux')
81
# Merging a branch pulls its revision into the tree
82
b = Branch.open('../b')
83
b_tip = b.last_revision()
84
self.failUnless(a.branch.repository.has_revision(b_tip))
85
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
86
a_tree.revert(backups=False)
87
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
88
self.assertTrue("Not a branch" in err)
89
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
90
%(ancestor,b.revno()))
91
self.assertEquals(a.get_parent_ids(),
92
[a.branch.last_revision(), b.last_revision()])
93
self.check_file_contents('goodbye', 'quux')
94
a_tree.revert(backups=False)
95
self.run_bzr('merge -r revno:%d:../b'%b.revno())
96
self.assertEquals(a.get_parent_ids(),
97
[a.branch.last_revision(), b.last_revision()])
98
a_tip = a.commit('merged')
99
self.run_bzr('merge ../b -r last:1')
100
self.assertEqual([a_tip], a.get_parent_ids())
102
def test_merge_with_missing_file(self):
103
"""Merge handles missing file conflicts"""
104
self.build_tree_contents([
107
('a/sub/a.txt', 'hello\n'),
108
('a/b.txt', 'hello\n'),
109
('a/sub/c.txt', 'hello\n')])
110
a_tree = self.make_branch_and_tree('a')
111
a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
112
a_tree.commit(message='added a')
113
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
114
self.build_tree_contents([
115
('a/sub/a.txt', 'hello\nthere\n'),
116
('a/b.txt', 'hello\nthere\n'),
117
('a/sub/c.txt', 'hello\nthere\n')])
118
a_tree.commit(message='Added there')
119
os.remove('a/sub/a.txt')
120
os.remove('a/sub/c.txt')
123
a_tree.commit(message='Removed a.txt')
124
self.build_tree_contents([
125
('b/sub/a.txt', 'hello\nsomething\n'),
126
('b/b.txt', 'hello\nsomething\n'),
127
('b/sub/c.txt', 'hello\nsomething\n')])
128
b_tree.commit(message='Modified a.txt')
130
self.run_bzr('merge ../a/', retcode=1)
131
self.failUnlessExists('sub/a.txt.THIS')
132
self.failUnlessExists('sub/a.txt.BASE')
134
self.run_bzr('merge ../b/', retcode=1)
135
self.failUnlessExists('sub/a.txt.OTHER')
136
self.failUnlessExists('sub/a.txt.BASE')
138
def test_merge_remember(self):
139
"""Merge changes from one branch to another and test parent location."""
140
tree_a = self.make_branch_and_tree('branch_a')
141
branch_a = tree_a.branch
142
self.build_tree(['branch_a/a'])
144
tree_a.commit('commit a')
145
branch_b = branch_a.bzrdir.sprout('branch_b').open_branch()
146
tree_b = branch_b.bzrdir.open_workingtree()
147
branch_c = branch_a.bzrdir.sprout('branch_c').open_branch()
148
tree_c = branch_c.bzrdir.open_workingtree()
149
self.build_tree(['branch_a/b'])
151
tree_a.commit('commit b')
152
self.build_tree(['branch_c/c'])
154
tree_c.commit('commit c')
156
parent = branch_b.get_parent()
157
branch_b.set_parent(None)
158
self.assertEqual(None, branch_b.get_parent())
159
# test merge for failure without parent set
161
out = self.run_bzr('merge', retcode=3)
162
self.assertEquals(out,
163
('','bzr: ERROR: No location specified or remembered\n'))
164
# test implicit --remember when no parent set, this merge conflicts
165
self.build_tree(['d'])
167
out = self.run_bzr('merge ../branch_a', retcode=3)
168
self.assertEquals(out,
169
('','bzr: ERROR: Working tree has uncommitted changes.\n'))
170
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
171
# test implicit --remember after resolving conflict
172
tree_b.commit('commit d')
173
out, err = self.run_bzr('merge')
175
base = urlutils.local_path_from_url(branch_a.base)
176
self.assertEquals(out, 'Merging from remembered location %s\n' % (base,))
177
self.assertEquals(err, '+N b\nAll changes applied successfully.\n')
178
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
179
# re-open tree as external run_bzr modified it
180
tree_b = branch_b.bzrdir.open_workingtree()
181
tree_b.commit('merge branch_a')
182
# test explicit --remember
183
out, err = self.run_bzr('merge ../branch_c --remember')
184
self.assertEquals(out, '')
185
self.assertEquals(err, '+N c\nAll changes applied successfully.\n')
186
self.assertEquals(abspath(branch_b.get_parent()),
187
abspath(branch_c.bzrdir.root_transport.base))
188
# re-open tree as external run_bzr modified it
189
tree_b = branch_b.bzrdir.open_workingtree()
190
tree_b.commit('merge branch_c')
192
def test_merge_bundle(self):
193
from bzrlib.testament import Testament
194
tree_a = self.make_branch_and_tree('branch_a')
195
self.build_tree_contents([('branch_a/a', 'hello')])
197
tree_a.commit('message')
199
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
200
self.build_tree_contents([('branch_a/a', 'hey there')])
201
tree_a.commit('message')
203
self.build_tree_contents([('branch_b/a', 'goodbye')])
204
tree_b.commit('message')
206
self.run_bzr('bundle ../branch_a -o ../bundle')
207
os.chdir('../branch_a')
208
self.run_bzr('merge ../bundle', retcode=1)
209
testament_a = Testament.from_revision(tree_a.branch.repository,
210
tree_b.get_parent_ids()[0])
211
testament_b = Testament.from_revision(tree_b.branch.repository,
212
tree_b.get_parent_ids()[0])
213
self.assertEqualDiff(testament_a.as_text(),
214
testament_b.as_text())
215
tree_a.set_conflicts(ConflictList())
216
tree_a.commit('message')
217
# it is legal to attempt to merge an already-merged bundle
218
output = self.run_bzr('merge ../bundle')[1]
219
# but it does nothing
220
self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
221
self.assertEqual('Nothing to do.\n', output)
223
def test_merge_uncommitted(self):
224
"""Check that merge --uncommitted behaves properly"""
225
tree_a = self.make_branch_and_tree('a')
226
self.build_tree(['a/file_1', 'a/file_2'])
227
tree_a.add(['file_1', 'file_2'])
228
tree_a.commit('commit 1')
229
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
230
self.failUnlessExists('b/file_1')
231
tree_a.rename_one('file_1', 'file_i')
232
tree_a.commit('commit 2')
233
tree_a.rename_one('file_2', 'file_ii')
235
self.run_bzr('merge a --uncommitted -d b')
236
self.failUnlessExists('b/file_1')
237
self.failUnlessExists('b/file_ii')
239
self.run_bzr_error(('Cannot use --uncommitted and --revision',),
240
'merge /a --uncommitted -r1 -d b')
242
def pullable_branch(self):
243
tree_a = self.make_branch_and_tree('a')
244
self.build_tree(['a/file'])
246
self.id1 = tree_a.commit('commit 1')
248
tree_b = self.make_branch_and_tree('b')
249
tree_b.pull(tree_a.branch)
250
file('b/file', 'wb').write('foo')
251
self.id2 = tree_b.commit('commit 2')
253
def test_merge_pull(self):
254
self.pullable_branch()
256
(out, err) = self.run_bzr('merge --pull ../b')
257
self.assertContainsRe(out, 'Now on revision 2\\.')
258
tree_a = WorkingTree.open('.')
259
self.assertEqual([self.id2], tree_a.get_parent_ids())
261
def test_merge_kind_change(self):
262
tree_a = self.make_branch_and_tree('tree_a')
263
self.build_tree_contents([('tree_a/file', 'content_1')])
264
tree_a.add('file', 'file-id')
265
tree_a.commit('added file')
266
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
267
os.unlink('tree_a/file')
268
self.build_tree(['tree_a/file/'])
269
tree_a.commit('changed file to directory')
271
self.run_bzr('merge ../tree_a')
272
self.assertEqual('directory', file_kind('file'))
274
self.assertEqual('file', file_kind('file'))
275
self.build_tree_contents([('file', 'content_2')])
276
tree_b.commit('content change')
277
self.run_bzr('merge ../tree_a', retcode=1)
278
self.assertEqual(tree_b.conflicts(),
279
[ContentsConflict('file', file_id='file-id')])
281
def test_directive_cherrypick(self):
282
source = self.make_branch_and_tree('source')
283
self.build_tree(['source/a'])
285
source.commit('Added a', rev_id='rev1')
286
self.build_tree(['source/b'])
288
source.commit('Added b', rev_id='rev2')
289
target = self.make_branch_and_tree('target')
290
target.commit('empty commit')
291
self.write_directive('directive', source.branch, 'target', 'rev2',
293
self.run_bzr('merge -d target directive')
294
self.failIfExists('target/a')
295
self.failUnlessExists('target/b')
297
def write_directive(self, filename, source, target, revision_id,
298
base_revision_id=None, mangle_patch=False):
299
md = merge_directive.MergeDirective2.from_objects(
300
source.repository, revision_id, 0, 0, target,
301
base_revision_id=base_revision_id)
304
self.build_tree_contents([(filename, ''.join(md.to_lines()))])
306
def test_directive_verify_warning(self):
307
source = self.make_branch_and_tree('source')
308
self.build_tree(['source/a'])
310
source.commit('Added a', rev_id='rev1')
311
target = self.make_branch_and_tree('target')
312
target.commit('empty commit')
313
self.write_directive('directive', source.branch, 'target', 'rev1')
314
err = self.run_bzr('merge -d target directive')[1]
315
self.assertNotContainsRe(err, 'Preview patch does not match changes')
317
self.write_directive('directive', source.branch, 'target', 'rev1',
319
err = self.run_bzr('merge -d target directive')[1]
320
self.assertContainsRe(err, 'Preview patch does not match changes')
322
def test_merge_arbitrary(self):
323
target = self.make_branch_and_tree('target')
324
target.commit('empty')
325
# We need a revision that has no integer revno
326
branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
327
self.build_tree(['branch_a/file1'])
328
branch_a.add('file1')
329
branch_a.commit('added file1', rev_id='rev2a')
330
branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
331
self.build_tree(['branch_b/file2'])
332
branch_b.add('file2')
333
branch_b.commit('added file2', rev_id='rev2b')
334
branch_b.merge_from_branch(branch_a.branch)
335
self.failUnlessExists('branch_b/file1')
336
branch_b.commit('merged branch_a', rev_id='rev3b')
338
# It works if the revid has an interger revno
339
self.run_bzr('merge -d target -r revid:rev2a branch_a')
340
self.failUnlessExists('target/file1')
341
self.failIfExists('target/file2')
344
# It should work if the revid has no integer revno
345
self.run_bzr('merge -d target -r revid:rev2a branch_b')
346
self.failUnlessExists('target/file1')
347
self.failIfExists('target/file2')