/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-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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.matchers import ContainsNoVfsCalls
 
42
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
 
43
from breezy.tests.script import run_script
 
44
from breezy.urlutils import local_path_to_url, strip_trailing_slash
 
45
from breezy.workingtree import WorkingTree
 
46
 
 
47
 
 
48
class TestBranch(tests.TestCaseWithTransport):
 
49
 
 
50
    def example_branch(self, path='.', format=None):
 
51
        tree = self.make_branch_and_tree(path, format=format)
 
52
        self.build_tree_contents([(path + '/hello', b'foo')])
46
53
        tree.add('hello')
47
54
        tree.commit(message='setup')
48
 
        self.build_tree_contents([(path + '/goodbye', 'baz')])
 
55
        self.build_tree_contents([(path + '/goodbye', b'baz')])
49
56
        tree.add('goodbye')
50
57
        tree.commit(message='setup')
 
58
        return tree
51
59
 
52
60
    def test_branch(self):
53
61
        """Branch from one branch to another."""
57
65
        self.run_bzr('branch a c -r 1')
58
66
        # previously was erroneously created by branching
59
67
        self.assertFalse(b._transport.has('branch-name'))
60
 
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
 
68
        b.controldir.open_workingtree().commit(message='foo', allow_pointless=True)
 
69
 
 
70
    def test_branch_no_to_location(self):
 
71
        """The to_location is derived from the source branch name."""
 
72
        os.mkdir("something")
 
73
        a = self.example_branch('something/a').branch
 
74
        self.run_bzr('branch something/a')
 
75
        b = branch.Branch.open('a')
 
76
        self.assertEqual(b.last_revision_info(), a.last_revision_info())
 
77
 
 
78
    def test_into_colocated(self):
 
79
        """Branch from a branch into a colocated branch."""
 
80
        self.example_branch('a')
 
81
        out, err = self.run_bzr(
 
82
            'init --format=development-colo file:b,branch=orig')
 
83
        self.assertEqual(
 
84
            """Created a standalone tree (format: development-colo)\n""",
 
85
            out)
 
86
        self.assertEqual('', err)
 
87
        out, err = self.run_bzr(
 
88
            'branch a file:b,branch=thiswasa')
 
89
        self.assertEqual('', out)
 
90
        self.assertEqual('Branched 2 revisions.\n', err)
 
91
        out, err = self.run_bzr('branches b')
 
92
        self.assertEqual("  orig\n  thiswasa\n", out)
 
93
        self.assertEqual('', err)
 
94
        out, err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
95
        self.assertEqual('', out)
 
96
        self.assertEqual(
 
97
            'brz: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
98
 
 
99
    def test_from_colocated(self):
 
100
        """Branch from a colocated branch into a regular branch."""
 
101
        os.mkdir('b')
 
102
        tree = self.example_branch('b/a', format='development-colo')
 
103
        tree.controldir.create_branch(name='somecolo')
 
104
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
105
                                local_path_to_url('b/a'))
 
106
        self.assertEqual('', out)
 
107
        self.assertEqual('Branched 0 revisions.\n', err)
 
108
        self.assertPathExists('a')
 
109
 
 
110
    def test_from_name(self):
 
111
        """Branch from a colocated branch into a regular branch."""
 
112
        os.mkdir('b')
 
113
        tree = self.example_branch('b/a', format='development-colo')
 
114
        tree.controldir.create_branch(name='somecolo')
 
115
        out, err = self.run_bzr('branch -b somecolo %s' %
 
116
                                local_path_to_url('b/a'))
 
117
        self.assertEqual('', out)
 
118
        self.assertEqual('Branched 0 revisions.\n', err)
 
119
        self.assertPathExists('a')
 
120
 
 
121
    def test_branch_broken_pack(self):
 
122
        """branching with a corrupted pack file."""
 
123
        self.example_branch('a')
 
124
        # add some corruption
 
125
        packs_dir = 'a/.bzr/repository/packs/'
 
126
        fname = packs_dir + os.listdir(packs_dir)[0]
 
127
        with open(fname, 'rb+') as f:
 
128
            # Start from the end of the file to avoid choosing a place bigger
 
129
            # than the file itself.
 
130
            f.seek(-5, os.SEEK_END)
 
131
            c = f.read(1)
 
132
            f.seek(-5, os.SEEK_END)
 
