~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_branch/test_stacking.py

merge 2.0 branch rev 4647

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    branch,
21
21
    bzrdir,
 
22
    check,
22
23
    errors,
23
24
    )
24
25
from bzrlib.revision import NULL_REVISION
25
26
from bzrlib.smart import server
26
27
from bzrlib.tests import TestNotApplicable, KnownFailure, transport_util
27
 
from bzrlib.tests.branch_implementations import TestCaseWithBranch
 
28
from bzrlib.tests.per_branch import TestCaseWithBranch
28
29
from bzrlib.transport import get_transport
29
30
 
30
31
 
78
79
            return
79
80
        self.assertEqual('../target', branch.get_stacked_on_url())
80
81
 
 
82
    def test_set_stacked_on_same_branch_raises(self):
 
83
        # Stacking on the same branch silently raises and doesn't execute the
 
84
        # change. Reported in bug 376243.
 
85
        branch = self.make_branch('branch')
 
86
        try:
 
87
            self.assertRaises(errors.UnstackableLocationError,
 
88
                branch.set_stacked_on_url, '../branch')
 
89
        except unstackable_format_errors:
 
90
            # if the set failed, so must the get
 
91
            self.assertRaises(unstackable_format_errors, branch.get_stacked_on_url)
 
92
            return
 
93
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
 
94
 
 
95
    def test_set_stacked_on_same_branch_after_being_stacked_raises(self):
 
96
        # Stacking on the same branch silently raises and doesn't execute the
 
97
        # change.
 
98
        branch = self.make_branch('branch')
 
99
        target = self.make_branch('target')
 
100
        try:
 
101
            branch.set_stacked_on_url('../target')
 
102
        except unstackable_format_errors:
 
103
            # if the set failed, so must the get
 
104
            self.assertRaises(unstackable_format_errors, branch.get_stacked_on_url)
 
105
            return
 
106
        self.assertRaises(errors.UnstackableLocationError,
 
107
            branch.set_stacked_on_url, '../branch')
 
108
        self.assertEqual('../target', branch.get_stacked_on_url())
 
109
 
81
110
    def assertRevisionInRepository(self, repo_path, revid):
82
111
        """Check that a revision is in a repository, disregarding stacking."""
83
112
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
121
150
            raise TestNotApplicable(e)
122
151
        # stacked repository
123
152
        self.assertRevisionNotInRepository('newbranch', trunk_revid)
124
 
        new_tree = new_dir.open_workingtree()
125
 
        new_branch_revid = new_tree.commit('something local')
 
153
        tree = new_dir.open_branch().create_checkout('local')
 
154
        new_branch_revid = tree.commit('something local')
126
155
        self.assertRevisionNotInRepository('mainline', new_branch_revid)
127
156
        self.assertRevisionInRepository('newbranch', new_branch_revid)
128
157
 
144
173
        new_dir = remote_bzrdir.sprout('newbranch', stacked=True)
145
174
        # stacked repository
146
175
        self.assertRevisionNotInRepository('newbranch', trunk_revid)
147
 
        new_tree = new_dir.open_workingtree()
148
 
        new_branch_revid = new_tree.commit('something local')
 
176
        tree = new_dir.open_branch().create_checkout('local')
 
177
        new_branch_revid = tree.commit('something local')
149
178
        self.assertRevisionNotInRepository('mainline', new_branch_revid)
150
179
        self.assertRevisionInRepository('newbranch', new_branch_revid)
151
180
 
156
185
        trunk_revid = trunk_tree.commit('revision on mainline')
157
186
        # and make branch from it which is stacked
158
187
        try:
159
 
            new_dir = trunk_tree.bzrdir.sprout('newbranch', stacked=True)
 
188
            new_dir = trunk_tree.bzrdir.sprout(self.get_url('newbranch'),
 
189
                stacked=True)
160
190
        except unstackable_format_errors, e:
161
191
            raise TestNotApplicable(e)
162
192
        # stacked repository
