22
22
from bzrlib import (
27
27
revision as _mod_revision,
29
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
from bzrlib.tests.blackbox import ExternalBase
30
from bzrlib.tests import TestCaseWithTransport
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(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)
86
self.assertEqual('bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
88
def test_from_colocated(self):
89
"""Branch from a colocated branch into a regular branch."""
90
tree = self.example_branch('a', format='development-colo')
91
tree.bzrdir.create_branch(name='somecolo')
92
out, err = self.run_bzr('branch %s,branch=somecolo' %
93
local_path_to_url('a'))
94
self.assertEqual('', out)
95
self.assertEqual('Branched 0 revisions.\n', err)
96
self.assertPathExists("somecolo")
98
def test_branch_broken_pack(self):
99
"""branching with a corrupted pack file."""
100
self.example_branch('a')
101
# add some corruption
102
packs_dir = 'a/.bzr/repository/packs/'
103
fname = packs_dir + os.listdir(packs_dir)[0]
104
with open(fname, 'rb+') as f:
105
# Start from the end of the file to avoid choosing a place bigger
106
# than the file itself.
107
f.seek(-5, os.SEEK_END)
109
f.seek(-5, os.SEEK_END)
110
# Make sure we inject a value different than the one we just read
115
f.write(corrupt) # make sure we corrupt something
116
self.run_bzr_error(['Corruption while decompressing repository file'],
117
'branch a b', retcode=3)
62
119
def test_branch_switch_no_branch(self):
63
120
# No branch in the current directory:
64
121
# => new branch will be created, but switch fails
174
231
target_stat = os.stat('target/file1')
175
232
self.assertEqual(source_stat, target_stat)
234
def test_branch_files_from(self):
235
source = self.make_branch_and_tree('source')
236
self.build_tree(['source/file1'])
238
source.commit('added file')
239
out, err = self.run_bzr('branch source target --files-from source')
240
self.assertPathExists('target/file1')
242
def test_branch_files_from_hardlink(self):
243
self.requireFeature(HardlinkFeature)
244
source = self.make_branch_and_tree('source')
245
self.build_tree(['source/file1'])
247
source.commit('added file')
248
source.bzrdir.sprout('second')
249
out, err = self.run_bzr('branch source target --files-from second'
251
source_stat = os.stat('source/file1')
252
second_stat = os.stat('second/file1')
253
target_stat = os.stat('target/file1')
254
self.assertNotEqual(source_stat, target_stat)
255
self.assertEqual(second_stat, target_stat)
177
257
def test_branch_standalone(self):
178
258
shared_repo = self.make_repository('repo', shared=True)
179
259
self.example_branch('source')
186
266
def test_branch_no_tree(self):
187
267
self.example_branch('source')
188
268
self.run_bzr('branch --no-tree source target')
189
self.failIfExists('target/hello')
190
self.failIfExists('target/goodbye')
269
self.assertPathDoesNotExist('target/hello')
270
self.assertPathDoesNotExist('target/goodbye')
192
272
def test_branch_into_existing_dir(self):
193
273
self.example_branch('a')
203
283
# force operation
204
284
self.run_bzr('branch a b --use-existing-dir')
205
285
# check conflicts
206
self.failUnlessExists('b/hello.moved')
207
self.failIfExists('b/godbye.moved')
286
self.assertPathExists('b/hello.moved')
287
self.assertPathDoesNotExist('b/godbye.moved')
208
288
# we can't branch into branch
209
289
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
290
self.assertEqual('', out)
247
327
self.run_bzr('checkout --lightweight a b')
248
328
self.assertLength(2, calls)
251
class TestBranchStacked(ExternalBase):
330
def test_branch_fetches_all_tags(self):
331
builder = self.make_branch_builder('source')
332
source = fixtures.build_branch_with_non_ancestral_rev(builder)
333
source.tags.set_tag('tag-a', 'rev-2')
334
source.get_config().set_user_option('branch.fetch_tags', 'True')
335
# Now source has a tag not in its ancestry. Make a branch from it.
336
self.run_bzr('branch source new-branch')
337
new_branch = branch.Branch.open('new-branch')
338
# The tag is present, and so is its revision.
339
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
340
new_branch.repository.get_revision('rev-2')
343
class TestBranchStacked(TestCaseWithTransport):
252
344
"""Tests for branch --stacked"""
254
346
def assertRevisionInRepository(self, repo_path, revid):
285
377
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
286
378
self.assertEqual('', out)
287
self.assertEqual('Branched 2 revision(s).\n',
379
self.assertEqual('Branched 2 revisions.\n',
289
381
# it should have preserved the branch format, and so it should be
290
382
# capable of supporting stacking, but not actually have a stacked_on
391
483
# being too low. If rpc_count increases, more network roundtrips have
392
484
# become necessary for this use case. Please do not adjust this number
393
485
# upwards without agreement from bzr's network support maintainers.
394
self.assertLength(38, self.hpss_calls)
486
self.assertLength(2, self.hpss_connections)
487
self.assertLength(33, self.hpss_calls)
488
self.expectFailure("branching to the same branch requires VFS access",
489
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
396
491
def test_branch_from_trivial_branch_streaming_acceptance(self):
397
492
self.setup_smart_server_with_call_log()
406
501
# being too low. If rpc_count increases, more network roundtrips have
407
502
# become necessary for this use case. Please do not adjust this number
408
503
# upwards without agreement from bzr's network support maintainers.
504
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
409
505
self.assertLength(10, self.hpss_calls)
506
self.assertLength(1, self.hpss_connections)
411
508
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
412
509
self.setup_smart_server_with_call_log()
427
524
# become necessary for this use case. Please do not adjust this number
428
525
# upwards without agreement from bzr's network support maintainers.
429
526
self.assertLength(15, self.hpss_calls)
527
self.assertLength(1, self.hpss_connections)
528
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
530
def test_branch_from_branch_with_tags(self):
531
self.setup_smart_server_with_call_log()
532
builder = self.make_branch_builder('source')
533
source = fixtures.build_branch_with_non_ancestral_rev(builder)
534
source.get_config().set_user_option('branch.fetch_tags', 'True')
535
source.tags.set_tag('tag-a', 'rev-2')
536
source.tags.set_tag('tag-missing', 'missing-rev')
537
# Now source has a tag not in its ancestry. Make a branch from it.
538
self.reset_smart_call_log()
539
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
540
# This figure represent the amount of work to perform this use case. It
541
# is entirely ok to reduce this number if a test fails due to rpc_count
542
# being too low. If rpc_count increases, more network roundtrips have
543
# become necessary for this use case. Please do not adjust this number
544
# upwards without agreement from bzr's network support maintainers.
545
self.assertLength(10, self.hpss_calls)
546
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
547
self.assertLength(1, self.hpss_connections)
549
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
550
self.setup_smart_server_with_call_log()
551
t = self.make_branch_and_tree('from')
552
for count in range(9):
553
t.commit(message='commit %d' % count)
554
self.reset_smart_call_log()
555
out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
557
# XXX: the number of hpss calls for this case isn't deterministic yet,
558
# so we can't easily assert about the number of calls.
559
#self.assertLength(XXX, self.hpss_calls)
560
# We can assert that none of the calls were readv requests for rix
561
# files, though (demonstrating that at least get_parent_map calls are
562
# not using VFS RPCs).
563
readvs_of_rix_files = [
564
c for c in self.hpss_calls
565
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
566
self.assertLength(1, self.hpss_connections)
567
self.assertLength(0, readvs_of_rix_files)
568
self.expectFailure("branching to stacked requires VFS access",
569
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
432
572
class TestRemoteBranch(TestCaseWithSFTPServer):
452
592
# Ensure that no working tree what created remotely
453
593
self.assertFalse(t.has('remote/file'))
596
class TestDeprecatedAliases(TestCaseWithTransport):
598
def test_deprecated_aliases(self):
599
"""bzr branch can be called clone or get, but those names are deprecated.
603
for command in ['clone', 'get']:
605
$ bzr %(command)s A B
606
2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
607
2>bzr: ERROR: Not a branch...
611
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
613
def _checkout_and_branch(self, option=''):
614
self.script_runner.run_script(self, '''
615
$ bzr checkout %(option)s repo/trunk checkout
617
$ bzr branch --switch ../repo/trunk ../repo/branched
618
2>Branched 0 revisions.
619
2>Tree is up to date at revision 0.
620
2>Switched to branch:...branched...
623
bound_branch = branch.Branch.open_containing('checkout')[0]
624
master_branch = branch.Branch.open_containing('repo/branched')[0]
625
return (bound_branch, master_branch)
627
def test_branch_switch_parent_lightweight(self):
628
"""Lightweight checkout using bzr branch --switch."""
629
bb, mb = self._checkout_and_branch(option='--lightweight')
630
self.assertParent('repo/trunk', bb)
631
self.assertParent('repo/trunk', mb)
633
def test_branch_switch_parent_heavyweight(self):
634
"""Heavyweight checkout using bzr branch --switch."""
635
bb, mb = self._checkout_and_branch()
636
self.assertParent('repo/trunk', bb)
637
self.assertParent('repo/trunk', mb)