142
151
branchb_id = tree2.commit('bar')
143
152
checkout = tree1.branch.create_checkout('heavyco/a', lightweight=False)
144
153
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())
154
# Refresh checkout as 'switch' modified it
155
checkout = checkout.controldir.open_workingtree()
156
self.assertEqual(branchb_id, checkout.last_revision())
157
self.assertEqual(tree2.branch.base,
158
checkout.branch.get_bound_location())
160
def test_switch_finds_relative_unicode_branch(self):
161
"""Switch will find 'foo' relative to the branch the checkout is of."""
162
self.requireFeature(UnicodeFilenameFeature)
163
self.build_tree(['repo/'])
164
tree1 = self.make_branch_and_tree('repo/brancha')
166
tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
167
tree2.pull(tree1.branch)
168
branchb_id = tree2.commit('bar')
169
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
170
self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
171
self.assertEqual(branchb_id, checkout.last_revision())
172
checkout = checkout.controldir.open_workingtree()
173
self.assertEqual(tree2.branch.base, checkout.branch.base)
175
def test_switch_finds_relative_unicode_branch(self):
176
"""Switch will find 'foo' relative to the branch the checkout is of."""
177
self.requireFeature(UnicodeFilenameFeature)
178
self.build_tree(['repo/'])
179
tree1 = self.make_branch_and_tree('repo/brancha')
181
tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
182
tree2.pull(tree1.branch)
183
branchb_id = tree2.commit('bar')
184
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
185
self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
186
self.assertEqual(branchb_id, checkout.last_revision())
187
checkout = checkout.controldir.open_workingtree()
188
self.assertEqual(tree2.branch.base, checkout.branch.base)
148
190
def test_switch_revision(self):
149
191
tree = self._create_sample_tree()
150
192
checkout = tree.branch.create_checkout('checkout', lightweight=True)
151
193
self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
152
self.failUnlessExists('checkout/file-1')
153
self.failIfExists('checkout/file-2')
194
self.assertPathExists('checkout/file-1')
195
self.assertPathDoesNotExist('checkout/file-2')
197
def test_switch_into_colocated(self):
198
# Create a new colocated branch from an existing non-colocated branch.
199
tree = self.make_branch_and_tree('.', format='development-colo')
200
self.build_tree(['file-1', 'file-2'])
202
revid1 = tree.commit('rev1')
204
revid2 = tree.commit('rev2')
205
self.run_bzr(['switch', '-b', 'anotherbranch'])
207
{'', 'anotherbranch'},
208
set(tree.branch.controldir.branch_names()))
210
def test_switch_into_unrelated_colocated(self):
211
# Create a new colocated branch from an existing non-colocated branch.
212
tree = self.make_branch_and_tree('.', format='development-colo')
213
self.build_tree(['file-1', 'file-2'])
215
revid1 = tree.commit('rev1')
217
revid2 = tree.commit('rev2')
218
tree.controldir.create_branch(name='foo')
219
self.run_bzr_error(['Cannot switch a branch, only a checkout.'],
221
self.run_bzr(['switch', '--force', 'foo'])
223
def test_switch_existing_colocated(self):
224
# Create a branch branch-1 that initially is a checkout of 'foo'
225
# Use switch to change it to 'anotherbranch'
226
repo = self.make_repository('branch-1', format='development-colo')
227
target_branch = repo.controldir.create_branch(name='foo')
228
repo.controldir.set_branch_reference(target_branch)
229
tree = repo.controldir.create_workingtree()
230
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
232
revid1 = tree.commit('rev1')
234
revid2 = tree.commit('rev2')
235
otherbranch = tree.controldir.create_branch(name='anotherbranch')
236
otherbranch.generate_revision_history(revid1)
237
self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
238
tree = WorkingTree.open("branch-1")
239
self.assertEqual(tree.last_revision(), revid1)
240
self.assertEqual(tree.branch.control_url, otherbranch.control_url)
242
def test_switch_new_colocated(self):
243
# Create a branch branch-1 that initially is a checkout of 'foo'
244
# Use switch to create 'anotherbranch' which derives from that
245
repo = self.make_repository('branch-1', format='development-colo')
246
target_branch = repo.controldir.create_branch(name='foo')
247
repo.controldir.set_branch_reference(target_branch)
248
tree = repo.controldir.create_workingtree()
249
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
251
revid1 = tree.commit('rev1')
252
self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
253
bzrdir = ControlDir.open("branch-1")
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)
260
def test_switch_new_colocated_unicode(self):
261
# Create a branch branch-1 that initially is a checkout of 'foo'
262
# Use switch to create 'branch\xe9' which derives from that
263
self.requireFeature(UnicodeFilenameFeature)
264
repo = self.make_repository('branch-1', format='development-colo')
265
target_branch = repo.controldir.create_branch(name='foo')
266
repo.controldir.set_branch_reference(target_branch)
267
tree = repo.controldir.create_workingtree()
268
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
270
revid1 = tree.commit('rev1')
271
self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
272
bzrdir = ControlDir.open("branch-1")
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)
155
279
def test_switch_only_revision(self):
156
280
tree = self._create_sample_tree()
157
281
checkout = tree.branch.create_checkout('checkout', lightweight=True)
158
self.failUnlessExists('checkout/file-1')
159
self.failUnlessExists('checkout/file-2')
282
self.assertPathExists('checkout/file-1')
283
self.assertPathExists('checkout/file-2')
160
284
self.run_bzr(['switch', '-r1'], working_dir='checkout')
161
self.failUnlessExists('checkout/file-1')
162
self.failIfExists('checkout/file-2')
285
self.assertPathExists('checkout/file-1')
286
self.assertPathDoesNotExist('checkout/file-2')
163
287
# Check that we don't accept a range
164
288
self.run_bzr_error(
165
['bzr switch --revision takes exactly one revision identifier'],
289
['brz switch --revision takes exactly one revision identifier'],
166
290
['switch', '-r0..2'], working_dir='checkout')
168
292
def prepare_lightweight_switch(self):
252
378
self.assertLength(0, calls)
253
379
out, err = self.run_bzr('switch ../branch2')
254
380
self.assertLength(1, calls)
382
def test_switch_lightweight_directory(self):
383
"""Test --directory option"""
385
# create a source branch
386
a_tree = self.make_branch_and_tree('a')
387
self.build_tree_contents([('a/a', b'initial\n')])
389
a_tree.commit(message='initial')
391
# clone and add a differing revision
392
b_tree = a_tree.controldir.sprout('b').open_workingtree()
393
self.build_tree_contents([('b/a', b'initial\nmore\n')])
394
b_tree.commit(message='more')
396
self.run_bzr('checkout --lightweight a checkout')
397
self.run_bzr('switch --directory checkout b')
398
self.assertFileEqual(b'initial\nmore\n', 'checkout/a')
401
class TestSwitchParentLocationBase(TestCaseWithTransport):
404
"""Set up a repository and branch ready for testing."""
405
super(TestSwitchParentLocationBase, self).setUp()
406
self.script_runner = script.ScriptRunner()
407
self.script_runner.run_script(self, '''
408
$ brz init-shared-repo --no-trees repo
411
shared repository: repo
412
$ brz init repo/trunk
413
Created a repository branch...
414
Using shared repository: ...
417
def assertParent(self, expected_parent, branch):
418
"""Verify that the parent is not None and is set correctly."""
419
actual_parent = branch.get_parent()
420
self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
424
class TestSwitchParentLocation(TestSwitchParentLocationBase):
426
def _checkout_and_switch(self, option=''):
427
self.script_runner.run_script(self, '''
428
$ brz checkout %(option)s repo/trunk checkout
430
$ brz switch --create-branch switched
431
2>Tree is up to date at revision 0.
432
2>Switched to branch at .../switched/
435
bound_branch = branch.Branch.open_containing('checkout')[0]
436
master_branch = branch.Branch.open_containing('repo/switched')[0]
437
return (bound_branch, master_branch)
439
def test_switch_parent_lightweight(self):
440
"""Lightweight checkout using brz switch."""
441
bb, mb = self._checkout_and_switch(option='--lightweight')
442
self.assertParent('repo/trunk', bb)
443
self.assertParent('repo/trunk', mb)
445
def test_switch_parent_heavyweight(self):
446
"""Heavyweight checkout using brz switch."""
447
bb, mb = self._checkout_and_switch()
448
self.assertParent('repo/trunk', bb)
449
self.assertParent('repo/trunk', mb)
452
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
453
# See https://bugs.launchpad.net/bzr/+bug/812285
454
# "brz switch --create-branch" can point the new branch's parent to the
455
# master branch, but it doesn't have to open it to do so.
457
def test_switch_create_doesnt_open_master_branch(self):
458
master = self.make_branch_and_tree('master')
460
# Note: not a lightweight checkout
461
checkout = master.branch.create_checkout('checkout')
464
def open_hook(branch):
465
# Just append the final directory of the branch
466
name = branch.base.rstrip('/').rsplit('/', 1)[1]
468
branch.Branch.hooks.install_named_hook('open', open_hook,
470
self.run_bzr('switch --create-branch -d checkout feature')
471
# We only open the master branch 1 time.
472
# This test should be cleaner to write, but see bug:
473
# https://bugs.launchpad.net/bzr/+bug/812295
474
self.assertEqual(1, opened.count('master'))
477
class TestSwitchUncommitted(TestCaseWithTransport):
480
tree = self.make_branch_and_tree('orig')
482
tree.branch.controldir.sprout('new')
483
checkout = tree.branch.create_checkout('checkout', lightweight=True)
484
self.build_tree(['checkout/a'])
485
self.assertPathExists('checkout/a')
489
def test_store_and_restore_uncommitted(self):
490
checkout = self.prepare()
491
self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
492
self.build_tree(['checkout/b'])
494
self.assertPathDoesNotExist('checkout/a')
495
self.assertPathExists('checkout/b')
496
self.run_bzr(['switch', '--store', '-d', 'checkout', 'orig'])
497
self.assertPathExists('checkout/a')
498
self.assertPathDoesNotExist('checkout/b')
500
def test_does_not_store(self):
502
self.run_bzr(['switch', '-d', 'checkout', 'new'])
503
self.assertPathExists('checkout/a')
505
def test_does_not_restore_changes(self):
507
self.run_bzr(['switch', '--store', '-d', 'checkout', 'new'])
508
self.assertPathDoesNotExist('checkout/a')
509
self.run_bzr(['switch', '-d', 'checkout', 'orig'])
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