163
193
        self.assertRevisionNotInRepository('newbranch', trunk_revid)
 
194
        # TODO: we'd like to commit in the stacked repository; that requires
 
195
        # some care (maybe a BranchBuilder) if it's remote and has no
 
196
        # workingtree
 
197
        ##newbranch_revid = new_dir.open_workingtree().commit('revision in '
 
198
            ##'newbranch')
164
199
        # now when we unstack that should implicitly fetch, to make sure that
165
200
        # the branch will still work
166
201
        new_branch = new_dir.open_branch()
239
274
        self.assertRaises((errors.NotStacked, errors.UnstackableBranchFormat),
240
275
                          cloned_bzrdir.open_branch().get_stacked_on_url)
241
276
 
 
277
    def make_stacked_on_matching(self, source):
 
278
        if source.repository.supports_rich_root():
 
279
            if source.repository._format.supports_chks:
 
280
                format = "2a"
 
281
            else:
 
282
                format = "1.9-rich-root"
 
283
        else:
 
284
            format = "1.9"
 
285
        return self.make_branch('stack-on', format)
 
286
 
242
287
    def test_sprout_stacking_policy_handling(self):
243
288
        """Obey policy where possible, ignore otherwise."""
244
 
        stack_on = self.make_branch('stack-on')
 
289
        if isinstance(self.branch_format, branch.BzrBranchFormat4):
 
290
            raise TestNotApplicable('Branch format 4 does not autoupgrade.')
 
291
        source = self.make_branch('source')
 
292
        stack_on = self.make_stacked_on_matching(source)
245
293
        parent_bzrdir = self.make_bzrdir('.', format='default')
246
294
        parent_bzrdir.get_config().set_default_stack_on('stack-on')
247
 
        source = self.make_branch('source')
248
295
        target = source.bzrdir.sprout('target').open_branch()
249
 
        if self.branch_format.supports_stacking():
 
296
        # When we sprout we upgrade the branch when there is a default stack_on
 
297
        # set by a config *and* the targeted branch supports stacking.
 
298
        if stack_on._format.supports_stacking():
250
299
            self.assertEqual('../stack-on', target.get_stacked_on_url())
251
300
        else:
