52
58
self.assertFalse(b._transport.has('branch-name'))
53
59
b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
61
def test_branch_switch_no_branch(self):
62
# No branch in the current directory:
63
# => new branch will be created, but switch fails
64
self.example_branch('a')
65
self.make_repository('current')
66
self.run_bzr_error(['No WorkingTree exists for'],
67
'branch --switch ../a ../b', working_dir='current')
68
a = branch.Branch.open('a')
69
b = branch.Branch.open('b')
70
self.assertEqual(a.last_revision(), b.last_revision())
72
def test_branch_switch_no_wt(self):
73
# No working tree in the current directory:
74
# => new branch will be created, but switch fails and the current
75
# branch is unmodified
76
self.example_branch('a')
77
self.make_branch('current')
78
self.run_bzr_error(['No WorkingTree exists for'],
79
'branch --switch ../a ../b', working_dir='current')
80
a = branch.Branch.open('a')
81
b = branch.Branch.open('b')
82
self.assertEqual(a.last_revision(), b.last_revision())
83
work = branch.Branch.open('current')
84
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
86
def test_branch_switch_no_checkout(self):
87
# Standalone branch in the current directory:
88
# => new branch will be created, but switch fails and the current
89
# branch is unmodified
90
self.example_branch('a')
91
self.make_branch_and_tree('current')
92
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
93
'branch --switch ../a ../b', working_dir='current')
94
a = branch.Branch.open('a')
95
b = branch.Branch.open('b')
96
self.assertEqual(a.last_revision(), b.last_revision())
97
work = branch.Branch.open('current')
98
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
100
def test_branch_switch_checkout(self):
101
# Checkout in the current directory:
102
# => new branch will be created and checkout bound to the new branch
103
self.example_branch('a')
104
self.run_bzr('checkout a current')
105
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
106
a = branch.Branch.open('a')
107
b = branch.Branch.open('b')
108
self.assertEqual(a.last_revision(), b.last_revision())
109
work = WorkingTree.open('current')
110
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
111
self.assertContainsRe(err, "Switched to branch: .*/b/")
113
def test_branch_switch_lightweight_checkout(self):
114
# Lightweight checkout in the current directory:
115
# => new branch will be created and lightweight checkout pointed to
117
self.example_branch('a')
118
self.run_bzr('checkout --lightweight a current')
119
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
120
a = branch.Branch.open('a')
121
b = branch.Branch.open('b')
122
self.assertEqual(a.last_revision(), b.last_revision())
123
work = WorkingTree.open('current')
124
self.assertEndsWith(work.branch.base, '/b/')
125
self.assertContainsRe(err, "Switched to branch: .*/b/")
55
127
def test_branch_only_copies_history(self):
56
128
# Knit branches should only push the history for the current revision.
57
129
format = bzrdir.BzrDirMetaFormat1()
149
221
class TestBranchStacked(ExternalBase):
150
222
"""Tests for branch --stacked"""
152
def check_shallow_branch(self, branch_revid, stacked_on):
153
"""Assert that the branch 'newbranch' has been published correctly.
155
:param stacked_on: url of a branch this one is stacked upon.
156
:param branch_revid: a revision id that should be the only
157
revision present in the stacked branch, and it should not be in
158
the reference branch.
160
new_branch = branch.Branch.open('newbranch')
161
# The branch refers to the mainline
162
self.assertEqual(stacked_on, new_branch.get_stacked_on_url())
163
# and the branch's work was pushed
164
self.assertTrue(new_branch.repository.has_revision(branch_revid))
165
# The newly committed revision shoud be present in the stacked branch,
166
# but not in the stacked-on branch. Because stacking is set up by the
167
# branch object, if we open the stacked branch's repository directly,
168
# bypassing the branch, we see only what's in the stacked repository.
169
stacked_repo = bzrdir.BzrDir.open('newbranch').open_repository()
170
stacked_repo_revisions = set(stacked_repo.all_revision_ids())
171
if len(stacked_repo_revisions) != 1:
172
self.fail("wrong revisions in stacked repository: %r"
173
% (stacked_repo_revisions,))
175
224
def assertRevisionInRepository(self, repo_path, revid):
176
225
"""Check that a revision is in a repository, disregarding stacking."""
177
226
repo = bzrdir.BzrDir.open(repo_path).open_repository()
199
248
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
200
249
# with some work on it
201
branch_tree.commit('moar work plz')
250
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
251
work_tree.commit('moar work plz')
252
work_tree.branch.push(branch_tree.branch)
202
253
# branching our local branch gives us a new stacked branch pointing at
204
255
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
205
256
self.assertEqual('', out)
206
self.assertEqual('Branched 1 revision(s).\n',
257
self.assertEqual('Branched 2 revision(s).\n',
208
259
# it should have preserved the branch format, and so it should be
209
260
# capable of supporting stacking, but not actually have a stacked_on
231
284
self.assertEqual(branch_tree.branch.base,
232
285
branch.Branch.open('branch2').get_stacked_on_url())
233
286
branch2_tree = WorkingTree.open('branch2')
234
branch2_revid = branch2_tree.commit('work on second stacked branch')
287
branch2_revid = work_tree.commit('work on second stacked branch')
288
work_tree.branch.push(branch2_tree.branch)
235
289
self.assertRevisionInRepository('branch2', branch2_revid)
236
290
self.assertRevisionsInBranchRepository(
237
291
[trunk_revid, branch_revid, branch2_revid],
250
304
self.assertEqual('Created new stacked branch referring to %s.\n' %
251
305
trunk_tree.branch.base, err)
252
306
self.assertRevisionNotInRepository('newbranch', original_revid)
253
new_tree = WorkingTree.open('newbranch')
254
new_revid = new_tree.commit('new work')
255
self.check_shallow_branch(new_revid, trunk_tree.branch.base)
307
new_branch = branch.Branch.open('newbranch')
308
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
257
310
def test_branch_stacked_from_smart_server(self):
258
311
# We can branch stacking on a smart server