~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Packman
  • Date: 2011-12-23 19:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 6405.
  • Revision ID: martin.packman@canonical.com-20111223193822-hesheea4o8aqwexv
Accept and document passing the medium rather than transport for smart connections

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
"""Black-box tests for bzr branch."""
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import branch, bzrdir
 
22
from bzrlib import (
 
23
    branch,
 
24
    bzrdir,
 
25
    controldir,
 
26
    errors,
 
27
    revision as _mod_revision,
 
28
    )
23
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
24
 
from bzrlib.tests.blackbox import ExternalBase
 
30
from bzrlib.tests import TestCaseWithTransport
 
31
from bzrlib.tests import (
 
32
    fixtures,
 
33
    test_server,
 
34
    )
 
35
from bzrlib.tests.features import (
 
36
    HardlinkFeature,
 
37
    )
 
38
from bzrlib.tests.blackbox import test_switch
 
39
from bzrlib.tests.matchers import ContainsNoVfsCalls
 
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
41
from bzrlib.tests.script import run_script
 
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
25
43
from bzrlib.workingtree import WorkingTree
26
44
 
27
45
 
28
 
class TestBranch(ExternalBase):
 
46
class TestBranch(TestCaseWithTransport):
29
47
 
30
 
    def example_branch(test):
31
 
        test.run_bzr('init')
32
 
        file('hello', 'wt').write('foo')
33
 
        test.run_bzr('add hello')
34
 
        test.run_bzr('commit -m setup hello')
35
 
        file('goodbye', 'wt').write('baz')
36
 
        test.run_bzr('add goodbye')
37
 
        test.run_bzr('commit -m setup goodbye')
 
48
    def example_branch(self, path='.', format=None):
 
49
        tree = self.make_branch_and_tree(path, format=format)
 
50
        self.build_tree_contents([(path + '/hello', 'foo')])
 
51
        tree.add('hello')
 
52
        tree.commit(message='setup')
 
53
        self.build_tree_contents([(path + '/goodbye', 'baz')])
 
54
        tree.add('goodbye')
 
55
        tree.commit(message='setup')
 
56
        return tree
38
57
 
39
58
    def test_branch(self):
40
59
        """Branch from one branch to another."""
41
 
        os.mkdir('a')
42
 
        os.chdir('a')
43
 
        self.example_branch()
44
 
        os.chdir('..')
 
60
        self.example_branch('a')
45
61
        self.run_bzr('branch a b')
46
62
        b = branch.Branch.open('b')
47
 
        self.assertEqual('b\n', b.control_files.get_utf8('branch-name').read())
48
63
        self.run_bzr('branch a c -r 1')
49
 
        os.chdir('b')
50
 
        self.run_bzr('commit -m foo --unchanged')
51
 
        os.chdir('..')
 
64
        # previously was erroneously created by branching
 
65
        self.assertFalse(b._transport.has('branch-name'))
 
66
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
 
67
 
 
68
    def test_into_colocated(self):
 
69
        """Branch from a branch into a colocated branch."""
 
70
        self.example_branch('a')
 
71
        out, err = self.run_bzr(
 
72
            'init --format=development-colo file:b,branch=orig')
 
73
        self.assertEqual(
 
74
            """Created a lightweight checkout (format: development-colo)\n""",
 
75
            out)
 
76
        self.assertEqual('', err)
 
77
        out, err = self.run_bzr(
 
78
            'branch a file:b,branch=thiswasa')
 
79
        self.assertEqual('', out)
 
80
        self.assertEqual('Branched 2 revisions.\n', err)
 
81
        out, err = self.run_bzr('branches b')
 
82
        self.assertEqual(" thiswasa\n orig\n", out)
 
83
        self.assertEqual('', err)
 
84
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
85
        self.assertEqual('', out)
 
