~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2012-02-14 17:22:37 UTC
  • mfrom: (6466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120214172237-7dv7er3n4uy8d5m4
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2012 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
22
22
from bzrlib import (
23
23
    branch,
24
24
    bzrdir,
 
25
    controldir,
25
26
    errors,
26
27
    revision as _mod_revision,
 
28
    tests,
27
29
    )
28
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
29
 
from bzrlib.tests import TestCaseWithTransport
30
31
from bzrlib.tests import (
31
32
    fixtures,
 
33
    test_server,
 
34
    )
 
35
from bzrlib.tests.features import (
32
36
    HardlinkFeature,
33
 
    test_server,
34
37
    )
 
38
from bzrlib.tests.blackbox import test_switch
 
39
from bzrlib.tests.matchers import ContainsNoVfsCalls
35
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
41
from bzrlib.tests.script import run_script
36
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
37
43
from bzrlib.workingtree import WorkingTree
38
44
 
39
45
 
40
 
class TestBranch(TestCaseWithTransport):
 
46
class TestBranch(tests.TestCaseWithTransport):
41
47
 
42
 
    def example_branch(self, path='.'):
43
 
        tree = self.make_branch_and_tree(path)
 
48
    def example_branch(self, path='.', format=None):
 
49
        tree = self.make_branch_and_tree(path, format=format)
44
50
        self.build_tree_contents([(path + '/hello', 'foo')])
45
51
        tree.add('hello')
46
52
        tree.commit(message='setup')
47
53
        self.build_tree_contents([(path + '/goodbye', 'baz')])
48
54
        tree.add('goodbye')
49
55
        tree.commit(message='setup')
 
56
        return tree
50
57
 
51
58
    def test_branch(self):
52
59
        """Branch from one branch to another."""
58
65
        self.assertFalse(b._transport.has('branch-name'))
59
66
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
60
67
 
 
68
    def test_branch_no_to_location(self):
 
69
        """The to_location is derived from the source branch name."""
 
70
        os.mkdir("something")
 
71
        a = self.example_branch('something/a').branch
 
72
        self.run_bzr('branch something/a')
 
73
        b = branch.Branch.open('a')
 
74
        self.assertEquals(b.last_revision_info(), a.last_revision_info())
 
75
 
 
76
    def test_into_colocated(self):
 
77
        """Branch from a branch into a colocated branch."""
 
78
        self.example_branch('a')
 
79
        out, err = self.run_bzr(
 
80
            'init --format=development-colo file:b,branch=orig')
 
81
        self.assertEqual(
 
82
            """Created a lightweight checkout (format: development-colo)\n""",
 
83
            out)
 
84
        self.assertEqual('', err)
 
85
        out, err = self.run_bzr(
 
86
            'branch a file:b,branch=thiswasa')
 
87
        self.assertEqual('', out)
 
88
        self.assertEqual('Branched 2 revisions.\n', err)
 
89
        out, err = self.run_bzr('branches b')
 
90
        self.assertEqual("  orig\n  thiswasa\n", out)
 
91
        self.assertEqual('', err)
 
92
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
93
        self.assertEqual('', out)
 
94
        self.assertEqual(
 
95
            'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
96
 
 
97
    def test_from_colocated(self):
 
98
        """Branch from a colocated branch into a regular branch."""
 
99
        tree = self.example_branch('a', format='development-colo')
 
100
        tree.bzrdir.create_branch(name='somecolo')
 
101
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
102
            local_path_to_url('a'))
 
103
        self.assertEqual('', out)
 
104
        self.assertEqual('Branched 0 revisions.\n', err)
 
105
        self.assertPathExists("somecolo")
 
106
 
 
107
    def test_branch_broken_pack(self):
 
108
        """branching with a corrupted pack file."""
 
109
        self.example_branch('a')
 
110
        # add some corruption
 
111
        packs_dir = 'a/.bzr/repository/packs/'
 
112
        fname = packs_dir + os.listdir(packs_dir)[0]
 
113
        with open(fname, 'rb+') as f:
 
114
            # Start from the end of the file to avoid choosing a place bigger
 
115
            # than the file itself.
 
116
            f.seek(-5, os.SEEK_END)
 
117
            c = f.read(1)
 
118
            f.seek(-5, os.SEEK_END)
 
119
            # Make sure we inject a value different than the one we just read
 
120
            if c == '\xFF':
 
121
                corrupt = '\x00'
 
122
            else:
 
123
                corrupt = '\xFF'
 
124
            f.write(corrupt) # make sure we corrupt something
 
125
        self.run_bzr_error(['Corruption while decompressing repository file'],
 
126
                            'branch a b', retcode=3)
 
127
 
61
128
    def test_branch_switch_no_branch(self):
62
129
        # No branch in the current directory:
63
130
        #  => new branch will be created, but switch fails
88
155
        #  => new branch will be created, but switch fails and the current
89
156
        #     branch is unmodified
90
157
        self.example_branch('a')
91
 
        self.make_branch_and_tree('current')
 
158
        tree = self.make_branch_and_tree('current')
 
159
        c1 = tree.commit('some diverged change')
92
160
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
93
161
            'branch --switch ../a ../b', working_dir='current')
