15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Black-box tests for brz branch."""
18
"""Black-box tests for bzr branch."""
26
27
revision as _mod_revision,
29
from breezy.bzr import (
32
from breezy.bzr.knitrepo import RepositoryFormatKnit1
33
from breezy.tests import (
30
from brzlib.repofmt.knitrepo import RepositoryFormatKnit1
31
from brzlib.tests import (
37
from breezy.tests.features import (
35
from brzlib.tests.features import (
40
from breezy.tests.blackbox import test_switch
41
from breezy.tests.matchers import ContainsNoVfsCalls
42
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
43
from breezy.tests.script import run_script
44
from breezy.urlutils import local_path_to_url, strip_trailing_slash
45
from breezy.workingtree import WorkingTree
38
from brzlib.tests.blackbox import test_switch
39
from brzlib.tests.matchers import ContainsNoVfsCalls
40
from brzlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from brzlib.tests.script import run_script
42
from brzlib.urlutils import local_path_to_url, strip_trailing_slash
43
from brzlib.workingtree import WorkingTree
48
46
class TestBranch(tests.TestCaseWithTransport):
50
48
def example_branch(self, path='.', format=None):
51
49
tree = self.make_branch_and_tree(path, format=format)
52
self.build_tree_contents([(path + '/hello', b'foo')])
50
self.build_tree_contents([(path + '/hello', 'foo')])
54
52
tree.commit(message='setup')
55
self.build_tree_contents([(path + '/goodbye', b'baz')])
53
self.build_tree_contents([(path + '/goodbye', 'baz')])
56
54
tree.add('goodbye')
57
55
tree.commit(message='setup')
91
89
out, err = self.run_bzr('branches b')
92
90
self.assertEqual(" orig\n thiswasa\n", out)
93
91
self.assertEqual('', err)
94
out, err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
92
out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
95
93
self.assertEqual('', out)
97
'brz: ERROR: Already a branch: "file:b,branch=orig".\n', err)
95
'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
99
97
def test_from_colocated(self):
100
98
"""Branch from a colocated branch into a regular branch."""
102
tree = self.example_branch('b/a', format='development-colo')
103
tree.controldir.create_branch(name='somecolo')
99
tree = self.example_branch('a', format='development-colo')
100
tree.bzrdir.create_branch(name='somecolo')
104
101
out, err = self.run_bzr('branch %s,branch=somecolo' %
105
local_path_to_url('b/a'))
102
local_path_to_url('a'))
106
103
self.assertEqual('', out)
107
104
self.assertEqual('Branched 0 revisions.\n', err)
108
self.assertPathExists('a')
105
self.assertPathExists("somecolo")
110
107
def test_branch_broken_pack(self):
111
108
"""branching with a corrupted pack file."""
161
158
tree = self.make_branch_and_tree('current')
162
159
c1 = tree.commit('some diverged change')
163
160
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
164
'branch --switch ../a ../b', working_dir='current')
161
'branch --switch ../a ../b', working_dir='current')
165
162
a = branch.Branch.open('a')
166
163
b = branch.Branch.open('b')
167
164
self.assertEqual(a.last_revision(), b.last_revision())
211
208
shared_repo.set_make_working_trees(True)
213
210
def make_shared_tree(path):
214
shared_repo.controldir.root_transport.mkdir(path)
211
shared_repo.bzrdir.root_transport.mkdir(path)
215
212
controldir.ControlDir.create_branch_convenience('repo/' + path)
216
213
return WorkingTree.open('repo/' + path)
217
214
tree_a = make_shared_tree('a')
218
215
self.build_tree(['repo/a/file'])
219
216
tree_a.add('file')
220
tree_a.commit('commit a-1', rev_id=b'a-1')
221
with open('repo/a/file', 'ab') as f:
222
f.write(b'more stuff\n')
223
tree_a.commit('commit a-2', rev_id=b'a-2')
217
tree_a.commit('commit a-1', rev_id='a-1')
218
f = open('repo/a/file', 'ab')
219
f.write('more stuff\n')
221
tree_a.commit('commit a-2', rev_id='a-2')
225
223
tree_b = make_shared_tree('b')
226
224
self.build_tree(['repo/b/file'])
227
225
tree_b.add('file')
228
tree_b.commit('commit b-1', rev_id=b'b-1')
226
tree_b.commit('commit b-1', rev_id='b-1')
230
self.assertTrue(shared_repo.has_revision(b'a-1'))
231
self.assertTrue(shared_repo.has_revision(b'a-2'))
232
self.assertTrue(shared_repo.has_revision(b'b-1'))
228
self.assertTrue(shared_repo.has_revision('a-1'))
229
self.assertTrue(shared_repo.has_revision('a-2'))
230
self.assertTrue(shared_repo.has_revision('b-1'))
234
232
# Now that we have a repository with shared files, make sure
235
233
# that things aren't copied out by a 'branch'
236
234
self.run_bzr('branch repo/b branch-b')
237
235
pushed_tree = WorkingTree.open('branch-b')
238
236
pushed_repo = pushed_tree.branch.repository
239
self.assertFalse(pushed_repo.has_revision(b'a-1'))
240
self.assertFalse(pushed_repo.has_revision(b'a-2'))
241
self.assertTrue(pushed_repo.has_revision(b'b-1'))
237
self.assertFalse(pushed_repo.has_revision('a-1'))
238
self.assertFalse(pushed_repo.has_revision('a-2'))
239
self.assertTrue(pushed_repo.has_revision('b-1'))
243
241
def test_branch_hardlink(self):
244
242
self.requireFeature(HardlinkFeature)
292
290
def test_branch_into_existing_dir(self):
293
291
self.example_branch('a')
294
# existing dir with similar files but no .brz dir
292
# existing dir with similar files but no .bzr dir
295
293
self.build_tree_contents([('b/',)])
296
self.build_tree_contents([('b/hello', b'bar')]) # different content
297
self.build_tree_contents([('b/goodbye', b'baz')]) # same content
294
self.build_tree_contents([('b/hello', 'bar')]) # different content
295
self.build_tree_contents([('b/goodbye', 'baz')])# same content
298
296
# fails without --use-existing-dir
299
out, err = self.run_bzr('branch a b', retcode=3)
297
out,err = self.run_bzr('branch a b', retcode=3)
300
298
self.assertEqual('', out)
301
self.assertEqual('brz: ERROR: Target directory "b" already exists.\n',
299
self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
303
301
# force operation
304
302
self.run_bzr('branch a b --use-existing-dir')
305
303
# check conflicts
306
304
self.assertPathExists('b/hello.moved')
307
305
self.assertPathDoesNotExist('b/godbye.moved')
308
306
# we can't branch into branch
309
out, err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
307
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
310
308
self.assertEqual('', out)
311
self.assertEqual('brz: ERROR: Already a branch: "b".\n', err)
309
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
313
311
def test_branch_bind(self):
314
312
self.example_branch('a')
350
348
def test_branch_fetches_all_tags(self):
351
349
builder = self.make_branch_builder('source')
352
source, rev1, rev2 = fixtures.build_branch_with_non_ancestral_rev(
354
source.tags.set_tag('tag-a', rev2)
350
source = fixtures.build_branch_with_non_ancestral_rev(builder)
351
source.tags.set_tag('tag-a', 'rev-2')
355
352
source.get_config_stack().set('branch.fetch_tags', True)
356
353
# Now source has a tag not in its ancestry. Make a branch from it.
357
354
self.run_bzr('branch source new-branch')
358
355
new_branch = branch.Branch.open('new-branch')
359
356
# The tag is present, and so is its revision.
360
self.assertEqual(rev2, new_branch.tags.lookup_tag('tag-a'))
361
new_branch.repository.get_revision(rev2)
363
def test_branch_with_nested_trees(self):
364
orig = self.make_branch_and_tree('source', format='development-subtree')
365
subtree = self.make_branch_and_tree('source/subtree')
366
self.build_tree(['source/subtree/a'])
368
subtree.commit('add subtree contents')
369
orig.add_reference(subtree)
370
orig.set_reference_info('subtree', subtree.branch.user_url)
371
orig.commit('add subtree')
373
self.run_bzr('branch source target')
375
target = WorkingTree.open('target')
376
target_subtree = WorkingTree.open('target/subtree')
377
self.assertTreesEqual(orig, target)
378
self.assertTreesEqual(subtree, target_subtree)
380
def test_branch_with_nested_trees_reference_unset(self):
381
orig = self.make_branch_and_tree('source', format='development-subtree')
382
subtree = self.make_branch_and_tree('source/subtree')
383
self.build_tree(['source/subtree/a'])
385
subtree.commit('add subtree contents')
386
orig.add_reference(subtree)
387
orig.commit('add subtree')
389
self.run_bzr('branch source target')
391
target = WorkingTree.open('target')
392
self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
394
def test_branch_with_nested_trees_no_recurse(self):
395
orig = self.make_branch_and_tree('source', format='development-subtree')
396
subtree = self.make_branch_and_tree('source/subtree')
397
self.build_tree(['source/subtree/a'])
399
subtree.commit('add subtree contents')
400
orig.add_reference(subtree)
401
orig.commit('add subtree')
403
self.run_bzr('branch --no-recurse-nested source target')
405
target = WorkingTree.open('target')
406
self.addCleanup(subtree.lock_read().unlock)
407
basis = subtree.basis_tree()
408
self.addCleanup(basis.lock_read().unlock)
409
self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
357
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
358
new_branch.repository.get_revision('rev-2')
412
361
class TestBranchStacked(tests.TestCaseWithTransport):
425
374
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
426
375
repo = branch.Branch.open(branch_path).repository
427
376
self.assertEqual(set(revid_list),
428
repo.has_revisions(revid_list))
377
repo.has_revisions(revid_list))
430
379
def test_branch_stacked_branch_not_stacked(self):
431
380
"""Branching a stacked branch is not stacked by default"""
432
381
# We have a mainline
433
382
trunk_tree = self.make_branch_and_tree('target',
435
384
trunk_tree.commit('mainline')
436
385
# and a branch from it which is stacked
437
386
branch_tree = self.make_branch_and_tree('branch',
439
388
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
440
389
# with some work on it
441
work_tree = trunk_tree.branch.controldir.sprout(
442
'local').open_workingtree()
390
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
443
391
work_tree.commit('moar work plz')
444
392
work_tree.branch.push(branch_tree.branch)
445
393
# branching our local branch gives us a new stacked branch pointing at
447
395
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
448
396
self.assertEqual('', out)
449
397
self.assertEqual('Branched 2 revisions.\n',
451
399
# it should have preserved the branch format, and so it should be
452
400
# capable of supporting stacking, but not actually have a stacked_on
453
401
# branch configured
454
402
self.assertRaises(errors.NotStacked,
455
controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
403
controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
457
405
def test_branch_stacked_branch_stacked(self):
458
406
"""Asking to stack on a stacked branch does work"""
459
407
# We have a mainline
460
408
trunk_tree = self.make_branch_and_tree('target',
462
410
trunk_revid = trunk_tree.commit('mainline')
463
411
# and a branch from it which is stacked
464
412
branch_tree = self.make_branch_and_tree('branch',
466
414
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
467
415
# with some work on it
468
work_tree = trunk_tree.branch.controldir.sprout(
469
'local').open_workingtree()
416
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
470
417
branch_revid = work_tree.commit('moar work plz')
471
418
work_tree.branch.push(branch_tree.branch)
472
419
# you can chain branches on from there
473
420
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
474
421
self.assertEqual('', out)
475
422
self.assertEqual('Created new stacked branch referring to %s.\n' %
476
branch_tree.branch.base, err)
423
branch_tree.branch.base, err)
477
424
self.assertEqual(branch_tree.branch.base,
478
branch.Branch.open('branch2').get_stacked_on_url())
425
branch.Branch.open('branch2').get_stacked_on_url())
479
426
branch2_tree = WorkingTree.open('branch2')
480
427
branch2_revid = work_tree.commit('work on second stacked branch')
481
428
work_tree.branch.push(branch2_tree.branch)
487
434
def test_branch_stacked(self):
488
435
# We have a mainline
489
436
trunk_tree = self.make_branch_and_tree('mainline',
491
438
original_revid = trunk_tree.commit('mainline')
492
439
self.assertRevisionInRepository('mainline', original_revid)
493
440
# and a branch from it which is stacked
494
441
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
496
443
self.assertEqual('', out)
497
444
self.assertEqual('Created new stacked branch referring to %s.\n' %
498
trunk_tree.branch.base, err)
445
trunk_tree.branch.base, err)
499
446
self.assertRevisionNotInRepository('newbranch', original_revid)
500
447
new_branch = branch.Branch.open('newbranch')
501
448
self.assertEqual(trunk_tree.branch.base,
550
496
t.commit(message='commit %d' % count)
551
497
self.reset_smart_call_log()
552
498
out, err = self.run_bzr(['branch', self.get_url('from'),
553
self.get_url('target')])
499
self.get_url('target')])
554
500
# This figure represent the amount of work to perform this use case. It
555
501
# is entirely ok to reduce this number if a test fails due to rpc_count
556
502
# being too low. If rpc_count increases, more network roundtrips have
557
503
# become necessary for this use case. Please do not adjust this number
558
504
# upwards without agreement from bzr's network support maintainers.
559
505
self.assertLength(2, self.hpss_connections)
560
self.assertLength(34, self.hpss_calls)
562
"branching to the same branch requires VFS access",
506
self.assertLength(33, self.hpss_calls)
507
self.expectFailure("branching to the same branch requires VFS access",
563
508
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
565
510
def test_branch_from_trivial_branch_streaming_acceptance(self):
569
514
t.commit(message='commit %d' % count)
570
515
self.reset_smart_call_log()
571
516
out, err = self.run_bzr(['branch', self.get_url('from'),
573
518
# This figure represent the amount of work to perform this use case. It
574
519
# is entirely ok to reduce this number if a test fails due to rpc_count
575
520
# being too low. If rpc_count increases, more network roundtrips have
576
521
# become necessary for this use case. Please do not adjust this number
577
522
# upwards without agreement from bzr's network support maintainers.
578
523
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
579
self.assertLength(11, self.hpss_calls)
524
self.assertLength(10, self.hpss_calls)
580
525
self.assertLength(1, self.hpss_connections)
582
527
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
584
529
t = self.make_branch_and_tree('trunk')
585
530
for count in range(8):
586
531
t.commit(message='commit %d' % count)
587
tree2 = t.branch.controldir.sprout('feature', stacked=True
589
local_tree = t.branch.controldir.sprout(
590
'local-working').open_workingtree()
532
tree2 = t.branch.bzrdir.sprout('feature', stacked=True
534
local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
591
535
local_tree.commit('feature change')
592
536
local_tree.branch.push(tree2.branch)
593
537
self.reset_smart_call_log()
594
538
out, err = self.run_bzr(['branch', self.get_url('feature'),
596
540
# This figure represent the amount of work to perform this use case. It
597
541
# is entirely ok to reduce this number if a test fails due to rpc_count
598
542
# being too low. If rpc_count increases, more network roundtrips have
599
543
# become necessary for this use case. Please do not adjust this number
600
544
# upwards without agreement from bzr's network support maintainers.
601
self.assertLength(16, self.hpss_calls)
545
self.assertLength(15, self.hpss_calls)
602
546
self.assertLength(1, self.hpss_connections)
603
547
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
605
549
def test_branch_from_branch_with_tags(self):
606
550
self.setup_smart_server_with_call_log()
607
551
builder = self.make_branch_builder('source')
608
source, rev1, rev2 = fixtures.build_branch_with_non_ancestral_rev(
552
source = fixtures.build_branch_with_non_ancestral_rev(builder)
610
553
source.get_config_stack().set('branch.fetch_tags', True)
611
source.tags.set_tag('tag-a', rev2)
612
source.tags.set_tag('tag-missing', b'missing-rev')
554
source.tags.set_tag('tag-a', 'rev-2')
555
source.tags.set_tag('tag-missing', 'missing-rev')
613
556
# Now source has a tag not in its ancestry. Make a branch from it.
614
557
self.reset_smart_call_log()
615
558
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
618
561
# being too low. If rpc_count increases, more network roundtrips have
619
562
# become necessary for this use case. Please do not adjust this number
620
563
# upwards without agreement from bzr's network support maintainers.
621
self.assertLength(11, self.hpss_calls)
564
self.assertLength(10, self.hpss_calls)
622
565
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
623
566
self.assertLength(1, self.hpss_connections)
642
585
self.assertLength(1, self.hpss_connections)
643
586
self.assertLength(0, readvs_of_rix_files)
644
587
self.expectFailure("branching to stacked requires VFS access",
645
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
647
def test_branch_from_branch_with_ghosts(self):
648
self.setup_smart_server_with_call_log()
649
t = self.make_branch_and_tree('from')
650
for count in range(9):
651
t.commit(message='commit %d' % count)
652
t.set_parent_ids([t.last_revision(), b'ghost'])
653
t.commit(message='add commit with parent')
654
self.reset_smart_call_log()
655
out, err = self.run_bzr(['branch', self.get_url('from'),
657
# This figure represent the amount of work to perform this use case. It
658
# is entirely ok to reduce this number if a test fails due to rpc_count
659
# being too low. If rpc_count increases, more network roundtrips have
660
# become necessary for this use case. Please do not adjust this number
661
# upwards without agreement from bzr's network support maintainers.
662
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
663
self.assertLength(12, self.hpss_calls)
664
self.assertLength(1, self.hpss_connections)
588
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
667
591
class TestRemoteBranch(TestCaseWithSFTPServer):
688
612
self.assertFalse(t.has('remote/file'))
615
class TestDeprecatedAliases(tests.TestCaseWithTransport):
617
def test_deprecated_aliases(self):
618
"""bzr branch can be called clone or get, but those names are
623
for command in ['clone', 'get']:
625
$ bzr %(command)s A B
626
2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
627
2>bzr: ERROR: Not a branch...
691
631
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
693
633
def _checkout_and_branch(self, option=''):
694
634
self.script_runner.run_script(self, '''
695
$ brz checkout %(option)s repo/trunk checkout
635
$ bzr checkout %(option)s repo/trunk checkout
697
$ brz branch --switch ../repo/trunk ../repo/branched
637
$ bzr branch --switch ../repo/trunk ../repo/branched
698
638
2>Branched 0 revisions.
699
639
2>Tree is up to date at revision 0.
700
640
2>Switched to branch:...branched...
705
645
return (bound_branch, master_branch)
707
647
def test_branch_switch_parent_lightweight(self):
708
"""Lightweight checkout using brz branch --switch."""
648
"""Lightweight checkout using bzr branch --switch."""
709
649
bb, mb = self._checkout_and_branch(option='--lightweight')
710
650
self.assertParent('repo/trunk', bb)
711
651
self.assertParent('repo/trunk', mb)
713
653
def test_branch_switch_parent_heavyweight(self):
714
"""Heavyweight checkout using brz branch --switch."""
654
"""Heavyweight checkout using bzr branch --switch."""
715
655
bb, mb = self._checkout_and_branch()
716
656
self.assertParent('repo/trunk', bb)
717
657
self.assertParent('repo/trunk', mb)