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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
"""Black-box tests for bzr branch."""
26
revision as _mod_revision,
22
from bzrlib import branch, bzrdir
28
23
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
29
from bzrlib.tests import TestCaseWithTransport
30
from bzrlib.tests import (
35
from bzrlib.tests.features import (
38
from bzrlib.tests.blackbox import test_switch
24
from bzrlib.tests.blackbox import ExternalBase
39
25
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
40
from bzrlib.tests.script import run_script
41
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
42
26
from bzrlib.workingtree import WorkingTree
45
class TestBranch(TestCaseWithTransport):
29
class TestBranch(ExternalBase):
47
def example_branch(self, path='.'):
48
tree = self.make_branch_and_tree(path)
49
self.build_tree_contents([(path + '/hello', 'foo')])
51
tree.commit(message='setup')
52
self.build_tree_contents([(path + '/goodbye', 'baz')])
54
tree.commit(message='setup')
31
def example_branch(test):
33
file('hello', 'wt').write('foo')
34
test.run_bzr('add hello')
35
test.run_bzr('commit -m setup hello')
36
file('goodbye', 'wt').write('baz')
37
test.run_bzr('add goodbye')
38
test.run_bzr('commit -m setup goodbye')
56
40
def test_branch(self):
57
41
"""Branch from one branch to another."""
58
self.example_branch('a')
59
46
self.run_bzr('branch a b')
60
47
b = branch.Branch.open('b')
48
self.assertEqual('b\n', b.control_files.get_utf8('branch-name').read())
61
49
self.run_bzr('branch a c -r 1')
62
# previously was erroneously created by branching
63
self.assertFalse(b._transport.has('branch-name'))
64
b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
66
def test_branch_broken_pack(self):
67
"""branching with a corrupted pack file."""
68
self.example_branch('a')
70
packs_dir = 'a/.bzr/repository/packs/'
71
fname = packs_dir + os.listdir(packs_dir)[0]
72
with open(fname, 'rb+') as f:
73
# Start from the end of the file to avoid choosing a place bigger
74
# than the file itself.
75
f.seek(-5, os.SEEK_END)
77
f.seek(-5, os.SEEK_END)
78
# Make sure we inject a value different than the one we just read
83
f.write(corrupt) # make sure we corrupt something
84
self.run_bzr_error(['Corruption while decompressing repository file'],
85
'branch a b', retcode=3)
87
def test_branch_switch_no_branch(self):
88
# No branch in the current directory:
89
# => new branch will be created, but switch fails
90
self.example_branch('a')
91
self.make_repository('current')
92
self.run_bzr_error(['No WorkingTree exists for'],
93
'branch --switch ../a ../b', working_dir='current')
94
a = branch.Branch.open('a')
95
b = branch.Branch.open('b')
96
self.assertEqual(a.last_revision(), b.last_revision())
98
def test_branch_switch_no_wt(self):
99
# No working tree in the current directory:
100
# => new branch will be created, but switch fails and the current
101
# branch is unmodified
102
self.example_branch('a')
103
self.make_branch('current')
104
self.run_bzr_error(['No WorkingTree exists for'],
105
'branch --switch ../a ../b', working_dir='current')
106
a = branch.Branch.open('a')
107
b = branch.Branch.open('b')
108
self.assertEqual(a.last_revision(), b.last_revision())
109
work = branch.Branch.open('current')
110
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
112
def test_branch_switch_no_checkout(self):
113
# Standalone branch in the current directory:
114
# => new branch will be created, but switch fails and the current
115
# branch is unmodified
116
self.example_branch('a')
117
self.make_branch_and_tree('current')
118
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
119
'branch --switch ../a ../b', working_dir='current')
120
a = branch.Branch.open('a')
121
b = branch.Branch.open('b')
122
self.assertEqual(a.last_revision(), b.last_revision())
123
work = branch.Branch.open('current')
124
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
126
def test_branch_switch_checkout(self):
127
# Checkout in the current directory:
128
# => new branch will be created and checkout bound to the new branch
129
self.example_branch('a')
130
self.run_bzr('checkout a current')
131
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
132
a = branch.Branch.open('a')
133
b = branch.Branch.open('b')
134
self.assertEqual(a.last_revision(), b.last_revision())
135
work = WorkingTree.open('current')
136
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
137
self.assertContainsRe(err, "Switched to branch: .*/b/")
139
def test_branch_switch_lightweight_checkout(self):
140
# Lightweight checkout in the current directory:
141
# => new branch will be created and lightweight checkout pointed to
143
self.example_branch('a')
144
self.run_bzr('checkout --lightweight a current')
145
out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
146
a = branch.Branch.open('a')
147
b = branch.Branch.open('b')
148
self.assertEqual(a.last_revision(), b.last_revision())
149
work = WorkingTree.open('current')
150
self.assertEndsWith(work.branch.base, '/b/')
151
self.assertContainsRe(err, "Switched to branch: .*/b/")
51
self.run_bzr('commit -m foo --unchanged')
153
54
def test_branch_only_copies_history(self):
154
55
# Knit branches should only push the history for the current revision.
188
89
self.assertFalse(pushed_repo.has_revision('a-2'))
189
90
self.assertTrue(pushed_repo.has_revision('b-1'))
191
def test_branch_hardlink(self):
192
self.requireFeature(HardlinkFeature)
193
source = self.make_branch_and_tree('source')
194
self.build_tree(['source/file1'])
196
source.commit('added file')
197
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
198
source_stat = os.stat('source/file1')
199
target_stat = os.stat('target/file1')
200
self.assertEqual(source_stat, target_stat)
202
def test_branch_files_from(self):
203
source = self.make_branch_and_tree('source')
204
self.build_tree(['source/file1'])
206
source.commit('added file')
207
out, err = self.run_bzr('branch source target --files-from source')
208
self.assertPathExists('target/file1')
210
def test_branch_files_from_hardlink(self):
211
self.requireFeature(HardlinkFeature)
212
source = self.make_branch_and_tree('source')
213
self.build_tree(['source/file1'])
215
source.commit('added file')
216
source.bzrdir.sprout('second')
217
out, err = self.run_bzr('branch source target --files-from second'
219
source_stat = os.stat('source/file1')
220
second_stat = os.stat('second/file1')
221
target_stat = os.stat('target/file1')
222
self.assertNotEqual(source_stat, target_stat)
223
self.assertEqual(second_stat, target_stat)
225
def test_branch_standalone(self):
226
shared_repo = self.make_repository('repo', shared=True)
227
self.example_branch('source')
228
self.run_bzr('branch --standalone source repo/target')
229
b = branch.Branch.open('repo/target')
230
expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
231
self.assertEqual(strip_trailing_slash(b.repository.base),
232
strip_trailing_slash(local_path_to_url(expected_repo_path)))
234
def test_branch_no_tree(self):
235
self.example_branch('source')
236
self.run_bzr('branch --no-tree source target')
237
self.assertPathDoesNotExist('target/hello')
238
self.assertPathDoesNotExist('target/goodbye')
240
def test_branch_into_existing_dir(self):
241
self.example_branch('a')
242
# existing dir with similar files but no .bzr dir
243
self.build_tree_contents([('b/',)])
244
self.build_tree_contents([('b/hello', 'bar')]) # different content
245
self.build_tree_contents([('b/goodbye', 'baz')])# same content
246
# fails without --use-existing-dir
247
out,err = self.run_bzr('branch a b', retcode=3)
248
self.assertEqual('', out)
249
self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
252
self.run_bzr('branch a b --use-existing-dir')
254
self.assertPathExists('b/hello.moved')
255
self.assertPathDoesNotExist('b/godbye.moved')
256
# we can't branch into branch
257
out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
258
self.assertEqual('', out)
259
self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
261
def test_branch_bind(self):
262
self.example_branch('a')
263
out, err = self.run_bzr('branch a b --bind')
264
self.assertEndsWith(err, "New branch bound to a\n")
265
b = branch.Branch.open('b')
266
self.assertEndsWith(b.get_bound_location(), '/a/')
268
def test_branch_with_post_branch_init_hook(self):
270
branch.Branch.hooks.install_named_hook('post_branch_init',
272
self.assertLength(0, calls)
273
self.example_branch('a')
274
self.assertLength(1, calls)
275
self.run_bzr('branch a b')
276
self.assertLength(2, calls)
278
def test_checkout_with_post_branch_init_hook(self):
280
branch.Branch.hooks.install_named_hook('post_branch_init',
282
self.assertLength(0, calls)
283
self.example_branch('a')
284
self.assertLength(1, calls)
285
self.run_bzr('checkout a b')
286
self.assertLength(2, calls)
288
def test_lightweight_checkout_with_post_branch_init_hook(self):
290
branch.Branch.hooks.install_named_hook('post_branch_init',
292
self.assertLength(0, calls)
293
self.example_branch('a')
294
self.assertLength(1, calls)
295
self.run_bzr('checkout --lightweight a b')
296
self.assertLength(2, calls)
298
def test_branch_fetches_all_tags(self):
299
builder = self.make_branch_builder('source')
300
source = fixtures.build_branch_with_non_ancestral_rev(builder)
301
source.tags.set_tag('tag-a', 'rev-2')
302
source.get_config().set_user_option('branch.fetch_tags', 'True')
303
# Now source has a tag not in its ancestry. Make a branch from it.
304
self.run_bzr('branch source new-branch')
305
new_branch = branch.Branch.open('new-branch')
306
# The tag is present, and so is its revision.
307
self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
308
new_branch.repository.get_revision('rev-2')
311
class TestBranchStacked(TestCaseWithTransport):
312
"""Tests for branch --stacked"""
314
def assertRevisionInRepository(self, repo_path, revid):
315
"""Check that a revision is in a repository, disregarding stacking."""
316
repo = bzrdir.BzrDir.open(repo_path).open_repository()
317
self.assertTrue(repo.has_revision(revid))
319
def assertRevisionNotInRepository(self, repo_path, revid):
320
"""Check that a revision is not in a repository, disregarding stacking."""
321
repo = bzrdir.BzrDir.open(repo_path).open_repository()
322
self.assertFalse(repo.has_revision(revid))
324
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
325
repo = branch.Branch.open(branch_path).repository
326
self.assertEqual(set(revid_list),
327
repo.has_revisions(revid_list))
329
def test_branch_stacked_branch_not_stacked(self):
330
"""Branching a stacked branch is not stacked by default"""
332
trunk_tree = self.make_branch_and_tree('target',
334
trunk_tree.commit('mainline')
335
# and a branch from it which is stacked
336
branch_tree = self.make_branch_and_tree('branch',
338
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
339
# with some work on it
340
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
341
work_tree.commit('moar work plz')
342
work_tree.branch.push(branch_tree.branch)
343
# branching our local branch gives us a new stacked branch pointing at
345
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
346
self.assertEqual('', out)
347
self.assertEqual('Branched 2 revisions.\n',
349
# it should have preserved the branch format, and so it should be
350
# capable of supporting stacking, but not actually have a stacked_on
352
self.assertRaises(errors.NotStacked,
353
bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
355
def test_branch_stacked_branch_stacked(self):
356
"""Asking to stack on a stacked branch does work"""
358
trunk_tree = self.make_branch_and_tree('target',
360
trunk_revid = trunk_tree.commit('mainline')
361
# and a branch from it which is stacked
362
branch_tree = self.make_branch_and_tree('branch',
364
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
365
# with some work on it
366
work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
367
branch_revid = work_tree.commit('moar work plz')
368
work_tree.branch.push(branch_tree.branch)
369
# you can chain branches on from there
370
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
371
self.assertEqual('', out)
372
self.assertEqual('Created new stacked branch referring to %s.\n' %
373
branch_tree.branch.base, err)
374
self.assertEqual(branch_tree.branch.base,
375
branch.Branch.open('branch2').get_stacked_on_url())
376
branch2_tree = WorkingTree.open('branch2')
377
branch2_revid = work_tree.commit('work on second stacked branch')
378
work_tree.branch.push(branch2_tree.branch)
379
self.assertRevisionInRepository('branch2', branch2_revid)
380
self.assertRevisionsInBranchRepository(
381
[trunk_revid, branch_revid, branch2_revid],
384
def test_branch_stacked(self):
386
trunk_tree = self.make_branch_and_tree('mainline',
388
original_revid = trunk_tree.commit('mainline')
389
self.assertRevisionInRepository('mainline', original_revid)
390
# and a branch from it which is stacked
391
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
393
self.assertEqual('', out)
394
self.assertEqual('Created new stacked branch referring to %s.\n' %
395
trunk_tree.branch.base, err)
396
self.assertRevisionNotInRepository('newbranch', original_revid)
397
new_branch = branch.Branch.open('newbranch')
398
self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
400
def test_branch_stacked_from_smart_server(self):
401
# We can branch stacking on a smart server
402
self.transport_server = test_server.SmartTCPServer_for_testing
403
trunk = self.make_branch('mainline', format='1.9')
404
out, err = self.run_bzr(
405
['branch', '--stacked', self.get_url('mainline'), 'shallow'])
407
def test_branch_stacked_from_non_stacked_format(self):
408
"""The origin format doesn't support stacking"""
409
trunk = self.make_branch('trunk', format='pack-0.92')
410
out, err = self.run_bzr(
411
['branch', '--stacked', 'trunk', 'shallow'])
412
# We should notify the user that we upgraded their format
413
self.assertEqualDiff(
414
'Source repository format does not support stacking, using format:\n'
415
' Packs 5 (adds stacking support, requires bzr 1.6)\n'
416
'Source branch format does not support stacking, using format:\n'
418
'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
419
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
420
'Created new stacked branch referring to %s.\n' % (trunk.base,),
423
def test_branch_stacked_from_rich_root_non_stackable(self):
424
trunk = self.make_branch('trunk', format='rich-root-pack')
425
out, err = self.run_bzr(
426
['branch', '--stacked', 'trunk', 'shallow'])
427
# We should notify the user that we upgraded their format
428
self.assertEqualDiff(
429
'Source repository format does not support stacking, using format:\n'
430
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
431
'Source branch format does not support stacking, using format:\n'
433
'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
434
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
435
'Created new stacked branch referring to %s.\n' % (trunk.base,),
439
class TestSmartServerBranching(TestCaseWithTransport):
441
def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
442
self.setup_smart_server_with_call_log()
443
t = self.make_branch_and_tree('from')
444
for count in range(9):
445
t.commit(message='commit %d' % count)
446
self.reset_smart_call_log()
447
out, err = self.run_bzr(['branch', self.get_url('from'),
448
self.get_url('target')])
449
# This figure represent the amount of work to perform this use case. It
450
# is entirely ok to reduce this number if a test fails due to rpc_count
451
# being too low. If rpc_count increases, more network roundtrips have
452
# become necessary for this use case. Please do not adjust this number
453
# upwards without agreement from bzr's network support maintainers.
454
self.assertLength(39, self.hpss_calls)
456
def test_branch_from_trivial_branch_streaming_acceptance(self):
457
self.setup_smart_server_with_call_log()
458
t = self.make_branch_and_tree('from')
459
for count in range(9):
460
t.commit(message='commit %d' % count)
461
self.reset_smart_call_log()
462
out, err = self.run_bzr(['branch', self.get_url('from'),
464
# This figure represent the amount of work to perform this use case. It
465
# is entirely ok to reduce this number if a test fails due to rpc_count
466
# being too low. If rpc_count increases, more network roundtrips have
467
# become necessary for this use case. Please do not adjust this number
468
# upwards without agreement from bzr's network support maintainers.
469
self.assertLength(10, self.hpss_calls)
471
def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
472
self.setup_smart_server_with_call_log()
473
t = self.make_branch_and_tree('trunk')
474
for count in range(8):
475
t.commit(message='commit %d' % count)
476
tree2 = t.branch.bzrdir.sprout('feature', stacked=True
478
local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
479
local_tree.commit('feature change')
480
local_tree.branch.push(tree2.branch)
481
self.reset_smart_call_log()
482
out, err = self.run_bzr(['branch', self.get_url('feature'),
484
# This figure represent the amount of work to perform this use case. It
485
# is entirely ok to reduce this number if a test fails due to rpc_count
486
# being too low. If rpc_count increases, more network roundtrips have
487
# become necessary for this use case. Please do not adjust this number
488
# upwards without agreement from bzr's network support maintainers.
489
self.assertLength(15, self.hpss_calls)
491
def test_branch_from_branch_with_tags(self):
492
self.setup_smart_server_with_call_log()
493
builder = self.make_branch_builder('source')
494
source = fixtures.build_branch_with_non_ancestral_rev(builder)
495
source.get_config().set_user_option('branch.fetch_tags', 'True')
496
source.tags.set_tag('tag-a', 'rev-2')
497
source.tags.set_tag('tag-missing', 'missing-rev')
498
# Now source has a tag not in its ancestry. Make a branch from it.
499
self.reset_smart_call_log()
500
out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
501
# This figure represent the amount of work to perform this use case. It
502
# is entirely ok to reduce this number if a test fails due to rpc_count
503
# being too low. If rpc_count increases, more network roundtrips have
504
# become necessary for this use case. Please do not adjust this number
505
# upwards without agreement from bzr's network support maintainers.
506
self.assertLength(10, self.hpss_calls)
508
def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
509
self.setup_smart_server_with_call_log()
510
t = self.make_branch_and_tree('from')
511
for count in range(9):
512
t.commit(message='commit %d' % count)
513
self.reset_smart_call_log()
514
out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
516
# XXX: the number of hpss calls for this case isn't deterministic yet,
517
# so we can't easily assert about the number of calls.
518
#self.assertLength(XXX, self.hpss_calls)
519
# We can assert that none of the calls were readv requests for rix
520
# files, though (demonstrating that at least get_parent_map calls are
521
# not using VFS RPCs).
522
readvs_of_rix_files = [
523
c for c in self.hpss_calls
524
if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
525
self.assertLength(0, readvs_of_rix_files)
528
93
class TestRemoteBranch(TestCaseWithSFTPServer):