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
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(test):
38
file('hello', 'wt').write('foo')
39
test.run_bzr('add hello')
40
test.run_bzr('commit -m setup hello')
41
file('goodbye', 'wt').write('baz')
42
test.run_bzr('add goodbye')
43
test.run_bzr('commit -m setup goodbye')
45
def test_merge_reprocess(self):
46
d = BzrDir.create_standalone_workingtree('.')
48
self.run_bzr('merge . --reprocess --merge-type weave')
51
from bzrlib.branch import Branch
56
ancestor = Branch.open('.').revno()
58
self.run_bzr('branch a b')
60
file('goodbye', 'wt').write('quux')
61
self.run_bzr(['commit', '-m', "more u's are always good"])
64
file('hello', 'wt').write('quuux')
65
# We can't merge when there are in-tree changes
66
self.run_bzr('merge ../b', retcode=3)
67
a = WorkingTree.open('.')
68
a_tip = a.commit("Like an epidemic of u's")
69
self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
71
self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
72
self.run_bzr('revert --no-backup')
73
self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
74
self.run_bzr('revert --no-backup')
75
self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
76
self.run_bzr('revert --no-backup')
77
self.run_bzr('merge ../b -r last:1')
78
self.check_file_contents('goodbye', 'quux')
79
# Merging a branch pulls its revision into the tree
80
b = Branch.open('../b')
81
b_tip = b.last_revision()
82
self.failUnless(a.branch.repository.has_revision(b_tip))
83
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
84
self.run_bzr('revert --no-backup')
85
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
86
self.assertTrue("Not a branch" in err)
87
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
88
%(ancestor,b.revno()))
89
self.assertEquals(a.get_parent_ids(),
90
[a.branch.last_revision(), b.last_revision()])
91
self.check_file_contents('goodbye', 'quux')
92
self.run_bzr('revert --no-backup')
93
self.run_bzr('merge -r revno:%d:../b'%b.revno())
94
self.assertEquals(a.get_parent_ids(),
95
[a.branch.last_revision(), b.last_revision()])
96
a_tip = a.commit('merged')
97
self.run_bzr('merge ../b -r last:1')
98
self.assertEqual([a_tip], a.get_parent_ids())
100
def test_merge_with_missing_file(self):
101
"""Merge handles missing file conflicts"""
105
print >> file('sub/a.txt', 'wb'), "hello"
106
print >> file('b.txt', 'wb'), "hello"
107
print >> file('sub/c.txt', 'wb'), "hello"
110
self.run_bzr(['commit', '-m', 'added a'])
111
self.run_bzr('branch . ../b')
112
print >> file('sub/a.txt', 'ab'), "there"
113
print >> file('b.txt', 'ab'), "there"
114
print >> file('sub/c.txt', 'ab'), "there"
115
self.run_bzr(['commit', '-m', 'Added there'])
116
os.unlink('sub/a.txt')
117
os.unlink('sub/c.txt')
120
self.run_bzr(['commit', '-m', 'Removed a.txt'])
122
print >> file('sub/a.txt', 'ab'), "something"
123
print >> file('b.txt', 'ab'), "something"
124
print >> file('sub/c.txt', 'ab'), "something"
125
self.run_bzr(['commit', '-m', 'Modified a.txt'])
126
self.run_bzr('merge ../a/', retcode=1)
127
self.assert_(os.path.exists('sub/a.txt.THIS'))
128
self.assert_(os.path.exists('sub/a.txt.BASE'))
130
self.run_bzr('merge ../b/', retcode=1)
131
self.assert_(os.path.exists('sub/a.txt.OTHER'))
132
self.assert_(os.path.exists('sub/a.txt.BASE'))
134
def test_merge_remember(self):
135
"""Merge changes from one branch to another and test parent location."""
136
tree_a = self.make_branch_and_tree('branch_a')
137
branch_a = tree_a.branch
138
self.build_tree(['branch_a/a'])
140
tree_a.commit('commit a')
141
branch_b = branch_a.bzrdir.sprout('branch_b').open_branch()
142
tree_b = branch_b.bzrdir.open_workingtree()
143
branch_c = branch_a.bzrdir.sprout('branch_c').open_branch()
144
tree_c = branch_c.bzrdir.open_workingtree()
145
self.build_tree(['branch_a/b'])
147
tree_a.commit('commit b')
148
self.build_tree(['branch_c/c'])
150
tree_c.commit('commit c')
152
parent = branch_b.get_parent()
153
branch_b.set_parent(None)
154
self.assertEqual(None, branch_b.get_parent())
155
# test merge for failure without parent set
157
out = self.run_bzr('merge', retcode=3)
158
self.assertEquals(out,
159
('','bzr: ERROR: No location specified or remembered\n'))
160
# test implicit --remember when no parent set, this merge conflicts
161
self.build_tree(['d'])
163
out = self.run_bzr('merge ../branch_a', retcode=3)
164
self.assertEquals(out,
165
('','bzr: ERROR: Working tree has uncommitted changes.\n'))
166
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
167
# test implicit --remember after resolving conflict
168
tree_b.commit('commit d')
169
out, err = self.run_bzr('merge')
171
base = urlutils.local_path_from_url(branch_a.base)
172
self.assertEquals(out, 'Merging from remembered location %s\n' % (base,))
173
self.assertEquals(err, '+N b\nAll changes applied successfully.\n')
174
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
175
# re-open tree as external run_bzr modified it
176
tree_b = branch_b.bzrdir.open_workingtree()
177
tree_b.commit('merge branch_a')
178
# test explicit --remember
179
out, err = self.run_bzr('merge ../branch_c --remember')
180
self.assertEquals(out, '')
181
self.assertEquals(err, '+N c\nAll changes applied successfully.\n')
182
self.assertEquals(abspath(branch_b.get_parent()),
183
abspath(branch_c.bzrdir.root_transport.base))
184
# re-open tree as external run_bzr modified it
185
tree_b = branch_b.bzrdir.open_workingtree()
186
tree_b.commit('merge branch_c')
188
def test_merge_bundle(self):
189
from bzrlib.testament import Testament
190
tree_a = self.make_branch_and_tree('branch_a')
191
f = file('branch_a/a', 'wb')
195
tree_a.commit('message')
197
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
198
f = file('branch_a/a', 'wb')
201
tree_a.commit('message')
203
f = file('branch_b/a', 'wb')
206
tree_b.commit('message')
208
self.run_bzr('bundle ../branch_a -o ../bundle')
209
os.chdir('../branch_a')
210
self.run_bzr('merge ../bundle', retcode=1)
211
testament_a = Testament.from_revision(tree_a.branch.repository,
212
tree_b.get_parent_ids()[0])
213
testament_b = Testament.from_revision(tree_b.branch.repository,
214
tree_b.get_parent_ids()[0])
215
self.assertEqualDiff(testament_a.as_text(),
216
testament_b.as_text())
217
tree_a.set_conflicts(ConflictList())
218
tree_a.commit('message')
219
# it is legal to attempt to merge an already-merged bundle
220
output = self.run_bzr('merge ../bundle')[1]
221
# but it does nothing
222
self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
223
self.assertEqual('Nothing to do.\n', output)
225
def test_merge_uncommitted(self):
226
"""Check that merge --uncommitted behaves properly"""
227
tree_a = self.make_branch_and_tree('a')
228
self.build_tree(['a/file_1', 'a/file_2'])
229
tree_a.add(['file_1', 'file_2'])
230
tree_a.commit('commit 1')
231
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
232
self.failUnlessExists('b/file_1')
233
tree_a.rename_one('file_1', 'file_i')
234
tree_a.commit('commit 2')
235
tree_a.rename_one('file_2', 'file_ii')
237
self.run_bzr('merge a --uncommitted -d b')
238
self.failUnlessExists('b/file_1')
239
self.failUnlessExists('b/file_ii')
241
self.run_bzr_error(('Cannot use --uncommitted and --revision',),
242
'merge /a --uncommitted -r1 -d b')
244
def pullable_branch(self):
247
self.example_branch()
249
self.run_bzr('branch a b')
251
file('goodbye', 'wt').write('quux')
252
self.run_bzr(['commit', '-m', "mode u's are always good"])
255
def pullable_branch(self):
256
tree_a = self.make_branch_and_tree('a')
257
self.build_tree(['a/file'])
259
self.id1 = tree_a.commit('commit 1')
261
tree_b = self.make_branch_and_tree('b')
262
tree_b.pull(tree_a.branch)
263
file('b/file', 'wb').write('foo')
264
self.id2 = tree_b.commit('commit 2')
266
def test_merge_pull(self):
267
self.pullable_branch()
269
(out, err) = self.run_bzr('merge --pull ../b')
270
self.assertContainsRe(out, 'Now on revision 2\\.')
271
tree_a = WorkingTree.open('.')
272
self.assertEqual([self.id2], tree_a.get_parent_ids())
274
def test_merge_kind_change(self):
275
tree_a = self.make_branch_and_tree('tree_a')
276
self.build_tree_contents([('tree_a/file', 'content_1')])
277
tree_a.add('file', 'file-id')
278
tree_a.commit('added file')
279
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
280
os.unlink('tree_a/file')
281
self.build_tree(['tree_a/file/'])
282
tree_a.commit('changed file to directory')
284
self.run_bzr('merge ../tree_a')
285
self.assertEqual('directory', file_kind('file'))
287
self.assertEqual('file', file_kind('file'))
288
self.build_tree_contents([('file', 'content_2')])
289
tree_b.commit('content change')
290
self.run_bzr('merge ../tree_a', retcode=1)
291
self.assertEqual(tree_b.conflicts(),
292
[ContentsConflict('file', file_id='file-id')])
294
def test_directive_cherrypick(self):
295
source = self.make_branch_and_tree('source')
296
self.build_tree(['source/a'])
298
source.commit('Added a', rev_id='rev1')
299
self.build_tree(['source/b'])
301
source.commit('Added b', rev_id='rev2')
302
target = self.make_branch_and_tree('target')
303
target.commit('empty commit')
304
self.write_directive('directive', source.branch, 'target', 'rev2',
306
self.run_bzr('merge -d target directive')
307
self.failIfExists('target/a')
308
self.failUnlessExists('target/b')
310
def write_directive(self, filename, source, target, revision_id,
311
base_revision_id=None, mangle_patch=False):
312
md = merge_directive.MergeDirective2.from_objects(
313
source.repository, revision_id, 0, 0, target,
314
base_revision_id=base_revision_id)
317
self.build_tree_contents([(filename, ''.join(md.to_lines()))])
319
def test_directive_verify_warning(self):
320
source = self.make_branch_and_tree('source')
321
self.build_tree(['source/a'])
323
source.commit('Added a', rev_id='rev1')
324
target = self.make_branch_and_tree('target')
325
target.commit('empty commit')
326
self.write_directive('directive', source.branch, 'target', 'rev1')
327
err = self.run_bzr('merge -d target directive')[1]
328
self.assertNotContainsRe(err, 'Preview patch does not match changes')
330
self.write_directive('directive', source.branch, 'target', 'rev1',
332
err = self.run_bzr('merge -d target directive')[1]
333
self.assertContainsRe(err, 'Preview patch does not match changes')
335
def test_merge_arbitrary(self):
336
target = self.make_branch_and_tree('target')
337
target.commit('empty')
338
# We need a revision that has no integer revno
339
branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
340
self.build_tree(['branch_a/file1'])
341
branch_a.add('file1')
342
branch_a.commit('added file1', rev_id='rev2a')
343
branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
344
self.build_tree(['branch_b/file2'])
345
branch_b.add('file2')
346
branch_b.commit('added file2', rev_id='rev2b')
347
branch_b.merge_from_branch(branch_a.branch)
348
self.failUnlessExists('branch_b/file1')
349
branch_b.commit('merged branch_a', rev_id='rev3b')
351
# It works if the revid has an interger revno
352
self.run_bzr('merge -d target -r revid:rev2a branch_a')
353
self.failUnlessExists('target/file1')
354
self.failIfExists('target/file2')
357
# It should work if the revid has no integer revno
358
self.run_bzr('merge -d target -r revid:rev2a branch_b')
359
self.failUnlessExists('target/file1')
360
self.failIfExists('target/file2')