77
75
tree1 = self.make_branch_and_tree('branch1')
78
76
tree2 = self.make_branch_and_tree('branch2')
79
77
tree2.pull(tree1.branch)
80
checkout = tree1.branch.create_checkout('checkout',
81
lightweight=lightweight)
78
checkout = tree1.branch.create_checkout('checkout',
79
lightweight=lightweight)
82
80
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
83
81
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
85
83
self.run_bzr('switch branch2', working_dir='checkout')
87
85
# we need to get the tree again, otherwise we don't get the new branch
88
86
checkout = WorkingTree.open('checkout')
89
87
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
90
88
self.assertEqual(checkout.branch.get_config().has_explicit_nickname(),
93
91
def test_switch_nick(self):
94
92
self._test_switch_nick(lightweight=False)
101
99
tree1 = self.make_branch_and_tree('branch1')
102
100
tree2 = self.make_branch_and_tree('branch2')
103
101
tree2.pull(tree1.branch)
104
checkout = tree1.branch.create_checkout('checkout',
105
lightweight=lightweight)
102
checkout = tree1.branch.create_checkout('checkout',
103
lightweight=lightweight)
106
104
self.assertEqual(checkout.branch.nick, tree1.branch.nick)
107
105
checkout.branch.nick = "explicit_nick"
108
106
self.assertEqual(checkout.branch.nick, "explicit_nick")
109
107
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
111
109
self.run_bzr('switch branch2', working_dir='checkout')
113
111
# we need to get the tree again, otherwise we don't get the new branch
114
112
checkout = WorkingTree.open('checkout')
115
113
self.assertEqual(checkout.branch.nick, tree2.branch.nick)
116
114
self.assertEqual(checkout.branch.get_config()._get_explicit_nickname(),
119
117
def test_switch_explicit_nick(self):
120
118
self._test_switch_explicit_nick(lightweight=False)
217
215
revid1 = tree.commit('rev1')
218
216
tree.add('file-2')
219
217
revid2 = tree.commit('rev2')
220
tree.bzrdir.create_branch(name='foo')
218
tree.controldir.create_branch(name='foo')
221
219
self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
223
221
self.run_bzr(['switch', '--force', 'foo'])
225
223
def test_switch_existing_colocated(self):
226
224
# Create a branch branch-1 that initially is a checkout of 'foo'
227
225
# Use switch to change it to 'anotherbranch'
228
226
repo = self.make_repository('branch-1', format='development-colo')
229
target_branch = repo.bzrdir.create_branch(name='foo')
230
repo.bzrdir.set_branch_reference(target_branch)
231
tree = repo.bzrdir.create_workingtree()
227
target_branch = repo.controldir.create_branch(name='foo')
228
repo.controldir.set_branch_reference(target_branch)
229
tree = repo.controldir.create_workingtree()
232
230
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
233
231
tree.add('file-1')
234
232
revid1 = tree.commit('rev1')
235
233
tree.add('file-2')
236
234
revid2 = tree.commit('rev2')
237
otherbranch = tree.bzrdir.create_branch(name='anotherbranch')
235
otherbranch = tree.controldir.create_branch(name='anotherbranch')
238
236
otherbranch.generate_revision_history(revid1)
239
237
self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
240
238
tree = WorkingTree.open("branch-1")
241
self.assertEquals(tree.last_revision(), revid1)
242
self.assertEquals(tree.branch.control_url, otherbranch.control_url)
239
self.assertEqual(tree.last_revision(), revid1)
240
self.assertEqual(tree.branch.control_url, otherbranch.control_url)
244
242
def test_switch_new_colocated(self):
245
243
# Create a branch branch-1 that initially is a checkout of 'foo'
246
244
# Use switch to create 'anotherbranch' which derives from that
247
245
repo = self.make_repository('branch-1', format='development-colo')
248
target_branch = repo.bzrdir.create_branch(name='foo')
249
repo.bzrdir.set_branch_reference(target_branch)
250
tree = repo.bzrdir.create_workingtree()
246
target_branch = repo.controldir.create_branch(name='foo')
247
repo.controldir.set_branch_reference(target_branch)
248
tree = repo.controldir.create_workingtree()
251
249
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
252
250
tree.add('file-1')
253
251
revid1 = tree.commit('rev1')
254
252
self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
255
253
bzrdir = ControlDir.open("branch-1")
257
set([b.name for b in bzrdir.list_branches()]),
258
set(["foo", "anotherbranch"]))
259
self.assertEquals(bzrdir.open_branch().name, "anotherbranch")
260
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
255
{b.name for b in bzrdir.list_branches()},
256
{"foo", "anotherbranch"})
257
self.assertEqual(bzrdir.open_branch().name, "anotherbranch")
258
self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
262
260
def test_switch_new_colocated_unicode(self):
263
261
# Create a branch branch-1 that initially is a checkout of 'foo'
264
262
# Use switch to create 'branch\xe9' which derives from that
265
263
self.requireFeature(UnicodeFilenameFeature)
266
264
repo = self.make_repository('branch-1', format='development-colo')
267
target_branch = repo.bzrdir.create_branch(name='foo')
268
repo.bzrdir.set_branch_reference(target_branch)
269
tree = repo.bzrdir.create_workingtree()
265
target_branch = repo.controldir.create_branch(name='foo')
266
repo.controldir.set_branch_reference(target_branch)
267
tree = repo.controldir.create_workingtree()
270
268
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
271
269
tree.add('file-1')
272
270
revid1 = tree.commit('rev1')
273
271
self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
274
272
bzrdir = ControlDir.open("branch-1")
276
set([b.name for b in bzrdir.list_branches()]),
277
set(["foo", u"branch\xe9"]))
278
self.assertEquals(bzrdir.open_branch().name, u"branch\xe9")
279
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
274
{b.name for b in bzrdir.list_branches()},
275
{"foo", u"branch\xe9"})
276
self.assertEqual(bzrdir.open_branch().name, u"branch\xe9")
277
self.assertEqual(bzrdir.open_branch().last_revision(), revid1)
281
279
def test_switch_only_revision(self):
282
280
tree = self._create_sample_tree()
312
310
def test_create_branch_no_branch(self):
313
311
self.prepare_lightweight_switch()
314
312
self.run_bzr_error(['cannot create branch without source branch'],
315
'switch --create-branch ../branch2', working_dir='tree')
313
'switch --create-branch ../branch2', working_dir='tree')
317
315
def test_create_branch(self):
318
316
branch = self.make_branch('branch')
319
317
tree = branch.create_checkout('tree', lightweight=True)
320
tree.commit('one', rev_id='rev-1')
318
tree.commit('one', rev_id=b'rev-1')
321
319
self.run_bzr('switch --create-branch ../branch2', working_dir='tree')
322
320
tree = WorkingTree.open('tree')
323
321
self.assertEndsWith(tree.branch.base, '/branch2/')
386
385
# create a source branch
387
386
a_tree = self.make_branch_and_tree('a')
388
self.build_tree_contents([('a/a', 'initial\n')])
387
self.build_tree_contents([('a/a', b'initial\n')])
390
389
a_tree.commit(message='initial')
392
391
# clone and add a differing revision
393
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
394
self.build_tree_contents([('b/a', 'initial\nmore\n')])
392
b_tree = a_tree.controldir.sprout('b').open_workingtree()
393
self.build_tree_contents([('b/a', b'initial\nmore\n')])
395
394
b_tree.commit(message='more')
397
396
self.run_bzr('checkout --lightweight a checkout')
398
397
self.run_bzr('switch --directory checkout b')
399
self.assertFileEqual('initial\nmore\n', 'checkout/a')
398
self.assertFileEqual(b'initial\nmore\n', 'checkout/a')
402
401
class TestSwitchParentLocationBase(TestCaseWithTransport):
438
437
return (bound_branch, master_branch)
440
439
def test_switch_parent_lightweight(self):
441
"""Lightweight checkout using bzr switch."""
440
"""Lightweight checkout using brz switch."""
442
441
bb, mb = self._checkout_and_switch(option='--lightweight')
443
442
self.assertParent('repo/trunk', bb)
444
443
self.assertParent('repo/trunk', mb)
446
445
def test_switch_parent_heavyweight(self):
447
"""Heavyweight checkout using bzr switch."""
446
"""Heavyweight checkout using brz switch."""
448
447
bb, mb = self._checkout_and_switch()
449
448
self.assertParent('repo/trunk', bb)
450
449
self.assertParent('repo/trunk', mb)
474
474
self.assertEqual(1, opened.count('master'))
477
class TestSmartServerSwitch(TestCaseWithTransport):
479
def test_switch_lightweight(self):
480
self.setup_smart_server_with_call_log()
481
t = self.make_branch_and_tree('from')
482
for count in range(9):
483
t.commit(message='commit %d' % count)
484
out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
486
self.reset_smart_call_log()
487
self.run_bzr(['switch', self.get_url('from')], working_dir='target')
488
# This figure represent the amount of work to perform this use case. It
489
# is entirely ok to reduce this number if a test fails due to rpc_count
490
# being too low. If rpc_count increases, more network roundtrips have
491
# become necessary for this use case. Please do not adjust this number
492
# upwards without agreement from bzr's network support maintainers.
493
self.assertLength(24, self.hpss_calls)
494
self.assertLength(4, self.hpss_connections)
495
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
498
477
class TestSwitchUncommitted(TestCaseWithTransport):
500
479
def prepare(self):
501
480
tree = self.make_branch_and_tree('orig')
503
tree.branch.bzrdir.sprout('new')
482
tree.branch.controldir.sprout('new')
504
483
checkout = tree.branch.create_checkout('checkout', lightweight=True)
505
484
self.build_tree(['checkout/a'])
506
485
self.assertPathExists('checkout/a')
529
508
self.assertPathDoesNotExist('checkout/a')
530
509
self.run_bzr(['switch', '-d', 'checkout', 'orig'])
531
510
self.assertPathDoesNotExist('checkout/a')
513
class TestSwitchStandAloneCorruption(TestCaseWithTransport):
515
def test_empty_tree_switch(self):
516
"""switch . on an empty tree gets infinite recursion
518
Inspired by: https://bugs.launchpad.net/bzr/+bug/1018628
520
self.script_runner = script.ScriptRunner()
521
self.script_runner.run_script(self, '''
523
Created a standalone tree (format: 2a)
525
2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.
528
def test_switch_on_previous_rev(self):
529
"""switch to previous rev in a standalone directory
531
Inspired by: https://bugs.launchpad.net/brz/+bug/1018628
533
self.script_runner = script.ScriptRunner()
534
self.script_runner.run_script(self, '''
536
Created a standalone tree (format: 2a)
537
$ brz commit -m 1 --unchanged
538
$ brz commit -m 2 --unchanged
540
2>brz: ERROR: switching would create a branch reference loop. Use the "bzr up" command to switch to a different revision.''',
541
null_output_matches_anything=True)
543
def test_switch_create_colo_locks_repo_path(self):
544
self.script_runner = script.ScriptRunner()
545
self.script_runner.run_script(self, '''
549
Created a standalone tree (format: 2a)
550
$ echo A > a && brz add a && brz commit -m A
557
''', null_output_matches_anything=True)
559
def test_switch_to_new_branch_on_old_rev(self):
560
"""switch to previous rev in a standalone directory
562
Inspired by: https://bugs.launchpad.net/brz/+bug/933362
564
self.script_runner = script.ScriptRunner()
565
self.script_runner.run_script(self, '''
567
Created a standalone tree (format: 2a)
568
$ brz switch -b trunk
569
2>Tree is up to date at revision 0.
570
2>Switched to branch trunk
571
$ brz commit -m 1 --unchanged
573
2>Committed revision 1.
574
$ brz commit -m 2 --unchanged
576
2>Committed revision 2.
577
$ brz switch -b blah -r1
578
2>Updated to revision 1.
579
2>Switched to branch blah