94
162
        a = branch.Branch.open('a')
95
163
        b = branch.Branch.open('b')
96
164
        self.assertEqual(a.last_revision(), b.last_revision())
97
165
        work = branch.Branch.open('current')
98
 
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
166
        self.assertEqual(work.last_revision(), c1)
 
167
 
 
168
    def test_branch_into_empty_dir(self):
 
169
        t = self.example_branch('source')
 
170
        self.make_bzrdir('target')
 
171
        self.run_bzr("branch source target")
 
172
        self.assertEquals(2, len(t.branch.repository.all_revision_ids()))
99
173
 
100
174
    def test_branch_switch_checkout(self):
101
175
        # Checkout in the current directory:
102
176
        #  => new branch will be created and checkout bound to the new branch
103
177
        self.example_branch('a')
104
178
        self.run_bzr('checkout a current')
105
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
179
        out, err = self.run_bzr('branch --switch ../a ../b',
 
180
                                working_dir='current')
106
181
        a = branch.Branch.open('a')
107
182
        b = branch.Branch.open('b')
108
183
        self.assertEqual(a.last_revision(), b.last_revision())
116
191
        #     the new branch
117
192
        self.example_branch('a')
118
193
        self.run_bzr('checkout --lightweight a current')
119
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
194
        out, err = self.run_bzr('branch --switch ../a ../b',
 
195
                                working_dir='current')
120
196
        a = branch.Branch.open('a')
121
197
        b = branch.Branch.open('b')
122
198
        self.assertEqual(a.last_revision(), b.last_revision())
133
209
 
134
210
        def make_shared_tree(path):
135
211
            shared_repo.bzrdir.root_transport.mkdir(path)
136
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
212
            controldir.ControlDir.create_branch_convenience('repo/' + path)
137
213
            return WorkingTree.open('repo/' + path)
138
214
        tree_a = make_shared_tree('a')
139
215
        self.build_tree(['repo/a/file'])
179
255
        source.add('file1')
180
256
        source.commit('added file')
181
257
        out, err = self.run_bzr('branch source target --files-from source')
182
 
        self.failUnlessExists('target/file1')
 
258
        self.assertPathExists('target/file1')
183
259
 
184
260
    def test_branch_files_from_hardlink(self):
185
261
        self.requireFeature(HardlinkFeature)
208
284
    def test_branch_no_tree(self):
209
285
        self.example_branch('source')
210
286
        self.run_bzr('branch --no-tree source target')
211
 
        self.failIfExists('target/hello')
212
 
        self.failIfExists('target/goodbye')
 
287
        self.assertPathDoesNotExist('target/hello')
 
288
        self.assertPathDoesNotExist('target/goodbye')
213
289
 
214
290
    def test_branch_into_existing_dir(self):
215
291
        self.example_branch('a')
225
301
        # force operation
