13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
"""Black-box tests for bzr branch."""
22
from bzrlib import branch, bzrdir
27
revision as _mod_revision,
23
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
24
from bzrlib.tests.blackbox import ExternalBase
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
25
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
from bzrlib.tests.script import run_script
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
26
43
from bzrlib.workingtree import WorkingTree
29
class TestBranch(ExternalBase):
46
class TestBranch(tests.TestCaseWithTransport):
31
def example_branch(self, path='.'):
32
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)
33
50
self.build_tree_contents([(path + '/hello', 'foo')])
35
52
tree.commit(message='setup')
36
53
self.build_tree_contents([(path + '/goodbye', 'baz')])
37
54
tree.add('goodbye')
38
55
tree.commit(message='setup')
40
58
def test_branch(self):
41
59
"""Branch from one branch to another."""
42
60
self.example_branch('a')
43
61
self.run_bzr('branch a b')
44
62
b = branch.Branch.open('b')
45
self.assertEqual('b\n', b.control_files.get_utf8('branch-name').read())
46
63
self.run_bzr('branch a c -r 1')
64
# previously was erroneously created by branching
65
self.assertFalse(b._transport.has('branch-name'))
47
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)
120
def test_branch_switch_no_branch(self):
121
# No branch in the current directory:
122
# => new branch will be created, but switch fails
123
self.example_branch('a')
124
self.make_repository('current')
125
self.run_bzr_error(['No WorkingTree exists for'],
126
'branch --switch ../a ../b', working_dir='current')
127
a = branch.Branch.open('a')
128
b = branch.Branch.open('b')
129
self.assertEqual(a.last_revision(), b.last_revision())
131
def test_branch_switch_no_wt(self):
132
# No working tree in the current directory:
133
# => new branch will be created, but switch fails and the current
134
# branch is unmodified
135
self.example_branch('a')
136
self.make_branch('current')
137
self.run_bzr_error(['No WorkingTree exists for'],
138
'branch --switch ../a ../b', working_dir='current')
139
a = branch.Branch.open('a')
140
b = branch.Branch.open('b')
141
self.assertEqual(a.last_revision(), b.last_revision())
142
work = branch.Branch.open('current')
143
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
145
def test_branch_switch_no_checkout(self):
146
# Standalone branch in the current directory:
147
# => new branch will be created, but switch fails and the current
148
# branch is unmodified
149
self.example_branch('a')
150
self.make_branch_and_tree('current')
151
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
152
'branch --switch ../a ../b', working_dir='current')
153
a = branch.Branch.open('a')
154
b = branch.Branch.open('b')
155
self.assertEqual(a.last_revision(), b.last_revision())
156
work = branch.Branch.open('current')
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()))
165
def test_branch_switch_checkout(self):
166
# Checkout in the current directory:
167
# => new branch will be created and checkout bound to the new branch
168
self.example_branch('a')
169
self.run_bzr('checkout a current')
170
out, err = self.run_bzr('branch --switch ../a ../b',
171
working_dir='current')
172
a = branch.Branch.open('a')
173
b = branch.Branch.open('b')
174
self.assertEqual(a.last_revision(), b.last_revision())
175
work = WorkingTree.open('current')
176
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
177
self.assertContainsRe(err, "Switched to branch: .*/b/")
179
def test_branch_switch_lightweight_checkout(self):
180
# Lightweight checkout in the current directory:
181
# => new branch will be created and lightweight checkout pointed to
183
self.example_branch('a')
184
self.run_bzr('checkout --lightweight a current')
185
out, err = self.run_bzr('branch --switch ../a ../b',
186
working_dir='current')
187
a = branch.Branch.open('a')
188
b = branch.Branch.open('b')
189
self.assertEqual(a.last_revision(), b.last_revision())
190
work = WorkingTree.open('current')
191
self.assertEndsWith(work.branch.base, '/b/')
192
self.assertContainsRe(err, "Switched to branch: .*/b/")
49
194
def test_branch_only_copies_history(self):
50
195
# Knit branches should only push the history for the current revision.
51
196
format = bzrdir.BzrDirMetaFormat1()
84
229
self.assertFalse(pushed_repo.has_revision('a-2'))
85
230
self.assertTrue(pushed_repo.has_revision('b-1'))
232
def test_branch_hardlink(self):
233
self.requireFeature(HardlinkFeature)
234
source = self.make_branch_and_tree('source')
235
self.build_tree(['source/file1'])
237
source.commit('added file')
238
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
239
source_stat = os.stat('source/file1')
240
target_stat = os.stat('target/file1')
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)
266
def test_branch_standalone(self):
267
shared_repo = self.make_repository('repo', shared=True)
268
self.example_branch('source')
269
self.run_bzr('branch --standalone source repo/target')
270
b = branch.Branch.open('repo/target')
271
expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
272
self.assertEqual(strip_trailing_slash(b.repository.base),
273
strip_trailing_slash(local_path_to_url(expected_repo_path)))
275
def test_branch_no_tree(self):
276
self.example_branch('source')
277
self.run_bzr('branch --no-tree source target')
278
self.assertPathDoesNotExist('target/hello')
279
self.assertPathDoesNotExist('target/goodbye')
281
def test_branch_into_existing_dir(self):
282
self.example_branch('a')
283
# existing dir with similar files but no .bzr dir
284
self.build_tree_contents([('b/',)])
285
self.build_tree_contents([('b/hello', 'bar')]) # different content
286
self.build_tree_contents([('b/goodbye', 'baz')])# same content
287
# fails without --use-existing-dir
288
out,err = self.run_bzr('branch a b', retcode=3)
289
self.assertEqual('', out)
290
self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
293
self.run_bzr('branch a b --use-existing-dir')
295
self.assertPathExists('b/hello.moved')
296
self.assertPathDoesNotExist('b/godbye.moved')
297
# we can't branch into branch
298
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
299
self.assertEqual('', out)
300
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
302
def test_branch_bind(self):
303
self.example_branch('a')
304
out, err = self.run_bzr('branch a b --bind')
305
self.assertEndsWith(err, "New branch bound to a\n")
306
b = branch.Branch.open('b')
307
self.assertEndsWith(b.get_bound_location(), '/a/')
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):
353
"""Tests for branch --stacked"""
355
def assertRevisionInRepository(self, repo_path, revid):
356
"""Check that a revision is in a repo, disregarding stacking."""
357
repo = bzrdir.BzrDir.open(repo_path).open_repository()
358
self.assertTrue(repo.has_revision(revid))
360
def assertRevisionNotInRepository(self, repo_path, revid):
361
"""Check that a revision is not in a repo, disregarding stacking."""
362
repo = bzrdir.BzrDir.open(repo_path).open_repository()
363
self.assertFalse(repo.has_revision(revid))
365
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
366
repo = branch.Branch.open(branch_path).repository
367
self.assertEqual(set(revid_list),
368
repo.has_revisions(revid_list))
370
def test_branch_stacked_branch_not_stacked(self):
371
"""Branching a stacked branch is not stacked by default"""
373
trunk_tree = self.make_branch_and_tree('target',
375
trunk_tree.commit('mainline')
376
# and a branch from it which is stacked
377
branch_tree = self.make_branch_and_tree('branch',
379
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
380
# with some work on it
381
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
382
work_tree.commit('moar work plz')
383
work_tree.branch.push(branch_tree.branch)
384
# branching our local branch gives us a new stacked branch pointing at
386
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
387
self.assertEqual('', out)
388
self.assertEqual('Branched 2 revisions.\n',
390
# it should have preserved the branch format, and so it should be
391
# capable of supporting stacking, but not actually have a stacked_on
393
self.assertRaises(errors.NotStacked,
394
bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
396
def test_branch_stacked_branch_stacked(self):
397
"""Asking to stack on a stacked branch does work"""
399
trunk_tree = self.make_branch_and_tree('target',
401
trunk_revid = trunk_tree.commit('mainline')
402
# and a branch from it which is stacked
403
branch_tree = self.make_branch_and_tree('branch',
405
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
406
# with some work on it
407
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
408
branch_revid = work_tree.commit('moar work plz')
409
work_tree.branch.push(branch_tree.branch)
410
# you can chain branches on from there
411
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
412
self.assertEqual('', out)
413
self.assertEqual('Created new stacked branch referring to %s.\n' %
414
branch_tree.branch.base, err)
415
self.assertEqual(branch_tree.branch.base,
416
branch.Branch.open('branch2').get_stacked_on_url())
417
branch2_tree = WorkingTree.open('branch2')
418
branch2_revid = work_tree.commit('work on second stacked branch')
419
work_tree.branch.push(branch2_tree.branch)
420
self.assertRevisionInRepository('branch2', branch2_revid)
421
self.assertRevisionsInBranchRepository(
422
[trunk_revid, branch_revid, branch2_revid],
425
def test_branch_stacked(self):
427
trunk_tree = self.make_branch_and_tree('mainline',
429
original_revid = trunk_tree.commit('mainline')
430
self.assertRevisionInRepository('mainline', original_revid)
431
# and a branch from it which is stacked
432
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
434
self.assertEqual('', out)
435
self.assertEqual('Created new stacked branch referring to %s.\n' %
436
trunk_tree.branch.base, err)
437
self.assertRevisionNotInRepository('newbranch', original_revid)
438
new_branch = branch.Branch.open('newbranch')
439
self.assertEqual(trunk_tree.branch.base,
440
new_branch.get_stacked_on_url())
442
def test_branch_stacked_from_smart_server(self):
443
# We can branch stacking on a smart server
444
self.transport_server = test_server.SmartTCPServer_for_testing
445
trunk = self.make_branch('mainline', format='1.9')
446
out, err = self.run_bzr(
447
['branch', '--stacked', self.get_url('mainline'), 'shallow'])
449
def test_branch_stacked_from_non_stacked_format(self):
450
"""The origin format doesn't support stacking"""
451
trunk = self.make_branch('trunk', format='pack-0.92')
452
out, err = self.run_bzr(
453
['branch', '--stacked', 'trunk', 'shallow'])
454
# We should notify the user that we upgraded their format
455
self.assertEqualDiff(
456
'Source repository format does not support stacking, using format:\n'
457
' Packs 5 (adds stacking support, requires bzr 1.6)\n'
458
'Source branch format does not support stacking, using format:\n'
460
'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
461
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
462
'Created new stacked branch referring to %s.\n' % (trunk.base,),
465
def test_branch_stacked_from_rich_root_non_stackable(self):
466
trunk = self.make_branch('trunk', format='rich-root-pack')
467
out, err = self.run_bzr(
468
['branch', '--stacked', 'trunk', 'shallow'])
469
# We should notify the user that we upgraded their format
470
self.assertEqualDiff(
471
'Source repository format does not support stacking, using format:\n'
472
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
473
'Source branch format does not support stacking, using format:\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'
477
'Created new stacked branch referring to %s.\n' % (trunk.base,),
481
class TestSmartServerBranching(tests.TestCaseWithTransport):
483
def test_branch_from_trivial_branch_to_same_server_branch_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', self.get_url('from'),
490
self.get_url('target')])
491
# This figure represent the amount of work to perform this use case. It
492
# is entirely ok to reduce this number if a test fails due to rpc_count
493
# being too low. If rpc_count increases, more network roundtrips have
494
# become necessary for this use case. Please do not adjust this number
495
# upwards without agreement from bzr's network support maintainers.
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)
501
def test_branch_from_trivial_branch_streaming_acceptance(self):
502
self.setup_smart_server_with_call_log()
503
t = self.make_branch_and_tree('from')
504
for count in range(9):
505
t.commit(message='commit %d' % count)
506
self.reset_smart_call_log()
507
out, err = self.run_bzr(['branch', self.get_url('from'),
509
# This figure represent the amount of work to perform this use case. It
510
# is entirely ok to reduce this number if a test fails due to rpc_count
511
# being too low. If rpc_count increases, more network roundtrips have
512
# become necessary for this use case. Please do not adjust this number
513
# upwards without agreement from bzr's network support maintainers.
514
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
515
self.assertLength(10, self.hpss_calls)
516
self.assertLength(1, self.hpss_connections)
518
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
519
self.setup_smart_server_with_call_log()
520
t = self.make_branch_and_tree('trunk')
521
for count in range(8):
522
t.commit(message='commit %d' % count)
523
tree2 = t.branch.bzrdir.sprout('feature', stacked=True
525
local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
526
local_tree.commit('feature change')
527
local_tree.branch.push(tree2.branch)
528
self.reset_smart_call_log()
529
out, err = self.run_bzr(['branch', self.get_url('feature'),
531
# This figure represent the amount of work to perform this use case. It
532
# is entirely ok to reduce this number if a test fails due to rpc_count
533
# being too low. If rpc_count increases, more network roundtrips have
534
# become necessary for this use case. Please do not adjust this number
535
# upwards without agreement from bzr's network support maintainers.
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)
88
582
class TestRemoteBranch(TestCaseWithSFTPServer):