22
from bzrlib import (branch, bzrdir, errors, repository)
26
revision as _mod_revision,
23
28
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
24
from bzrlib.tests.blackbox import ExternalBase
29
from bzrlib.tests import TestCaseWithTransport
25
30
from bzrlib.tests import (
36
from bzrlib.tests.blackbox import test_switch
29
37
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
38
from bzrlib.tests.script import run_script
30
39
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
31
40
from bzrlib.workingtree import WorkingTree
34
class TestBranch(ExternalBase):
43
class TestBranch(TestCaseWithTransport):
36
45
def example_branch(self, path='.'):
37
46
tree = self.make_branch_and_tree(path)
52
61
self.assertFalse(b._transport.has('branch-name'))
53
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/")
55
130
def test_branch_only_copies_history(self):
56
131
# Knit branches should only push the history for the current revision.
57
132
format = bzrdir.BzrDirMetaFormat1()
99
174
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
100
175
source_stat = os.stat('source/file1')
101
176
target_stat = os.stat('target/file1')
102
same_file = (source_stat == target_stat)
106
# https://bugs.edge.launchpad.net/bzr/+bug/408193
107
self.assertContainsRe(err, "hardlinking working copy files is "
108
"not currently supported")
109
raise KnownFailure("--hardlink doesn't work in formats "
110
"that support content filtering (#408193)")
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)
112
202
def test_branch_standalone(self):
113
203
shared_repo = self.make_repository('repo', shared=True)
138
228
# force operation
139
229
self.run_bzr('branch a b --use-existing-dir')
140
230
# check conflicts
141
self.failUnlessExists('b/hello.moved')
142
self.failIfExists('b/godbye.moved')
231
self.assertPathExists('b/hello.moved')
232
self.assertPathDoesNotExist('b/godbye.moved')
143
233
# we can't branch into branch
144
234
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
145
235
self.assertEqual('', out)
146
236
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
149
class TestBranchStacked(ExternalBase):
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):
150
288
"""Tests for branch --stacked"""
152
290
def assertRevisionInRepository(self, repo_path, revid):
267
406
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
268
407
'Source branch format does not support stacking, using format:\n'
269
408
' Branch format 7\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'
270
411
'Created new stacked branch referring to %s.\n' % (trunk.base,),
274
class TestSmartServerBranching(ExternalBase):
415
class TestSmartServerBranching(TestCaseWithTransport):
276
417
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
277
418
self.setup_smart_server_with_call_log()
321
462
# being too low. If rpc_count increases, more network roundtrips have
322
463
# become necessary for this use case. Please do not adjust this number
323
464
# upwards without agreement from bzr's network support maintainers.
324
self.assertLength(15, self.hpss_calls)
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)
327
503
class TestRemoteBranch(TestCaseWithSFTPServer):
347
523
# Ensure that no working tree what created remotely
348
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)