1
# Copyright (C) 2006-2011 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."""
26
revision as _mod_revision,
28
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
29
from bzrlib.tests import TestCaseWithTransport
30
from bzrlib.tests import (
36
from bzrlib.tests.blackbox import test_switch
37
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
38
from bzrlib.tests.script import run_script
39
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
40
from bzrlib.workingtree import WorkingTree
43
class TestBranch(TestCaseWithTransport):
45
def example_branch(self, path='.'):
46
tree = self.make_branch_and_tree(path)
47
self.build_tree_contents([(path + '/hello', 'foo')])
49
tree.commit(message='setup')
50
self.build_tree_contents([(path + '/goodbye', 'baz')])
52
tree.commit(message='setup')
54
def test_branch(self):
55
"""Branch from one branch to another."""
56
self.example_branch('a')
57
self.run_bzr('branch a b')
58
b = branch.Branch.open('b')
59
self.run_bzr('branch a c -r 1')
60
# previously was erroneously created by branching
61
self.assertFalse(b._transport.has('branch-name'))
62
b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
64
def test_branch_switch_no_branch(self):
65
# No branch in the current directory:
66
# => new branch will be created, but switch fails
67
self.example_branch('a')
68
self.make_repository('current')
69
self.run_bzr_error(['No WorkingTree exists for'],
70
'branch --switch ../a ../b', working_dir='current')
71
a = branch.Branch.open('a')
72
b = branch.Branch.open('b')
73
self.assertEqual(a.last_revision(), b.last_revision())
75
def test_branch_switch_no_wt(self):
76
# No working tree in the current directory:
77
# => new branch will be created, but switch fails and the current
78
# branch is unmodified
79
self.example_branch('a')
80
self.make_branch('current')
81
self.run_bzr_error(['No WorkingTree exists for'],
82
'branch --switch ../a ../b', working_dir='current')
83
a = branch.Branch.open('a')
84
b = branch.Branch.open('b')
85
self.assertEqual(a.last_revision(), b.last_revision())
86
work = branch.Branch.open('current')
87
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
89
def test_branch_switch_no_checkout(self):
90
# Standalone branch in the current directory:
91
# => new branch will be created, but switch fails and the current
92
# branch is unmodified
93
self.example_branch('a')
94
self.make_branch_and_tree('current')
95
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
96
'branch --switch ../a ../b', working_dir='current')
97
a = branch.Branch.open('a')
98
b = branch.Branch.open('b')
99
self.assertEqual(a.last_revision(), b.last_revision())
100
work = branch.Branch.open('current')
101
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
103
def test_branch_switch_checkout(self):
104
# Checkout in the current directory:
105
# => new branch will be created and checkout bound to the new branch
106
self.example_branch('a')
107
self.run_bzr('checkout a current')
108
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
109
a = branch.Branch.open('a')
110
b = branch.Branch.open('b')
111
self.assertEqual(a.last_revision(), b.last_revision())
112
work = WorkingTree.open('current')
113
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
114
self.assertContainsRe(err, "Switched to branch: .*/b/")
116
def test_branch_switch_lightweight_checkout(self):
117
# Lightweight checkout in the current directory:
118
# => new branch will be created and lightweight checkout pointed to
120
self.example_branch('a')
121
self.run_bzr('checkout --lightweight a current')
122
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
123
a = branch.Branch.open('a')
124
b = branch.Branch.open('b')
125
self.assertEqual(a.last_revision(), b.last_revision())
126
work = WorkingTree.open('current')
127
self.assertEndsWith(work.branch.base, '/b/')
128
self.assertContainsRe(err, "Switched to branch: .*/b/")
130
def test_branch_only_copies_history(self):
131
# Knit branches should only push the history for the current revision.
132
format = bzrdir.BzrDirMetaFormat1()
133
format.repository_format = RepositoryFormatKnit1()
134
shared_repo = self.make_repository('repo', format=format, shared=True)
135
shared_repo.set_make_working_trees(True)
137
def make_shared_tree(path):
138
shared_repo.bzrdir.root_transport.mkdir(path)
139
shared_repo.bzrdir.create_branch_convenience('repo/' + path)
140
return WorkingTree.open('repo/' + path)
141
tree_a = make_shared_tree('a')
142
self.build_tree(['repo/a/file'])
144
tree_a.commit('commit a-1', rev_id='a-1')
145
f = open('repo/a/file', 'ab')
146
f.write('more stuff\n')
148
tree_a.commit('commit a-2', rev_id='a-2')
150
tree_b = make_shared_tree('b')
151
self.build_tree(['repo/b/file'])
153
tree_b.commit('commit b-1', rev_id='b-1')
155
self.assertTrue(shared_repo.has_revision('a-1'))
156
self.assertTrue(shared_repo.has_revision('a-2'))
157
self.assertTrue(shared_repo.has_revision('b-1'))
159
# Now that we have a repository with shared files, make sure
160
# that things aren't copied out by a 'branch'
161
self.run_bzr('branch repo/b branch-b')
162
pushed_tree = WorkingTree.open('branch-b')
163
pushed_repo = pushed_tree.branch.repository
164
self.assertFalse(pushed_repo.has_revision('a-1'))
165
self.assertFalse(pushed_repo.has_revision('a-2'))
166
self.assertTrue(pushed_repo.has_revision('b-1'))
168
def test_branch_hardlink(self):
169
self.requireFeature(HardlinkFeature)
170
source = self.make_branch_and_tree('source')
171
self.build_tree(['source/file1'])
173
source.commit('added file')
174
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
175
source_stat = os.stat('source/file1')
176
target_stat = os.stat('target/file1')
177
self.assertEqual(source_stat, target_stat)
179
def test_branch_files_from(self):
180
source = self.make_branch_and_tree('source')
181
self.build_tree(['source/file1'])
183
source.commit('added file')
184
out, err = self.run_bzr('branch source target --files-from source')
185
self.assertPathExists('target/file1')
187
def test_branch_files_from_hardlink(self):
188
self.requireFeature(HardlinkFeature)
189
source = self.make_branch_and_tree('source')
190
self.build_tree(['source/file1'])
192
source.commit('added file')
193
source.bzrdir.sprout('second')
194
out, err = self.run_bzr('branch source target --files-from second'
196
source_stat = os.stat('source/file1')
197
second_stat = os.stat('second/file1')
198
target_stat = os.stat('target/file1')
199
self.assertNotEqual(source_stat, target_stat)
200
self.assertEqual(second_stat, target_stat)
202
def test_branch_standalone(self):
203
shared_repo = self.make_repository('repo', shared=True)
204
self.example_branch('source')
205
self.run_bzr('branch --standalone source repo/target')
206
b = branch.Branch.open('repo/target')
207
expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
208
self.assertEqual(strip_trailing_slash(b.repository.base),
209
strip_trailing_slash(local_path_to_url(expected_repo_path)))
211
def test_branch_no_tree(self):
212
self.example_branch('source')
213
self.run_bzr('branch --no-tree source target')
214
self.assertPathDoesNotExist('target/hello')
215
self.assertPathDoesNotExist('target/goodbye')
217
def test_branch_into_existing_dir(self):
218
self.example_branch('a')
219
# existing dir with similar files but no .bzr dir
220
self.build_tree_contents([('b/',)])
221
self.build_tree_contents([('b/hello', 'bar')]) # different content
222
self.build_tree_contents([('b/goodbye', 'baz')])# same content
223
# fails without --use-existing-dir
224
out,err = self.run_bzr('branch a b', retcode=3)
225
self.assertEqual('', out)
226
self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
229
self.run_bzr('branch a b --use-existing-dir')
231
self.assertPathExists('b/hello.moved')
232
self.assertPathDoesNotExist('b/godbye.moved')
233
# we can't branch into branch
234
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
235
self.assertEqual('', out)
236
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
238
def test_branch_bind(self):
239
self.example_branch('a')
240
out, err = self.run_bzr('branch a b --bind')
241
self.assertEndsWith(err, "New branch bound to a\n")
242
b = branch.Branch.open('b')
243
self.assertEndsWith(b.get_bound_location(), '/a/')
245
def test_branch_with_post_branch_init_hook(self):
247
branch.Branch.hooks.install_named_hook('post_branch_init',
249
self.assertLength(0, calls)
250
self.example_branch('a')
251
self.assertLength(1, calls)
252
self.run_bzr('branch a b')
253
self.assertLength(2, calls)
255
def test_checkout_with_post_branch_init_hook(self):
257
branch.Branch.hooks.install_named_hook('post_branch_init',
259
self.assertLength(0, calls)
260
self.example_branch('a')
261
self.assertLength(1, calls)
262
self.run_bzr('checkout a b')
263
self.assertLength(2, calls)
265
def test_lightweight_checkout_with_post_branch_init_hook(self):
267
branch.Branch.hooks.install_named_hook('post_branch_init',
269
self.assertLength(0, calls)
270
self.example_branch('a')
271
self.assertLength(1, calls)
272
self.run_bzr('checkout --lightweight a b')
273
self.assertLength(2, calls)
275
def test_branch_fetches_all_tags(self):
276
builder = self.make_branch_builder('source')
277
source = fixtures.build_branch_with_non_ancestral_rev(builder)
278
source.tags.set_tag('tag-a', 'rev-2')
279
# Now source has a tag not in its ancestry. Make a branch from it.
280
self.run_bzr('branch source new-branch')
281
new_branch = branch.Branch.open('new-branch')
282
# The tag is present, and so is its revision.
283
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
284
new_branch.repository.get_revision('rev-2')
287
class TestBranchStacked(TestCaseWithTransport):
288
"""Tests for branch --stacked"""
290
def assertRevisionInRepository(self, repo_path, revid):
291
"""Check that a revision is in a repository, disregarding stacking."""
292
repo = bzrdir.BzrDir.open(repo_path).open_repository()
293
self.assertTrue(repo.has_revision(revid))
295
def assertRevisionNotInRepository(self, repo_path, revid):
296
"""Check that a revision is not in a repository, disregarding stacking."""
297
repo = bzrdir.BzrDir.open(repo_path).open_repository()
298
self.assertFalse(repo.has_revision(revid))
300
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
301
repo = branch.Branch.open(branch_path).repository
302
self.assertEqual(set(revid_list),
303
repo.has_revisions(revid_list))
305
def test_branch_stacked_branch_not_stacked(self):
306
"""Branching a stacked branch is not stacked by default"""
308
trunk_tree = self.make_branch_and_tree('target',
310
trunk_tree.commit('mainline')
311
# and a branch from it which is stacked
312
branch_tree = self.make_branch_and_tree('branch',
314
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
315
# with some work on it
316
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
317
work_tree.commit('moar work plz')
318
work_tree.branch.push(branch_tree.branch)
319
# branching our local branch gives us a new stacked branch pointing at
321
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
322
self.assertEqual('', out)
323
self.assertEqual('Branched 2 revision(s).\n',
325
# it should have preserved the branch format, and so it should be
326
# capable of supporting stacking, but not actually have a stacked_on
328
self.assertRaises(errors.NotStacked,
329
bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
331
def test_branch_stacked_branch_stacked(self):
332
"""Asking to stack on a stacked branch does work"""
334
trunk_tree = self.make_branch_and_tree('target',
336
trunk_revid = trunk_tree.commit('mainline')
337
# and a branch from it which is stacked
338
branch_tree = self.make_branch_and_tree('branch',
340
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
341
# with some work on it
342
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
343
branch_revid = work_tree.commit('moar work plz')
344
work_tree.branch.push(branch_tree.branch)
345
# you can chain branches on from there
346
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
347
self.assertEqual('', out)
348
self.assertEqual('Created new stacked branch referring to %s.\n' %
349
branch_tree.branch.base, err)
350
self.assertEqual(branch_tree.branch.base,
351
branch.Branch.open('branch2').get_stacked_on_url())
352
branch2_tree = WorkingTree.open('branch2')
353
branch2_revid = work_tree.commit('work on second stacked branch')
354
work_tree.branch.push(branch2_tree.branch)
355
self.assertRevisionInRepository('branch2', branch2_revid)
356
self.assertRevisionsInBranchRepository(
357
[trunk_revid, branch_revid, branch2_revid],
360
def test_branch_stacked(self):
362
trunk_tree = self.make_branch_and_tree('mainline',
364
original_revid = trunk_tree.commit('mainline')
365
self.assertRevisionInRepository('mainline', original_revid)
366
# and a branch from it which is stacked
367
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
369
self.assertEqual('', out)
370
self.assertEqual('Created new stacked branch referring to %s.\n' %
371
trunk_tree.branch.base, err)
372
self.assertRevisionNotInRepository('newbranch', original_revid)
373
new_branch = branch.Branch.open('newbranch')
374
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
376
def test_branch_stacked_from_smart_server(self):
377
# We can branch stacking on a smart server
378
self.transport_server = test_server.SmartTCPServer_for_testing
379
trunk = self.make_branch('mainline', format='1.9')
380
out, err = self.run_bzr(
381
['branch', '--stacked', self.get_url('mainline'), 'shallow'])
383
def test_branch_stacked_from_non_stacked_format(self):
384
"""The origin format doesn't support stacking"""
385
trunk = self.make_branch('trunk', format='pack-0.92')
386
out, err = self.run_bzr(
387
['branch', '--stacked', 'trunk', 'shallow'])
388
# We should notify the user that we upgraded their format
389
self.assertEqualDiff(
390
'Source repository format does not support stacking, using format:\n'
391
' Packs 5 (adds stacking support, requires bzr 1.6)\n'
392
'Source branch format does not support stacking, using format:\n'
394
'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
395
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
396
'Created new stacked branch referring to %s.\n' % (trunk.base,),
399
def test_branch_stacked_from_rich_root_non_stackable(self):
400
trunk = self.make_branch('trunk', format='rich-root-pack')
401
out, err = self.run_bzr(
402
['branch', '--stacked', 'trunk', 'shallow'])
403
# We should notify the user that we upgraded their format
404
self.assertEqualDiff(
405
'Source repository format does not support stacking, using format:\n'
406
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
407
'Source branch format does not support stacking, using format:\n'
409
'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
410
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
411
'Created new stacked branch referring to %s.\n' % (trunk.base,),
415
class TestSmartServerBranching(TestCaseWithTransport):
417
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
418
self.setup_smart_server_with_call_log()
419
t = self.make_branch_and_tree('from')
420
for count in range(9):
421
t.commit(message='commit %d' % count)
422
self.reset_smart_call_log()
423
out, err = self.run_bzr(['branch', self.get_url('from'),
424
self.get_url('target')])
425
# This figure represent the amount of work to perform this use case. It
426
# is entirely ok to reduce this number if a test fails due to rpc_count
427
# being too low. If rpc_count increases, more network roundtrips have
428
# become necessary for this use case. Please do not adjust this number
429
# upwards without agreement from bzr's network support maintainers.
430
self.assertLength(36, self.hpss_calls)
432
def test_branch_from_trivial_branch_streaming_acceptance(self):
433
self.setup_smart_server_with_call_log()
434
t = self.make_branch_and_tree('from')
435
for count in range(9):
436
t.commit(message='commit %d' % count)
437
self.reset_smart_call_log()
438
out, err = self.run_bzr(['branch', self.get_url('from'),
440
# This figure represent the amount of work to perform this use case. It
441
# is entirely ok to reduce this number if a test fails due to rpc_count
442
# being too low. If rpc_count increases, more network roundtrips have
443
# become necessary for this use case. Please do not adjust this number
444
# upwards without agreement from bzr's network support maintainers.
445
self.assertLength(9, self.hpss_calls)
447
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
448
self.setup_smart_server_with_call_log()
449
t = self.make_branch_and_tree('trunk')
450
for count in range(8):
451
t.commit(message='commit %d' % count)
452
tree2 = t.branch.bzrdir.sprout('feature', stacked=True
454
local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
455
local_tree.commit('feature change')
456
local_tree.branch.push(tree2.branch)
457
self.reset_smart_call_log()
458
out, err = self.run_bzr(['branch', self.get_url('feature'),
460
# This figure represent the amount of work to perform this use case. It
461
# is entirely ok to reduce this number if a test fails due to rpc_count
462
# being too low. If rpc_count increases, more network roundtrips have
463
# become necessary for this use case. Please do not adjust this number
464
# upwards without agreement from bzr's network support maintainers.
465
self.assertLength(14, self.hpss_calls)
467
def test_branch_from_branch_with_tags(self):
468
self.setup_smart_server_with_call_log()
469
builder = self.make_branch_builder('source')
470
source = fixtures.build_branch_with_non_ancestral_rev(builder)
471
source.tags.set_tag('tag-a', 'rev-2')
472
source.tags.set_tag('tag-missing', 'missing-rev')
473
# Now source has a tag not in its ancestry. Make a branch from it.
474
self.reset_smart_call_log()
475
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
476
# This figure represent the amount of work to perform this use case. It
477
# is entirely ok to reduce this number if a test fails due to rpc_count
478
# being too low. If rpc_count increases, more network roundtrips have
479
# become necessary for this use case. Please do not adjust this number
480
# upwards without agreement from bzr's network support maintainers.
481
self.assertLength(9, self.hpss_calls)
483
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
484
self.setup_smart_server_with_call_log()
485
t = self.make_branch_and_tree('from')
486
for count in range(9):
487
t.commit(message='commit %d' % count)
488
self.reset_smart_call_log()
489
out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
491
# XXX: the number of hpss calls for this case isn't deterministic yet,
492
# so we can't easily assert about the number of calls.
493
#self.assertLength(XXX, self.hpss_calls)
494
# We can assert that none of the calls were readv requests for rix
495
# files, though (demonstrating that at least get_parent_map calls are
496
# not using VFS RPCs).
497
readvs_of_rix_files = [
498
c for c in self.hpss_calls
499
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
500
self.assertLength(0, readvs_of_rix_files)
503
class TestRemoteBranch(TestCaseWithSFTPServer):
506
super(TestRemoteBranch, self).setUp()
507
tree = self.make_branch_and_tree('branch')
508
self.build_tree_contents([('branch/file', 'file content\n')])
510
tree.commit('file created')
512
def test_branch_local_remote(self):
513
self.run_bzr(['branch', 'branch', self.get_url('remote')])
514
t = self.get_transport()
515
# Ensure that no working tree what created remotely
516
self.assertFalse(t.has('remote/file'))
518
def test_branch_remote_remote(self):
519
# Light cheat: we access the branch remotely
520
self.run_bzr(['branch', self.get_url('branch'),
521
self.get_url('remote')])
522
t = self.get_transport()
523
# Ensure that no working tree what created remotely
524
self.assertFalse(t.has('remote/file'))
527
class TestDeprecatedAliases(TestCaseWithTransport):
529
def test_deprecated_aliases(self):
530
"""bzr branch can be called clone or get, but those names are deprecated.
534
for command in ['clone', 'get']:
536
$ bzr %(command)s A B
537
2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
538
2>bzr: ERROR: Not a branch...
542
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
544
def _checkout_and_branch(self, option=''):
545
self.script_runner.run_script(self, '''
546
$ bzr checkout %(option)s repo/trunk checkout
548
$ bzr branch --switch ../repo/trunk ../repo/branched
549
2>Branched 0 revision(s).
550
2>Tree is up to date at revision 0.
551
2>Switched to branch:...branched...
554
bound_branch = branch.Branch.open_containing('checkout')[0]
555
master_branch = branch.Branch.open_containing('repo/branched')[0]
556
return (bound_branch, master_branch)
558
def test_branch_switch_parent_lightweight(self):
559
"""Lightweight checkout using bzr branch --switch."""
560
bb, mb = self._checkout_and_branch(option='--lightweight')
561
self.assertParent('repo/trunk', bb)
562
self.assertParent('repo/trunk', mb)
564
def test_branch_switch_parent_heavyweight(self):
565
"""Heavyweight checkout using bzr branch --switch."""
566
bb, mb = self._checkout_and_branch()
567
self.assertParent('repo/trunk', bb)
568
self.assertParent('repo/trunk', mb)