226
302
        self.run_bzr('branch a b --use-existing-dir')
227
303
        # check conflicts
228
 
        self.failUnlessExists('b/hello.moved')
229
 
        self.failIfExists('b/godbye.moved')
 
304
        self.assertPathExists('b/hello.moved')
 
305
        self.assertPathDoesNotExist('b/godbye.moved')
230
306
        # we can't branch into branch
231
307
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
232
308
        self.assertEqual('', out)
273
349
        builder = self.make_branch_builder('source')
274
350
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
275
351
        source.tags.set_tag('tag-a', 'rev-2')
 
352
        source.get_config_stack().set('branch.fetch_tags', True)
276
353
        # Now source has a tag not in its ancestry.  Make a branch from it.
277
354
        self.run_bzr('branch source new-branch')
278
355
        new_branch = branch.Branch.open('new-branch')
281
358
        new_branch.repository.get_revision('rev-2')
282
359
 
283
360
 
284
 
class TestBranchStacked(TestCaseWithTransport):
 
361
class TestBranchStacked(tests.TestCaseWithTransport):
285
362
    """Tests for branch --stacked"""
286
363
 
287
364
    def assertRevisionInRepository(self, repo_path, revid):
288
 
        """Check that a revision is in a repository, disregarding stacking."""
 
365
        """Check that a revision is in a repo, disregarding stacking."""
289
366
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
290
367
        self.assertTrue(repo.has_revision(revid))
291
368
 
292
369
    def assertRevisionNotInRepository(self, repo_path, revid):
293
 
        """Check that a revision is not in a repository, disregarding stacking."""
 
370
        """Check that a revision is not in a repo, disregarding stacking."""
294
371
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
295
372
        self.assertFalse(repo.has_revision(revid))
296
373
 
317
394
        # mainline.
318
395
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
319
396
        self.assertEqual('', out)
320
 
        self.assertEqual('Branched 2 revision(s).\n',
 
397
        self.assertEqual('Branched 2 revisions.\n',
321
398
            err)
322
399
        # it should have preserved the branch format, and so it should be
323
400
        # capable of supporting stacking, but not actually have a stacked_on
368
445
            trunk_tree.branch.base, err)
369
446
        self.assertRevisionNotInRepository('newbranch', original_revid)
370
447
        new_branch = branch.Branch.open('newbranch')
371
 
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
448
        self.assertEqual(trunk_tree.branch.base,
 
449
                         new_branch.get_stacked_on_url())
372
450
 
373
451
    def test_branch_stacked_from_smart_server(self):
374
452
        # We can branch stacking on a smart server
409
487
            err)
410
488
 
411
489
 
412
 
class TestSmartServerBranching(TestCaseWithTransport):
 
490
class TestSmartServerBranching(tests.TestCaseWithTransport):
413
491
 
414
492
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
415
493
        self.setup_smart_server_with_call_log()
424
502
        # being too low. If rpc_count increases, more network roundtrips have
425
503
        # become necessary for this use case. Please do not adjust this number
426
504
        # upwards without agreement from bzr's network support maintainers.
427
 
        self.assertLength(36, self.hpss_calls)
 
505
        self.assertLength(2, self.hpss_connections)
 
506
        self.assertLength(33, self.hpss_calls)
 