86
        self.assertEqual('bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
87
 
 
88
    def test_from_colocated(self):
 
89
        """Branch from a colocated branch into a regular branch."""
 
90
        tree = self.example_branch('a', format='development-colo')
 
91
        tree.bzrdir.create_branch(name='somecolo')
 
92
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
93
            local_path_to_url('a'))
 
94
        self.assertEqual('', out)
 
95
        self.assertEqual('Branched 0 revisions.\n', err)
 
96
        self.assertPathExists("somecolo")
 
97
 
 
98
    def test_branch_broken_pack(self):
 
99
        """branching with a corrupted pack file."""
 
100
        self.example_branch('a')
 
101
        # add some corruption
 
102
        packs_dir = 'a/.bzr/repository/packs/'
 
103
        fname = packs_dir + os.listdir(packs_dir)[0]
 
104
        with open(fname, 'rb+') as f:
 
105
            # Start from the end of the file to avoid choosing a place bigger
 
106
            # than the file itself.
 
107
            f.seek(-5, os.SEEK_END)
 
108
            c = f.read(1)
 
109
            f.seek(-5, os.SEEK_END)
 
110
            # Make sure we inject a value different than the one we just read
 
111
            if c == '\xFF':
 
112
                corrupt = '\x00'
 
113
            else:
 
114
                corrupt = '\xFF'
 
115
            f.write(corrupt) # make sure we corrupt something
 
116
        self.run_bzr_error(['Corruption while decompressing repository file'], 
 
117
                            'branch a b', retcode=3)
 
118
 
 
119
    def test_branch_switch_no_branch(self):
 
120
        # No branch in the current directory:
 
121
        #  => new branch will be created, but switch fails
 
122
        self.example_branch('a')
 
123
        self.make_repository('current')
 
124
        self.run_bzr_error(['No WorkingTree exists for'],
 
125
            'branch --switch ../a ../b', working_dir='current')
 
126
        a = branch.Branch.open('a')
 
127
        b = branch.Branch.open('b')
 
128
        self.assertEqual(a.last_revision(), b.last_revision())
 
129
 
 
130
    def test_branch_switch_no_wt(self):
 
131
        # No working tree in the current directory:
 
132
        #  => new branch will be created, but switch fails and the current
 
133
        #     branch is unmodified
 
134
        self.example_branch('a')
 
135
        self.make_branch('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
        work = branch.Branch.open('current')
 
142
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
143
 
 
144
    def test_branch_switch_no_checkout(self):
 
145
        # Standalone branch in the current directory:
 
146
        #  => new branch will be created, but switch fails and the current
 
147
        #     branch is unmodified
 
148
        self.example_branch('a')
 
149
        self.make_branch_and_tree('current')
 
150
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
 
151
            'branch --switch ../a ../b', working_dir='current')
 
152
        a = branch.Branch.open('a')
 
153
        b = branch.Branch.open('b')
 
154
        self.assertEqual(a.last_revision(), b.last_revision())
 
155
        work = branch.Branch.open('current')
 
156
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
157
 
 
158
    def test_branch_switch_checkout(self):
 
159
        # Checkout in the current directory:
 
160
        #  => new branch will be created and checkout bound to the new branch
 
161
        self.example_branch('a')
 
162
        self.run_bzr('checkout a current')
 
163
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
164
        a = branch.Branch.open('a')
 
165
        b = branch.Branch.open('b')
 
166
        self.assertEqual(a.last_revision(), b.last_revision())
 
167
        work = WorkingTree.open('current')
 
168
        self.assertEndsWith(work.branch.get_bound_location(), '/b/')
 
169
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
170
 
 
171
    def test_branch_switch_lightweight_checkout(self):
 
172
        # Lightweight checkout in the current directory:
 
173
        #  => new branch will be created and lightweight checkout pointed to
 
174
        #     the new branch
 
175
        self.example_branch('a')
 
176
        self.run_bzr('checkout --lightweight a current')
 