133
            # Make sure we inject a value different than the one we just read
 
134
            if c == b'\xFF':
 
135
                corrupt = b'\x00'
 
136
            else:
 
137
                corrupt = b'\xFF'
 
138
            f.write(corrupt)  # make sure we corrupt something
 
139
        self.run_bzr_error(['Corruption while decompressing repository file'],
 
140
                           'branch a b', retcode=3)
61
141
 
62
142
    def test_branch_switch_no_branch(self):
63
143
        # No branch in the current directory:
65
145
        self.example_branch('a')
66
146
        self.make_repository('current')
67
147
        self.run_bzr_error(['No WorkingTree exists for'],
68
 
            'branch --switch ../a ../b', working_dir='current')
 
148
                           'branch --switch ../a ../b', working_dir='current')
69
149
        a = branch.Branch.open('a')
70
150
        b = branch.Branch.open('b')
71
151
        self.assertEqual(a.last_revision(), b.last_revision())
77
157
        self.example_branch('a')
78
158
        self.make_branch('current')
79
159
        self.run_bzr_error(['No WorkingTree exists for'],
80
 
            'branch --switch ../a ../b', working_dir='current')
 
160
                           'branch --switch ../a ../b', working_dir='current')
81
161
        a = branch.Branch.open('a')
82
162
        b = branch.Branch.open('b')
83
163
        self.assertEqual(a.last_revision(), b.last_revision())
89
169
        #  => new branch will be created, but switch fails and the current
90
170
        #     branch is unmodified
91
171
        self.example_branch('a')
92
 
        self.make_branch_and_tree('current')
 
172
        tree = self.make_branch_and_tree('current')
 
173
        c1 = tree.commit('some diverged change')
93
174
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
94
 
            'branch --switch ../a ../b', working_dir='current')
 
175
                           'branch --switch ../a ../b', working_dir='current')
95
176
        a = branch.Branch.open('a')
96
177
        b = branch.Branch.open('b')
97
178
        self.assertEqual(a.last_revision(), b.last_revision())
98
179
        work = branch.Branch.open('current')
99
 
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
180
        self.assertEqual(work.last_revision(), c1)
 
181
 
 
182
    def test_branch_into_empty_dir(self):
 
183
        t = self.example_branch('source')
 
184
        self.make_controldir('target')
 
185
        self.run_bzr("branch source target")
 
186
        self.assertEqual(2, len(t.branch.repository.all_revision_ids()))
100
187
 
101
188
    def test_branch_switch_checkout(self):
102
189
        # Checkout in the current directory:
103
190
        #  => new branch will be created and checkout bound to the new branch
104
191
        self.example_branch('a')
105
192
        self.run_bzr('checkout a current')
106
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
193
        out, err = self.run_bzr('branch --switch ../a ../b',
 
194
                                working_dir='current')
107
195
        a = branch.Branch.open('a')
108
196
        b = branch.Branch.open('b')
109
197
        self.assertEqual(a.last_revision(), b.last_revision())
117
205
        #     the new branch
118
206
        self.example_branch('a')
119
207
        self.run_bzr('checkout --lightweight a current')
120
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
208
        out, err = self.run_bzr('branch --switch ../a ../b',
 
209
                                working_dir='current')
121
210
        a = branch.Branch.open('a')
122
211
        b = branch.Branch.open('b')
123
212
        self.assertEqual(a.last_revision(), b.last_revision())
133
222
        shared_repo.set_make_working_trees(True)
134
223
 
135
224
        def make_shared_tree(path):
136
 
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
225
            shared_repo.controldir.root_transport.mkdir(path)
 
226
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
227
            return WorkingTree.open('repo/' + path)
139
228
        tree_a = make_shared_tree('a')
140
229
        self.build_tree(['repo/a/file'])
141
230
        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')
 
231
        tree_a.commit('commit a-1', rev_id=b'a-1')
 
232
        with open('repo/a/file', 'ab') as f:
 
233
            f.write(b'more stuff\n')
 
234
        tree_a.commit('commit a-2', rev_id=b'a-2')
147
235
 
148
236
        tree_b = make_shared_tree('b')
149
237
        self.build_tree(['repo/b/file'])
150
238
        tree_b.add('file')
151
 
        tree_b.commit('commit b-1', rev_id='b-1')
 
239
        tree_b.commit('commit b-1', rev_id=b'b-1')
