~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Copyright (C) 2006-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


"""Black-box tests for bzr branch."""

import os

from bzrlib import (
    branch,
    bzrdir,
    errors,
    repository,
    revision as _mod_revision,
    )
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
from bzrlib.tests.blackbox import ExternalBase
from bzrlib.tests import (
    KnownFailure,
    HardlinkFeature,
    test_server,
    )
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
from bzrlib.workingtree import WorkingTree


class TestBranch(ExternalBase):

    def example_branch(self, path='.'):
        tree = self.make_branch_and_tree(path)
        self.build_tree_contents([(path + '/hello', 'foo')])
        tree.add('hello')
        tree.commit(message='setup')
        self.build_tree_contents([(path + '/goodbye', 'baz')])
        tree.add('goodbye')
        tree.commit(message='setup')

    def test_branch(self):
        """Branch from one branch to another."""
        self.example_branch('a')
        self.run_bzr('branch a b')
        b = branch.Branch.open('b')
        self.run_bzr('branch a c -r 1')
        # previously was erroneously created by branching
        self.assertFalse(b._transport.has('branch-name'))
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)

    def test_branch_switch_no_branch(self):
        # No branch in the current directory:
        #  => new branch will be created, but switch fails
        self.example_branch('a')
        self.make_repository('current')
        self.run_bzr_error(['No WorkingTree exists for'],
            'branch --switch ../a ../b', working_dir='current')
        a = branch.Branch.open('a')
        b = branch.Branch.open('b')
        self.assertEqual(a.last_revision(), b.last_revision())

    def test_branch_switch_no_wt(self):
        # No working tree in the current directory:
        #  => new branch will be created, but switch fails and the current
        #     branch is unmodified
        self.example_branch('a')
        self.make_branch('current')
        self.run_bzr_error(['No WorkingTree exists for'],
            'branch --switch ../a ../b', working_dir='current')
        a = branch.Branch.open('a')
        b = branch.Branch.open('b')
        self.assertEqual(a.last_revision(), b.last_revision())
        work = branch.Branch.open('current')
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)

    def test_branch_switch_no_checkout(self):
        # Standalone branch in the current directory:
        #  => new branch will be created, but switch fails and the current
        #     branch is unmodified
        self.example_branch('a')
        self.make_branch_and_tree('current')
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
            'branch --switch ../a ../b', working_dir='current')
        a = branch.Branch.open('a')
        b = branch.Branch.open('b')
        self.assertEqual(a.last_revision(), b.last_revision())
        work = branch.Branch.open('current')
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)

    def test_branch_switch_checkout(self):
        # Checkout in the current directory:
        #  => new branch will be created and checkout bound to the new branch
        self.example_branch('a')
        self.run_bzr('checkout a current')
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
        a = branch.Branch.open('a')
        b = branch.Branch.open('b')
        self.assertEqual(a.last_revision(), b.last_revision())
        work = WorkingTree.open('current')
        self.assertEndsWith(work.branch.get_bound_location(), '/b/')
        self.assertContainsRe(err, "Switched to branch: .*/b/")

    def test_branch_switch_lightweight_checkout(self):
        # Lightweight checkout in the current directory:
        #  => new branch will be created and lightweight checkout pointed to
        #     the new branch
        self.example_branch('a')
        self.run_bzr('checkout --lightweight a current')
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
        a = branch.Branch.open('a')
        b = branch.Branch.open('b')
        self.assertEqual(a.last_revision(), b.last_revision())
        work = WorkingTree.open('current')
        self.assertEndsWith(work.branch.base, '/b/')
        self.assertContainsRe(err, "Switched to branch: .*/b/")

    def test_branch_only_copies_history(self):
        # Knit branches should only push the history for the current revision.
        format = bzrdir.BzrDirMetaFormat1()
        format.repository_format = RepositoryFormatKnit1()
        shared_repo = self.make_repository('repo', format=format, shared=True)
        shared_repo.set_make_working_trees(True)

        def make_shared_tree(path):
            shared_repo.bzrdir.root_transport.mkdir(path)
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
            return WorkingTree.open('repo/' + path)
        tree_a = make_shared_tree('a')
        self.build_tree(['repo/a/file'])
        tree_a.add('file')
        tree_a.commit('commit a-1', rev_id='a-1')
        f = open('repo/a/file', 'ab')
        f.write('more stuff\n')
        f.close()
        tree_a.commit('commit a-2', rev_id='a-2')

        tree_b = make_shared_tree('b')
        self.build_tree(['repo/b/file'])
        tree_b.add('file')
        tree_b.commit('commit b-1', rev_id='b-1')

        self.assertTrue(shared_repo.has_revision('a-1'))
        self.assertTrue(shared_repo.has_revision('a-2'))
        self.assertTrue(shared_repo.has_revision('b-1'))

        # Now that we have a repository with shared files, make sure
        # that things aren't copied out by a 'branch'
        self.run_bzr('branch repo/b branch-b')
        pushed_tree = WorkingTree.open('branch-b')
        pushed_repo = pushed_tree.branch.repository
        self.assertFalse(pushed_repo.has_revision('a-1'))
        self.assertFalse(pushed_repo.has_revision('a-2'))
        self.assertTrue(pushed_repo.has_revision('b-1'))

    def test_branch_hardlink(self):
        self.requireFeature(HardlinkFeature)
        source = self.make_branch_and_tree('source')
        self.build_tree(['source/file1'])
        source.add('file1')
        source.commit('added file')
        out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
        source_stat = os.stat('source/file1')
        target_stat = os.stat('target/file1')
        self.assertEqual(source_stat, target_stat)

    def test_branch_standalone(self):
        shared_repo = self.make_repository('repo', shared=True)
        self.example_branch('source')
        self.run_bzr('branch --standalone source repo/target')
        b = branch.Branch.open('repo/target')
        expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
        self.assertEqual(strip_trailing_slash(b.repository.base),
            strip_trailing_slash(local_path_to_url(expected_repo_path)))

    def test_branch_no_tree(self):
        self.example_branch('source')
        self.run_bzr('branch --no-tree source target')
        self.failIfExists('target/hello')
        self.failIfExists('target/goodbye')

    def test_branch_into_existing_dir(self):
        self.example_branch('a')
        # existing dir with similar files but no .bzr dir
        self.build_tree_contents([('b/',)])
        self.build_tree_contents([('b/hello', 'bar')])  # different content
        self.build_tree_contents([('b/goodbye', 'baz')])# same content
        # fails without --use-existing-dir
        out,err = self.run_bzr('branch a b', retcode=3)
        self.assertEqual('', out)
        self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
            err)
        # force operation
        self.run_bzr('branch a b --use-existing-dir')
        # check conflicts
        self.failUnlessExists('b/hello.moved')
        self.failIfExists('b/godbye.moved')
        # we can't branch into branch
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
        self.assertEqual('', out)
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)

    def test_branch_bind(self):
        self.example_branch('a')
        out, err = self.run_bzr('branch a b --bind')
        self.assertEndsWith(err, "New branch bound to a\n")
        b = branch.Branch.open('b')
        self.assertEndsWith(b.get_bound_location(), '/a/')