177
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
178
        a = branch.Branch.open('a')
 
179
        b = branch.Branch.open('b')
 
180
        self.assertEqual(a.last_revision(), b.last_revision())
 
181
        work = WorkingTree.open('current')
 
182
        self.assertEndsWith(work.branch.base, '/b/')
 
183
        self.assertContainsRe(err, "Switched to branch: .*/b/")
52
184
 
53
185
    def test_branch_only_copies_history(self):
54
186
        # Knit branches should only push the history for the current revision.
59
191
 
60
192
        def make_shared_tree(path):
61
193
            shared_repo.bzrdir.root_transport.mkdir(path)
62
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
194
            controldir.ControlDir.create_branch_convenience('repo/' + path)
63
195
            return WorkingTree.open('repo/' + path)
64
196
        tree_a = make_shared_tree('a')
65
197
        self.build_tree(['repo/a/file'])
81
213
 
82
214
        # Now that we have a repository with shared files, make sure
83
215
        # that things aren't copied out by a 'branch'
84
 
        self.run_bzr('branch', 'repo/b', 'branch-b')
 
216
        self.run_bzr('branch repo/b branch-b')
85
217
        pushed_tree = WorkingTree.open('branch-b')
86
218
        pushed_repo = pushed_tree.branch.repository
87
219
        self.assertFalse(pushed_repo.has_revision('a-1'))
88
220
        self.assertFalse(pushed_repo.has_revision('a-2'))
89
221
        self.assertTrue(pushed_repo.has_revision('b-1'))
90
222
 
91
 
 
 
223
    def test_branch_hardlink(self):
 
224
        self.requireFeature(HardlinkFeature)
 
225
        source = self.make_branch_and_tree('source')
 
226
        self.build_tree(['source/file1'])
 
227
        source.add('file1')
 
228
        source.commit('added file')
 
229
        out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
 
230
        source_stat = os.stat('source/file1')
 
231
        target_stat = os.stat('target/file1')
 
232
        self.assertEqual(source_stat, target_stat)
 
233
 
 
234
    def test_branch_files_from(self):
 
235
        source = self.make_branch_and_tree('source')
 
236
        self.build_tree(['source/file1'])
 
237
        source.add('file1')
 
238
        source.commit('added file')
 
239
        out, err = self.run_bzr('branch source target --files-from source')
 
240
        self.assertPathExists('target/file1')
 
241
 
 
242
    def test_branch_files_from_hardlink(self):
 
243
        self.requireFeature(HardlinkFeature)
 
244
        source = self.make_branch_and_tree('source')
 
245
        self.build_tree(['source/file1'])
 
246
        source.add('file1')
 
247
        source.commit('added file')
 
248
        source.bzrdir.sprout('second')
 
249
        out, err = self.run_bzr('branch source target --files-from second'
 
250
                                ' --hardlink')
 
251
        source_stat = os.stat('source/file1')
 
252
        second_stat = os.stat('second/file1')
 
253
        target_stat = os.stat('target/file1')
 
254
        self.assertNotEqual(source_stat, target_stat)
 
255
        self.assertEqual(second_stat, target_stat)
 
256
 
 
257
    def test_branch_standalone(self):
 
258
        shared_repo = self.make_repository('repo', shared=True)
 
259
        self.example_branch('source')
 
260
        self.run_bzr('branch --standalone source repo/target')
 
261
        b = branch.Branch.open('repo/target')
 
262
        expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
 
263
        self.assertEqual(strip_trailing_slash(b.repository.base),
 
264
            strip_trailing_slash(local_path_to_url(expected_repo_path)))
 
265
 
 
266
    def test_branch_no_tree(self):
 
267
        self.example_branch('source')
 
268
        self.run_bzr('branch --no-tree source target')
 
269
        self.assertPathDoesNotExist('target/hello')
 
270
        self.assertPathDoesNotExist('target/goodbye')
 