152
240
 
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'))
 
241
        self.assertTrue(shared_repo.has_revision(b'a-1'))
 
242
        self.assertTrue(shared_repo.has_revision(b'a-2'))
 
243
        self.assertTrue(shared_repo.has_revision(b'b-1'))
156
244
 
157
245
        # Now that we have a repository with shared files, make sure
158
246
        # that things aren't copied out by a 'branch'
159
247
        self.run_bzr('branch repo/b branch-b')
160
248
        pushed_tree = WorkingTree.open('branch-b')
161
249
        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'))
 
250
        self.assertFalse(pushed_repo.has_revision(b'a-1'))
 
251
        self.assertFalse(pushed_repo.has_revision(b'a-2'))
 
252
        self.assertTrue(pushed_repo.has_revision(b'b-1'))
165
253
 
166
254
    def test_branch_hardlink(self):
167
255
        self.requireFeature(HardlinkFeature)
174
262
        target_stat = os.stat('target/file1')
175
263
        self.assertEqual(source_stat, target_stat)
176
264
 
 
265
    def test_branch_files_from(self):
 
266
        source = self.make_branch_and_tree('source')
 
267
        self.build_tree(['source/file1'])
 
268
        source.add('file1')
 
269
        source.commit('added file')
 
270
        out, err = self.run_bzr('branch source target --files-from source')
 
271
        self.assertPathExists('target/file1')
 
272
 
 
273
    def test_branch_files_from_hardlink(self):
 
274
        self.requireFeature(HardlinkFeature)
 
275
        source = self.make_branch_and_tree('source')
 
276
        self.build_tree(['source/file1'])
 
277
        source.add('file1')
 
278
        source.commit('added file')
 
279
        source.controldir.sprout('second')
 
280
        out, err = self.run_bzr('branch source target --files-from second'
 
281
                                ' --hardlink')
 
282
        source_stat = os.stat('source/file1')
 
283
        second_stat = os.stat('second/file1')
 
284
        target_stat = os.stat('target/file1')
 
285
        self.assertNotEqual(source_stat, target_stat)
 
286
        self.assertEqual(second_stat, target_stat)
 
287
 
177
288
    def test_branch_standalone(self):
178
289
        shared_repo = self.make_repository('repo', shared=True)
179
290
        self.example_branch('source')
181
292
        b = branch.Branch.open('repo/target')
182
293
        expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
183
294
        self.assertEqual(strip_trailing_slash(b.repository.base),
184
 
            strip_trailing_slash(local_path_to_url(expected_repo_path)))
 
295
                         strip_trailing_slash(local_path_to_url(expected_repo_path)))
185
296
 
186
297
    def test_branch_no_tree(self):
187
298
        self.example_branch('source')
188
299
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
300
        self.assertPathDoesNotExist('target/hello')
 
301
        self.assertPathDoesNotExist('target/goodbye')
191
302
 
192
303
    def test_branch_into_existing_dir(self):
193
304
        self.example_branch('a')
194
 
        # existing dir with similar files but no .bzr dir
 
305
        # existing dir with similar files but no .brz dir
195
306
        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
 
307
        self.build_tree_contents([('b/hello', b'bar')])  # different content
 
308
        self.build_tree_contents([('b/goodbye', b'baz')])  # same content
198
309
        # fails without --use-existing-dir
199
 
        out,err = self.run_bzr('branch a b', retcode=3)
 
310
        out, err = self.run_bzr('branch a b', retcode=3)
200
311
        self.assertEqual('', out)
201
 
        self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
202
 
            err)
 
312
        self.assertEqual('brz: ERROR: Target directory "b" already exists.\n',
 
313
                         err)
203
314
        # force operation
204
315
        self.run_bzr('branch a b --use-existing-dir')
205
316
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
317
        self.assertPathExists('b/hello.moved')
 
318
        self.assertPathDoesNotExist('b/godbye.moved')
208
319
        # we can't branch into branch
209
 
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
 
320
        out, err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
321
        self.assertEqual('', out)
211
 
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
 
322
        self.assertEqual('brz: ERROR: Already a branch: "b".\n', err)
212
323
 
213
324
    def test_branch_bind(self):
214
325
        self.example_branch('a')
220
331
    def test_branch_with_post_branch_init_hook(self):
221
332
        calls = []