class TestBranchStacked(ExternalBase):
    """Tests for branch --stacked"""

    def assertRevisionInRepository(self, repo_path, revid):
        """Check that a revision is in a repository, disregarding stacking."""
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
        self.assertTrue(repo.has_revision(revid))

    def assertRevisionNotInRepository(self, repo_path, revid):
        """Check that a revision is not in a repository, disregarding stacking."""
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
        self.assertFalse(repo.has_revision(revid))

    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
        repo = branch.Branch.open(branch_path).repository
        self.assertEqual(set(revid_list),
            repo.has_revisions(revid_list))

    def test_branch_stacked_branch_not_stacked(self):
        """Branching a stacked branch is not stacked by default"""
        # We have a mainline
        trunk_tree = self.make_branch_and_tree('target',
            format='1.9')
        trunk_tree.commit('mainline')
        # and a branch from it which is stacked
        branch_tree = self.make_branch_and_tree('branch',
            format='1.9')
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
        # with some work on it
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
        work_tree.commit('moar work plz')
        work_tree.branch.push(branch_tree.branch)
        # branching our local branch gives us a new stacked branch pointing at
        # mainline.
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
        self.assertEqual('', out)
        self.assertEqual('Branched 2 revision(s).\n',
            err)
        # it should have preserved the branch format, and so it should be
        # capable of supporting stacking, but not actually have a stacked_on
        # branch configured
        self.assertRaises(errors.NotStacked,
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)

    def test_branch_stacked_branch_stacked(self):
        """Asking to stack on a stacked branch does work"""
        # We have a mainline
        trunk_tree = self.make_branch_and_tree('target',
            format='1.9')
        trunk_revid = trunk_tree.commit('mainline')
        # and a branch from it which is stacked
        branch_tree = self.make_branch_and_tree('branch',
            format='1.9')
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
        # with some work on it
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
        branch_revid = work_tree.commit('moar work plz')
        work_tree.branch.push(branch_tree.branch)
        # you can chain branches on from there
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
        self.assertEqual('', out)
        self.assertEqual('Created new stacked branch referring to %s.\n' %
            branch_tree.branch.base, err)
        self.assertEqual(branch_tree.branch.base,
            branch.Branch.open('branch2').get_stacked_on_url())
        branch2_tree = WorkingTree.open('branch2')
        branch2_revid = work_tree.commit('work on second stacked branch')
        work_tree.branch.push(branch2_tree.branch)
        self.assertRevisionInRepository('branch2', branch2_revid)
        self.assertRevisionsInBranchRepository(
            [trunk_revid, branch_revid, branch2_revid],
            'branch2')

    def test_branch_stacked(self):
        # We have a mainline
        trunk_tree = self.make_branch_and_tree('mainline',
            format='1.9')
        original_revid = trunk_tree.commit('mainline')
        self.assertRevisionInRepository('mainline', original_revid)
        # and a branch from it which is stacked
        out, err = self.run_bzr(['branch', '--stacked', 'mainline',
            'newbranch'])
        self.assertEqual('', out)
        self.assertEqual('Created new stacked branch referring to %s.\n' %
            trunk_tree.branch.base, err)
        self.assertRevisionNotInRepository('newbranch', original_revid)
        new_branch = branch.Branch.open('newbranch')
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())

    def test_branch_stacked_from_smart_server(self):
        # We can branch stacking on a smart server
        self.transport_server = test_server.SmartTCPServer_for_testing
        trunk = self.make_branch('mainline', format='1.9')
        out, err = self.run_bzr(
            ['branch', '--stacked', self.get_url('mainline'), 'shallow'])

    def test_branch_stacked_from_non_stacked_format(self):
        """The origin format doesn't support stacking"""
        trunk = self.make_branch('trunk', format='pack-0.92')
        out, err = self.run_bzr(
            ['branch', '--stacked', 'trunk', 'shallow'])
        # We should notify the user that we upgraded their format
        self.assertEqualDiff(
            'Source repository format does not support stacking, using format:\n'
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
            'Source branch format does not support stacking, using format:\n'
            '  Branch format 7\n'
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
            err)

    def test_branch_stacked_from_rich_root_non_stackable(self):
        trunk = self.make_branch('trunk', format='rich-root-pack')
        out, err = self.run_bzr(
            ['branch', '--stacked', 'trunk', 'shallow'])
        # We should notify the user that we upgraded their format
        self.assertEqualDiff(
            'Source repository format does not support stacking, using format:\n'
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
            'Source branch format does not support stacking, using format:\n'
            '  Branch format 7\n'
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
            err)


