34
34
class TestMerge(ExternalBase):
36
def example_branch(test):
38
file('hello', 'wt').write('foo')
39
test.runbzr('add hello')
40
test.runbzr('commit -m setup hello')
41
file('goodbye', 'wt').write('baz')
42
test.runbzr('add goodbye')
43
test.runbzr('commit -m setup goodbye')
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')
45
47
def test_merge_reprocess(self):
46
48
d = BzrDir.create_standalone_workingtree('.')
48
self.run_bzr('merge', '.', '--reprocess', '--merge-type', 'weave')
50
self.run_bzr('merge . --reprocess --merge-type weave')
50
52
def test_merge(self):
51
53
from bzrlib.branch import Branch
57
self.runbzr('branch a b')
59
file('goodbye', 'wt').write('quux')
60
self.runbzr(['commit', '-m', "more u's are always good"])
63
file('hello', 'wt').write('quuux')
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')])
64
62
# We can't merge when there are in-tree changes
65
self.runbzr('merge ../b', retcode=3)
66
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
67
self.runbzr('merge ../b -r last:1..last:1 --merge-type blooof',
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.runbzr('merge ../b -r last:1..last:1 --merge-type merge3')
70
self.runbzr('revert --no-backup')
71
self.runbzr('merge ../b -r last:1..last:1 --merge-type weave')
72
self.runbzr('revert --no-backup')
73
self.runbzr('merge ../b -r last:1..last:1 --reprocess')
74
self.runbzr('revert --no-backup')
75
self.runbzr('merge ../b -r last:1')
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('merge ../b -r last:1..last:1 --merge-type lca')
74
a_tree.revert(backups=False)
75
self.run_bzr_error(['Show-base is not supported for this merge type'],
76
'merge ../b -r last:1..last:1 --merge-type weave'
78
a_tree.revert(backups=False)
79
self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
80
a_tree.revert(backups=False)
81
self.run_bzr('merge ../b -r last:1')
76
82
self.check_file_contents('goodbye', 'quux')
77
83
# Merging a branch pulls its revision into the tree
78
a = WorkingTree.open('.')
79
84
b = Branch.open('../b')
80
a.branch.repository.get_revision_xml(b.last_revision())
81
self.log('pending merges: %s', a.pending_merges())
82
self.assertEquals(a.pending_merges(),
84
self.runbzr('commit -m merged')
85
self.runbzr('merge ../b -r last:1')
86
self.assertEqual(a.pending_merges(), [])
85
b_tip = b.last_revision()
86
self.failUnless(a.branch.repository.has_revision(b_tip))
87
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
88
a_tree.revert(backups=False)
89
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
90
self.assertTrue("Not a branch" in err)
91
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
92
%(ancestor,b.revno()))
93
self.assertEquals(a.get_parent_ids(),
94
[a.branch.last_revision(), b.last_revision()])
95
self.check_file_contents('goodbye', 'quux')
96
a_tree.revert(backups=False)
97
self.run_bzr('merge -r revno:%d:../b'%b.revno())
98
self.assertEquals(a.get_parent_ids(),
99
[a.branch.last_revision(), b.last_revision()])
100
a_tip = a.commit('merged')
101
self.run_bzr('merge ../b -r last:1')
102
self.assertEqual([a_tip], a.get_parent_ids())
88
104
def test_merge_with_missing_file(self):
89
105
"""Merge handles missing file conflicts"""
93
print >> file('sub/a.txt', 'wb'), "hello"
94
print >> file('b.txt', 'wb'), "hello"
95
print >> file('sub/c.txt', 'wb'), "hello"
98
self.runbzr(('commit', '-m', 'added a'))
99
self.runbzr('branch . ../b')
100
print >> file('sub/a.txt', 'ab'), "there"
101
print >> file('b.txt', 'ab'), "there"
102
print >> file('sub/c.txt', 'ab'), "there"
103
self.runbzr(('commit', '-m', 'Added there'))
104
os.unlink('sub/a.txt')
105
os.unlink('sub/c.txt')
108
self.runbzr(('commit', '-m', 'Removed a.txt'))
110
print >> file('sub/a.txt', 'ab'), "something"
111
print >> file('b.txt', 'ab'), "something"
112
print >> file('sub/c.txt', 'ab'), "something"
113
self.runbzr(('commit', '-m', 'Modified a.txt'))
114
self.runbzr('merge ../a/', retcode=1)
115
self.assert_(os.path.exists('sub/a.txt.THIS'))
116
self.assert_(os.path.exists('sub/a.txt.BASE'))
106
self.build_tree_contents([
109
('a/sub/a.txt', 'hello\n'),
110
('a/b.txt', 'hello\n'),
111
('a/sub/c.txt', 'hello\n')])
112
a_tree = self.make_branch_and_tree('a')
113
a_tree.add(['sub', 'b.txt', 'sub/c.txt', 'sub/a.txt'])
114
a_tree.commit(message='added a')
115
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
116
self.build_tree_contents([
117
('a/sub/a.txt', 'hello\nthere\n'),
118
('a/b.txt', 'hello\nthere\n'),
119
('a/sub/c.txt', 'hello\nthere\n')])
120
a_tree.commit(message='Added there')
121
os.remove('a/sub/a.txt')
122
os.remove('a/sub/c.txt')
125
a_tree.commit(message='Removed a.txt')
126
self.build_tree_contents([
127
('b/sub/a.txt', 'hello\nsomething\n'),
128
('b/b.txt', 'hello\nsomething\n'),
129
('b/sub/c.txt', 'hello\nsomething\n')])
130
b_tree.commit(message='Modified a.txt')
132
self.run_bzr('merge ../a/', retcode=1)
133
self.failUnlessExists('sub/a.txt.THIS')
134
self.failUnlessExists('sub/a.txt.BASE')
118
self.runbzr('merge ../b/', retcode=1)
119
self.assert_(os.path.exists('sub/a.txt.OTHER'))
120
self.assert_(os.path.exists('sub/a.txt.BASE'))
136
self.run_bzr('merge ../b/', retcode=1)
137
self.failUnlessExists('sub/a.txt.OTHER')
138
self.failUnlessExists('sub/a.txt.BASE')
122
140
def test_merge_remember(self):
123
"""Merge changes from one branch to another and test parent location."""
141
"""Merge changes from one branch to another, test submit location."""
124
142
tree_a = self.make_branch_and_tree('branch_a')
125
143
branch_a = tree_a.branch
126
144
self.build_tree(['branch_a/a'])
142
160
self.assertEqual(None, branch_b.get_parent())
143
161
# test merge for failure without parent set
144
162
os.chdir('branch_b')
145
out = self.runbzr('merge', retcode=3)
163
out = self.run_bzr('merge', retcode=3)
146
164
self.assertEquals(out,
147
165
('','bzr: ERROR: No location specified or remembered\n'))
148
166
# test implicit --remember when no parent set, this merge conflicts
149
167
self.build_tree(['d'])
151
out = self.runbzr('merge ../branch_a', retcode=3)
152
self.assertEquals(out,
153
('','bzr: ERROR: Working tree has uncommitted changes.\n'))
154
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
169
self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
171
self.assertEquals(abspath(branch_b.get_submit_branch()),
155
173
# test implicit --remember after resolving conflict
156
174
tree_b.commit('commit d')
157
out, err = self.runbzr('merge')
175
out, err = self.run_bzr('merge')
159
177
base = urlutils.local_path_from_url(branch_a.base)
160
self.assertEquals(out, 'Merging from remembered location %s\n' % (base,))
161
self.assertEquals(err, 'All changes applied successfully.\n')
162
self.assertEquals(abspath(branch_b.get_parent()), abspath(parent))
163
# re-open tree as external runbzr modified it
178
self.assertStartsWith(err,
179
'Merging from remembered submit location %s\n' % (base,))
180
self.assertEndsWith(err, '+N b\nAll changes applied successfully.\n')
181
self.assertEquals(abspath(branch_b.get_submit_branch()),
183
# re-open tree as external run_bzr modified it
164
184
tree_b = branch_b.bzrdir.open_workingtree()
165
185
tree_b.commit('merge branch_a')
166
186
# test explicit --remember
167
out, err = self.runbzr('merge ../branch_c --remember')
187
out, err = self.run_bzr('merge ../branch_c --remember')
168
188
self.assertEquals(out, '')
169
self.assertEquals(err, 'All changes applied successfully.\n')
170
self.assertEquals(abspath(branch_b.get_parent()),
189
self.assertEquals(err, '+N c\nAll changes applied successfully.\n')
190
self.assertEquals(abspath(branch_b.get_submit_branch()),
171
191
abspath(branch_c.bzrdir.root_transport.base))
172
# re-open tree as external runbzr modified it
192
# re-open tree as external run_bzr modified it
173
193
tree_b = branch_b.bzrdir.open_workingtree()
174
194
tree_b.commit('merge branch_c')
176
196
def test_merge_bundle(self):
177
197
from bzrlib.testament import Testament
178
198
tree_a = self.make_branch_and_tree('branch_a')
179
f = file('branch_a/a', 'wb')
199
self.build_tree_contents([('branch_a/a', 'hello')])
183
201
tree_a.commit('message')
185
203
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
186
f = file('branch_a/a', 'wb')
204
self.build_tree_contents([('branch_a/a', 'hey there')])
189
205
tree_a.commit('message')
191
f = file('branch_b/a', 'wb')
207
self.build_tree_contents([('branch_b/a', 'goodbye')])
194
208
tree_b.commit('message')
195
209
os.chdir('branch_b')
196
file('../bundle', 'wb').write(self.runbzr('bundle ../branch_a')[0])
210
self.run_bzr('bundle ../branch_a -o ../bundle')
197
211
os.chdir('../branch_a')
198
self.runbzr('merge ../bundle', retcode=1)
199
testament_a = Testament.from_revision(tree_a.branch.repository,
200
tree_b.last_revision())
212
self.run_bzr('merge ../bundle', retcode=1)
213
testament_a = Testament.from_revision(tree_a.branch.repository,
214
tree_b.get_parent_ids()[0])
201
215
testament_b = Testament.from_revision(tree_b.branch.repository,
202
tree_b.last_revision())
216
tree_b.get_parent_ids()[0])
203
217
self.assertEqualDiff(testament_a.as_text(),
204
218
testament_b.as_text())
205
219
tree_a.set_conflicts(ConflictList())
206
220
tree_a.commit('message')
207
221
# it is legal to attempt to merge an already-merged bundle
208
output = self.runbzr('merge ../bundle')[1]
222
output = self.run_bzr('merge ../bundle')[1]
209
223
# but it does nothing
210
self.assertFalse(compare_trees(tree_a.basis_tree(),
211
tree_a).has_changed())
224
self.assertFalse(tree_a.changes_from(tree_a.basis_tree()).has_changed())
212
225
self.assertEqual('Nothing to do.\n', output)
227
def test_merge_uncommitted(self):
228
"""Check that merge --uncommitted behaves properly"""
229
tree_a = self.make_branch_and_tree('a')
230
self.build_tree(['a/file_1', 'a/file_2'])
231
tree_a.add(['file_1', 'file_2'])
232
tree_a.commit('commit 1')
233
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
234
self.failUnlessExists('b/file_1')
235
tree_a.rename_one('file_1', 'file_i')
236
tree_a.commit('commit 2')
237
tree_a.rename_one('file_2', 'file_ii')
239
self.run_bzr('merge a --uncommitted -d b')
240
self.failUnlessExists('b/file_1')
241
self.failUnlessExists('b/file_ii')
243
self.run_bzr_error(('Cannot use --uncommitted and --revision',),
244
'merge /a --uncommitted -r1 -d b')
246
def test_merge_uncommitted_file(self):
247
"""It should be possible to merge changes from a single file."""
248
tree_a = self.make_branch_and_tree('tree_a')
249
tree_a.commit('initial commit')
250
tree_a.bzrdir.sprout('tree_b')
251
self.build_tree(['tree_a/file1', 'tree_a/file2'])
252
tree_a.add(['file1', 'file2'])
254
self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
255
self.failUnlessExists('file1')
256
self.failIfExists('file2')
258
def pullable_branch(self):
259
tree_a = self.make_branch_and_tree('a')
260
self.build_tree(['a/file'])
262
self.id1 = tree_a.commit('commit 1')
264
tree_b = self.make_branch_and_tree('b')
265
tree_b.pull(tree_a.branch)
266
file('b/file', 'wb').write('foo')
267
self.id2 = tree_b.commit('commit 2')
269
def test_merge_pull(self):
270
self.pullable_branch()
272
(out, err) = self.run_bzr('merge --pull ../b')
273
self.assertContainsRe(out, 'Now on revision 2\\.')
274
tree_a = WorkingTree.open('.')
275
self.assertEqual([self.id2], tree_a.get_parent_ids())
277
def test_merge_kind_change(self):
278
tree_a = self.make_branch_and_tree('tree_a')
279
self.build_tree_contents([('tree_a/file', 'content_1')])
280
tree_a.add('file', 'file-id')
281
tree_a.commit('added file')
282
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
283
os.unlink('tree_a/file')
284
self.build_tree(['tree_a/file/'])
285
tree_a.commit('changed file to directory')
287
self.run_bzr('merge ../tree_a')
288
self.assertEqual('directory', file_kind('file'))
290
self.assertEqual('file', file_kind('file'))
291
self.build_tree_contents([('file', 'content_2')])
292
tree_b.commit('content change')
293
self.run_bzr('merge ../tree_a', retcode=1)
294
self.assertEqual(tree_b.conflicts(),
295
[ContentsConflict('file', file_id='file-id')])
297
def test_directive_cherrypick(self):
298
source = self.make_branch_and_tree('source')
299
self.build_tree(['source/a'])
301
source.commit('Added a', rev_id='rev1')
302
self.build_tree(['source/b'])
304
source.commit('Added b', rev_id='rev2')
305
target = self.make_branch_and_tree('target')
306
target.commit('empty commit')
307
self.write_directive('directive', source.branch, 'target', 'rev2',
309
out, err = self.run_bzr('merge -d target directive')
310
self.failIfExists('target/a')
311
self.failUnlessExists('target/b')
312
self.assertContainsRe(err, 'Performing cherrypick')
314
def write_directive(self, filename, source, target, revision_id,
315
base_revision_id=None, mangle_patch=False):
316
md = merge_directive.MergeDirective2.from_objects(
317
source.repository, revision_id, 0, 0, target,
318
base_revision_id=base_revision_id)
321
self.build_tree_contents([(filename, ''.join(md.to_lines()))])
323
def test_directive_verify_warning(self):
324
source = self.make_branch_and_tree('source')
325
self.build_tree(['source/a'])
327
source.commit('Added a', rev_id='rev1')
328
target = self.make_branch_and_tree('target')
329
target.commit('empty commit')
330
self.write_directive('directive', source.branch, 'target', 'rev1')
331
err = self.run_bzr('merge -d target directive')[1]
332
self.assertNotContainsRe(err, 'Preview patch does not match changes')
334
self.write_directive('directive', source.branch, 'target', 'rev1',
336
err = self.run_bzr('merge -d target directive')[1]
337
self.assertContainsRe(err, 'Preview patch does not match changes')
339
def test_merge_arbitrary(self):
340
target = self.make_branch_and_tree('target')
341
target.commit('empty')
342
# We need a revision that has no integer revno
343
branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
344
self.build_tree(['branch_a/file1'])
345
branch_a.add('file1')
346
branch_a.commit('added file1', rev_id='rev2a')
347
branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
348
self.build_tree(['branch_b/file2'])
349
branch_b.add('file2')
350
branch_b.commit('added file2', rev_id='rev2b')
351
branch_b.merge_from_branch(branch_a.branch)
352
self.failUnlessExists('branch_b/file1')
353
branch_b.commit('merged branch_a', rev_id='rev3b')
355
# It works if the revid has an interger revno
356
self.run_bzr('merge -d target -r revid:rev2a branch_a')
357
self.failUnlessExists('target/file1')
358
self.failIfExists('target/file2')
361
# It should work if the revid has no integer revno
362
self.run_bzr('merge -d target -r revid:rev2a branch_b')
363
self.failUnlessExists('target/file1')
364
self.failIfExists('target/file2')
366
def assertDirectoryContent(self, directory, entries, message=''):
367
"""Assert whether entries (file or directories) exist in a directory.
369
It also checks that there are no extra entries.
371
ondisk = os.listdir(directory)
372
if set(ondisk) == set(entries):
376
raise AssertionError(
377
'%s"%s" directory content is different:\na = %s\nb = %s\n'
378
% (message, directory, sorted(entries), sorted(ondisk)))
380
def test_cherrypicking_merge(self):
382
source = self.make_branch_and_tree('source')
383
for f in ('a', 'b', 'c', 'd'):
384
self.build_tree(['source/'+f])
386
source.commit('added '+f, rev_id='rev_'+f)
388
target = source.bzrdir.sprout('target', 'rev_a').open_workingtree()
389
self.assertDirectoryContent('target', ['.bzr', 'a'])
391
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_c source')
392
self.assertDirectoryContent('target', ['.bzr', 'a', 'c'])
395
self.run_bzr('merge -d target -r revid:rev_b..revid:rev_d source')
396
self.assertDirectoryContent('target', ['.bzr', 'a', 'c', 'd'])
398
# pick 1 revision with option --changes
399
self.run_bzr('merge -d target -c revid:rev_d source')
400
self.assertDirectoryContent('target', ['.bzr', 'a', 'd'])
402
def test_merge_criss_cross(self):
403
tree_a = self.make_branch_and_tree('a')
404
tree_a.commit('', rev_id='rev1')
405
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
406
tree_a.commit('', rev_id='rev2a')
407
tree_b.commit('', rev_id='rev2b')
408
tree_a.merge_from_branch(tree_b.branch)
409
tree_b.merge_from_branch(tree_a.branch)
410
tree_a.commit('', rev_id='rev3a')
411
tree_b.commit('', rev_id='rev3b')
412
graph = tree_a.branch.repository.get_graph(tree_b.branch.repository)
413
out, err = self.run_bzr(['merge', '-d', 'a', 'b'])
414
self.assertContainsRe(err, 'Warning: criss-cross merge encountered.')
416
def test_merge_from_submit(self):
417
tree_a = self.make_branch_and_tree('a')
418
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
419
tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
420
out, err = self.run_bzr(['merge', '-d', 'c'])
421
self.assertContainsRe(err, 'Merging from remembered parent location .*a\/')
422
tree_c.branch.set_submit_branch(tree_b.bzrdir.root_transport.base)
423
out, err = self.run_bzr(['merge', '-d', 'c'])
424
self.assertContainsRe(err, 'Merging from remembered submit location .*b\/')
426
def test_remember_sets_submit(self):
427
tree_a = self.make_branch_and_tree('a')
428
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
429
self.assertIs(tree_b.branch.get_submit_branch(), None)
431
# Remember should not happen if using default from parent
432
out, err = self.run_bzr(['merge', '-d', 'b'])
433
self.assertIs(tree_b.branch.get_submit_branch(), None)
435
# Remember should happen if user supplies location
436
out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
437
self.assertEqual(tree_b.branch.get_submit_branch(),
438
tree_a.bzrdir.root_transport.base)
440
def test_weave_cherrypick(self):
441
this_tree = self.make_branch_and_tree('this')
442
self.build_tree_contents([('this/file', "a\n")])
443
this_tree.add('file')
444
this_tree.commit('rev1')
445
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
446
self.build_tree_contents([('other/file', "a\nb\n")])
447
other_tree.commit('rev2b')
448
self.build_tree_contents([('other/file', "c\na\nb\n")])
449
other_tree.commit('rev3b')
450
self.run_bzr('merge --weave -d this other -r -2..-1')
451
self.assertFileEqual('c\na\n', 'this/file')
453
def test_lca_merge_criss_cross(self):
454
tree_a = self.make_branch_and_tree('a')
455
self.build_tree_contents([('a/file', 'base-contents\n')])
457
tree_a.commit('', rev_id='rev1')
458
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
459
self.build_tree_contents([('a/file',
460
'base-contents\nthis-contents\n')])
461
tree_a.commit('', rev_id='rev2a')
462
self.build_tree_contents([('b/file',
463
'base-contents\nother-contents\n')])
464
tree_b.commit('', rev_id='rev2b')
465
tree_a.merge_from_branch(tree_b.branch)
466
self.build_tree_contents([('a/file',
467
'base-contents\nthis-contents\n')])
468
tree_a.set_conflicts(ConflictList())
469
tree_b.merge_from_branch(tree_a.branch)
470
self.build_tree_contents([('b/file',
471
'base-contents\nother-contents\n')])
472
tree_b.set_conflicts(ConflictList())
473
tree_a.commit('', rev_id='rev3a')
474
tree_b.commit('', rev_id='rev3b')
475
out, err = self.run_bzr(['merge', '-d', 'a', 'b', '--lca'], retcode=1)
476
self.assertFileEqual('base-contents\n<<<<<<< TREE\nthis-contents\n'
477
'=======\nother-contents\n>>>>>>> MERGE-SOURCE\n',
480
def test_merge_preview(self):
481
this_tree = self.make_branch_and_tree('this')
482
this_tree.commit('rev1')
483
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
484
self.build_tree_contents([('other/file', 'new line')])
485
other_tree.add('file')
486
other_tree.commit('rev2a')
487
this_tree.commit('rev2b')
488
out, err = self.run_bzr(['merge', '-d', 'this', 'other', '--preview'])
489
self.assertContainsRe(out, '\+new line')
490
self.assertNotContainsRe(err, '\+N file\n')
491
this_tree.lock_read()
492
self.addCleanup(this_tree.unlock)
494
list(this_tree.iter_changes(this_tree.basis_tree())))