222
333
        branch.Branch.hooks.install_named_hook('post_branch_init',
223
 
            calls.append, None)
 
334
                                               calls.append, None)
224
335
        self.assertLength(0, calls)
225
336
        self.example_branch('a')
226
337
        self.assertLength(1, calls)
230
341
    def test_checkout_with_post_branch_init_hook(self):
231
342
        calls = []
232
343
        branch.Branch.hooks.install_named_hook('post_branch_init',
233
 
            calls.append, None)
 
344
                                               calls.append, None)
234
345
        self.assertLength(0, calls)
235
346
        self.example_branch('a')
236
347
        self.assertLength(1, calls)
240
351
    def test_lightweight_checkout_with_post_branch_init_hook(self):
241
352
        calls = []
242
353
        branch.Branch.hooks.install_named_hook('post_branch_init',
243
 
            calls.append, None)
 
354
                                               calls.append, None)
244
355
        self.assertLength(0, calls)
245
356
        self.example_branch('a')
246
357
        self.assertLength(1, calls)
247
358
        self.run_bzr('checkout --lightweight a b')
248
359
        self.assertLength(2, calls)
249
360
 
250
 
 
251
 
class TestBranchStacked(ExternalBase):
 
361
    def test_branch_fetches_all_tags(self):
 
362
        builder = self.make_branch_builder('source')
 
363
        source, rev1, rev2 = fixtures.build_branch_with_non_ancestral_rev(
 
364
            builder)
 
365
        source.tags.set_tag('tag-a', rev2)
 
366
        source.get_config_stack().set('branch.fetch_tags', True)
 
367
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
368
        self.run_bzr('branch source new-branch')
 
369
        new_branch = branch.Branch.open('new-branch')
 
370
        # The tag is present, and so is its revision.
 
371
        self.assertEqual(rev2, new_branch.tags.lookup_tag('tag-a'))
 
372
        new_branch.repository.get_revision(rev2)
 
373
 
 
374
    def test_branch_with_nested_trees(self):
 
375
        orig = self.make_branch_and_tree('source', format='development-subtree')
 
376
        subtree = self.make_branch_and_tree('source/subtree')
 
377
        self.build_tree(['source/subtree/a'])
 
378
        subtree.add(['a'])
 
379
        subtree.commit('add subtree contents')
 
380
        orig.add_reference(subtree)
 
381
        orig.set_reference_info('subtree', subtree.branch.user_url)
 
382
        orig.commit('add subtree')
 
383
 
 
384
        self.run_bzr('branch source target')
 
385
 
 
386
        target = WorkingTree.open('target')
 
387
        target_subtree = WorkingTree.open('target/subtree')
 
388
        self.assertTreesEqual(orig, target)
 
389
        self.assertTreesEqual(subtree, target_subtree)
 
390
 
 
391
    def test_branch_with_nested_trees_reference_unset(self):
 
392
        orig = self.make_branch_and_tree('source', format='development-subtree')
 
393
        subtree = self.make_branch_and_tree('source/subtree')
 
394
        self.build_tree(['source/subtree/a'])
 
395
        subtree.add(['a'])
 
396
        subtree.commit('add subtree contents')
 
397
        orig.add_reference(subtree)
 
398
        orig.commit('add subtree')
 
399
 
 
400
        self.run_bzr('branch source target')
 
401
 
 
402
        target = WorkingTree.open('target')
 
403
        self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
 
404
 
 
405
    def test_branch_with_nested_trees_no_recurse(self):
 
406
        orig = self.make_branch_and_tree('source', format='development-subtree')
 
407
        subtree = self.make_branch_and_tree('source/subtree')
 
408
        self.build_tree(['source/subtree/a'])
 
409
        subtree.add(['a'])
 
410
        subtree.commit('add subtree contents')
 
411
        orig.add_reference(subtree)
 
412
        orig.commit('add subtree')
 
413
 
 
414
        self.run_bzr('branch --no-recurse-nested source target')
 
415
 
 
416
        target = WorkingTree.open('target')
 
417
        self.addCleanup(subtree.lock_read().unlock)
 
418
        basis = subtree.basis_tree()
 
419
        self.addCleanup(basis.lock_read().unlock)
 
420
        self.assertRaises(errors.NotBranchError, WorkingTree.open, 'target/subtree')
 
421
 
 
422
 
 
423
class TestBranchStacked(tests.TestCaseWithTransport):
252
424
    """Tests for branch --stacked"""
