56
65
builder = self.make_branch_builder('branch')
57
builder.build_snapshot('rev1', None,
66
builder.build_snapshot(None,
58
67
[('add', ('', 'root-id', 'directory', None)),
59
('add', ('fname', 'f-id', 'file', 'a\nb\nc\n'))])
60
builder.build_snapshot('rev2other', ['rev1'],
61
[('modify', ('f-id', 'a\nB\nD\n'))])
62
other = builder.get_branch().bzrdir.sprout('other').open_branch()
63
builder.build_snapshot('rev2this', ['rev1'],
64
[('modify', ('f-id', 'a\nB\nC\n'))])
68
('add', ('fname', 'f-id', 'file', 'a\nb\nc\n'))],
70
builder.build_snapshot(['rev1'],
71
[('modify', ('f-id', 'a\nB\nD\n'))],
72
revision_id='rev2other')
73
other = builder.get_branch().controldir.sprout('other').open_branch()
74
builder.build_snapshot(['rev1'],
75
[('modify', ('f-id', 'a\nB\nC\n'))], revision_id='rev2this')
65
76
tree = builder.get_branch().create_checkout('tree', lightweight=True)
68
79
def test_merge_reprocess(self):
69
d = bzrdir.BzrDir.create_standalone_workingtree('.')
80
d = controldir.ControlDir.create_standalone_workingtree('.')
71
82
self.run_bzr('merge . --reprocess --merge-type weave')
73
84
def test_merge(self):
74
85
a_tree = self.example_branch('a')
75
86
ancestor = a_tree.branch.revno()
76
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
87
b_tree = a_tree.controldir.sprout('b').open_workingtree()
77
88
self.build_tree_contents([('b/goodbye', 'quux')])
78
89
b_tree.commit(message="more u's are always good")
80
91
self.build_tree_contents([('a/hello', 'quuux')])
81
92
# We can't merge when there are in-tree changes
83
self.run_bzr('merge ../b', retcode=3)
84
a = workingtree.WorkingTree.open('.')
93
self.run_bzr('merge ../b', retcode=3, working_dir='a')
94
a = workingtree.WorkingTree.open('a')
85
95
a_tip = a.commit("Like an epidemic of u's")
86
self.run_bzr('merge ../b -r last:1..last:1 --merge-type blooof',
88
self.run_bzr('merge ../b -r last:1..last:1 --merge-type merge3')
89
a_tree.revert(backups=False)
90
self.run_bzr('merge ../b -r last:1..last:1 --merge-type weave')
91
a_tree.revert(backups=False)
92
self.run_bzr('merge ../b -r last:1..last:1 --merge-type lca')
93
a_tree.revert(backups=False)
97
def run_merge_then_revert(args, retcode=None, working_dir='a'):
98
self.run_bzr(['merge', '../b', '-r', 'last:1..last:1'] + args,
99
retcode=retcode, working_dir=working_dir)
101
a_tree.revert(backups=False)
103
run_merge_then_revert(['--merge-type', 'bloof'], retcode=3)
104
run_merge_then_revert(['--merge-type', 'merge3'])
105
run_merge_then_revert(['--merge-type', 'weave'])
106
run_merge_then_revert(['--merge-type', 'lca'])
94
107
self.run_bzr_error(['Show-base is not supported for this merge type'],
95
108
'merge ../b -r last:1..last:1 --merge-type weave'
97
a_tree.revert(backups=False)
98
self.run_bzr('merge ../b -r last:1..last:1 --reprocess')
99
a_tree.revert(backups=False)
100
self.run_bzr('merge ../b -r last:1')
101
self.check_file_contents('goodbye', 'quux')
109
' --show-base', working_dir='a')
110
a_tree.revert(backups=False)
111
self.run_bzr('merge ../b -r last:1..last:1 --reprocess',
113
a_tree.revert(backups=False)
114
self.run_bzr('merge ../b -r last:1', working_dir='a')
115
self.check_file_contents('a/goodbye', 'quux')
102
116
# Merging a branch pulls its revision into the tree
103
b = branch.Branch.open('../b')
117
b = branch.Branch.open('b')
104
118
b_tip = b.last_revision()
105
self.failUnless(a.branch.repository.has_revision(b_tip))
119
self.assertTrue(a.branch.repository.has_revision(b_tip))
106
120
self.assertEqual([a_tip, b_tip], a.get_parent_ids())
107
121
a_tree.revert(backups=False)
108
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
122
out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3,
109
124
self.assertTrue("Not a branch" in err)
110
125
self.run_bzr('merge -r revno:%d:./..revno:%d:../b'
111
%(ancestor,b.revno()))
112
self.assertEquals(a.get_parent_ids(),
126
%(ancestor, b.revno()), working_dir='a')
127
self.assertEqual(a.get_parent_ids(),
113
128
[a.branch.last_revision(), b.last_revision()])
114
self.check_file_contents('goodbye', 'quux')
129
self.check_file_contents('a/goodbye', 'quux')
115
130
a_tree.revert(backups=False)
116
self.run_bzr('merge -r revno:%d:../b'%b.revno())
117
self.assertEquals(a.get_parent_ids(),
131
self.run_bzr('merge -r revno:%d:../b'%b.revno(), working_dir='a')
132
self.assertEqual(a.get_parent_ids(),
118
133
[a.branch.last_revision(), b.last_revision()])
119
134
a_tip = a.commit('merged')
120
self.run_bzr('merge ../b -r last:1')
135
self.run_bzr('merge ../b -r last:1', working_dir='a')
121
136
self.assertEqual([a_tip], a.get_parent_ids())
123
138
def test_merge_defaults_to_reprocess(self):
249
264
branch_b.set_parent(None)
250
265
self.assertEqual(None, branch_b.get_parent())
251
266
# test merge for failure without parent set
253
out = self.run_bzr('merge', retcode=3)
254
self.assertEquals(out,
255
('','bzr: ERROR: No location specified or remembered\n'))
267
out = self.run_bzr('merge', retcode=3, working_dir='branch_b')
268
self.assertEqual(out,
269
('', 'brz: ERROR: No location specified or remembered\n'))
257
271
# test uncommitted changes
258
self.build_tree(['d'])
272
self.build_tree(['branch_b/d'])
260
274
self.run_bzr_error(['Working tree ".*" has uncommitted changes'],
275
'merge', working_dir='branch_b')
263
277
# merge should now pass and implicitly remember merge location
264
278
tree_b.commit('commit d')
265
out, err = self.run_bzr('merge ../branch_a')
279
out, err = self.run_bzr('merge ../branch_a', working_dir='branch_b')
267
281
base = urlutils.local_path_from_url(branch_a.base)
268
282
self.assertEndsWith(err, '+N b\nAll changes applied successfully.\n')
269
self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
283
# re-open branch as external run_brz modified it
284
branch_b = branch_b.controldir.open_branch()
285
self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
270
286
osutils.abspath(parent))
271
287
# test implicit --remember when committing new file
272
self.build_tree(['e'])
288
self.build_tree(['branch_b/e'])
274
290
tree_b.commit('commit e')
275
out, err = self.run_bzr('merge')
276
self.assertStartsWith(err,
277
'Merging from remembered submit location %s\n' % (base,))
278
# re-open tree as external run_bzr modified it
279
tree_b = branch_b.bzrdir.open_workingtree()
291
out, err = self.run_bzr('merge', working_dir='branch_b')
292
self.assertStartsWith(
293
err, 'Merging from remembered submit location %s\n' % (base,))
294
# re-open tree as external run_brz modified it
295
tree_b = branch_b.controldir.open_workingtree()
280
296
tree_b.commit('merge branch_a')
281
297
# test explicit --remember
282
out, err = self.run_bzr('merge ../branch_c --remember')
283
self.assertEquals(out, '')
284
self.assertEquals(err, '+N c\nAll changes applied successfully.\n')
285
self.assertEquals(osutils.abspath(branch_b.get_submit_branch()),
286
osutils.abspath(branch_c.bzrdir.root_transport.base))
287
# re-open tree as external run_bzr modified it
288
tree_b = branch_b.bzrdir.open_workingtree()
298
out, err = self.run_bzr('merge ../branch_c --remember',
299
working_dir='branch_b')
300
self.assertEqual(out, '')
301
self.assertEqual(err, '+N c\nAll changes applied successfully.\n')
302
# re-open branch as external run_brz modified it
303
branch_b = branch_b.controldir.open_branch()
304
self.assertEqual(osutils.abspath(branch_b.get_submit_branch()),
305
osutils.abspath(branch_c.controldir.root_transport.base))
306
# re-open tree as external run_brz modified it
307
tree_b = branch_b.controldir.open_workingtree()
289
308
tree_b.commit('merge branch_c')
291
310
def test_merge_bundle(self):
292
from bzrlib.testament import Testament
311
from breezy.testament import Testament
293
312
tree_a = self.make_branch_and_tree('branch_a')
294
313
self.build_tree_contents([('branch_a/a', 'hello')])
296
315
tree_a.commit('message')
298
tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
317
tree_b = tree_a.controldir.sprout('branch_b').open_workingtree()
299
318
self.build_tree_contents([('branch_a/a', 'hey there')])
300
319
tree_a.commit('message')
302
321
self.build_tree_contents([('branch_b/a', 'goodbye')])
303
322
tree_b.commit('message')
305
self.run_bzr('bundle ../branch_a -o ../bundle')
306
os.chdir('../branch_a')
307
self.run_bzr('merge ../bundle', retcode=1)
323
self.run_bzr('bundle ../branch_a -o ../bundle', working_dir='branch_b')
324
self.run_bzr('merge ../bundle', retcode=1, working_dir='branch_a')
308
325
testament_a = Testament.from_revision(tree_a.branch.repository,
309
326
tree_b.get_parent_ids()[0])
310
327
testament_b = Testament.from_revision(tree_b.branch.repository,
342
358
"""It should be possible to merge changes from a single file."""
343
359
tree_a = self.make_branch_and_tree('tree_a')
344
360
tree_a.commit('initial commit')
345
tree_a.bzrdir.sprout('tree_b')
361
tree_a.controldir.sprout('tree_b')
346
362
self.build_tree(['tree_a/file1', 'tree_a/file2'])
347
363
tree_a.add(['file1', 'file2'])
349
self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
350
self.failUnlessExists('file1')
351
self.failIfExists('file2')
364
self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'],
365
working_dir='tree_b')
366
self.assertPathExists('tree_b/file1')
367
self.assertPathDoesNotExist('tree_b/file2')
369
def test_merge_nonexistent_file(self):
370
"""It should not be possible to merge changes from a file which
372
tree_a = self.make_branch_and_tree('tree_a')
373
self.build_tree_contents([('tree_a/file', 'bar\n')])
375
tree_a.commit('commit 1')
376
self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
377
['merge', 'non/existing'], working_dir='tree_a')
353
379
def pullable_branch(self):
354
380
tree_a = self.make_branch_and_tree('a')
355
self.build_tree(['a/file'])
381
self.build_tree_contents([('a/file', 'bar\n')])
356
382
tree_a.add(['file'])
357
383
self.id1 = tree_a.commit('commit 1')
359
385
tree_b = self.make_branch_and_tree('b')
360
386
tree_b.pull(tree_a.branch)
361
file('b/file', 'wb').write('foo')
387
self.build_tree_contents([('b/file', 'foo\n')])
362
388
self.id2 = tree_b.commit('commit 2')
364
390
def test_merge_pull(self):
365
391
self.pullable_branch()
367
(out, err) = self.run_bzr('merge --pull ../b')
392
(out, err) = self.run_bzr('merge --pull ../b', working_dir='a')
368
393
self.assertContainsRe(out, 'Now on revision 2\\.')
369
tree_a = workingtree.WorkingTree.open('.')
394
tree_a = workingtree.WorkingTree.open('a')
370
395
self.assertEqual([self.id2], tree_a.get_parent_ids())
397
def test_merge_pull_preview(self):
398
self.pullable_branch()
399
(out, err) = self.run_bzr('merge --pull --preview -d a b')
400
self.assertThat(out, matchers.DocTestMatches(
401
"""=== modified file 'file'
408
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
409
tree_a = workingtree.WorkingTree.open('a')
410
self.assertEqual([self.id1], tree_a.get_parent_ids())
372
412
def test_merge_kind_change(self):
373
413
tree_a = self.make_branch_and_tree('tree_a')
374
414
self.build_tree_contents([('tree_a/file', 'content_1')])
375
415
tree_a.add('file', 'file-id')
376
416
tree_a.commit('added file')
377
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
417
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
378
418
os.unlink('tree_a/file')
379
419
self.build_tree(['tree_a/file/'])
380
420
tree_a.commit('changed file to directory')
382
self.run_bzr('merge ../tree_a')
383
self.assertEqual('directory', osutils.file_kind('file'))
421
self.run_bzr('merge ../tree_a', working_dir='tree_b')
422
self.assertEqual('directory', osutils.file_kind('tree_b/file'))
385
self.assertEqual('file', osutils.file_kind('file'))
386
self.build_tree_contents([('file', 'content_2')])
424
self.assertEqual('file', osutils.file_kind('tree_b/file'))
425
self.build_tree_contents([('tree_b/file', 'content_2')])
387
426
tree_b.commit('content change')
388
self.run_bzr('merge ../tree_a', retcode=1)
427
self.run_bzr('merge ../tree_a', retcode=1, working_dir='tree_b')
389
428
self.assertEqual(tree_b.conflicts(),
390
429
[conflicts.ContentsConflict('file',
391
430
file_id='file-id')])
441
480
target = self.make_branch_and_tree('target')
442
481
target.commit('empty')
443
482
# We need a revision that has no integer revno
444
branch_a = target.bzrdir.sprout('branch_a').open_workingtree()
483
branch_a = target.controldir.sprout('branch_a').open_workingtree()
445
484
self.build_tree(['branch_a/file1'])
446
485
branch_a.add('file1')
447
486
branch_a.commit('added file1', rev_id='rev2a')
448
branch_b = target.bzrdir.sprout('branch_b').open_workingtree()
487
branch_b = target.controldir.sprout('branch_b').open_workingtree()
449
488
self.build_tree(['branch_b/file2'])
450
489
branch_b.add('file2')
451
490
branch_b.commit('added file2', rev_id='rev2b')
452
491
branch_b.merge_from_branch(branch_a.branch)
453
self.failUnlessExists('branch_b/file1')
492
self.assertPathExists('branch_b/file1')
454
493
branch_b.commit('merged branch_a', rev_id='rev3b')
456
495
# It works if the revid has an interger revno
457
496
self.run_bzr('merge -d target -r revid:rev2a branch_a')
458
self.failUnlessExists('target/file1')
459
self.failIfExists('target/file2')
497
self.assertPathExists('target/file1')
498
self.assertPathDoesNotExist('target/file2')
462
501
# It should work if the revid has no integer revno
463
502
self.run_bzr('merge -d target -r revid:rev2a branch_b')
464
self.failUnlessExists('target/file1')
465
self.failIfExists('target/file2')
503
self.assertPathExists('target/file1')
504
self.assertPathDoesNotExist('target/file2')
467
506
def assertDirectoryContent(self, directory, entries, message=''):
468
507
"""Assert whether entries (file or directories) exist in a directory.
517
556
def test_merge_from_submit(self):
518
557
tree_a = self.make_branch_and_tree('a')
519
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
520
tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
521
out, err = self.run_bzr(['merge', '-d', 'c'])
522
self.assertContainsRe(err, 'Merging from remembered parent location .*a\/')
523
tree_c.branch.set_submit_branch(tree_b.bzrdir.root_transport.base)
524
out, err = self.run_bzr(['merge', '-d', 'c'])
525
self.assertContainsRe(err, 'Merging from remembered submit location .*b\/')
558
tree_a.commit('test')
559
tree_b = tree_a.controldir.sprout('b').open_workingtree()
560
tree_c = tree_a.controldir.sprout('c').open_workingtree()
561
out, err = self.run_bzr(['merge', '-d', 'c'])
562
self.assertContainsRe(err,
563
'Merging from remembered parent location .*a\/')
564
tree_c.branch.lock_write()
566
tree_c.branch.set_submit_branch(tree_b.controldir.root_transport.base)
568
tree_c.branch.unlock()
569
out, err = self.run_bzr(['merge', '-d', 'c'])
570
self.assertContainsRe(err,
571
'Merging from remembered submit location .*b\/')
527
573
def test_remember_sets_submit(self):
528
574
tree_a = self.make_branch_and_tree('a')
529
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
575
tree_a.commit('rev1')
576
tree_b = tree_a.controldir.sprout('b').open_workingtree()
530
577
self.assertIs(tree_b.branch.get_submit_branch(), None)
532
579
# Remember should not happen if using default from parent
533
580
out, err = self.run_bzr(['merge', '-d', 'b'])
534
self.assertIs(tree_b.branch.get_submit_branch(), None)
581
refreshed = workingtree.WorkingTree.open('b')
582
self.assertIs(refreshed.branch.get_submit_branch(), None)
536
584
# Remember should happen if user supplies location
537
585
out, err = self.run_bzr(['merge', '-d', 'b', 'a'])
538
self.assertEqual(tree_b.branch.get_submit_branch(),
539
tree_a.bzrdir.root_transport.base)
586
refreshed = workingtree.WorkingTree.open('b')
587
self.assertEqual(refreshed.branch.get_submit_branch(),
588
tree_a.controldir.root_transport.base)
590
def test_no_remember_dont_set_submit(self):
591
tree_a = self.make_branch_and_tree('a')
592
self.build_tree_contents([('a/file', "a\n")])
594
tree_a.commit('rev1')
595
tree_b = tree_a.controldir.sprout('b').open_workingtree()
596
self.assertIs(tree_b.branch.get_submit_branch(), None)
598
# Remember should not happen if using default from parent
599
out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
600
self.assertEqual(None, tree_b.branch.get_submit_branch())
602
# Remember should not happen if user supplies location but ask for not
604
out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
605
self.assertEqual(None, tree_b.branch.get_submit_branch())
541
607
def test_weave_cherrypick(self):
542
608
this_tree = self.make_branch_and_tree('this')
543
609
self.build_tree_contents([('this/file', "a\n")])
544
610
this_tree.add('file')
545
611
this_tree.commit('rev1')
546
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
612
other_tree = this_tree.controldir.sprout('other').open_workingtree()
547
613
self.build_tree_contents([('other/file', "a\nb\n")])
548
614
other_tree.commit('rev2b')
549
615
self.build_tree_contents([('other/file', "c\na\nb\n")])
603
669
other.add('other_file')
604
670
other.commit('rev1b')
605
671
self.run_bzr('merge -d this other -r0..')
606
self.failUnlessExists('this/other_file')
672
self.assertPathExists('this/other_file')
608
674
def test_merge_interactive_unlocks_branch(self):
609
675
this = self.make_branch_and_tree('this')
610
other = self.make_branch_and_tree('other')
611
other.commit('empty commit')
676
this.commit('empty commit')
677
other = this.controldir.sprout('other').open_workingtree()
678
other.commit('empty commit 2')
612
679
self.run_bzr('merge -i -d this other')
613
680
this.lock_write()
616
def test_merge_reversed_revision_range(self):
617
tree = self.make_branch_and_tree(".")
683
def test_merge_fetches_tags(self):
684
"""Tags are updated by merge, and revisions named in those tags are
687
# Make a source, sprout a target off it
688
builder = self.make_branch_builder('source')
689
builder.build_commit(message="Rev 1", rev_id='rev-1')
690
source = builder.get_branch()
691
target_bzrdir = source.controldir.sprout('target')
692
# Add a non-ancestry tag to source
693
builder.build_commit(message="Rev 2a", rev_id='rev-2a')
694
source.tags.set_tag('tag-a', 'rev-2a')
695
source.set_last_revision_info(1, 'rev-1')
696
source.get_config_stack().set('branch.fetch_tags', True)
697
builder.build_commit(message="Rev 2b", rev_id='rev-2b')
699
self.run_bzr('merge -d target source')
700
target = target_bzrdir.open_branch()
701
# The tag is present, and so is its revision.
702
self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
703
target.repository.get_revision('rev-2a')
706
class TestMergeRevisionRange(tests.TestCaseWithTransport):
708
scenarios = (('whole-tree', dict(context='.')),
709
('file-only', dict(context='a')))
712
super(TestMergeRevisionRange, self).setUp()
713
self.tree = self.make_branch_and_tree(".")
714
self.tree.commit('initial commit')
618
715
for f in ("a", "b"):
619
716
self.build_tree([f])
621
tree.commit("added "+f)
622
for context in (".", "", "a"):
623
self.run_bzr("merge -r 1..0 " + context)
624
self.failIfExists("a")
626
self.failUnlessExists("a")
718
self.tree.commit("added " + f)
720
def test_merge_reversed_revision_range(self):
721
self.run_bzr("merge -r 2..1 " + self.context)
722
self.assertPathDoesNotExist("a")
723
self.assertPathExists("b")
726
class TestMergeScript(script.TestCaseWithTransportAndScript):
727
def test_merge_empty_branch(self):
728
source = self.make_branch_and_tree('source')
729
self.build_tree(['source/a'])
731
source.commit('Added a', rev_id='rev1')
732
target = self.make_branch_and_tree('target')
734
$ brz merge -d target source
735
2>brz: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
629
738
class TestMergeForce(tests.TestCaseWithTransport):