1
# Copyright (C) 2006-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Black-box tests for bzr branch."""
27
revision as _mod_revision,
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
from bzrlib.tests.blackbox import ExternalBase
31
from bzrlib.tests import (
36
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
37
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
from bzrlib.workingtree import WorkingTree
41
class TestBranch(ExternalBase):
43
def example_branch(self, path='.'):
44
tree = self.make_branch_and_tree(path)
45
self.build_tree_contents([(path + '/hello', 'foo')])
47
tree.commit(message='setup')
48
self.build_tree_contents([(path + '/goodbye', 'baz')])
50
tree.commit(message='setup')
52
def test_branch(self):
53
"""Branch from one branch to another."""
54
self.example_branch('a')
55
self.run_bzr('branch a b')
56
b = branch.Branch.open('b')
57
self.run_bzr('branch a c -r 1')
58
# previously was erroneously created by branching
59
self.assertFalse(b._transport.has('branch-name'))
60
b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
62
def test_branch_switch_no_branch(self):
63
# No branch in the current directory:
64
# => new branch will be created, but switch fails
65
self.example_branch('a')
66
self.make_repository('current')
67
self.run_bzr_error(['No WorkingTree exists for'],
68
'branch --switch ../a ../b', working_dir='current')
69
a = branch.Branch.open('a')
70
b = branch.Branch.open('b')
71
self.assertEqual(a.last_revision(), b.last_revision())
73
def test_branch_switch_no_wt(self):
74
# No working tree in the current directory:
75
# => new branch will be created, but switch fails and the current
76
# branch is unmodified
77
self.example_branch('a')
78
self.make_branch('current')
79
self.run_bzr_error(['No WorkingTree exists for'],
80
'branch --switch ../a ../b', working_dir='current')
81
a = branch.Branch.open('a')
82
b = branch.Branch.open('b')
83
self.assertEqual(a.last_revision(), b.last_revision())
84
work = branch.Branch.open('current')
85
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
87
def test_branch_switch_no_checkout(self):
88
# Standalone branch in the current directory:
89
# => new branch will be created, but switch fails and the current
90
# branch is unmodified
91
self.example_branch('a')
92
self.make_branch_and_tree('current')
93
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
94
'branch --switch ../a ../b', working_dir='current')
95
a = branch.Branch.open('a')
96
b = branch.Branch.open('b')
97
self.assertEqual(a.last_revision(), b.last_revision())
98
work = branch.Branch.open('current')
99
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
101
def test_branch_switch_checkout(self):
102
# Checkout in the current directory:
103
# => new branch will be created and checkout bound to the new branch
104
self.example_branch('a')
105
self.run_bzr('checkout a current')
106
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
107
a = branch.Branch.open('a')
108
b = branch.Branch.open('b')
109
self.assertEqual(a.last_revision(), b.last_revision())
110
work = WorkingTree.open('current')
111
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
112
self.assertContainsRe(err, "Switched to branch: .*/b/")
114
def test_branch_switch_lightweight_checkout(self):
115
# Lightweight checkout in the current directory:
116
# => new branch will be created and lightweight checkout pointed to
118
self.example_branch('a')
119
self.run_bzr('checkout --lightweight a current')
120
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
121
a = branch.Branch.open('a')
122
b = branch.Branch.open('b')
123
self.assertEqual(a.last_revision(), b.last_revision())
124
work = WorkingTree.open('current')
125
self.assertEndsWith(work.branch.base, '/b/')
126
self.assertContainsRe(err, "Switched to branch: .*/b/")
128
def test_branch_only_copies_history(self):
129
# Knit branches should only push the history for the current revision.
130
format = bzrdir.BzrDirMetaFormat1()
131
format.repository_format = RepositoryFormatKnit1()
132
shared_repo = self.make_repository('repo', format=format, shared=True)
133
shared_repo.set_make_working_trees(True)
135
def make_shared_tree(path):
136
shared_repo.bzrdir.root_transport.mkdir(path)
137
shared_repo.bzrdir.create_branch_convenience('repo/' + path)
138
return WorkingTree.open('repo/' + path)
139
tree_a = make_shared_tree('a')
140
self.build_tree(['repo/a/file'])
142
tree_a.commit('commit a-1', rev_id='a-1')
143
f = open('repo/a/file', 'ab')
144
f.write('more stuff\n')
146
tree_a.commit('commit a-2', rev_id='a-2')
148
tree_b = make_shared_tree('b')
149
self.build_tree(['repo/b/file'])
151
tree_b.commit('commit b-1', rev_id='b-1')
153
self.assertTrue(shared_repo.has_revision('a-1'))
154
self.assertTrue(shared_repo.has_revision('a-2'))
155
self.assertTrue(shared_repo.has_revision('b-1'))
157
# Now that we have a repository with shared files, make sure
158
# that things aren't copied out by a 'branch'
159
self.run_bzr('branch repo/b branch-b')
160
pushed_tree = WorkingTree.open('branch-b')
161
pushed_repo = pushed_tree.branch.repository
162
self.assertFalse(pushed_repo.has_revision('a-1'))
163
self.assertFalse(pushed_repo.has_revision('a-2'))
164
self.assertTrue(pushed_repo.has_revision('b-1'))
166
def test_branch_hardlink(self):
167
self.requireFeature(HardlinkFeature)
168
source = self.make_branch_and_tree('source')
169
self.build_tree(['source/file1'])
171
source.commit('added file')
172
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
173
source_stat = os.stat('source/file1')
174
target_stat = os.stat('target/file1')
175
self.assertEqual(source_stat, target_stat)
177
def test_branch_standalone(self):
178
shared_repo = self.make_repository('repo', shared=True)
179
self.example_branch('source')
180
self.run_bzr('branch --standalone source repo/target')
181
b = branch.Branch.open('repo/target')
182
expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
183
self.assertEqual(strip_trailing_slash(b.repository.base),
184
strip_trailing_slash(local_path_to_url(expected_repo_path)))
186
def test_branch_no_tree(self):
187
self.example_branch('source')
188
self.run_bzr('branch --no-tree source target')
189
self.failIfExists('target/hello')
190
self.failIfExists('target/goodbye')
192
def test_branch_into_existing_dir(self):
193
self.example_branch('a')
194
# existing dir with similar files but no .bzr dir
195
self.build_tree_contents([('b/',)])
196
self.build_tree_contents([('b/hello', 'bar')]) # different content
197
self.build_tree_contents([('b/goodbye', 'baz')])# same content
198
# fails without --use-existing-dir
199
out,err = self.run_bzr('branch a b', retcode=3)
200
self.assertEqual('', out)
201
self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
204
self.run_bzr('branch a b --use-existing-dir')
206
self.failUnlessExists('b/hello.moved')
207
self.failIfExists('b/godbye.moved')
208
# we can't branch into branch
209
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
self.assertEqual('', out)
211
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
213
def test_branch_bind(self):
214
self.example_branch('a')
215
out, err = self.run_bzr('branch a b --bind')
216
self.assertEndsWith(err, "New branch bound to a\n")
217
b = branch.Branch.open('b')
218
self.assertEndsWith(b.get_bound_location(), '/a/')
221
class TestBranchStacked(ExternalBase):
222
"""Tests for branch --stacked"""
224
def assertRevisionInRepository(self, repo_path, revid):
225
"""Check that a revision is in a repository, disregarding stacking."""
226
repo = bzrdir.BzrDir.open(repo_path).open_repository()
227
self.assertTrue(repo.has_revision(revid))
229
def assertRevisionNotInRepository(self, repo_path, revid):
230
"""Check that a revision is not in a repository, disregarding stacking."""
231
repo = bzrdir.BzrDir.open(repo_path).open_repository()
232
self.assertFalse(repo.has_revision(revid))
234
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
235
repo = branch.Branch.open(branch_path).repository
236
self.assertEqual(set(revid_list),
237
repo.has_revisions(revid_list))
239
def test_branch_stacked_branch_not_stacked(self):
240
"""Branching a stacked branch is not stacked by default"""
242
trunk_tree = self.make_branch_and_tree('target',
244
trunk_tree.commit('mainline')
245
# and a branch from it which is stacked
246
branch_tree = self.make_branch_and_tree('branch',
248
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
249
# with some work on it
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)
253
# branching our local branch gives us a new stacked branch pointing at
255
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
256
self.assertEqual('', out)
257
self.assertEqual('Branched 2 revision(s).\n',
259
# it should have preserved the branch format, and so it should be
260
# capable of supporting stacking, but not actually have a stacked_on
262
self.assertRaises(errors.NotStacked,
263
bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
265
def test_branch_stacked_branch_stacked(self):
266
"""Asking to stack on a stacked branch does work"""
268
trunk_tree = self.make_branch_and_tree('target',
270
trunk_revid = trunk_tree.commit('mainline')
271
# and a branch from it which is stacked
272
branch_tree = self.make_branch_and_tree('branch',
274
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
275
# with some work on it
276
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
277
branch_revid = work_tree.commit('moar work plz')
278
work_tree.branch.push(branch_tree.branch)
279
# you can chain branches on from there
280
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
281
self.assertEqual('', out)
282
self.assertEqual('Created new stacked branch referring to %s.\n' %
283
branch_tree.branch.base, err)
284
self.assertEqual(branch_tree.branch.base,
285
branch.Branch.open('branch2').get_stacked_on_url())
286
branch2_tree = WorkingTree.open('branch2')
287
branch2_revid = work_tree.commit('work on second stacked branch')
288
work_tree.branch.push(branch2_tree.branch)
289
self.assertRevisionInRepository('branch2', branch2_revid)
290
self.assertRevisionsInBranchRepository(
291
[trunk_revid, branch_revid, branch2_revid],
294
def test_branch_stacked(self):
296
trunk_tree = self.make_branch_and_tree('mainline',
298
original_revid = trunk_tree.commit('mainline')
299
self.assertRevisionInRepository('mainline', original_revid)
300
# and a branch from it which is stacked
301
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
303
self.assertEqual('', out)
304
self.assertEqual('Created new stacked branch referring to %s.\n' %
305
trunk_tree.branch.base, err)
306
self.assertRevisionNotInRepository('newbranch', original_revid)
307
new_branch = branch.Branch.open('newbranch')
308
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
310
def test_branch_stacked_from_smart_server(self):
311
# We can branch stacking on a smart server
312
self.transport_server = test_server.SmartTCPServer_for_testing
313
trunk = self.make_branch('mainline', format='1.9')
314
out, err = self.run_bzr(
315
['branch', '--stacked', self.get_url('mainline'), 'shallow'])
317
def test_branch_stacked_from_non_stacked_format(self):
318
"""The origin format doesn't support stacking"""
319
trunk = self.make_branch('trunk', format='pack-0.92')
320
out, err = self.run_bzr(
321
['branch', '--stacked', 'trunk', 'shallow'])
322
# We should notify the user that we upgraded their format
323
self.assertEqualDiff(
324
'Source repository format does not support stacking, using format:\n'
325
' Packs 5 (adds stacking support, requires bzr 1.6)\n'
326
'Source branch format does not support stacking, using format:\n'
328
'Created new stacked branch referring to %s.\n' % (trunk.base,),
331
def test_branch_stacked_from_rich_root_non_stackable(self):
332
trunk = self.make_branch('trunk', format='rich-root-pack')
333
out, err = self.run_bzr(
334
['branch', '--stacked', 'trunk', 'shallow'])
335
# We should notify the user that we upgraded their format
336
self.assertEqualDiff(
337
'Source repository format does not support stacking, using format:\n'
338
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
339
'Source branch format does not support stacking, using format:\n'
341
'Created new stacked branch referring to %s.\n' % (trunk.base,),
345
class TestSmartServerBranching(ExternalBase):
347
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
348
self.setup_smart_server_with_call_log()
349
t = self.make_branch_and_tree('from')
350
for count in range(9):
351
t.commit(message='commit %d' % count)
352
self.reset_smart_call_log()
353
out, err = self.run_bzr(['branch', self.get_url('from'),
354
self.get_url('target')])
355
# This figure represent the amount of work to perform this use case. It
356
# is entirely ok to reduce this number if a test fails due to rpc_count
357
# being too low. If rpc_count increases, more network roundtrips have
358
# become necessary for this use case. Please do not adjust this number
359
# upwards without agreement from bzr's network support maintainers.
360
self.assertLength(38, self.hpss_calls)
362
def test_branch_from_trivial_branch_streaming_acceptance(self):
363
self.setup_smart_server_with_call_log()
364
t = self.make_branch_and_tree('from')
365
for count in range(9):
366
t.commit(message='commit %d' % count)
367
self.reset_smart_call_log()
368
out, err = self.run_bzr(['branch', self.get_url('from'),
370
# This figure represent the amount of work to perform this use case. It
371
# is entirely ok to reduce this number if a test fails due to rpc_count
372
# being too low. If rpc_count increases, more network roundtrips have
373
# become necessary for this use case. Please do not adjust this number
374
# upwards without agreement from bzr's network support maintainers.
375
self.assertLength(10, self.hpss_calls)
377
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
378
self.setup_smart_server_with_call_log()
379
t = self.make_branch_and_tree('trunk')
380
for count in range(8):
381
t.commit(message='commit %d' % count)
382
tree2 = t.branch.bzrdir.sprout('feature', stacked=True
384
local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
385
local_tree.commit('feature change')
386
local_tree.branch.push(tree2.branch)
387
self.reset_smart_call_log()
388
out, err = self.run_bzr(['branch', self.get_url('feature'),
390
# This figure represent the amount of work to perform this use case. It
391
# is entirely ok to reduce this number if a test fails due to rpc_count
392
# being too low. If rpc_count increases, more network roundtrips have
393
# become necessary for this use case. Please do not adjust this number
394
# upwards without agreement from bzr's network support maintainers.
395
self.assertLength(15, self.hpss_calls)
398
class TestRemoteBranch(TestCaseWithSFTPServer):
401
super(TestRemoteBranch, self).setUp()
402
tree = self.make_branch_and_tree('branch')
403
self.build_tree_contents([('branch/file', 'file content\n')])
405
tree.commit('file created')
407
def test_branch_local_remote(self):
408
self.run_bzr(['branch', 'branch', self.get_url('remote')])
409
t = self.get_transport()
410
# Ensure that no working tree what created remotely
411
self.assertFalse(t.has('remote/file'))
413
def test_branch_remote_remote(self):
414
# Light cheat: we access the branch remotely
415
self.run_bzr(['branch', self.get_url('branch'),
416
self.get_url('remote')])
417
t = self.get_transport()
418
# Ensure that no working tree what created remotely
419
self.assertFalse(t.has('remote/file'))