271
 
 
272
    def test_branch_into_existing_dir(self):
 
273
        self.example_branch('a')
 
274
        # existing dir with similar files but no .bzr dir
 
275
        self.build_tree_contents([('b/',)])
 
276
        self.build_tree_contents([('b/hello', 'bar')])  # different content
 
277
        self.build_tree_contents([('b/goodbye', 'baz')])# same content
 
278
        # fails without --use-existing-dir
 
279
        out,err = self.run_bzr('branch a b', retcode=3)
 
280
        self.assertEqual('', out)
 
281
        self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
 
282
            err)
 
283
        # force operation
 
284
        self.run_bzr('branch a b --use-existing-dir')
 
285
        # check conflicts
 
286
        self.assertPathExists('b/hello.moved')
 
287
        self.assertPathDoesNotExist('b/godbye.moved')
 
288
        # we can't branch into branch
 
289
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
 
290
        self.assertEqual('', out)
 
291
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
 
292
 
 
293
    def test_branch_bind(self):
 
294
        self.example_branch('a')
 
295
        out, err = self.run_bzr('branch a b --bind')
 
296
        self.assertEndsWith(err, "New branch bound to a\n")
 
297
        b = branch.Branch.open('b')
 
298
        self.assertEndsWith(b.get_bound_location(), '/a/')
 
299
 
 
300
    def test_branch_with_post_branch_init_hook(self):
 
301
        calls = []
 
302
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
303
            calls.append, None)
 
304
        self.assertLength(0, calls)
 
305
        self.example_branch('a')
 
306
        self.assertLength(1, calls)
 
307
        self.run_bzr('branch a b')
 
308
        self.assertLength(2, calls)
 
309
 
 
310
    def test_checkout_with_post_branch_init_hook(self):
 
311
        calls = []
 
312
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
313
            calls.append, None)
 
314
        self.assertLength(0, calls)
 
315
        self.example_branch('a')
 
316
        self.assertLength(1, calls)
 
317
        self.run_bzr('checkout a b')
 
318
        self.assertLength(2, calls)
 
319
 
 
320
    def test_lightweight_checkout_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('checkout --lightweight a b')
 
328
        self.assertLength(2, calls)
 
329
 
 
330
    def test_branch_fetches_all_tags(self):
 
331
        builder = self.make_branch_builder('source')
 
332
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
333
        source.tags.set_tag('tag-a', 'rev-2')
 
334
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
335
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
336
        self.run_bzr('branch source new-branch')
 
337
        new_branch = branch.Branch.open('new-branch')
 
338
        # The tag is present, and so is its revision.
 
339
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
340
        new_branch.repository.get_revision('rev-2')
 
341
 
 
342
 
 
343
class TestBranchStacked(TestCaseWithTransport):
 
344
    """Tests for branch --stacked"""
 
345
 
 
346
    def assertRevisionInRepository(self, repo_path, revid):
 
347
        """Check that a revision is in a repository, disregarding stacking."""
 
348
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
349
        self.assertTrue(repo.has_revision(revid))
 
350
 
 
351
    def assertRevisionNotInRepository(self, repo_path, revid):
 
352
        """Check that a revision is not in a repository, disregarding stacking."""
 
353
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
354
        self.assertFalse(repo.has_revision(revid))
 
355
 
 
356
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
 
357
        repo = branch.Branch.open(branch_path).repository
 
358
        self.assertEqual(set(revid_list),
 
359
            repo.has_revisions(revid_list))
 
360
 
 
361
    def test_branch_stacked_branch_not_stacked(self):
 
362
        """Branching a stacked branch is not stacked by default"""
 
363
        # We have a mainline
 
364
        trunk_tree = self.make_branch_and_tree('target',
 
365
            format='1.9')
 
366
        trunk_tree.commit('mainline')
 
367
        # and a branch from it which is stacked
 
