22
22
from bzrlib import (
27
27
revision as _mod_revision,
29
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
from bzrlib.tests.blackbox import ExternalBase
31
31
from bzrlib.tests import (
35
from bzrlib.tests.features import (
38
from bzrlib.tests.blackbox import test_switch
39
from bzrlib.tests.matchers import ContainsNoVfsCalls
36
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from bzrlib.tests.script import run_script
37
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
43
from bzrlib.workingtree import WorkingTree
41
class TestBranch(ExternalBase):
46
class TestBranch(tests.TestCaseWithTransport):
43
def example_branch(self, path='.'):
44
tree = self.make_branch_and_tree(path)
48
def example_branch(self, path='.', format=None):
49
tree = self.make_branch_and_tree(path, format=format)
45
50
self.build_tree_contents([(path + '/hello', 'foo')])
47
52
tree.commit(message='setup')
48
53
self.build_tree_contents([(path + '/goodbye', 'baz')])
49
54
tree.add('goodbye')
50
55
tree.commit(message='setup')
52
58
def test_branch(self):
53
59
"""Branch from one branch to another."""
59
65
self.assertFalse(b._transport.has('branch-name'))
60
66
b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
68
def test_into_colocated(self):
69
"""Branch from a branch into a colocated branch."""
70
self.example_branch('a')
71
out, err = self.run_bzr(
72
'init --format=development-colo file:b,branch=orig')
74
"""Created a lightweight checkout (format: development-colo)\n""",
76
self.assertEqual('', err)
77
out, err = self.run_bzr(
78
'branch a file:b,branch=thiswasa')
79
self.assertEqual('', out)
80
self.assertEqual('Branched 2 revisions.\n', err)
81
out, err = self.run_bzr('branches b')
82
self.assertEqual(" orig\n thiswasa\n", out)
83
self.assertEqual('', err)
84
out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
85
self.assertEqual('', out)
87
'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
89
def test_from_colocated(self):
90
"""Branch from a colocated branch into a regular branch."""
91
tree = self.example_branch('a', format='development-colo')
92
tree.bzrdir.create_branch(name='somecolo')
93
out, err = self.run_bzr('branch %s,branch=somecolo' %
94
local_path_to_url('a'))
95
self.assertEqual('', out)
96
self.assertEqual('Branched 0 revisions.\n', err)
97
self.assertPathExists("somecolo")
99
def test_branch_broken_pack(self):
100
"""branching with a corrupted pack file."""
101
self.example_branch('a')
102
# add some corruption
103
packs_dir = 'a/.bzr/repository/packs/'
104
fname = packs_dir + os.listdir(packs_dir)[0]
105
with open(fname, 'rb+') as f:
106
# Start from the end of the file to avoid choosing a place bigger
107
# than the file itself.
108
f.seek(-5, os.SEEK_END)
110
f.seek(-5, os.SEEK_END)
111
# Make sure we inject a value different than the one we just read
116
f.write(corrupt) # make sure we corrupt something
117
self.run_bzr_error(['Corruption while decompressing repository file'],
118
'branch a b', retcode=3)
62
120
def test_branch_switch_no_branch(self):
63
121
# No branch in the current directory:
64
122
# => new branch will be created, but switch fails
98
156
work = branch.Branch.open('current')
99
157
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
159
def test_branch_into_empty_dir(self):
160
t = self.example_branch('source')
161
self.make_bzrdir('target')
162
self.run_bzr("branch source target")
163
self.assertEquals(2, len(t.branch.repository.all_revision_ids()))
101
165
def test_branch_switch_checkout(self):
102
166
# Checkout in the current directory:
103
167
# => new branch will be created and checkout bound to the new branch
104
168
self.example_branch('a')
105
169
self.run_bzr('checkout a current')
106
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
170
out, err = self.run_bzr('branch --switch ../a ../b',
171
working_dir='current')
107
172
a = branch.Branch.open('a')
108
173
b = branch.Branch.open('b')
109
174
self.assertEqual(a.last_revision(), b.last_revision())
118
183
self.example_branch('a')
119
184
self.run_bzr('checkout --lightweight a current')
120
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
185
out, err = self.run_bzr('branch --switch ../a ../b',
186
working_dir='current')
121
187
a = branch.Branch.open('a')
122
188
b = branch.Branch.open('b')
123
189
self.assertEqual(a.last_revision(), b.last_revision())
174
240
target_stat = os.stat('target/file1')
175
241
self.assertEqual(source_stat, target_stat)
243
def test_branch_files_from(self):
244
source = self.make_branch_and_tree('source')
245
self.build_tree(['source/file1'])
247
source.commit('added file')
248
out, err = self.run_bzr('branch source target --files-from source')
249
self.assertPathExists('target/file1')
251
def test_branch_files_from_hardlink(self):
252
self.requireFeature(HardlinkFeature)
253
source = self.make_branch_and_tree('source')
254
self.build_tree(['source/file1'])
256
source.commit('added file')
257
source.bzrdir.sprout('second')
258
out, err = self.run_bzr('branch source target --files-from second'
260
source_stat = os.stat('source/file1')
261
second_stat = os.stat('second/file1')
262
target_stat = os.stat('target/file1')
263
self.assertNotEqual(source_stat, target_stat)
264
self.assertEqual(second_stat, target_stat)
177
266
def test_branch_standalone(self):
178
267
shared_repo = self.make_repository('repo', shared=True)
179
268
self.example_branch('source')
186
275
def test_branch_no_tree(self):
187
276
self.example_branch('source')
188
277
self.run_bzr('branch --no-tree source target')
189
self.failIfExists('target/hello')
190
self.failIfExists('target/goodbye')
278
self.assertPathDoesNotExist('target/hello')
279
self.assertPathDoesNotExist('target/goodbye')
192
281
def test_branch_into_existing_dir(self):
193
282
self.example_branch('a')
203
292
# force operation
204
293
self.run_bzr('branch a b --use-existing-dir')
205
294
# check conflicts
206
self.failUnlessExists('b/hello.moved')
207
self.failIfExists('b/godbye.moved')
295
self.assertPathExists('b/hello.moved')
296
self.assertPathDoesNotExist('b/godbye.moved')
208
297
# we can't branch into branch
209
298
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
299
self.assertEqual('', out)
217
306
b = branch.Branch.open('b')
218
307
self.assertEndsWith(b.get_bound_location(), '/a/')
221
class TestBranchStacked(ExternalBase):
309
def test_branch_with_post_branch_init_hook(self):
311
branch.Branch.hooks.install_named_hook('post_branch_init',
313
self.assertLength(0, calls)
314
self.example_branch('a')
315
self.assertLength(1, calls)
316
self.run_bzr('branch a b')
317
self.assertLength(2, calls)
319
def test_checkout_with_post_branch_init_hook(self):
321
branch.Branch.hooks.install_named_hook('post_branch_init',
323
self.assertLength(0, calls)
324
self.example_branch('a')
325
self.assertLength(1, calls)
326
self.run_bzr('checkout a b')
327
self.assertLength(2, calls)
329
def test_lightweight_checkout_with_post_branch_init_hook(self):
331
branch.Branch.hooks.install_named_hook('post_branch_init',
333
self.assertLength(0, calls)
334
self.example_branch('a')
335
self.assertLength(1, calls)
336
self.run_bzr('checkout --lightweight a b')
337
self.assertLength(2, calls)
339
def test_branch_fetches_all_tags(self):
340
builder = self.make_branch_builder('source')
341
source = fixtures.build_branch_with_non_ancestral_rev(builder)
342
source.tags.set_tag('tag-a', 'rev-2')
343
source.get_config_stack().set('branch.fetch_tags', True)
344
# Now source has a tag not in its ancestry. Make a branch from it.
345
self.run_bzr('branch source new-branch')
346
new_branch = branch.Branch.open('new-branch')
347
# The tag is present, and so is its revision.
348
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
349
new_branch.repository.get_revision('rev-2')
352
class TestBranchStacked(tests.TestCaseWithTransport):
222
353
"""Tests for branch --stacked"""
224
355
def assertRevisionInRepository(self, repo_path, revid):
225
"""Check that a revision is in a repository, disregarding stacking."""
356
"""Check that a revision is in a repo, disregarding stacking."""
226
357
repo = bzrdir.BzrDir.open(repo_path).open_repository()
227
358
self.assertTrue(repo.has_revision(revid))
229
360
def assertRevisionNotInRepository(self, repo_path, revid):
230
"""Check that a revision is not in a repository, disregarding stacking."""
361
"""Check that a revision is not in a repo, disregarding stacking."""
231
362
repo = bzrdir.BzrDir.open(repo_path).open_repository()
232
363
self.assertFalse(repo.has_revision(revid))
255
386
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
256
387
self.assertEqual('', out)
257
self.assertEqual('Branched 2 revision(s).\n',
388
self.assertEqual('Branched 2 revisions.\n',
259
390
# it should have preserved the branch format, and so it should be
260
391
# capable of supporting stacking, but not actually have a stacked_on
305
436
trunk_tree.branch.base, err)
306
437
self.assertRevisionNotInRepository('newbranch', original_revid)
307
438
new_branch = branch.Branch.open('newbranch')
308
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
439
self.assertEqual(trunk_tree.branch.base,
440
new_branch.get_stacked_on_url())
310
442
def test_branch_stacked_from_smart_server(self):
311
443
# We can branch stacking on a smart server
338
472
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
339
473
'Source branch format does not support stacking, using format:\n'
340
474
' Branch format 7\n'
475
'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
476
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
341
477
'Created new stacked branch referring to %s.\n' % (trunk.base,),
345
class TestSmartServerBranching(ExternalBase):
481
class TestSmartServerBranching(tests.TestCaseWithTransport):
347
483
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
348
484
self.setup_smart_server_with_call_log()
357
493
# being too low. If rpc_count increases, more network roundtrips have
358
494
# become necessary for this use case. Please do not adjust this number
359
495
# upwards without agreement from bzr's network support maintainers.
360
self.assertLength(38, self.hpss_calls)
496
self.assertLength(2, self.hpss_connections)
497
self.assertLength(33, self.hpss_calls)
498
self.expectFailure("branching to the same branch requires VFS access",
499
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
362
501
def test_branch_from_trivial_branch_streaming_acceptance(self):
363
502
self.setup_smart_server_with_call_log()
372
511
# being too low. If rpc_count increases, more network roundtrips have
373
512
# become necessary for this use case. Please do not adjust this number
374
513
# upwards without agreement from bzr's network support maintainers.
514
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
375
515
self.assertLength(10, self.hpss_calls)
516
self.assertLength(1, self.hpss_connections)
377
518
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
378
519
self.setup_smart_server_with_call_log()
393
534
# become necessary for this use case. Please do not adjust this number
394
535
# upwards without agreement from bzr's network support maintainers.
395
536
self.assertLength(15, self.hpss_calls)
537
self.assertLength(1, self.hpss_connections)
538
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
540
def test_branch_from_branch_with_tags(self):
541
self.setup_smart_server_with_call_log()
542
builder = self.make_branch_builder('source')
543
source = fixtures.build_branch_with_non_ancestral_rev(builder)
544
source.get_config_stack().set('branch.fetch_tags', True)
545
source.tags.set_tag('tag-a', 'rev-2')
546
source.tags.set_tag('tag-missing', 'missing-rev')
547
# Now source has a tag not in its ancestry. Make a branch from it.
548
self.reset_smart_call_log()
549
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
550
# This figure represent the amount of work to perform this use case. It
551
# is entirely ok to reduce this number if a test fails due to rpc_count
552
# being too low. If rpc_count increases, more network roundtrips have
553
# become necessary for this use case. Please do not adjust this number
554
# upwards without agreement from bzr's network support maintainers.
555
self.assertLength(10, self.hpss_calls)
556
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
557
self.assertLength(1, self.hpss_connections)
559
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
560
self.setup_smart_server_with_call_log()
561
t = self.make_branch_and_tree('from')
562
for count in range(9):
563
t.commit(message='commit %d' % count)
564
self.reset_smart_call_log()
565
out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
567
# XXX: the number of hpss calls for this case isn't deterministic yet,
568
# so we can't easily assert about the number of calls.
569
#self.assertLength(XXX, self.hpss_calls)
570
# We can assert that none of the calls were readv requests for rix
571
# files, though (demonstrating that at least get_parent_map calls are
572
# not using VFS RPCs).
573
readvs_of_rix_files = [
574
c for c in self.hpss_calls
575
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
576
self.assertLength(1, self.hpss_connections)
577
self.assertLength(0, readvs_of_rix_files)
578
self.expectFailure("branching to stacked requires VFS access",
579
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
398
582
class TestRemoteBranch(TestCaseWithSFTPServer):
418
602
# Ensure that no working tree what created remotely
419
603
self.assertFalse(t.has('remote/file'))
606
class TestDeprecatedAliases(tests.TestCaseWithTransport):
608
def test_deprecated_aliases(self):
609
"""bzr branch can be called clone or get, but those names are
614
for command in ['clone', 'get']:
616
$ bzr %(command)s A B
617
2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
618
2>bzr: ERROR: Not a branch...
622
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
624
def _checkout_and_branch(self, option=''):
625
self.script_runner.run_script(self, '''
626
$ bzr checkout %(option)s repo/trunk checkout
628
$ bzr branch --switch ../repo/trunk ../repo/branched
629
2>Branched 0 revisions.
630
2>Tree is up to date at revision 0.
631
2>Switched to branch:...branched...
634
bound_branch = branch.Branch.open_containing('checkout')[0]
635
master_branch = branch.Branch.open_containing('repo/branched')[0]
636
return (bound_branch, master_branch)
638
def test_branch_switch_parent_lightweight(self):
639
"""Lightweight checkout using bzr branch --switch."""
640
bb, mb = self._checkout_and_branch(option='--lightweight')
641
self.assertParent('repo/trunk', bb)
642
self.assertParent('repo/trunk', mb)
644
def test_branch_switch_parent_heavyweight(self):
645
"""Heavyweight checkout using bzr branch --switch."""
646
bb, mb = self._checkout_and_branch()
647
self.assertParent('repo/trunk', bb)
648
self.assertParent('repo/trunk', mb)