/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-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

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