/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/blackbox/test_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2020-08-22 22:46:24 UTC
  • mfrom: (7490.40.105 work)
  • mto: This revision was merged to the branch mainline in revision 7521.
  • Revision ID: jelmer@jelmer.uk-20200822224624-om4a4idsr7cn8jew
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for bzr branch."""
 
18
"""Black-box tests for brz branch."""
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import (
 
22
from breezy import (
23
23
    branch,
24
 
    bzrdir,
 
24
    controldir,
25
25
    errors,
26
 
    repository,
27
26
    revision as _mod_revision,
28
 
    )
29
 
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
 
from bzrlib.tests.blackbox import ExternalBase
31
 
from bzrlib.tests import (
32
 
    KnownFailure,
 
27
    tests,
 
28
    )
 
29
from breezy.bzr import (
 
30
    bzrdir,
 
31
    )
 
32
from breezy.bzr.knitrepo import RepositoryFormatKnit1
 
33
from breezy.tests import (
 
34
    fixtures,
 
35
    test_server,
 
36
    )
 
37
from breezy.tests.features import (
33
38
    HardlinkFeature,
34
 
    test_server,
35
39
    )
36
 
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
37
 
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
 
from bzrlib.workingtree import WorkingTree
39
 
 
40
 
 
41
 
class TestBranch(ExternalBase):
42
 
 
43
 
    def example_branch(self, path='.'):
44
 
        tree = self.make_branch_and_tree(path)
45
 
        self.build_tree_contents([(path + '/hello', 'foo')])
 
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
 
45
 
 
46
 
 
47
class TestBranch(tests.TestCaseWithTransport):
 
48
 
 
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')])
46
52
        tree.add('hello')
47
53
        tree.commit(message='setup')
48
 
        self.build_tree_contents([(path + '/goodbye', 'baz')])
 
54
        self.build_tree_contents([(path + '/goodbye', b'baz')])
49
55
        tree.add('goodbye')
50
56
        tree.commit(message='setup')
 
57
        return tree
51
58
 
52
59
    def test_branch(self):
53
60
        """Branch from one branch to another."""
57
64
        self.run_bzr('branch a c -r 1')
58
65
        # previously was erroneously created by branching
59
66
        self.assertFalse(b._transport.has('branch-name'))
60
 
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
 
67
        b.controldir.open_workingtree().commit(message='foo', allow_pointless=True)
 
68
 
 
69
    def test_branch_no_to_location(self):
 
70
        """The to_location is derived from the source branch name."""
 
71
        os.mkdir("something")
 
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())
 
76
 
 
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')
 
82
        self.assertEqual(
 
83
            """Created a standalone tree (format: development-colo)\n""",
 
84
            out)
 
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)
 
95
        self.assertEqual(
 
96
            'brz: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
97
 
 
98
    def test_from_colocated(self):
 
99
        """Branch from a colocated branch into a regular branch."""
 
100
        os.mkdir('b')
 
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')
 
108
 
 
109
    def test_from_name(self):
 
110
        """Branch from a colocated branch into a regular branch."""
 
111
        os.mkdir('b')
 
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')
 
119
 
 
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)
 
130
            c = f.read(1)
 
131
            f.seek(-5, os.SEEK_END)
 
132
            # Make sure we inject a value different than the one we just read
 
133
            if c == b'\xFF':
 
134
                corrupt = b'\x00'
 
135
            else:
 
136
                corrupt = b'\xFF'
 
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)
61
140
 
62
141
    def test_branch_switch_no_branch(self):
63
142
        # No branch in the current directory:
65
144
        self.example_branch('a')
66
145
        self.make_repository('current')
67
146
        self.run_bzr_error(['No WorkingTree exists for'],
68
 
            'branch --switch ../a ../b', working_dir='current')
 
147
                           'branch --switch ../a ../b', working_dir='current')
69
148
        a = branch.Branch.open('a')
70
149
        b = branch.Branch.open('b')
71
150
        self.assertEqual(a.last_revision(), b.last_revision())
77
156
        self.example_branch('a')
78
157
        self.make_branch('current')
79
158
        self.run_bzr_error(['No WorkingTree exists for'],
80
 
            'branch --switch ../a ../b', working_dir='current')
 
159
                           'branch --switch ../a ../b', working_dir='current')
81
160
        a = branch.Branch.open('a')
82
161
        b = branch.Branch.open('b')
83
162
        self.assertEqual(a.last_revision(), b.last_revision())
89
168
        #  => new branch will be created, but switch fails and the current
90
169
        #     branch is unmodified
91
170
        self.example_branch('a')
92
 
        self.make_branch_and_tree('current')
 
171
        tree = self.make_branch_and_tree('current')
 
172
        c1 = tree.commit('some diverged change')
93
173
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
94
 
            'branch --switch ../a ../b', working_dir='current')
 
174
                           'branch --switch ../a ../b', working_dir='current')
95
175
        a = branch.Branch.open('a')
96
176
        b = branch.Branch.open('b')
97
177
        self.assertEqual(a.last_revision(), b.last_revision())
98
178
        work = branch.Branch.open('current')
99
 
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
179
        self.assertEqual(work.last_revision(), c1)
 
180
 
 
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()))
100
186
 
101
187
    def test_branch_switch_checkout(self):
102
188
        # Checkout in the current directory:
103
189
        #  => new branch will be created and checkout bound to the new branch
104
190
        self.example_branch('a')
105
191
        self.run_bzr('checkout a current')
106
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
192
        out, err = self.run_bzr('branch --switch ../a ../b',
 
193
                                working_dir='current')
107
194
        a = branch.Branch.open('a')
108
195
        b = branch.Branch.open('b')
109
196
        self.assertEqual(a.last_revision(), b.last_revision())
117
204
        #     the new branch
118
205
        self.example_branch('a')
119
206
        self.run_bzr('checkout --lightweight a current')
120
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
207
        out, err = self.run_bzr('branch --switch ../a ../b',
 
208
                                working_dir='current')
121
209
        a = branch.Branch.open('a')
122
210
        b = branch.Branch.open('b')
123
211
        self.assertEqual(a.last_revision(), b.last_revision())
133
221
        shared_repo.set_make_working_trees(True)
134
222
 
135
223
        def make_shared_tree(path):
136
 
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
224
            shared_repo.controldir.root_transport.mkdir(path)
 
225
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
226
            return WorkingTree.open('repo/' + path)
139
227
        tree_a = make_shared_tree('a')
140
228
        self.build_tree(['repo/a/file'])
141
229
        tree_a.add('file')
142
 
        tree_a.commit('commit a-1', rev_id='a-1')
143
 
        f = open('repo/a/file', 'ab')
144
 
        f.write('more stuff\n')
145
 
        f.close()
146
 
        tree_a.commit('commit a-2', rev_id='a-2')
 
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')
147
234
 
148
235
        tree_b = make_shared_tree('b')
149
236
        self.build_tree(['repo/b/file'])
150
237
        tree_b.add('file')
151
 
        tree_b.commit('commit b-1', rev_id='b-1')
 
238
        tree_b.commit('commit b-1', rev_id=b'b-1')
152
239
 
153
 
        self.assertTrue(shared_repo.has_revision('a-1'))
154
 
        self.assertTrue(shared_repo.has_revision('a-2'))
155
 
        self.assertTrue(shared_repo.has_revision('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'))
156
243
 
157
244
        # Now that we have a repository with shared files, make sure
158
245
        # that things aren't copied out by a 'branch'
159
246
        self.run_bzr('branch repo/b branch-b')
160
247
        pushed_tree = WorkingTree.open('branch-b')
161
248
        pushed_repo = pushed_tree.branch.repository
162
 
        self.assertFalse(pushed_repo.has_revision('a-1'))
163
 
        self.assertFalse(pushed_repo.has_revision('a-2'))
164
 
        self.assertTrue(pushed_repo.has_revision('b-1'))
 
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'))
165
252
 
166
253
    def test_branch_hardlink(self):
167
254
        self.requireFeature(HardlinkFeature)
174
261
        target_stat = os.stat('target/file1')
175
262
        self.assertEqual(source_stat, target_stat)
176
263
 
 
264
    def test_branch_files_from(self):
 
265
        source = self.make_branch_and_tree('source')
 
266
        self.build_tree(['source/file1'])
 
267
        source.add('file1')
 
268
        source.commit('added file')
 
269
        out, err = self.run_bzr('branch source target --files-from source')
 
270
        self.assertPathExists('target/file1')
 
271
 
 
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'])
 
276
        source.add('file1')
 
277
        source.commit('added file')
 
278
        source.controldir.sprout('second')
 
279
        out, err = self.run_bzr('branch source target --files-from second'
 
280
                                ' --hardlink')
 
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)
 
286
 
177
287
    def test_branch_standalone(self):
178
288
        shared_repo = self.make_repository('repo', shared=True)
179
289
        self.example_branch('source')
181
291
        b = branch.Branch.open('repo/target')
182
292
        expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
183
293
        self.assertEqual(strip_trailing_slash(b.repository.base),
184
 
            strip_trailing_slash(local_path_to_url(expected_repo_path)))
 
294
                         strip_trailing_slash(local_path_to_url(expected_repo_path)))
185
295
 
186
296
    def test_branch_no_tree(self):
187
297
        self.example_branch('source')
188
298
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
299
        self.assertPathDoesNotExist('target/hello')
 
300
        self.assertPathDoesNotExist('target/goodbye')
191
301
 
192
302
    def test_branch_into_existing_dir(self):
193
303
        self.example_branch('a')
194
 
        # existing dir with similar files but no .bzr dir
 
304
        # existing dir with similar files but no .brz dir
195
305
        self.build_tree_contents([('b/',)])
196
 
        self.build_tree_contents([('b/hello', 'bar')])  # different content
197
 
        self.build_tree_contents([('b/goodbye', 'baz')])# same content
 
306
        self.build_tree_contents([('b/hello', b'bar')])  # different content
 
307
        self.build_tree_contents([('b/goodbye', b'baz')])  # same content
198
308
        # fails without --use-existing-dir
199
 
        out,err = self.run_bzr('branch a b', retcode=3)
 
309
        out, err = self.run_bzr('branch a b', retcode=3)
200
310
        self.assertEqual('', out)
201
 
        self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
202
 
            err)
 
311
        self.assertEqual('brz: ERROR: Target directory "b" already exists.\n',
 
312
                         err)
203
313
        # force operation
204
314
        self.run_bzr('branch a b --use-existing-dir')
205
315
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
316
        self.assertPathExists('b/hello.moved')
 
317
        self.assertPathDoesNotExist('b/godbye.moved')
208
318
        # we can't branch into branch
209
 
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
 
319
        out, err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
320
        self.assertEqual('', out)
211
 
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
 
321
        self.assertEqual('brz: ERROR: Already a branch: "b".\n', err)
212
322
 
213
323
    def test_branch_bind(self):
214
324
        self.example_branch('a')
220
330
    def test_branch_with_post_branch_init_hook(self):
221
331
        calls = []
222
332
        branch.Branch.hooks.install_named_hook('post_branch_init',
223
 
            calls.append, None)
 
333
                                               calls.append, None)
224
334
        self.assertLength(0, calls)
225
335
        self.example_branch('a')
226
336
        self.assertLength(1, calls)
230
340
    def test_checkout_with_post_branch_init_hook(self):
231
341
        calls = []
232
342
        branch.Branch.hooks.install_named_hook('post_branch_init',
233
 
            calls.append, None)
 
343
                                               calls.append, None)
234
344
        self.assertLength(0, calls)
235
345
        self.example_branch('a')
236
346
        self.assertLength(1, calls)
240
350
    def test_lightweight_checkout_with_post_branch_init_hook(self):
241
351
        calls = []
242
352
        branch.Branch.hooks.install_named_hook('post_branch_init',
243
 
            calls.append, None)
 
353
                                               calls.append, None)
244
354
        self.assertLength(0, calls)
245
355
        self.example_branch('a')
246
356
        self.assertLength(1, calls)
247
357
        self.run_bzr('checkout --lightweight a b')
248
358
        self.assertLength(2, calls)
249
359
 
250
 
 
251
 
class TestBranchStacked(ExternalBase):
 
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(
 
363
            builder)
 
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)
 
372
 
 
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'])
 
377
        subtree.add(['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')
 
382
 
 
383
        self.run_bzr('branch source target')
 
384
 
 
385
        target = WorkingTree.open('target')
 
386
        target_subtree = WorkingTree.open('target/subtree')
 
387
        self.assertTreesEqual(orig, target)
 
388
        self.assertTreesEqual(subtree, target_subtree)
 
389
 
 
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'])
 
394
        subtree.add(['a'])
 
395
        subtree.commit('add subtree contents')
 
396
        orig.add_reference(subtree)
 
397
        orig.commit('add subtree')
 
398
 
 
399
        self.run_bzr('branch source target')
 
400
 
 
401
        target = WorkingTree.open('target')
 
402
        self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
 
403
 
 
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'])
 
408
        subtree.add(['a'])
 
409
        subtree.commit('add subtree contents')
 
410
        orig.add_reference(subtree)
 
411
        orig.commit('add subtree')
 
412
 
 
413
        self.run_bzr('branch --no-recurse-nested source target')
 
414
 
 
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')
 
420
 
 
421
 
 
422
class TestBranchStacked(tests.TestCaseWithTransport):
252
423
    """Tests for branch --stacked"""
253
424
 
254
425
    def assertRevisionInRepository(self, repo_path, revid):
255
 
        """Check that a revision is in a repository, disregarding stacking."""
256
 
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
426
        """Check that a revision is in a repo, disregarding stacking."""
 
427
        repo = controldir.ControlDir.open(repo_path).open_repository()
257
428
        self.assertTrue(repo.has_revision(revid))
258
429
 
259
430
    def assertRevisionNotInRepository(self, repo_path, revid):
260
 
        """Check that a revision is not in a repository, disregarding stacking."""
261
 
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
431
        """Check that a revision is not in a repo, disregarding stacking."""
 
432
        repo = controldir.ControlDir.open(repo_path).open_repository()
262
433
        self.assertFalse(repo.has_revision(revid))
263
434
 
264
435
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
265
436
        repo = branch.Branch.open(branch_path).repository
266
437
        self.assertEqual(set(revid_list),
267
 
            repo.has_revisions(revid_list))
 
438
                         repo.has_revisions(revid_list))
268
439
 
269
440
    def test_branch_stacked_branch_not_stacked(self):
270
441
        """Branching a stacked branch is not stacked by default"""
271
442
        # We have a mainline
272
443
        trunk_tree = self.make_branch_and_tree('target',
273
 
            format='1.9')
 
444
                                               format='1.9')
274
445
        trunk_tree.commit('mainline')
275
446
        # and a branch from it which is stacked
276
447
        branch_tree = self.make_branch_and_tree('branch',
277
 
            format='1.9')
 
448
                                                format='1.9')
278
449
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
279
450
        # with some work on it
280
 
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
451
        work_tree = trunk_tree.branch.controldir.sprout(
 
452
            'local').open_workingtree()
281
453
        work_tree.commit('moar work plz')
282
454
        work_tree.branch.push(branch_tree.branch)
283
455
        # branching our local branch gives us a new stacked branch pointing at
284
456
        # mainline.
285
457
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
286
458
        self.assertEqual('', out)
287
 
        self.assertEqual('Branched 2 revision(s).\n',
288
 
            err)
 
459
        self.assertEqual('Branched 2 revisions.\n',
 
460
                         err)
289
461
        # it should have preserved the branch format, and so it should be
290
462
        # capable of supporting stacking, but not actually have a stacked_on
291
463
        # branch configured
292
464
        self.assertRaises(errors.NotStacked,
293
 
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
 
465
                          controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
294
466
 
295
467
    def test_branch_stacked_branch_stacked(self):
296
468
        """Asking to stack on a stacked branch does work"""
297
469
        # We have a mainline
298
470
        trunk_tree = self.make_branch_and_tree('target',
299
 
            format='1.9')
 
471
                                               format='1.9')
300
472
        trunk_revid = trunk_tree.commit('mainline')
301
473
        # and a branch from it which is stacked
302
474
        branch_tree = self.make_branch_and_tree('branch',
303
 
            format='1.9')
 
475
                                                format='1.9')
304
476
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
305
477
        # with some work on it
306
 
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
478
        work_tree = trunk_tree.branch.controldir.sprout(
 
479
            'local').open_workingtree()
307
480
        branch_revid = work_tree.commit('moar work plz')
308
481
        work_tree.branch.push(branch_tree.branch)
309
482
        # you can chain branches on from there
310
483
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
311
484
        self.assertEqual('', out)
312
485
        self.assertEqual('Created new stacked branch referring to %s.\n' %
313
 
            branch_tree.branch.base, err)
 
486
                         branch_tree.branch.base, err)
314
487
        self.assertEqual(branch_tree.branch.base,
315
 
            branch.Branch.open('branch2').get_stacked_on_url())
 
488
                         branch.Branch.open('branch2').get_stacked_on_url())
316
489
        branch2_tree = WorkingTree.open('branch2')
317
490
        branch2_revid = work_tree.commit('work on second stacked branch')
318
491
        work_tree.branch.push(branch2_tree.branch)
324
497
    def test_branch_stacked(self):
325
498
        # We have a mainline
326
499
        trunk_tree = self.make_branch_and_tree('mainline',
327
 
            format='1.9')
 
500
                                               format='1.9')
328
501
        original_revid = trunk_tree.commit('mainline')
329
502
        self.assertRevisionInRepository('mainline', original_revid)
330
503
        # and a branch from it which is stacked
331
504
        out, err = self.run_bzr(['branch', '--stacked', 'mainline',
332
 
            'newbranch'])
 
505
                                 'newbranch'])
333
506
        self.assertEqual('', out)
334
507
        self.assertEqual('Created new stacked branch referring to %s.\n' %
335
 
            trunk_tree.branch.base, err)
 
508
                         trunk_tree.branch.base, err)
336
509
        self.assertRevisionNotInRepository('newbranch', original_revid)
337
510
        new_branch = branch.Branch.open('newbranch')
338
 
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
511
        self.assertEqual(trunk_tree.branch.base,
 
512
                         new_branch.get_stacked_on_url())
339
513
 
340
514
    def test_branch_stacked_from_smart_server(self):
341
515
        # We can branch stacking on a smart server
357
531
            '  Branch format 7\n'
358
532
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
359
533
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
360
 
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
 
534
            'Created new stacked branch referring to %s.\n' %
 
535
            (trunk.base,),
361
536
            err)
362
537
 
363
538
    def test_branch_stacked_from_rich_root_non_stackable(self):
376
551
            err)
377
552
 
378
553
 
379
 
class TestSmartServerBranching(ExternalBase):
380
 
 
381
 
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
382
 
        self.setup_smart_server_with_call_log()
383
 
        t = self.make_branch_and_tree('from')
384
 
        for count in range(9):
385
 
            t.commit(message='commit %d' % count)
386
 
        self.reset_smart_call_log()
387
 
        out, err = self.run_bzr(['branch', self.get_url('from'),
388
 
            self.get_url('target')])
389
 
        # This figure represent the amount of work to perform this use case. It
390
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
391
 
        # being too low. If rpc_count increases, more network roundtrips have
392
 
        # become necessary for this use case. Please do not adjust this number
393
 
        # upwards without agreement from bzr's network support maintainers.
394
 
        self.assertLength(38, self.hpss_calls)
395
 
 
396
 
    def test_branch_from_trivial_branch_streaming_acceptance(self):
397
 
        self.setup_smart_server_with_call_log()
398
 
        t = self.make_branch_and_tree('from')
399
 
        for count in range(9):
400
 
            t.commit(message='commit %d' % count)
401
 
        self.reset_smart_call_log()
402
 
        out, err = self.run_bzr(['branch', self.get_url('from'),
403
 
            'local-target'])
404
 
        # This figure represent the amount of work to perform this use case. It
405
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
406
 
        # being too low. If rpc_count increases, more network roundtrips have
407
 
        # become necessary for this use case. Please do not adjust this number
408
 
        # upwards without agreement from bzr's network support maintainers.
409
 
        self.assertLength(10, self.hpss_calls)
410
 
 
411
 
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
412
 
        self.setup_smart_server_with_call_log()
413
 
        t = self.make_branch_and_tree('trunk')
414
 
        for count in range(8):
415
 
            t.commit(message='commit %d' % count)
416
 
        tree2 = t.branch.bzrdir.sprout('feature', stacked=True
417
 
            ).open_workingtree()
418
 
        local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
419
 
        local_tree.commit('feature change')
420
 
        local_tree.branch.push(tree2.branch)
421
 
        self.reset_smart_call_log()
422
 
        out, err = self.run_bzr(['branch', self.get_url('feature'),
423
 
            'local-target'])
424
 
        # This figure represent the amount of work to perform this use case. It
425
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
426
 
        # being too low. If rpc_count increases, more network roundtrips have
427
 
        # become necessary for this use case. Please do not adjust this number
428
 
        # upwards without agreement from bzr's network support maintainers.
429
 
        self.assertLength(15, self.hpss_calls)
430
 
 
431
 
 
432
554
class TestRemoteBranch(TestCaseWithSFTPServer):
433
555
 
434
556
    def setUp(self):
435
557
        super(TestRemoteBranch, self).setUp()
436
558
        tree = self.make_branch_and_tree('branch')
437
 
        self.build_tree_contents([('branch/file', 'file content\n')])
 
559
        self.build_tree_contents([('branch/file', b'file content\n')])
438
560
        tree.add('file')
439
561
        tree.commit('file created')
440
562
 
452
574
        # Ensure that no working tree what created remotely
453
575
        self.assertFalse(t.has('remote/file'))
454
576
 
 
577
 
 
578
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
579
 
 
580
    def _checkout_and_branch(self, option=''):
 
581
        self.script_runner.run_script(self, '''
 
582
                $ brz checkout %(option)s repo/trunk checkout
 
583
                $ cd 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...
 
588
                $ cd ..
 
589
                ''' % locals())
 
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)
 
593
 
 
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)
 
599
 
 
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)