142
153
branchb_id = tree2.commit('bar')
143
154
checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
144
155
self.run_bzr(['switch', 'branchb'], working_dir='heavyco/a')
145
self.assertEqual(branchb_id, checkout.last_revision())
146
self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
156
# Refresh checkout as 'switch' modified it
157
checkout = checkout.bzrdir.open_workingtree()
158
self.assertEqual(branchb_id, checkout.last_revision())
159
self.assertEqual(tree2.branch.base,
160
checkout.branch.get_bound_location())
162
def test_switch_finds_relative_unicode_branch(self):
163
"""Switch will find 'foo' relative to the branch the checkout is of."""
164
self.requireFeature(UnicodeFilenameFeature)
165
self.build_tree(['repo/'])
166
tree1 = self.make_branch_and_tree('repo/brancha')
168
tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
169
tree2.pull(tree1.branch)
170
branchb_id = tree2.commit('bar')
171
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
172
self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
173
self.assertEqual(branchb_id, checkout.last_revision())
174
checkout = checkout.bzrdir.open_workingtree()
175
self.assertEqual(tree2.branch.base, checkout.branch.base)
148
177
def test_switch_revision(self):
149
178
tree = self._create_sample_tree()
150
179
checkout = tree.branch.create_checkout('checkout', lightweight=True)
151
180
self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
152
self.failUnlessExists('checkout/file-1')
153
self.failIfExists('checkout/file-2')
181
self.assertPathExists('checkout/file-1')
182
self.assertPathDoesNotExist('checkout/file-2')
184
def test_switch_existing_colocated(self):
185
# Create a branch branch-1 that initially is a checkout of 'foo'
186
# Use switch to change it to 'anotherbranch'
187
repo = self.make_repository('branch-1', format='development-colo')
188
target_branch = repo.bzrdir.create_branch(name='foo')
189
branch.BranchReferenceFormat().initialize(
190
repo.bzrdir, target_branch=target_branch)
191
tree = repo.bzrdir.create_workingtree()
192
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
194
revid1 = tree.commit('rev1')
196
revid2 = tree.commit('rev2')
197
otherbranch = tree.bzrdir.create_branch(name='anotherbranch')
198
otherbranch.generate_revision_history(revid1)
199
self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
200
tree = WorkingTree.open("branch-1")
201
self.assertEquals(tree.last_revision(), revid1)
202
self.assertEquals(tree.branch.control_url, otherbranch.control_url)
204
def test_switch_new_colocated(self):
205
# Create a branch branch-1 that initially is a checkout of 'foo'
206
# Use switch to create 'anotherbranch' which derives from that
207
repo = self.make_repository('branch-1', format='development-colo')
208
target_branch = repo.bzrdir.create_branch(name='foo')
209
branch.BranchReferenceFormat().initialize(
210
repo.bzrdir, target_branch=target_branch)
211
tree = repo.bzrdir.create_workingtree()
212
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
214
revid1 = tree.commit('rev1')
215
self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
216
bzrdir = BzrDir.open("branch-1")
218
set([b.name for b in bzrdir.list_branches()]),
219
set(["foo", "anotherbranch"]))
220
self.assertEquals(bzrdir.open_branch().name, "anotherbranch")
221
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
223
def test_switch_new_colocated_unicode(self):
224
# Create a branch branch-1 that initially is a checkout of 'foo'
225
# Use switch to create 'branch\xe9' which derives from that
226
self.requireFeature(UnicodeFilenameFeature)
227
repo = self.make_repository('branch-1', format='development-colo')
228
target_branch = repo.bzrdir.create_branch(name='foo')
229
branch.BranchReferenceFormat().initialize(
230
repo.bzrdir, target_branch=target_branch)
231
tree = repo.bzrdir.create_workingtree()
232
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
234
revid1 = tree.commit('rev1')
235
self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
236
bzrdir = BzrDir.open("branch-1")
238
set([b.name for b in bzrdir.list_branches()]),
239
set(["foo", u"branch\xe9"]))
240
self.assertEquals(bzrdir.open_branch().name, u"branch\xe9")
241
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
155
243
def test_switch_only_revision(self):
156
244
tree = self._create_sample_tree()
157
245
checkout = tree.branch.create_checkout('checkout', lightweight=True)
158
self.failUnlessExists('checkout/file-1')
159
self.failUnlessExists('checkout/file-2')
246
self.assertPathExists('checkout/file-1')
247
self.assertPathExists('checkout/file-2')
160
248
self.run_bzr(['switch', '-r1'], working_dir='checkout')
161
self.failUnlessExists('checkout/file-1')
162
self.failIfExists('checkout/file-2')
249
self.assertPathExists('checkout/file-1')
250
self.assertPathDoesNotExist('checkout/file-2')
163
251
# Check that we don't accept a range
164
252
self.run_bzr_error(
165
253
['bzr switch --revision takes exactly one revision identifier'],
252
340
self.assertLength(0, calls)
253
341
out, err = self.run_bzr('switch ../branch2')
254
342
self.assertLength(1, calls)
344
def test_switch_lightweight_directory(self):
345
"""Test --directory option"""
347
# create a source branch
348
a_tree = self.make_branch_and_tree('a')
349
self.build_tree_contents([('a/a', 'initial\n')])
351
a_tree.commit(message='initial')
353
# clone and add a differing revision
354
b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
355
self.build_tree_contents([('b/a', 'initial\nmore\n')])
356
b_tree.commit(message='more')
358
self.run_bzr('checkout --lightweight a checkout')
359
self.run_bzr('switch --directory checkout b')
360
self.assertFileEqual('initial\nmore\n', 'checkout/a')
363
class TestSwitchParentLocationBase(TestCaseWithTransport):
366
"""Set up a repository and branch ready for testing."""
367
super(TestSwitchParentLocationBase, self).setUp()
368
self.script_runner = script.ScriptRunner()
369
self.script_runner.run_script(self, '''
370
$ bzr init-repo --no-trees repo
373
shared repository: repo
374
$ bzr init repo/trunk
375
Created a repository branch...
376
Using shared repository: ...
379
def assertParent(self, expected_parent, branch):
380
"""Verify that the parent is not None and is set correctly."""
381
actual_parent = branch.get_parent()
382
self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
386
class TestSwitchParentLocation(TestSwitchParentLocationBase):
388
def _checkout_and_switch(self, option=''):
389
self.script_runner.run_script(self, '''
390
$ bzr checkout %(option)s repo/trunk checkout
392
$ bzr switch --create-branch switched
393
2>Tree is up to date at revision 0.
394
2>Switched to branch:...switched...
397
bound_branch = branch.Branch.open_containing('checkout')[0]
398
master_branch = branch.Branch.open_containing('repo/switched')[0]
399
return (bound_branch, master_branch)
401
def test_switch_parent_lightweight(self):
402
"""Lightweight checkout using bzr switch."""
403
bb, mb = self._checkout_and_switch(option='--lightweight')
404
self.assertParent('repo/trunk', bb)
405
self.assertParent('repo/trunk', mb)
407
def test_switch_parent_heavyweight(self):
408
"""Heavyweight checkout using bzr switch."""
409
bb, mb = self._checkout_and_switch()
410
self.assertParent('repo/trunk', bb)
411
self.assertParent('repo/trunk', mb)
414
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
415
# See https://bugs.launchpad.net/bzr/+bug/812285
416
# "bzr switch --create-branch" can point the new branch's parent to the
417
# master branch, but it doesn't have to open it to do so.
419
def test_switch_create_doesnt_open_master_branch(self):
420
master = self.make_branch_and_tree('master')
422
# Note: not a lightweight checkout
423
checkout = master.branch.create_checkout('checkout')
425
def open_hook(branch):
426
# Just append the final directory of the branch
427
name = branch.base.rstrip('/').rsplit('/', 1)[1]
429
branch.Branch.hooks.install_named_hook('open', open_hook,
431
self.run_bzr('switch --create-branch -d checkout feature')
432
# We only open the master branch 1 time.
433
# This test should be cleaner to write, but see bug:
434
# https://bugs.launchpad.net/bzr/+bug/812295
435
self.assertEqual(1, opened.count('master'))
438
class TestSmartServerSwitch(TestCaseWithTransport):
440
def test_switch_lightweight(self):
441
self.setup_smart_server_with_call_log()
442
t = self.make_branch_and_tree('from')
443
for count in range(9):
444
t.commit(message='commit %d' % count)
445
out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
447
self.reset_smart_call_log()
448
self.run_bzr(['switch', self.get_url('from')], working_dir='target')
449
# This figure represent the amount of work to perform this use case. It
450
# is entirely ok to reduce this number if a test fails due to rpc_count
451
# being too low. If rpc_count increases, more network roundtrips have
452
# become necessary for this use case. Please do not adjust this number
453
# upwards without agreement from bzr's network support maintainers.
454
self.assertLength(24, self.hpss_calls)
455
self.assertLength(5, self.hpss_connections)
456
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)