368
        branch_tree = self.make_branch_and_tree('branch',
 
369
            format='1.9')
 
370
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
 
371
        # with some work on it
 
372
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
373
        work_tree.commit('moar work plz')
 
374
        work_tree.branch.push(branch_tree.branch)
 
375
        # branching our local branch gives us a new stacked branch pointing at
 
376
        # mainline.
 
377
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
 
378
        self.assertEqual('', out)
 
379
        self.assertEqual('Branched 2 revisions.\n',
 
380
            err)
 
381
        # it should have preserved the branch format, and so it should be
 
382
        # capable of supporting stacking, but not actually have a stacked_on
 
383
        # branch configured
 
384
        self.assertRaises(errors.NotStacked,
 
385
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
 
386
 
 
387
    def test_branch_stacked_branch_stacked(self):
 
388
        """Asking to stack on a stacked branch does work"""
 
389
        # We have a mainline
 
390
        trunk_tree = self.make_branch_and_tree('target',
 
391
            format='1.9')
 
392
        trunk_revid = trunk_tree.commit('mainline')
 
393
        # and a branch from it which is stacked
 
394
        branch_tree = self.make_branch_and_tree('branch',
 
395
            format='1.9')
 
396
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
 
397
        # with some work on it
 
398
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
399
        branch_revid = work_tree.commit('moar work plz')
 
400
        work_tree.branch.push(branch_tree.branch)
 
401
        # you can chain branches on from there
 
402
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
 
403
        self.assertEqual('', out)
 
404
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
405
            branch_tree.branch.base, err)
 
406
        self.assertEqual(branch_tree.branch.base,
 
407
            branch.Branch.open('branch2').get_stacked_on_url())
 
408
        branch2_tree = WorkingTree.open('branch2')
 
409
        branch2_revid = work_tree.commit('work on second stacked branch')
 
410
        work_tree.branch.push(branch2_tree.branch)
 
411
        self.assertRevisionInRepository('branch2', branch2_revid)
 
412
        self.assertRevisionsInBranchRepository(
 
413
            [trunk_revid, branch_revid, branch2_revid],
 
414
            'branch2')
 
415
 
 
416
    def test_branch_stacked(self):
 
417
        # We have a mainline
 
418
        trunk_tree = self.make_branch_and_tree('mainline',
 
419
            format='1.9')
 
420
        original_revid = trunk_tree.commit('mainline')
 
421
        self.assertRevisionInRepository('mainline', original_revid)
 
422
        # and a branch from it which is stacked
 
423
        out, err = self.run_bzr(['branch', '--stacked', 'mainline',
 
424
            'newbranch'])
 
425
        self.assertEqual('', out)
 
426
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
427
            trunk_tree.branch.base, err)
 
428
        self.assertRevisionNotInRepository('newbranch', original_revid)
 
429
        new_branch = branch.Branch.open('newbranch')
 
430
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
431
 
 
432
    def test_branch_stacked_from_smart_server(self):
 
433
        # We can branch stacking on a smart server
 
434
        self.transport_server = test_server.SmartTCPServer_for_testing
 
435
        trunk = self.make_branch('mainline', format='1.9')
 
436
        out, err = self.run_bzr(
 
437
            ['branch', '--stacked', self.get_url('mainline'), 'shallow'])
 
438
 
 
439
    def test_branch_stacked_from_non_stacked_format(self):
 
440
        """The origin format doesn't support stacking"""
 
441
        trunk = self.make_branch('trunk', format='pack-0.92')
 
442
        out, err = self.run_bzr(
 
443
            ['branch', '--stacked', 'trunk', 'shallow'])
 
444
        # We should notify the user that we upgraded their format
 
445
        self.assertEqualDiff(
 
446
            'Source repository format does not support stacking, using format:\n'
 
447
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
 
448
            'Source branch format does not support stacking, using format:\n'
 
449
            '  Branch format 7\n'
 
450
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
451
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
 
452
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
 
453
            err)
 