507
        self.expectFailure("branching to the same branch requires VFS access",
 
508
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
428
509
 
429
510
    def test_branch_from_trivial_branch_streaming_acceptance(self):
430
511
        self.setup_smart_server_with_call_log()
439
520
        # being too low. If rpc_count increases, more network roundtrips have
440
521
        # become necessary for this use case. Please do not adjust this number
441
522
        # upwards without agreement from bzr's network support maintainers.
442
 
        self.assertLength(9, self.hpss_calls)
 
523
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
524
        self.assertLength(10, self.hpss_calls)
 
525
        self.assertLength(1, self.hpss_connections)
443
526
 
444
527
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
445
528
        self.setup_smart_server_with_call_log()
459
542
        # being too low. If rpc_count increases, more network roundtrips have
460
543
        # become necessary for this use case. Please do not adjust this number
461
544
        # upwards without agreement from bzr's network support maintainers.
462
 
        self.assertLength(14, self.hpss_calls)
 
545
        self.assertLength(15, self.hpss_calls)
 
546
        self.assertLength(1, self.hpss_connections)
 
547
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
463
548
 
464
549
    def test_branch_from_branch_with_tags(self):
465
550
        self.setup_smart_server_with_call_log()
466
551
        builder = self.make_branch_builder('source')
467
552
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
553
        source.get_config_stack().set('branch.fetch_tags', True)
468
554
        source.tags.set_tag('tag-a', 'rev-2')
469
555
        source.tags.set_tag('tag-missing', 'missing-rev')
470
556
        # Now source has a tag not in its ancestry.  Make a branch from it.
475
561
        # being too low. If rpc_count increases, more network roundtrips have
476
562
        # become necessary for this use case. Please do not adjust this number
477
563
        # upwards without agreement from bzr's network support maintainers.
478
 
        self.assertLength(9, self.hpss_calls)
 
564
        self.assertLength(10, self.hpss_calls)
 
565
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
566
        self.assertLength(1, self.hpss_connections)
 
567
 
 
568
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
569
        self.setup_smart_server_with_call_log()
 
570
        t = self.make_branch_and_tree('from')
 
571
        for count in range(9):
 
572
            t.commit(message='commit %d' % count)
 
573
        self.reset_smart_call_log()
 
574
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
575
            'local-target'])
 
576
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
577
        # so we can't easily assert about the number of calls.
 
578
        #self.assertLength(XXX, self.hpss_calls)
 
579
        # We can assert that none of the calls were readv requests for rix
 
580
        # files, though (demonstrating that at least get_parent_map calls are
 
581
        # not using VFS RPCs).
 
582
        readvs_of_rix_files = [
 
583
            c for c in self.hpss_calls
 
584
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
585
        self.assertLength(1, self.hpss_connections)
 
586
        self.assertLength(0, readvs_of_rix_files)
 
587
        self.expectFailure("branching to stacked requires VFS access",
 
588
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
479
589
 
480
590
 
481
591
class TestRemoteBranch(TestCaseWithSFTPServer):
501
611
        # Ensure that no working tree what created remotely
502
612
        self.assertFalse(t.has('remote/file'))
503
613
 
 
614
 
 
615
class TestDeprecatedAliases(tests.TestCaseWithTransport):
 
616
 
 
617
    def test_deprecated_aliases(self):
 
618
        """bzr branch can be called clone or get, but those names are
 
619
        deprecated.
 
620
 
 
621
        See bug 506265.
 
622
        """
 
623
        for command in ['clone', 'get']:
 
624
            run_script(self, """
 
625
            $ bzr %(command)s A B
 
626
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
627
            2>bzr: ERROR: Not a branch...
 
628
            """ % locals())
 
629
 
 
630
 
 
631
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
632
 
 
633
    def _checkout_and_branch(self, option=''):
 
634
        self.script_runner.run_script(self, '''
 
635
                $ bzr checkout %(option)s repo/trunk checkout
 
636
                $ cd checkout
 
637
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
638
                2>Branched 0 revisions.
 
639
                2>Tree is up to date at revision 0.
 
640
                2>Switched to branch:...branched...
 
641
                $ cd ..
 
642
                ''' % locals())
 
643
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
644
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
645
        return (bound_branch, master_branch)
 
646
 
 
647
    def test_branch_switch_parent_lightweight(self):
 
648
        """Lightweight checkout using bzr branch --switch."""
 
649
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
650
        self.assertParent('repo/trunk', bb)
 
651
        self.assertParent('repo/trunk', mb)
 
652
 
 
653
    def test_branch_switch_parent_heavyweight(self):
 
654
        """Heavyweight checkout using bzr branch --switch."""
 
655
        bb, mb = self._checkout_and_branch()
 
656
        self.assertParent('repo/trunk', bb)
 
657
        self.assertParent('repo/trunk', mb)