252
301
            self.assertRaises(
254
303
 
255
304
    def test_clone_stacking_policy_handling(self):
256
305
        """Obey policy where possible, ignore otherwise."""
257
 
        stack_on = self.make_branch('stack-on')
 
306
        if isinstance(self.branch_format, branch.BzrBranchFormat4):
 
307
            raise TestNotApplicable('Branch format 4 does not autoupgrade.')
 
308
        source = self.make_branch('source')
 
309
        stack_on = self.make_stacked_on_matching(source)
258
310
        parent_bzrdir = self.make_bzrdir('.', format='default')
259
311
        parent_bzrdir.get_config().set_default_stack_on('stack-on')
260
 
        source = self.make_branch('source')
261
312
        target = source.bzrdir.clone('target').open_branch()
262
 
        if self.branch_format.supports_stacking():
 
313
        # When we clone we upgrade the branch when there is a default stack_on
 
314
        # set by a config *and* the targeted branch supports stacking.
 
315
        if stack_on._format.supports_stacking():
263
316
            self.assertEqual('../stack-on', target.get_stacked_on_url())
264
317
        else:
265
318
            self.assertRaises(
269
322
        """Obey policy where possible, ignore otherwise."""
270
323
        if isinstance(self.branch_format, branch.BzrBranchFormat4):
271
324
            raise TestNotApplicable('Branch format 4 is not usable via HPSS.')
272
 
        stack_on = self.make_branch('stack-on')
 
325
        source = self.make_branch('source')
 
326
        stack_on = self.make_stacked_on_matching(source)
273
327
        parent_bzrdir = self.make_bzrdir('.', format='default')
274
328
        parent_bzrdir.get_config().set_default_stack_on('stack-on')
275
 
        source = self.make_branch('source')
276
329
        url = self.make_smart_server('target').base
277
330
        target = source.bzrdir.sprout(url).open_branch()
278
 
        if self.branch_format.supports_stacking():
 
331
        # When we sprout we upgrade the branch when there is a default stack_on
 
332
        # set by a config *and* the targeted branch supports stacking.
 
333
        if stack_on._format.supports_stacking():
279
334
            self.assertEqual('../stack-on', target.get_stacked_on_url())
280
335
        else:
281
336
            self.assertRaises(
298
353
 
299
354
    def test_fetch_copies_from_stacked_on_and_stacked(self):
300
355
        stacked, unstacked = self.prepare_stacked_on_fetch()
301
 
        stacked.commit('second commit', rev_id='rev2')
 
356
        tree = stacked.branch.create_checkout('local')
 
357
        tree.commit('second commit', rev_id='rev2')
302
358
        unstacked.fetch(stacked.branch.repository, 'rev2')
303
359
        unstacked.get_revision('rev1')
304
360
        unstacked.get_revision('rev2')
311
367
        # repository boundaries.  however, i didn't actually get this test to
312
368
        # fail on that code. -- mbp
313
369
        # see https://bugs.launchpad.net/bzr/+bug/252821
314
 
        if not self.branch_format.supports_stacking():
 
370
        stack_on = self.make_branch_and_tree('stack-on')
 
371
        if not stack_on.branch._format.supports_stacking():
315
372
            raise TestNotApplicable("%r does not support stacking"
316
373
                % self.branch_format)
317
 
        stack_on = self.make_branch_and_tree('stack-on')
318
374
        text_lines = ['line %d blah blah blah\n' % i for i in range(20)]
319
375
        self.build_tree_contents([('stack-on/a', ''.join(text_lines))])
320
376
        stack_on.add('a')
321
377
        stack_on.commit('base commit')
322
378
        stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
323
 
        stacked_tree = stacked_dir.open_workingtree()
 
379
        stacked_branch = stacked_dir.open_branch()
 
380
        local_tree = stack_on.bzrdir.sprout('local').open_workingtree()
324
381
        for i in range(20):
325
382
            text_lines[0] = 'changed in %d\n' % i
326
 
            self.build_tree_contents([('stacked/a', ''.join(text_lines))])
327
 
            stacked_tree.commit('commit %d' % i)
328
 
        stacked_tree.branch.repository.pack()
329
 
        stacked_tree.branch.check()
 
383
            self.build_tree_contents([('local/a', ''.join(text_lines))])
 
384
            local_tree.commit('commit %d' % i)
 
385
            local_tree.branch.push(stacked_branch)
 
386
        stacked_branch.repository.pack()
 
387
        check.check_dwim(stacked_branch.base, False, True, True)
330
388
 
331
389
    def test_pull_delta_when_stacked(self):
332
390
        if not self.branch_format.supports_stacking():
350
408
        # bug 252821 caused a RevisionNotPresent here...
351
409
        stacked_tree.pull(other_tree.branch)
352
410
        stacked_tree.branch.repository.pack()
353
 
        stacked_tree.branch.check()
 
411
        check.check_dwim(stacked_tree.branch.base, False, True, True)
354
412
        self.check_lines_added_or_present(stacked_tree.branch, stacked_revid)
355
413
 
356
414
    def test_fetch_revisions_with_file_changes(self):
435
493
        except errors.NoWorkingTree:
436
494
            stacked = stacked_dir.open_branch().create_checkout(
437
495
                'stacked-checkout', lightweight=True)
438
 
        stacked.commit('second commit', rev_id='rev2')
 
496
        tree = stacked.branch.create_checkout('local')
 
497
        tree.commit('second commit', rev_id='rev2')
439
498
        # Sanity check: stacked's repo should not contain rev1, otherwise this
440
499
        # test isn't testing what it's supposed to.
441
500
        repo = stacked.branch.repository.bzrdir.open_repository()