454
 
 
455
    def test_branch_stacked_from_rich_root_non_stackable(self):
 
456
        trunk = self.make_branch('trunk', format='rich-root-pack')
 
457
        out, err = self.run_bzr(
 
458
            ['branch', '--stacked', 'trunk', 'shallow'])
 
459
        # We should notify the user that we upgraded their format
 
460
        self.assertEqualDiff(
 
461
            'Source repository format does not support stacking, using format:\n'
 
462
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
 
463
            'Source branch format does not support stacking, using format:\n'
 
464
            '  Branch format 7\n'
 
465
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
466
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
 
467
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
 
468
            err)
 
469
 
 
470
 
 
471
class TestSmartServerBranching(TestCaseWithTransport):
 
472
 
 
473
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
 
474
        self.setup_smart_server_with_call_log()
 
475
        t = self.make_branch_and_tree('from')
 
476
        for count in range(9):
 
477
            t.commit(message='commit %d' % count)
 
478
        self.reset_smart_call_log()
 
479
        out, err = self.run_bzr(['branch', self.get_url('from'),
 
480
            self.get_url('target')])
 
481
        # This figure represent the amount of work to perform this use case. It
 
482
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
483
        # being too low. If rpc_count increases, more network roundtrips have
 
484
        # become necessary for this use case. Please do not adjust this number
 
485
        # upwards without agreement from bzr's network support maintainers.
 
486
        self.assertLength(40, self.hpss_calls)
 
487
        self.expectFailure("branching to the same branch requires VFS access",
 
488
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
 
489
 
 
490
    def test_branch_from_trivial_branch_streaming_acceptance(self):
 
491
        self.setup_smart_server_with_call_log()
 
492
        t = self.make_branch_and_tree('from')
 
493
        for count in range(9):
 
494
            t.commit(message='commit %d' % count)
 
495
        self.reset_smart_call_log()
 
496
        out, err = self.run_bzr(['branch', self.get_url('from'),
 
497
            'local-target'])
 
498
        # This figure represent the amount of work to perform this use case. It
 
499
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
500
        # being too low. If rpc_count increases, more network roundtrips have
 
501
        # become necessary for this use case. Please do not adjust this number
 
502
        # upwards without agreement from bzr's network support maintainers.
 
503
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
504
        self.assertLength(10, self.hpss_calls)
 
505
 
 
506
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
 
507
        self.setup_smart_server_with_call_log()
 
508
        t = self.make_branch_and_tree('trunk')
 
509
        for count in range(8):
 
510
            t.commit(message='commit %d' % count)
 
511
        tree2 = t.branch.bzrdir.sprout('feature', stacked=True
 
512
            ).open_workingtree()
 
513
        local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
 
514
        local_tree.commit('feature change')
 
515
        local_tree.branch.push(tree2.branch)
 
516
        self.reset_smart_call_log()
 
517
        out, err = self.run_bzr(['branch', self.get_url('feature'),
 
518
            'local-target'])
 
519
        # This figure represent the amount of work to perform this use case. It
 
520
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
521
        # being too low. If rpc_count increases, more network roundtrips have
 
522
        # become necessary for this use case. Please do not adjust this number
 
523
        # upwards without agreement from bzr's network support maintainers.
 
524
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
525
        self.assertLength(15, self.hpss_calls)
 
526
 
 
527
    def test_branch_from_branch_with_tags(self):
 
528
        self.setup_smart_server_with_call_log()
 
529
        builder = self.make_branch_builder('source')
 
530
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
531
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
532
        source.tags.set_tag('tag-a', 'rev-2')
 
533
        source.tags.set_tag('tag-missing', 'missing-rev')
 
534
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
535
        self.reset_smart_call_log()
 
536
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
537
        # This figure represent the amount of work to perform this use case. It
 
538
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
539
        # being too low. If rpc_count increases, more network roundtrips have
 
540
        # become necessary for this use case. Please do not adjust this number
 
541
        # upwards without agreement from bzr's network support maintainers.
 
542
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
543
        self.assertLength(10, self.hpss_calls)
 
544
 
 
545
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
546
        self.setup_smart_server_with_call_log()
 
547
        t = self.make_branch_and_tree('from')
 
548
        for count in range(9):
 
549
            t.commit(message='commit %d' % count)
 
550
        self.reset_smart_call_log()
 
551
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
552
            'local-target'])
 