253
425
 
254
426
    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()
 
427
        """Check that a revision is in a repo, disregarding stacking."""
 
428
        repo = controldir.ControlDir.open(repo_path).open_repository()
257
429
        self.assertTrue(repo.has_revision(revid))
258
430
 
259
431
    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()
 
432
        """Check that a revision is not in a repo, disregarding stacking."""
 
433
        repo = controldir.ControlDir.open(repo_path).open_repository()
262
434
        self.assertFalse(repo.has_revision(revid))
263
435
 
264
436
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
265
437
        repo = branch.Branch.open(branch_path).repository
266
438
        self.assertEqual(set(revid_list),
267
 
            repo.has_revisions(revid_list))
 
439
                         repo.has_revisions(revid_list))
268
440
 
269
441
    def test_branch_stacked_branch_not_stacked(self):
270
442
        """Branching a stacked branch is not stacked by default"""
271
443
        # We have a mainline
272
444
        trunk_tree = self.make_branch_and_tree('target',
273
 
            format='1.9')
 
445
                                               format='1.9')
274
446
        trunk_tree.commit('mainline')
275
447
        # and a branch from it which is stacked
276
448
        branch_tree = self.make_branch_and_tree('branch',
277
 
            format='1.9')
 
449
                                                format='1.9')
278
450
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
279
451
        # with some work on it
280
 
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
452
        work_tree = trunk_tree.branch.controldir.sprout(
 
453
            'local').open_workingtree()
281
454
        work_tree.commit('moar work plz')
282
455
        work_tree.branch.push(branch_tree.branch)
283
456
        # branching our local branch gives us a new stacked branch pointing at
284
457
        # mainline.
285
458
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
286
459
        self.assertEqual('', out)
287
 
        self.assertEqual('Branched 2 revision(s).\n',
288
 
            err)
 
460
        self.assertEqual('Branched 2 revisions.\n',
 
461
                         err)
289
462
        # it should have preserved the branch format, and so it should be
290
463
        # capable of supporting stacking, but not actually have a stacked_on
291
464
        # branch configured
292
465
        self.assertRaises(errors.NotStacked,
293
 
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
 
466
                          controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
294
467
 
295
468
    def test_branch_stacked_branch_stacked(self):
296
469
        """Asking to stack on a stacked branch does work"""
297
470
        # We have a mainline
298
471
        trunk_tree = self.make_branch_and_tree('target',
299
 
            format='1.9')
 
472
                                               format='1.9')
300
473
        trunk_revid = trunk_tree.commit('mainline')
301
474
        # and a branch from it which is stacked
302
475
        branch_tree = self.make_branch_and_tree('branch',
303
 
            format='1.9')
 
476
                                                format='1.9')
304
477
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
305
478
        # with some work on it
306
 
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
479
        work_tree = trunk_tree.branch.controldir.sprout(
 
480
            'local').open_workingtree()
307
481
        branch_revid = work_tree.commit('moar work plz')
308
482
        work_tree.branch.push(branch_tree.branch)
309
483
        # you can chain branches on from there
310
484
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
311
485
        self.assertEqual('', out)
312
486
        self.assertEqual('Created new stacked branch referring to %s.\n' %
313
 
            branch_tree.branch.base, err)
 
487
                         branch_tree.branch.base, err)
314
488
        self.assertEqual(branch_tree.branch.base,
315
 
            branch.Branch.open('branch2').get_stacked_on_url())
 
489
                         branch.Branch.open('branch2').get_stacked_on_url())
316
490
        branch2_tree = WorkingTree.open('branch2')
317
491
        branch2_revid = work_tree.commit('work on second stacked branch')
318
492
        work_tree.branch.push(branch2_tree.branch)
324
498
    def test_branch_stacked(self):
325
499
        # We have a mainline
326
500
        trunk_tree = self.make_branch_and_tree('mainline',
327
 
            format='1.9')
 
501
                                               format='1.9')
328
502
        original_revid = trunk_tree.commit('mainline')
329
503
        self.assertRevisionInRepository('mainline', original_revid)
330
504
        # and a branch from it which is stacked
331
505
        out, err = self.run_bzr(['branch', '--stacked', 'mainline',
332
 
            'newbranch'])
 
506
                                 'newbranch'])
333
507
        self.assertEqual('', out)
