1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
18
"""Black-box tests for brz branch."""
26
revision as _mod_revision,
29
from breezy.bzr import (
32
from breezy.bzr.knitrepo import RepositoryFormatKnit1
33
from breezy.tests import (
37
from breezy.tests.features import (
40
from breezy.tests.blackbox import test_switch
41
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
42
from breezy.tests.script import run_script
43
from breezy.urlutils import local_path_to_url, strip_trailing_slash
44
from breezy.workingtree import WorkingTree
47
class TestBranch(tests.TestCaseWithTransport):
49
def example_branch(self, path='.', format=None):
50
tree = self.make_branch_and_tree(path, format=format)
51
self.build_tree_contents([(path + '/hello', b'foo')])
53
tree.commit(message='setup')
54
self.build_tree_contents([(path + '/goodbye', b'baz')])
56
tree.commit(message='setup')
59
def test_branch(self):
60
"""Branch from one branch to another."""
61
self.example_branch('a')
62
self.run_bzr('branch a b')
63
b = branch.Branch.open('b')
64
self.run_bzr('branch a c -r 1')
65
# previously was erroneously created by branching
66
self.assertFalse(b._transport.has('branch-name'))
67
b.controldir.open_workingtree().commit(message='foo', allow_pointless=True)
69
def test_branch_no_to_location(self):
70
"""The to_location is derived from the source branch name."""
72
a = self.example_branch('something/a').branch
73
self.run_bzr('branch something/a')
74
b = branch.Branch.open('a')
75
self.assertEqual(b.last_revision_info(), a.last_revision_info())
77
def test_into_colocated(self):
78
"""Branch from a branch into a colocated branch."""
79
self.example_branch('a')
80
out, err = self.run_bzr(
81
'init --format=development-colo file:b,branch=orig')
83
"""Created a standalone tree (format: development-colo)\n""",
85
self.assertEqual('', err)
86
out, err = self.run_bzr(
87
'branch a file:b,branch=thiswasa')
88
self.assertEqual('', out)
89
self.assertEqual('Branched 2 revisions.\n', err)
90
out, err = self.run_bzr('branches b')
91
self.assertEqual(" orig\n thiswasa\n", out)
92
self.assertEqual('', err)
93
out, err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
94
self.assertEqual('', out)
96
'brz: ERROR: Already a branch: "file:b,branch=orig".\n', err)
98
def test_from_colocated(self):
99
"""Branch from a colocated branch into a regular branch."""
101
tree = self.example_branch('b/a', format='development-colo')
102
tree.controldir.create_branch(name='somecolo')
103
out, err = self.run_bzr('branch %s,branch=somecolo' %
104
local_path_to_url('b/a'))
105
self.assertEqual('', out)
106
self.assertEqual('Branched 0 revisions.\n', err)
107
self.assertPathExists('a')
109
def test_from_name(self):
110
"""Branch from a colocated branch into a regular branch."""
112
tree = self.example_branch('b/a', format='development-colo')
113
tree.controldir.create_branch(name='somecolo')
114
out, err = self.run_bzr('branch -b somecolo %s' %
115
local_path_to_url('b/a'))
116
self.assertEqual('', out)
117
self.assertEqual('Branched 0 revisions.\n', err)
118
self.assertPathExists('a')
120
def test_branch_broken_pack(self):
121
"""branching with a corrupted pack file."""
122
self.example_branch('a')
123
# add some corruption
124
packs_dir = 'a/.bzr/repository/packs/'
125
fname = packs_dir + os.listdir(packs_dir)[0]
126
with open(fname, 'rb+') as f:
127
# Start from the end of the file to avoid choosing a place bigger
128
# than the file itself.
129
f.seek(-5, os.SEEK_END)
131
f.seek(-5, os.SEEK_END)
132
# Make sure we inject a value different than the one we just read
137
f.write(corrupt) # make sure we corrupt something
138
self.run_bzr_error(['Corruption while decompressing repository file'],
139
'branch a b', retcode=3)
141
def test_branch_switch_no_branch(self):
142
# No branch in the current directory:
143
# => new branch will be created, but switch fails
144
self.example_branch('a')
145
self.make_repository('current')
146
self.run_bzr_error(['No WorkingTree exists for'],
147
'branch --switch ../a ../b', working_dir='current')
148
a = branch.Branch.open('a')
149
b = branch.Branch.open('b')
150
self.assertEqual(a.last_revision(), b.last_revision())
152
def test_branch_switch_no_wt(self):
153
# No working tree in the current directory:
154
# => new branch will be created, but switch fails and the current
155
# branch is unmodified
156
self.example_branch('a')
157
self.make_branch('current')
158
self.run_bzr_error(['No WorkingTree exists for'],
159
'branch --switch ../a ../b', working_dir='current')
160
a = branch.Branch.open('a')
161
b = branch.Branch.open('b')
162
self.assertEqual(a.last_revision(), b.last_revision())
163
work = branch.Branch.open('current')
164
self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
166
def test_branch_switch_no_checkout(self):
167
# Standalone branch in the current directory:
168
# => new branch will be created, but switch fails and the current
169
# branch is unmodified
170
self.example_branch('a')
171
tree = self.make_branch_and_tree('current')
172
c1 = tree.commit('some diverged change')
173
self.run_bzr_error(['Cannot switch a branch, only a checkout'],
174
'branch --switch ../a ../b', working_dir='current')
175
a = branch.Branch.open('a')
176
b = branch.Branch.open('b')
177
self.assertEqual(a.last_revision(), b.last_revision())
178
work = branch.Branch.open('current')
179
self.assertEqual(work.last_revision(), c1)
181
def test_branch_into_empty_dir(self):
182
t = self.example_branch('source')
183
self.make_controldir('target')
184
self.run_bzr("branch source target")
185
self.assertEqual(2, len(t.branch.repository.all_revision_ids()))
187
def test_branch_switch_checkout(self):
188
# Checkout in the current directory:
189
# => new branch will be created and checkout bound to the new branch
190
self.example_branch('a')
191
self.run_bzr('checkout a current')
192
out, err = self.run_bzr('branch --switch ../a ../b',
193
working_dir='current')
194
a = branch.Branch.open('a')
195
b = branch.Branch.open('b')
196
self.assertEqual(a.last_revision(), b.last_revision())
197
work = WorkingTree.open('current')
198
self.assertEndsWith(work.branch.get_bound_location(), '/b/')
199
self.assertContainsRe(err, "Switched to branch: .*/b/")
201
def test_branch_switch_lightweight_checkout(self):
202
# Lightweight checkout in the current directory:
203
# => new branch will be created and lightweight checkout pointed to
205
self.example_branch('a')
206
self.run_bzr('checkout --lightweight a current')
207
out, err = self.run_bzr('branch --switch ../a ../b',
208
working_dir='current')
209
a = branch.Branch.open('a')
210
b = branch.Branch.open('b')
211
self.assertEqual(a.last_revision(), b.last_revision())
212
work = WorkingTree.open('current')
213
self.assertEndsWith(work.branch.base, '/b/')
214
self.assertContainsRe(err, "Switched to branch: .*/b/")
216
def test_branch_only_copies_history(self):
217
# Knit branches should only push the history for the current revision.
218
format = bzrdir.BzrDirMetaFormat1()
219
format.repository_format = RepositoryFormatKnit1()
220
shared_repo = self.make_repository('repo', format=format, shared=True)
221
shared_repo.set_make_working_trees(True)
223
def make_shared_tree(path):
224
shared_repo.controldir.root_transport.mkdir(path)
225
controldir.ControlDir.create_branch_convenience('repo/' + path)
226
return WorkingTree.open('repo/' + path)
227
tree_a = make_shared_tree('a')
228
self.build_tree(['repo/a/file'])
230
tree_a.commit('commit a-1', rev_id=b'a-1')
231
with open('repo/a/file', 'ab') as f:
232
f.write(b'more stuff\n')
233
tree_a.commit('commit a-2', rev_id=b'a-2')
235
tree_b = make_shared_tree('b')
236
self.build_tree(['repo/b/file'])
238
tree_b.commit('commit b-1', rev_id=b'b-1')
240
self.assertTrue(shared_repo.has_revision(b'a-1'))
241
self.assertTrue(shared_repo.has_revision(b'a-2'))
242
self.assertTrue(shared_repo.has_revision(b'b-1'))
244
# Now that we have a repository with shared files, make sure
245
# that things aren't copied out by a 'branch'
246
self.run_bzr('branch repo/b branch-b')
247
pushed_tree = WorkingTree.open('branch-b')
248
pushed_repo = pushed_tree.branch.repository
249
self.assertFalse(pushed_repo.has_revision(b'a-1'))
250
self.assertFalse(pushed_repo.has_revision(b'a-2'))
251
self.assertTrue(pushed_repo.has_revision(b'b-1'))
253
def test_branch_hardlink(self):
254
self.requireFeature(HardlinkFeature)
255
source = self.make_branch_and_tree('source')
256
self.build_tree(['source/file1'])
258
source.commit('added file')
259
out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
260
source_stat = os.stat('source/file1')
261
target_stat = os.stat('target/file1')
262
self.assertEqual(source_stat, target_stat)
264
def test_branch_files_from(self):
265
source = self.make_branch_and_tree('source')
266
self.build_tree(['source/file1'])
268
source.commit('added file')
269
out, err = self.run_bzr('branch source target --files-from source')
270
self.assertPathExists('target/file1')
272
def test_branch_files_from_hardlink(self):
273
self.requireFeature(HardlinkFeature)
274
source = self.make_branch_and_tree('source')
275
self.build_tree(['source/file1'])
277
source.commit('added file')
278
source.controldir.sprout('second')
279
out, err = self.run_bzr('branch source target --files-from second'
281
source_stat = os.stat('source/file1')
282
second_stat = os.stat('second/file1')
283
target_stat = os.stat('target/file1')
284
self.assertNotEqual(source_stat, target_stat)
285
self.assertEqual(second_stat, target_stat)
287
def test_branch_standalone(self):
288
shared_repo = self.make_repository('repo', shared=True)
289
self.example_branch('source')
290
self.run_bzr('branch --standalone source repo/target')
291
b = branch.Branch.open('repo/target')
292
expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
293
self.assertEqual(strip_trailing_slash(b.repository.base),
294
strip_trailing_slash(local_path_to_url(expected_repo_path)))
296
def test_branch_no_tree(self):
297
self.example_branch('source')
298
self.run_bzr('branch --no-tree source target')
299
self.assertPathDoesNotExist('target/hello')
300
self.assertPathDoesNotExist('target/goodbye')
302
def test_branch_into_existing_dir(self):
303
self.example_branch('a')
304
# existing dir with similar files but no .brz dir
305
self.build_tree_contents([('b/',)])
306
self.build_tree_contents([('b/hello', b'bar')]) # different content
307
self.build_tree_contents([('b/goodbye', b'baz')]) # same content
308
# fails without --use-existing-dir
309
out, err = self.run_bzr('branch a b', retcode=3)
310
self.assertEqual('', out)
311
self.assertEqual('brz: ERROR: Target directory "b" already exists.\n',
314
self.run_bzr('branch a b --use-existing-dir')
316
self.assertPathExists('b/hello.moved')
317
self.assertPathDoesNotExist('b/godbye.moved')
318
# we can't branch into branch
319
out, err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
320
self.assertEqual('', out)
321
self.assertEqual('brz: ERROR: Already a branch: "b".\n', err)
323
def test_branch_bind(self):
324
self.example_branch('a')
325
out, err = self.run_bzr('branch a b --bind')
326
self.assertEndsWith(err, "New branch bound to a\n")
327
b = branch.Branch.open('b')
328
self.assertEndsWith(b.get_bound_location(), '/a/')
330
def test_branch_with_post_branch_init_hook(self):
332
branch.Branch.hooks.install_named_hook('post_branch_init',
334
self.assertLength(0, calls)
335
self.example_branch('a')
336
self.assertLength(1, calls)
337
self.run_bzr('branch a b')
338
self.assertLength(2, calls)
340
def test_checkout_with_post_branch_init_hook(self):
342
branch.Branch.hooks.install_named_hook('post_branch_init',
344
self.assertLength(0, calls)
345
self.example_branch('a')
346
self.assertLength(1, calls)
347
self.run_bzr('checkout a b')
348
self.assertLength(2, calls)
350
def test_lightweight_checkout_with_post_branch_init_hook(self):
352
branch.Branch.hooks.install_named_hook('post_branch_init',
354
self.assertLength(0, calls)
355
self.example_branch('a')
356
self.assertLength(1, calls)
357
self.run_bzr('checkout --lightweight a b')
358
self.assertLength(2, calls)
360
def test_branch_fetches_all_tags(self):
361
builder = self.make_branch_builder('source')
362
source, rev1, rev2 = fixtures.build_branch_with_non_ancestral_rev(
364
source.tags.set_tag('tag-a', rev2)
365
source.get_config_stack().set('branch.fetch_tags', True)
366
# Now source has a tag not in its ancestry. Make a branch from it.
367
self.run_bzr('branch source new-branch')
368
new_branch = branch.Branch.open('new-branch')
369
# The tag is present, and so is its revision.
370
self.assertEqual(rev2, new_branch.tags.lookup_tag('tag-a'))
371
new_branch.repository.get_revision(rev2)
373
def test_branch_with_nested_trees(self):
374
orig = self.make_branch_and_tree('source', format='development-subtree')
375
subtree = self.make_branch_and_tree('source/subtree')
376
self.build_tree(['source/subtree/a'])
378
subtree.commit('add subtree contents')
379
orig.add_reference(subtree)
380
orig.set_reference_info('subtree', subtree.branch.user_url)
381
orig.commit('add subtree')
383
self.run_bzr('branch source target')
385
target = WorkingTree.open('target')
386
target_subtree = WorkingTree.open('target/subtree')
387
self.assertTreesEqual(orig, target)
388
self.assertTreesEqual(subtree, target_subtree)
390
def test_branch_with_nested_trees_reference_unset(self):
391
orig = self.make_branch_and_tree('source', format='development-subtree')
392
subtree = self.make_branch_and_tree('source/subtree')
393
self.build_tree(['source/subtree/a'])
395
subtree.commit('add subtree contents')
396
orig.add_reference(subtree)
397
orig.commit('add subtree')
399
self.run_bzr('branch source target')
401
target = WorkingTree.open('target')
402
self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
404
def test_branch_with_nested_trees_no_recurse(self):
405
orig = self.make_branch_and_tree('source', format='development-subtree')
406
subtree = self.make_branch_and_tree('source/subtree')
407
self.build_tree(['source/subtree/a'])
409
subtree.commit('add subtree contents')
410
orig.add_reference(subtree)
411
orig.commit('add subtree')
413
self.run_bzr('branch --no-recurse-nested source target')
415
target = WorkingTree.open('target')
416
self.addCleanup(subtree.lock_read().unlock)
417
basis = subtree.basis_tree()
418
self.addCleanup(basis.lock_read().unlock)
419
self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
422
class TestBranchStacked(tests.TestCaseWithTransport):
423
"""Tests for branch --stacked"""
425
def assertRevisionInRepository(self, repo_path, revid):
426
"""Check that a revision is in a repo, disregarding stacking."""
427
repo = controldir.ControlDir.open(repo_path).open_repository()
428
self.assertTrue(repo.has_revision(revid))
430
def assertRevisionNotInRepository(self, repo_path, revid):
431
"""Check that a revision is not in a repo, disregarding stacking."""
432
repo = controldir.ControlDir.open(repo_path).open_repository()
433
self.assertFalse(repo.has_revision(revid))
435
def assertRevisionsInBranchRepository(self, revid_list, branch_path):
436
repo = branch.Branch.open(branch_path).repository
437
self.assertEqual(set(revid_list),
438
repo.has_revisions(revid_list))
440
def test_branch_stacked_branch_not_stacked(self):
441
"""Branching a stacked branch is not stacked by default"""
443
trunk_tree = self.make_branch_and_tree('target',
445
trunk_tree.commit('mainline')
446
# and a branch from it which is stacked
447
branch_tree = self.make_branch_and_tree('branch',
449
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
450
# with some work on it
451
work_tree = trunk_tree.branch.controldir.sprout(
452
'local').open_workingtree()
453
work_tree.commit('moar work plz')
454
work_tree.branch.push(branch_tree.branch)
455
# branching our local branch gives us a new stacked branch pointing at
457
out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
458
self.assertEqual('', out)
459
self.assertEqual('Branched 2 revisions.\n',
461
# it should have preserved the branch format, and so it should be
462
# capable of supporting stacking, but not actually have a stacked_on
464
self.assertRaises(errors.NotStacked,
465
controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
467
def test_branch_stacked_branch_stacked(self):
468
"""Asking to stack on a stacked branch does work"""
470
trunk_tree = self.make_branch_and_tree('target',
472
trunk_revid = trunk_tree.commit('mainline')
473
# and a branch from it which is stacked
474
branch_tree = self.make_branch_and_tree('branch',
476
branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
477
# with some work on it
478
work_tree = trunk_tree.branch.controldir.sprout(
479
'local').open_workingtree()
480
branch_revid = work_tree.commit('moar work plz')
481
work_tree.branch.push(branch_tree.branch)
482
# you can chain branches on from there
483
out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
484
self.assertEqual('', out)
485
self.assertEqual('Created new stacked branch referring to %s.\n' %
486
branch_tree.branch.base, err)
487
self.assertEqual(branch_tree.branch.base,
488
branch.Branch.open('branch2').get_stacked_on_url())
489
branch2_tree = WorkingTree.open('branch2')
490
branch2_revid = work_tree.commit('work on second stacked branch')
491
work_tree.branch.push(branch2_tree.branch)
492
self.assertRevisionInRepository('branch2', branch2_revid)
493
self.assertRevisionsInBranchRepository(
494
[trunk_revid, branch_revid, branch2_revid],
497
def test_branch_stacked(self):
499
trunk_tree = self.make_branch_and_tree('mainline',
501
original_revid = trunk_tree.commit('mainline')
502
self.assertRevisionInRepository('mainline', original_revid)
503
# and a branch from it which is stacked
504
out, err = self.run_bzr(['branch', '--stacked', 'mainline',
506
self.assertEqual('', out)
507
self.assertEqual('Created new stacked branch referring to %s.\n' %
508
trunk_tree.branch.base, err)
509
self.assertRevisionNotInRepository('newbranch', original_revid)
510
new_branch = branch.Branch.open('newbranch')
511
self.assertEqual(trunk_tree.branch.base,
512
new_branch.get_stacked_on_url())
514
def test_branch_stacked_from_smart_server(self):
515
# We can branch stacking on a smart server
516
self.transport_server = test_server.SmartTCPServer_for_testing
517
trunk = self.make_branch('mainline', format='1.9')
518
out, err = self.run_bzr(
519
['branch', '--stacked', self.get_url('mainline'), 'shallow'])
521
def test_branch_stacked_from_non_stacked_format(self):
522
"""The origin format doesn't support stacking"""
523
trunk = self.make_branch('trunk', format='pack-0.92')
524
out, err = self.run_bzr(
525
['branch', '--stacked', 'trunk', 'shallow'])
526
# We should notify the user that we upgraded their format
527
self.assertEqualDiff(
528
'Source repository format does not support stacking, using format:\n'
529
' Packs 5 (adds stacking support, requires bzr 1.6)\n'
530
'Source branch format does not support stacking, using format:\n'
532
'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
533
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
534
'Created new stacked branch referring to %s.\n' %
538
def test_branch_stacked_from_rich_root_non_stackable(self):
539
trunk = self.make_branch('trunk', format='rich-root-pack')
540
out, err = self.run_bzr(
541
['branch', '--stacked', 'trunk', 'shallow'])
542
# We should notify the user that we upgraded their format
543
self.assertEqualDiff(
544
'Source repository format does not support stacking, using format:\n'
545
' Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
546
'Source branch format does not support stacking, using format:\n'
548
'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
549
'This may take some time. Upgrade the repositories to the same format for better performance.\n'
550
'Created new stacked branch referring to %s.\n' % (trunk.base,),
554
class TestRemoteBranch(TestCaseWithSFTPServer):
557
super(TestRemoteBranch, self).setUp()
558
tree = self.make_branch_and_tree('branch')
559
self.build_tree_contents([('branch/file', b'file content\n')])
561
tree.commit('file created')
563
def test_branch_local_remote(self):
564
self.run_bzr(['branch', 'branch', self.get_url('remote')])
565
t = self.get_transport()
566
# Ensure that no working tree what created remotely
567
self.assertFalse(t.has('remote/file'))
569
def test_branch_remote_remote(self):
570
# Light cheat: we access the branch remotely
571
self.run_bzr(['branch', self.get_url('branch'),
572
self.get_url('remote')])
573
t = self.get_transport()
574
# Ensure that no working tree what created remotely
575
self.assertFalse(t.has('remote/file'))
578
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
580
def _checkout_and_branch(self, option=''):
581
self.script_runner.run_script(self, '''
582
$ brz checkout %(option)s repo/trunk checkout
584
$ brz branch --switch ../repo/trunk ../repo/branched
585
2>Branched 0 revisions.
586
2>Tree is up to date at revision 0.
587
2>Switched to branch:...branched...
590
bound_branch = branch.Branch.open_containing('checkout')[0]
591
master_branch = branch.Branch.open_containing('repo/branched')[0]
592
return (bound_branch, master_branch)
594
def test_branch_switch_parent_lightweight(self):
595
"""Lightweight checkout using brz branch --switch."""
596
bb, mb = self._checkout_and_branch(option='--lightweight')
597
self.assertParent('repo/trunk', bb)
598
self.assertParent('repo/trunk', mb)
600
def test_branch_switch_parent_heavyweight(self):
601
"""Heavyweight checkout using brz branch --switch."""
602
bb, mb = self._checkout_and_branch()
603
self.assertParent('repo/trunk', bb)
604
self.assertParent('repo/trunk', mb)