553
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
554
        # so we can't easily assert about the number of calls.
 
555
        #self.assertLength(XXX, self.hpss_calls)
 
556
        # We can assert that none of the calls were readv requests for rix
 
557
        # files, though (demonstrating that at least get_parent_map calls are
 
558
        # not using VFS RPCs).
 
559
        readvs_of_rix_files = [
 
560
            c for c in self.hpss_calls
 
561
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
562
        self.assertLength(0, readvs_of_rix_files)
 
563
        self.expectFailure("branching to stacked requires VFS access",
 
564
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
 
565
 
 
566
 
 
567
class TestRemoteBranch(TestCaseWithSFTPServer):
 
568
 
 
569
    def setUp(self):
 
570
        super(TestRemoteBranch, self).setUp()
 
571
        tree = self.make_branch_and_tree('branch')
 
572
        self.build_tree_contents([('branch/file', 'file content\n')])
 
573
        tree.add('file')
 
574
        tree.commit('file created')
 
575
 
 
576
    def test_branch_local_remote(self):
 
577
        self.run_bzr(['branch', 'branch', self.get_url('remote')])
 
578
        t = self.get_transport()
 
579
        # Ensure that no working tree what created remotely
 
580
        self.assertFalse(t.has('remote/file'))
 
581
 
 
582
    def test_branch_remote_remote(self):
 
583
        # Light cheat: we access the branch remotely
 
584
        self.run_bzr(['branch', self.get_url('branch'),
 
585
                      self.get_url('remote')])
 
586
        t = self.get_transport()
 
587
        # Ensure that no working tree what created remotely
 
588
        self.assertFalse(t.has('remote/file'))
 
589
 
 
590
 
 
591
class TestDeprecatedAliases(TestCaseWithTransport):
 
592
 
 
593
    def test_deprecated_aliases(self):
 
594
        """bzr branch can be called clone or get, but those names are deprecated.
 
595
 
 
596
        See bug 506265.
 
597
        """
 
598
        for command in ['clone', 'get']:
 
599
            run_script(self, """
 
600
            $ bzr %(command)s A B
 
601
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
602
            2>bzr: ERROR: Not a branch...
 
603
            """ % locals())
 
604
 
 
605
 
 
606
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
607
 
 
608
    def _checkout_and_branch(self, option=''):
 
609
        self.script_runner.run_script(self, '''
 
610
                $ bzr checkout %(option)s repo/trunk checkout
 
611
                $ cd checkout
 
612
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
613
                2>Branched 0 revisions.
 
614
                2>Tree is up to date at revision 0.
 
615
                2>Switched to branch:...branched...
 
616
                $ cd ..
 
617
                ''' % locals())
 
618
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
619
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
620
        return (bound_branch, master_branch)
 
621
 
 
622
    def test_branch_switch_parent_lightweight(self):
 
623
        """Lightweight checkout using bzr branch --switch."""
 
624
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
625
        self.assertParent('repo/trunk', bb)
 
626
        self.assertParent('repo/trunk', mb)
 
627
 
 
628
    def test_branch_switch_parent_heavyweight(self):
 
629
        """Heavyweight checkout using bzr branch --switch."""
 
630
        bb, mb = self._checkout_and_branch()
 
631
        self.assertParent('repo/trunk', bb)
 
632
        self.assertParent('repo/trunk', mb)