1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006-2012 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
from bzrlib import (
26
27
revision as _mod_revision,
28
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
29
from bzrlib.tests import TestCaseWithTransport
30
31
from bzrlib.tests import (
35
from bzrlib.tests.features import (
36
38
from bzrlib.tests.blackbox import test_switch
39
from bzrlib.tests.matchers import ContainsNoVfsCalls
37
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
38
41
from bzrlib.tests.script import run_script
39
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
40
43
from bzrlib.workingtree import WorkingTree
43
class TestBranch(TestCaseWithTransport):
46
class TestBranch(tests.TestCaseWithTransport):
45
def example_branch(self, path='.'):
46
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)
47
50
self.build_tree_contents([(path + '/hello', 'foo')])
49
52
tree.commit(message='setup')
50
53
self.build_tree_contents([(path + '/goodbye', 'baz')])
51
54
tree.add('goodbye')
52
55
tree.commit(message='setup')
54
58
def test_branch(self):
55
59
"""Branch from one branch to another."""
61
65
self.assertFalse(b._transport.has('branch-name'))
62
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")
64
99
def test_branch_broken_pack(self):
65
100
"""branching with a corrupted pack file."""
66
101
self.example_branch('a')
81
116
f.write(corrupt) # make sure we corrupt something
82
self.run_bzr_error(['Corruption while decompressing repository file'],
117
self.run_bzr_error(['Corruption while decompressing repository file'],
83
118
'branch a b', retcode=3)
85
120
def test_branch_switch_no_branch(self):
121
156
work = branch.Branch.open('current')
122
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()))
124
165
def test_branch_switch_checkout(self):
125
166
# Checkout in the current directory:
126
167
# => new branch will be created and checkout bound to the new branch
127
168
self.example_branch('a')
128
169
self.run_bzr('checkout a current')
129
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')
130
172
a = branch.Branch.open('a')
131
173
b = branch.Branch.open('b')
132
174
self.assertEqual(a.last_revision(), b.last_revision())
141
183
self.example_branch('a')
142
184
self.run_bzr('checkout --lightweight a current')
143
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')
144
187
a = branch.Branch.open('a')
145
188
b = branch.Branch.open('b')
146
189
self.assertEqual(a.last_revision(), b.last_revision())
158
201
def make_shared_tree(path):
159
202
shared_repo.bzrdir.root_transport.mkdir(path)
160
shared_repo.bzrdir.create_branch_convenience('repo/' + path)
203
controldir.ControlDir.create_branch_convenience('repo/' + path)
161
204
return WorkingTree.open('repo/' + path)
162
205
tree_a = make_shared_tree('a')
163
206
self.build_tree(['repo/a/file'])
297
340
builder = self.make_branch_builder('source')
298
341
source = fixtures.build_branch_with_non_ancestral_rev(builder)
299
342
source.tags.set_tag('tag-a', 'rev-2')
300
source.get_config().set_user_option('branch.fetch_tags', 'True')
343
source.get_config_stack().set('branch.fetch_tags', True)
301
344
# Now source has a tag not in its ancestry. Make a branch from it.
302
345
self.run_bzr('branch source new-branch')
303
346
new_branch = branch.Branch.open('new-branch')
306
349
new_branch.repository.get_revision('rev-2')
309
class TestBranchStacked(TestCaseWithTransport):
352
class TestBranchStacked(tests.TestCaseWithTransport):
310
353
"""Tests for branch --stacked"""
312
355
def assertRevisionInRepository(self, repo_path, revid):
313
"""Check that a revision is in a repository, disregarding stacking."""
356
"""Check that a revision is in a repo, disregarding stacking."""
314
357
repo = bzrdir.BzrDir.open(repo_path).open_repository()
315
358
self.assertTrue(repo.has_revision(revid))
317
360
def assertRevisionNotInRepository(self, repo_path, revid):
318
"""Check that a revision is not in a repository, disregarding stacking."""
361
"""Check that a revision is not in a repo, disregarding stacking."""
319
362
repo = bzrdir.BzrDir.open(repo_path).open_repository()
320
363
self.assertFalse(repo.has_revision(revid))
343
386
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
344
387
self.assertEqual('', out)
345
self.assertEqual('Branched 2 revision(s).\n',
388
self.assertEqual('Branched 2 revisions.\n',
347
390
# it should have preserved the branch format, and so it should be
348
391
# capable of supporting stacking, but not actually have a stacked_on
393
436
trunk_tree.branch.base, err)
394
437
self.assertRevisionNotInRepository('newbranch', original_revid)
395
438
new_branch = branch.Branch.open('newbranch')
396
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())
398
442
def test_branch_stacked_from_smart_server(self):
399
443
# We can branch stacking on a smart server
437
class TestSmartServerBranching(TestCaseWithTransport):
481
class TestSmartServerBranching(tests.TestCaseWithTransport):
439
483
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
440
484
self.setup_smart_server_with_call_log()
449
493
# being too low. If rpc_count increases, more network roundtrips have
450
494
# become necessary for this use case. Please do not adjust this number
451
495
# upwards without agreement from bzr's network support maintainers.
452
self.assertLength(37, 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)
454
501
def test_branch_from_trivial_branch_streaming_acceptance(self):
455
502
self.setup_smart_server_with_call_log()
464
511
# being too low. If rpc_count increases, more network roundtrips have
465
512
# become necessary for this use case. Please do not adjust this number
466
513
# upwards without agreement from bzr's network support maintainers.
514
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
467
515
self.assertLength(10, self.hpss_calls)
516
self.assertLength(1, self.hpss_connections)
469
518
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
470
519
self.setup_smart_server_with_call_log()
485
534
# become necessary for this use case. Please do not adjust this number
486
535
# upwards without agreement from bzr's network support maintainers.
487
536
self.assertLength(15, self.hpss_calls)
537
self.assertLength(1, self.hpss_connections)
538
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
489
540
def test_branch_from_branch_with_tags(self):
490
541
self.setup_smart_server_with_call_log()
491
542
builder = self.make_branch_builder('source')
492
543
source = fixtures.build_branch_with_non_ancestral_rev(builder)
493
source.get_config().set_user_option('branch.fetch_tags', 'True')
544
source.get_config_stack().set('branch.fetch_tags', True)
494
545
source.tags.set_tag('tag-a', 'rev-2')
495
546
source.tags.set_tag('tag-missing', 'missing-rev')
496
547
# Now source has a tag not in its ancestry. Make a branch from it.
502
553
# become necessary for this use case. Please do not adjust this number
503
554
# upwards without agreement from bzr's network support maintainers.
504
555
self.assertLength(10, self.hpss_calls)
556
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
557
self.assertLength(1, self.hpss_connections)
506
559
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
507
560
self.setup_smart_server_with_call_log()
520
573
readvs_of_rix_files = [
521
574
c for c in self.hpss_calls
522
575
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
576
self.assertLength(1, self.hpss_connections)
523
577
self.assertLength(0, readvs_of_rix_files)
578
self.expectFailure("branching to stacked requires VFS access",
579
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
526
582
class TestRemoteBranch(TestCaseWithSFTPServer):
547
603
self.assertFalse(t.has('remote/file'))
550
class TestDeprecatedAliases(TestCaseWithTransport):
606
class TestDeprecatedAliases(tests.TestCaseWithTransport):
552
608
def test_deprecated_aliases(self):
553
"""bzr branch can be called clone or get, but those names are deprecated.
609
"""bzr branch can be called clone or get, but those names are
569
626
$ bzr checkout %(option)s repo/trunk checkout
571
628
$ bzr branch --switch ../repo/trunk ../repo/branched
572
2>Branched 0 revision(s).
629
2>Branched 0 revisions.
573
630
2>Tree is up to date at revision 0.
574
631
2>Switched to branch:...branched...