334
508
        self.assertEqual('Created new stacked branch referring to %s.\n' %
335
 
            trunk_tree.branch.base, err)
 
509
                         trunk_tree.branch.base, err)
336
510
        self.assertRevisionNotInRepository('newbranch', original_revid)
337
511
        new_branch = branch.Branch.open('newbranch')
338
 
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
512
        self.assertEqual(trunk_tree.branch.base,
 
513
                         new_branch.get_stacked_on_url())
339
514
 
340
515
    def test_branch_stacked_from_smart_server(self):
341
516
        # We can branch stacking on a smart server
357
532
            '  Branch format 7\n'
358
533
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
359
534
            '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,),
 
535
            'Created new stacked branch referring to %s.\n' %
 
536
            (trunk.base,),
361
537
            err)
362
538
 
363
539
    def test_branch_stacked_from_rich_root_non_stackable(self):
376
552
            err)
377
553
 
378
554
 
379
 
class TestSmartServerBranching(ExternalBase):
 
555
class TestSmartServerBranching(tests.TestCaseWithTransport):
380
556
 
381
557
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
382
558
        self.setup_smart_server_with_call_log()
385
561
            t.commit(message='commit %d' % count)
386
562
        self.reset_smart_call_log()
387
563
        out, err = self.run_bzr(['branch', self.get_url('from'),
388
 
            self.get_url('target')])
 
564
                                 self.get_url('target')])
389
565
        # This figure represent the amount of work to perform this use case. It
390
566
        # is entirely ok to reduce this number if a test fails due to rpc_count
391
567
        # being too low. If rpc_count increases, more network roundtrips have
392
568
        # become necessary for this use case. Please do not adjust this number
393
569
        # upwards without agreement from bzr's network support maintainers.
394
 
        self.assertLength(38, self.hpss_calls)
 
570
        self.assertLength(2, self.hpss_connections)
 
571
        self.assertLength(34, self.hpss_calls)
 
572
        self.expectFailure(
 
573
            "branching to the same branch requires VFS access",
 
574
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
395
575
 
396
576
    def test_branch_from_trivial_branch_streaming_acceptance(self):
397
577
        self.setup_smart_server_with_call_log()
400
580
            t.commit(message='commit %d' % count)
401
581
        self.reset_smart_call_log()
402
582
        out, err = self.run_bzr(['branch', self.get_url('from'),
403
 
            'local-target'])
 
583
                                 'local-target'])
404
584
        # This figure represent the amount of work to perform this use case. It
405
585
        # is entirely ok to reduce this number if a test fails due to rpc_count
406
586
        # being too low. If rpc_count increases, more network roundtrips have
407
587
        # become necessary for this use case. Please do not adjust this number
408
588
        # upwards without agreement from bzr's network support maintainers.
409
 
        self.assertLength(10, self.hpss_calls)
 
589
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
590
        self.assertLength(11, self.hpss_calls)
 
591
        self.assertLength(1, self.hpss_connections)
410
592
 
411
593
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
412
594
        self.setup_smart_server_with_call_log()
413
595
        t = self.make_branch_and_tree('trunk')
414
596
        for count in range(8):
415
597
            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()
 
598
        tree2 = t.branch.controldir.sprout('feature', stacked=True
 
599
                                           ).open_workingtree()
 
600
        local_tree = t.branch.controldir.sprout(
 
601
            'local-working').open_workingtree()
419
602
        local_tree.commit('feature change')
420
603
        local_tree.branch.push(tree2.branch)
421
604
        self.reset_smart_call_log()
422
605
        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)
 
606
                                 'local-target'])
 
607
        # This figure represent the amount of work to perform this use case. It
 
608
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
609
        # being too low. If rpc_count increases, more network roundtrips have
 
610
        # become necessary for this use case. Please do not adjust this number
 
611
        # upwards without agreement from bzr's network support maintainers.
 
612
        self.assertLength(16, self.hpss_calls)
 
613
        self.assertLength(1, self.hpss_connections)
 
614
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
615
 
 
616
    def test_branch_from_branch_with_tags(self):
 
617
        self.setup_smart_server_with_call_log()
 
618
        builder = self.make_branch_builder('source')
 
619
        source, rev1, rev2 = fixtures.build_branch_with_non_ancestral_rev(
 
620
            builder)
 
621
        source.get_config_stack().set('branch.fetch_tags', True)
 