class TestSmartServerBranching(ExternalBase):

    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
        self.setup_smart_server_with_call_log()
        t = self.make_branch_and_tree('from')
        for count in range(9):
            t.commit(message='commit %d' % count)
        self.reset_smart_call_log()
        out, err = self.run_bzr(['branch', self.get_url('from'),
            self.get_url('target')])
        # This figure represent the amount of work to perform this use case. It
        # is entirely ok to reduce this number if a test fails due to rpc_count
        # being too low. If rpc_count increases, more network roundtrips have
        # become necessary for this use case. Please do not adjust this number
        # upwards without agreement from bzr's network support maintainers.
        self.assertLength(38, self.hpss_calls)

    def test_branch_from_trivial_branch_streaming_acceptance(self):
        self.setup_smart_server_with_call_log()
        t = self.make_branch_and_tree('from')
        for count in range(9):
            t.commit(message='commit %d' % count)
        self.reset_smart_call_log()
        out, err = self.run_bzr(['branch', self.get_url('from'),
            'local-target'])
        # This figure represent the amount of work to perform this use case. It
        # is entirely ok to reduce this number if a test fails due to rpc_count
        # being too low. If rpc_count increases, more network roundtrips have
        # become necessary for this use case. Please do not adjust this number
        # upwards without agreement from bzr's network support maintainers.
        self.assertLength(10, self.hpss_calls)

    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
        self.setup_smart_server_with_call_log()
        t = self.make_branch_and_tree('trunk')
        for count in range(8):
            t.commit(message='commit %d' % count)
        tree2 = t.branch.bzrdir.sprout('feature', stacked=True
            ).open_workingtree()
        local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
        local_tree.commit('feature change')
        local_tree.branch.push(tree2.branch)
        self.reset_smart_call_log()
        out, err = self.run_bzr(['branch', self.get_url('feature'),
            'local-target'])
        # This figure represent the amount of work to perform this use case. It
        # is entirely ok to reduce this number if a test fails due to rpc_count
        # being too low. If rpc_count increases, more network roundtrips have
        # become necessary for this use case. Please do not adjust this number
        # upwards without agreement from bzr's network support maintainers.
        self.assertLength(15, self.hpss_calls)


class TestRemoteBranch(TestCaseWithSFTPServer):

    def setUp(self):
        super(TestRemoteBranch, self).setUp()
        tree = self.make_branch_and_tree('branch')
        self.build_tree_contents([('branch/file', 'file content\n')])
        tree.add('file')
        tree.commit('file created')

    def test_branch_local_remote(self):
        self.run_bzr(['branch', 'branch', self.get_url('remote')])
        t = self.get_transport()
        # Ensure that no working tree what created remotely
        self.assertFalse(t.has('remote/file'))

    def test_branch_remote_remote(self):
        # Light cheat: we access the branch remotely
        self.run_bzr(['branch', self.get_url('branch'),
                      self.get_url('remote')])
        t = self.get_transport()
        # Ensure that no working tree what created remotely
        self.assertFalse(t.has('remote/file'))