145
154
self.assertEqual(branchb_id, checkout.last_revision())
146
155
self.assertEqual(tree2.branch.base, checkout.branch.get_bound_location())
157
def test_switch_finds_relative_unicode_branch(self):
158
"""Switch will find 'foo' relative to the branch the checkout is of."""
159
self.requireFeature(UnicodeFilenameFeature)
160
self.build_tree(['repo/'])
161
tree1 = self.make_branch_and_tree('repo/brancha')
163
tree2 = self.make_branch_and_tree(u'repo/branch\xe9')
164
tree2.pull(tree1.branch)
165
branchb_id = tree2.commit('bar')
166
checkout = tree1.branch.create_checkout('checkout', lightweight=True)
167
self.run_bzr(['switch', u'branch\xe9'], working_dir='checkout')
168
self.assertEqual(branchb_id, checkout.last_revision())
169
checkout = checkout.bzrdir.open_workingtree()
170
self.assertEqual(tree2.branch.base, checkout.branch.base)
148
172
def test_switch_revision(self):
149
173
tree = self._create_sample_tree()
150
174
checkout = tree.branch.create_checkout('checkout', lightweight=True)
151
175
self.run_bzr(['switch', 'branch-1', '-r1'], working_dir='checkout')
152
self.failUnlessExists('checkout/file-1')
153
self.failIfExists('checkout/file-2')
176
self.assertPathExists('checkout/file-1')
177
self.assertPathDoesNotExist('checkout/file-2')
179
def test_switch_existing_colocated(self):
180
# Create a branch branch-1 that initially is a checkout of 'foo'
181
# Use switch to change it to 'anotherbranch'
182
repo = self.make_repository('branch-1', format='development-colo')
183
target_branch = repo.bzrdir.create_branch(name='foo')
184
branch.BranchReferenceFormat().initialize(
185
repo.bzrdir, target_branch=target_branch)
186
tree = repo.bzrdir.create_workingtree()
187
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
189
revid1 = tree.commit('rev1')
191
revid2 = tree.commit('rev2')
192
otherbranch = tree.bzrdir.create_branch(name='anotherbranch')
193
otherbranch.generate_revision_history(revid1)
194
self.run_bzr(['switch', 'anotherbranch'], working_dir='branch-1')
195
tree = WorkingTree.open("branch-1")
196
self.assertEquals(tree.last_revision(), revid1)
197
self.assertEquals(tree.branch.control_url, otherbranch.control_url)
199
def test_switch_new_colocated(self):
200
# Create a branch branch-1 that initially is a checkout of 'foo'
201
# Use switch to create 'anotherbranch' which derives from that
202
repo = self.make_repository('branch-1', format='development-colo')
203
target_branch = repo.bzrdir.create_branch(name='foo')
204
branch.BranchReferenceFormat().initialize(
205
repo.bzrdir, target_branch=target_branch)
206
tree = repo.bzrdir.create_workingtree()
207
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
209
revid1 = tree.commit('rev1')
210
self.run_bzr(['switch', '-b', 'anotherbranch'], working_dir='branch-1')
211
bzrdir = BzrDir.open("branch-1")
213
set([b.name for b in bzrdir.list_branches()]),
214
set(["foo", "anotherbranch"]))
215
self.assertEquals(bzrdir.open_branch().name, "anotherbranch")
216
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
218
def test_switch_new_colocated_unicode(self):
219
# Create a branch branch-1 that initially is a checkout of 'foo'
220
# Use switch to create 'branch\xe9' which derives from that
221
self.requireFeature(UnicodeFilenameFeature)
222
repo = self.make_repository('branch-1', format='development-colo')
223
target_branch = repo.bzrdir.create_branch(name='foo')
224
branch.BranchReferenceFormat().initialize(
225
repo.bzrdir, target_branch=target_branch)
226
tree = repo.bzrdir.create_workingtree()
227
self.build_tree(['branch-1/file-1', 'branch-1/file-2'])
229
revid1 = tree.commit('rev1')
230
self.run_bzr(['switch', '-b', u'branch\xe9'], working_dir='branch-1')
231
bzrdir = BzrDir.open("branch-1")
233
set([b.name for b in bzrdir.list_branches()]),
234
set(["foo", u"branch\xe9"]))
235
self.assertEquals(bzrdir.open_branch().name, u"branch\xe9")
236
self.assertEquals(bzrdir.open_branch().last_revision(), revid1)
155
238
def test_switch_only_revision(self):
156
239
tree = self._create_sample_tree()
157
240
checkout = tree.branch.create_checkout('checkout', lightweight=True)
158
self.failUnlessExists('checkout/file-1')
159
self.failUnlessExists('checkout/file-2')
241
self.assertPathExists('checkout/file-1')
242
self.assertPathExists('checkout/file-2')
160
243
self.run_bzr(['switch', '-r1'], working_dir='checkout')
161
self.failUnlessExists('checkout/file-1')
162
self.failIfExists('checkout/file-2')
244
self.assertPathExists('checkout/file-1')
245
self.assertPathDoesNotExist('checkout/file-2')
163
246
# Check that we don't accept a range
164
247
self.run_bzr_error(
165
248
['bzr switch --revision takes exactly one revision identifier'],
270
353
self.run_bzr('checkout --lightweight a checkout')
271
354
self.run_bzr('switch --directory checkout b')
272
355
self.assertFileEqual('initial\nmore\n', 'checkout/a')
358
class TestSwitchParentLocationBase(TestCaseWithTransport):
361
"""Set up a repository and branch ready for testing."""
362
super(TestSwitchParentLocationBase, self).setUp()
363
self.script_runner = script.ScriptRunner()
364
self.script_runner.run_script(self, '''
365
$ bzr init-repo --no-trees repo
368
shared repository: repo
369
$ bzr init repo/trunk
370
Created a repository branch...
371
Using shared repository: ...
374
def assertParent(self, expected_parent, branch):
375
"""Verify that the parent is not None and is set correctly."""
376
actual_parent = branch.get_parent()
377
self.assertIsSameRealPath(urlutils.local_path_to_url(expected_parent),
381
class TestSwitchParentLocation(TestSwitchParentLocationBase):
383
def _checkout_and_switch(self, option=''):
384
self.script_runner.run_script(self, '''
385
$ bzr checkout %(option)s repo/trunk checkout
387
$ bzr switch --create-branch switched
388
2>Tree is up to date at revision 0.
389
2>Switched to branch:...switched...
392
bound_branch = branch.Branch.open_containing('checkout')[0]
393
master_branch = branch.Branch.open_containing('repo/switched')[0]
394
return (bound_branch, master_branch)
396
def test_switch_parent_lightweight(self):
397
"""Lightweight checkout using bzr switch."""
398
bb, mb = self._checkout_and_switch(option='--lightweight')
399
self.assertParent('repo/trunk', bb)
400
self.assertParent('repo/trunk', mb)
402
def test_switch_parent_heavyweight(self):
403
"""Heavyweight checkout using bzr switch."""
404
bb, mb = self._checkout_and_switch()
405
self.assertParent('repo/trunk', bb)
406
self.assertParent('repo/trunk', mb)
409
class TestSwitchDoesntOpenMasterBranch(TestCaseWithTransport):
410
# See https://bugs.launchpad.net/bzr/+bug/812285
411
# "bzr switch --create-branch" can point the new branch's parent to the
412
# master branch, but it doesn't have to open it to do so.
414
def test_switch_create_doesnt_open_master_branch(self):
415
master = self.make_branch_and_tree('master')
417
# Note: not a lightweight checkout
418
checkout = master.branch.create_checkout('checkout')
420
def open_hook(branch):
421
# Just append the final directory of the branch
422
name = branch.base.rstrip('/').rsplit('/', 1)[1]
424
branch.Branch.hooks.install_named_hook('open', open_hook,
426
self.run_bzr('switch --create-branch -d checkout feature')
427
# We only open the master branch 1 time.
428
# This test should be cleaner to write, but see bug:
429
# https://bugs.launchpad.net/bzr/+bug/812295
430
self.assertEqual(1, opened.count('master'))