622
        source.tags.set_tag('tag-a', rev2)
 
623
        source.tags.set_tag('tag-missing', b'missing-rev')
 
624
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
625
        self.reset_smart_call_log()
 
626
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
627
        # This figure represent the amount of work to perform this use case. It
 
628
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
629
        # being too low. If rpc_count increases, more network roundtrips have
 
630
        # become necessary for this use case. Please do not adjust this number
 
631
        # upwards without agreement from bzr's network support maintainers.
 
632
        self.assertLength(11, self.hpss_calls)
 
633
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
634
        self.assertLength(1, self.hpss_connections)
 
635
 
 
636
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
637
        self.setup_smart_server_with_call_log()
 
638
        t = self.make_branch_and_tree('from')
 
639
        for count in range(9):
 
640
            t.commit(message='commit %d' % count)
 
641
        self.reset_smart_call_log()
 
642
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
643
                                 'local-target'])
 
644
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
645
        # so we can't easily assert about the number of calls.
 
646
        #self.assertLength(XXX, self.hpss_calls)
 
647
        # We can assert that none of the calls were readv requests for rix
 
648
        # files, though (demonstrating that at least get_parent_map calls are
 
649
        # not using VFS RPCs).
 
650
        readvs_of_rix_files = [
 
651
            c for c in self.hpss_calls
 
652
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
653
        self.assertLength(1, self.hpss_connections)
 
654
        self.assertLength(0, readvs_of_rix_files)
 
655
        self.expectFailure("branching to stacked requires VFS access",
 
656
                           self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
 
657
 
 
658
    def test_branch_from_branch_with_ghosts(self):
 
659
        self.setup_smart_server_with_call_log()
 
660
        t = self.make_branch_and_tree('from')
 
661
        for count in range(9):
 
662
            t.commit(message='commit %d' % count)
 
663
        t.set_parent_ids([t.last_revision(), b'ghost'])
 
664
        t.commit(message='add commit with parent')
 
665
        self.reset_smart_call_log()
 
666
        out, err = self.run_bzr(['branch', self.get_url('from'),
 
667
                                 'local-target'])
 
668
        # This figure represent the amount of work to perform this use case. It
 
669
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
670
        # being too low. If rpc_count increases, more network roundtrips have
 
671
        # become necessary for this use case. Please do not adjust this number
 
672
        # upwards without agreement from bzr's network support maintainers.
 
673
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
674
        self.assertLength(12, self.hpss_calls)
 
675
        self.assertLength(1, self.hpss_connections)
430
676
 
431
677
 
432
678
class TestRemoteBranch(TestCaseWithSFTPServer):
434
680
    def setUp(self):
435
681
        super(TestRemoteBranch, self).setUp()
436
682
        tree = self.make_branch_and_tree('branch')
437
 
        self.build_tree_contents([('branch/file', 'file content\n')])
 
683
        self.build_tree_contents([('branch/file', b'file content\n')])
438
684
        tree.add('file')
439
685
        tree.commit('file created')
440
686
 
452
698
        # Ensure that no working tree what created remotely
453
699
        self.assertFalse(t.has('remote/file'))
454
700
 
 
701
 
 
702
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
703
 
 
704
    def _checkout_and_branch(self, option=''):
 
705
        self.script_runner.run_script(self, '''
 
706
                $ brz checkout %(option)s repo/trunk checkout
 
707
                $ cd checkout
 
708
                $ brz branch --switch ../repo/trunk ../repo/branched
 
709
                2>Branched 0 revisions.
 
710
                2>Tree is up to date at revision 0.
 
711
                2>Switched to branch:...branched...
 
712
                $ cd ..
 
713
                ''' % locals())
 
714
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
715
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
716
        return (bound_branch, master_branch)
 
717
 
 
718
    def test_branch_switch_parent_lightweight(self):
 
719
        """Lightweight checkout using brz branch --switch."""
 
720
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
721
        self.assertParent('repo/trunk', bb)
 
722
        self.assertParent('repo/trunk', mb)
 
723
 
 
724
    def test_branch_switch_parent_heavyweight(self):
 
725
        """Heavyweight checkout using brz branch --switch."""
 
726
        bb, mb = self._checkout_and_branch()
 
727
        self.assertParent('repo/trunk', bb)
 
728
        self.assertParent('repo/trunk', mb)