22
22
from bzrlib import (
27
27
revision as _mod_revision,
29
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
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(TestCaseWithTransport):
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_branch_no_to_location(self):
69
"""The to_location is derived from the source branch name."""
71
a = self.example_branch('something/a').branch
72
self.run_bzr('branch something/a')
73
b = branch.Branch.open('a')
74
self.assertEquals(b.last_revision_info(), a.last_revision_info())
76
def test_into_colocated(self):
77
"""Branch from a branch into a colocated branch."""
78
self.example_branch('a')
79
out, err = self.run_bzr(
80
'init --format=development-colo file:b,branch=orig')
82
"""Created a lightweight checkout (format: development-colo)\n""",
84
self.assertEqual('', err)
85
out, err = self.run_bzr(
86
'branch a file:b,branch=thiswasa')
87
self.assertEqual('', out)
88
self.assertEqual('Branched 2 revisions.\n', err)
89
out, err = self.run_bzr('branches b')
90
self.assertEqual(" orig\n thiswasa\n", out)
91
self.assertEqual('', err)
92
out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
93
self.assertEqual('', out)
95
'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
97
def test_from_colocated(self):
98
"""Branch from a colocated branch into a regular branch."""
99
tree = self.example_branch('a', format='development-colo')
100
tree.bzrdir.create_branch(name='somecolo')
101
out, err = self.run_bzr('branch %s,branch=somecolo' %
102
local_path_to_url('a'))
103
self.assertEqual('', out)
104
self.assertEqual('Branched 0 revisions.\n', err)
105
self.assertPathExists("somecolo")
107
def test_branch_broken_pack(self):
108
"""branching with a corrupted pack file."""
109
self.example_branch('a')
110
# add some corruption
111
packs_dir = 'a/.bzr/repository/packs/'
112
fname = packs_dir + os.listdir(packs_dir)[0]
113
with open(fname, 'rb+') as f:
114
# Start from the end of the file to avoid choosing a place bigger
115
# than the file itself.
116
f.seek(-5, os.SEEK_END)
118
f.seek(-5, os.SEEK_END)
119
# Make sure we inject a value different than the one we just read
124
f.write(corrupt) # make sure we corrupt something
125
self.run_bzr_error(['Corruption while decompressing repository file'],
126
'branch a b', retcode=3)
62
128
def test_branch_switch_no_branch(self):
63
129
# No branch in the current directory:
64
130
# => new branch will be created, but switch fails
89
155
# => new branch will be created, but switch fails and the current
90
156
# branch is unmodified
91
157
self.example_branch('a')
92
self.make_branch_and_tree('current')
158
tree = self.make_branch_and_tree('current')
159
c1 = tree.commit('some diverged change')
93
160
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
94
161
'branch --switch ../a ../b', working_dir='current')
95
162
a = branch.Branch.open('a')
96
163
b = branch.Branch.open('b')
97
164
self.assertEqual(a.last_revision(), b.last_revision())
98
165
work = branch.Branch.open('current')
99
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
166
self.assertEqual(work.last_revision(), c1)
168
def test_branch_into_empty_dir(self):
169
t = self.example_branch('source')
170
self.make_bzrdir('target')
171
self.run_bzr("branch source target")
172
self.assertEquals(2, len(t.branch.repository.all_revision_ids()))
101
174
def test_branch_switch_checkout(self):
102
175
# Checkout in the current directory:
103
176
# => new branch will be created and checkout bound to the new branch
104
177
self.example_branch('a')
105
178
self.run_bzr('checkout a current')
106
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
179
out, err = self.run_bzr('branch --switch ../a ../b',
180
working_dir='current')
107
181
a = branch.Branch.open('a')
108
182
b = branch.Branch.open('b')
109
183
self.assertEqual(a.last_revision(), b.last_revision())
118
192
self.example_branch('a')
119
193
self.run_bzr('checkout --lightweight a current')
120
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
194
out, err = self.run_bzr('branch --switch ../a ../b',
195
working_dir='current')
121
196
a = branch.Branch.open('a')
122
197
b = branch.Branch.open('b')
123
198
self.assertEqual(a.last_revision(), b.last_revision())
209
284
def test_branch_no_tree(self):
210
285
self.example_branch('source')
211
286
self.run_bzr('branch --no-tree source target')
212
self.failIfExists('target/hello')
213
self.failIfExists('target/goodbye')
287
self.assertPathDoesNotExist('target/hello')
288
self.assertPathDoesNotExist('target/goodbye')
215
290
def test_branch_into_existing_dir(self):
216
291
self.example_branch('a')
226
301
# force operation
227
302
self.run_bzr('branch a b --use-existing-dir')
228
303
# check conflicts
229
self.failUnlessExists('b/hello.moved')
230
self.failIfExists('b/godbye.moved')
304
self.assertPathExists('b/hello.moved')
305
self.assertPathDoesNotExist('b/godbye.moved')
231
306
# we can't branch into branch
232
307
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
233
308
self.assertEqual('', out)
270
345
self.run_bzr('checkout --lightweight a b')
271
346
self.assertLength(2, calls)
274
class TestBranchStacked(TestCaseWithTransport):
348
def test_branch_fetches_all_tags(self):
349
builder = self.make_branch_builder('source')
350
source = fixtures.build_branch_with_non_ancestral_rev(builder)
351
source.tags.set_tag('tag-a', 'rev-2')
352
source.get_config_stack().set('branch.fetch_tags', True)
353
# Now source has a tag not in its ancestry. Make a branch from it.
354
self.run_bzr('branch source new-branch')
355
new_branch = branch.Branch.open('new-branch')
356
# The tag is present, and so is its revision.
357
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
358
new_branch.repository.get_revision('rev-2')
361
class TestBranchStacked(tests.TestCaseWithTransport):
275
362
"""Tests for branch --stacked"""
277
364
def assertRevisionInRepository(self, repo_path, revid):
278
"""Check that a revision is in a repository, disregarding stacking."""
279
repo = bzrdir.BzrDir.open(repo_path).open_repository()
365
"""Check that a revision is in a repo, disregarding stacking."""
366
repo = controldir.ControlDir.open(repo_path).open_repository()
280
367
self.assertTrue(repo.has_revision(revid))
282
369
def assertRevisionNotInRepository(self, repo_path, revid):
283
"""Check that a revision is not in a repository, disregarding stacking."""
284
repo = bzrdir.BzrDir.open(repo_path).open_repository()
370
"""Check that a revision is not in a repo, disregarding stacking."""
371
repo = controldir.ControlDir.open(repo_path).open_repository()
285
372
self.assertFalse(repo.has_revision(revid))
287
374
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
308
395
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
309
396
self.assertEqual('', out)
310
self.assertEqual('Branched 2 revision(s).\n',
397
self.assertEqual('Branched 2 revisions.\n',
312
399
# it should have preserved the branch format, and so it should be
313
400
# capable of supporting stacking, but not actually have a stacked_on
314
401
# branch configured
315
402
self.assertRaises(errors.NotStacked,
316
bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
403
controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
318
405
def test_branch_stacked_branch_stacked(self):
319
406
"""Asking to stack on a stacked branch does work"""
358
445
trunk_tree.branch.base, err)
359
446
self.assertRevisionNotInRepository('newbranch', original_revid)
360
447
new_branch = branch.Branch.open('newbranch')
361
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
448
self.assertEqual(trunk_tree.branch.base,
449
new_branch.get_stacked_on_url())
363
451
def test_branch_stacked_from_smart_server(self):
364
452
# We can branch stacking on a smart server
414
502
# being too low. If rpc_count increases, more network roundtrips have
415
503
# become necessary for this use case. Please do not adjust this number
416
504
# upwards without agreement from bzr's network support maintainers.
417
self.assertLength(38, self.hpss_calls)
505
self.assertLength(2, self.hpss_connections)
506
self.assertLength(33, self.hpss_calls)
507
self.expectFailure("branching to the same branch requires VFS access",
508
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
419
510
def test_branch_from_trivial_branch_streaming_acceptance(self):
420
511
self.setup_smart_server_with_call_log()
429
520
# being too low. If rpc_count increases, more network roundtrips have
430
521
# become necessary for this use case. Please do not adjust this number
431
522
# upwards without agreement from bzr's network support maintainers.
523
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
432
524
self.assertLength(10, self.hpss_calls)
525
self.assertLength(1, self.hpss_connections)
434
527
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
435
528
self.setup_smart_server_with_call_log()
450
543
# become necessary for this use case. Please do not adjust this number
451
544
# upwards without agreement from bzr's network support maintainers.
452
545
self.assertLength(15, self.hpss_calls)
546
self.assertLength(1, self.hpss_connections)
547
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
549
def test_branch_from_branch_with_tags(self):
550
self.setup_smart_server_with_call_log()
551
builder = self.make_branch_builder('source')
552
source = fixtures.build_branch_with_non_ancestral_rev(builder)
553
source.get_config_stack().set('branch.fetch_tags', True)
554
source.tags.set_tag('tag-a', 'rev-2')
555
source.tags.set_tag('tag-missing', 'missing-rev')
556
# Now source has a tag not in its ancestry. Make a branch from it.
557
self.reset_smart_call_log()
558
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
559
# This figure represent the amount of work to perform this use case. It
560
# is entirely ok to reduce this number if a test fails due to rpc_count
561
# being too low. If rpc_count increases, more network roundtrips have
562
# become necessary for this use case. Please do not adjust this number
563
# upwards without agreement from bzr's network support maintainers.
564
self.assertLength(10, self.hpss_calls)
565
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
566
self.assertLength(1, self.hpss_connections)
568
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
569
self.setup_smart_server_with_call_log()
570
t = self.make_branch_and_tree('from')
571
for count in range(9):
572
t.commit(message='commit %d' % count)
573
self.reset_smart_call_log()
574
out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
576
# XXX: the number of hpss calls for this case isn't deterministic yet,
577
# so we can't easily assert about the number of calls.
578
#self.assertLength(XXX, self.hpss_calls)
579
# We can assert that none of the calls were readv requests for rix
580
# files, though (demonstrating that at least get_parent_map calls are
581
# not using VFS RPCs).
582
readvs_of_rix_files = [
583
c for c in self.hpss_calls
584
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
585
self.assertLength(1, self.hpss_connections)
586
self.assertLength(0, readvs_of_rix_files)
587
self.expectFailure("branching to stacked requires VFS access",
588
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
455
591
class TestRemoteBranch(TestCaseWithSFTPServer):
475
611
# Ensure that no working tree what created remotely
476
612
self.assertFalse(t.has('remote/file'))
615
class TestDeprecatedAliases(tests.TestCaseWithTransport):
617
def test_deprecated_aliases(self):
618
"""bzr branch can be called clone or get, but those names are
623
for command in ['clone', 'get']:
625
$ bzr %(command)s A B
626
2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
627
2>bzr: ERROR: Not a branch...
631
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
633
def _checkout_and_branch(self, option=''):
634
self.script_runner.run_script(self, '''
635
$ bzr checkout %(option)s repo/trunk checkout
637
$ bzr branch --switch ../repo/trunk ../repo/branched
638
2>Branched 0 revisions.
639
2>Tree is up to date at revision 0.
640
2>Switched to branch:...branched...
643
bound_branch = branch.Branch.open_containing('checkout')[0]
644
master_branch = branch.Branch.open_containing('repo/branched')[0]
645
return (bound_branch, master_branch)
647
def test_branch_switch_parent_lightweight(self):
648
"""Lightweight checkout using bzr branch --switch."""
649
bb, mb = self._checkout_and_branch(option='--lightweight')
650
self.assertParent('repo/trunk', bb)
651
self.assertParent('repo/trunk', mb)
653
def test_branch_switch_parent_heavyweight(self):
654
"""Heavyweight checkout using bzr branch --switch."""
655
bb, mb = self._checkout_and_branch()
656
self.assertParent('repo/trunk', bb)
657
self.assertParent('repo/trunk', mb)