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
17
17
"""Tests for Branch.get_stacked_on_url and set_stacked_on_url."""
19
19
from bzrlib import (
23
25
from bzrlib.revision import NULL_REVISION
24
from bzrlib.tests import TestNotApplicable, KnownFailure
25
from bzrlib.tests.branch_implementations import TestCaseWithBranch
26
from bzrlib.smart import server
27
from bzrlib.tests import TestNotApplicable, KnownFailure, transport_util
28
from bzrlib.tests.per_branch import TestCaseWithBranch
29
from bzrlib.transport import get_transport
32
unstackable_format_errors = (
33
errors.UnstackableBranchFormat,
34
errors.UnstackableRepositoryFormat,
28
38
class TestStacking(TestCaseWithBranch):
40
def check_lines_added_or_present(self, stacked_branch, revid):
41
# similar to a failure seen in bug 288751 by mbp 20081120
42
stacked_repo = stacked_branch.repository
43
stacked_repo.lock_read()
45
list(stacked_repo.inventories.iter_lines_added_or_present_in_keys(
30
50
def test_get_set_stacked_on_url(self):
31
51
# branches must either:
32
52
# raise UnstackableBranchFormat or
34
54
# permit stacking to be done and then return the stacked location.
35
55
branch = self.make_branch('branch')
36
56
target = self.make_branch('target')
38
errors.UnstackableBranchFormat,
39
errors.UnstackableRepositoryFormat,
42
58
branch.set_stacked_on_url(target.base)
43
except old_format_errors:
59
except unstackable_format_errors:
44
60
# if the set failed, so must the get
45
self.assertRaises(old_format_errors, branch.get_stacked_on_url)
61
self.assertRaises(unstackable_format_errors, branch.get_stacked_on_url)
62
self.assertFalse(branch._format.supports_stacking())
64
self.assertTrue(branch._format.supports_stacking())
47
65
# now we have a stacked branch:
48
66
self.assertEqual(target.base, branch.get_stacked_on_url())
49
67
branch.set_stacked_on_url(None)
53
71
# Branches can be stacked on other branches using relative paths.
54
72
branch = self.make_branch('branch')
55
73
target = self.make_branch('target')
57
errors.UnstackableBranchFormat,
58
errors.UnstackableRepositoryFormat,
61
branch.set_stacked_on_url('../target')
62
except old_format_errors:
63
# if the set failed, so must the get
64
self.assertRaises(old_format_errors, branch.get_stacked_on_url)
75
branch.set_stacked_on_url('../target')
76
except unstackable_format_errors:
77
# if the set failed, so must the get
78
self.assertRaises(unstackable_format_errors, branch.get_stacked_on_url)
80
self.assertEqual('../target', branch.get_stacked_on_url())
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')
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)
93
self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
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
98
branch = self.make_branch('branch')
99
target = self.make_branch('target')
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)
106
self.assertRaises(errors.UnstackableLocationError,
107
branch.set_stacked_on_url, '../branch')
66
108
self.assertEqual('../target', branch.get_stacked_on_url())
68
110
def assertRevisionInRepository(self, repo_path, revid):
105
146
# and make branch from it which is stacked
107
148
new_dir = trunk_tree.bzrdir.sprout('newbranch', stacked=True)
108
except (errors.UnstackableBranchFormat,
109
errors.UnstackableRepositoryFormat), e:
110
raise TestNotApplicable(e)
112
self.assertRevisionNotInRepository('newbranch', trunk_revid)
113
new_tree = new_dir.open_workingtree()
114
new_branch_revid = new_tree.commit('something local')
149
except unstackable_format_errors, e:
150
raise TestNotApplicable(e)
152
self.assertRevisionNotInRepository('newbranch', trunk_revid)
153
tree = new_dir.open_branch().create_checkout('local')
154
new_branch_revid = tree.commit('something local')
155
self.assertRevisionNotInRepository('mainline', new_branch_revid)
156
self.assertRevisionInRepository('newbranch', new_branch_revid)
158
def test_sprout_stacked_from_smart_server(self):
159
if isinstance(self.branch_format, branch.BzrBranchFormat4):
160
raise TestNotApplicable('Branch format 4 is not usable via HPSS.')
162
trunk_tree = self.make_branch_and_tree('mainline')
163
trunk_revid = trunk_tree.commit('mainline')
164
# Make sure that we can make a stacked branch from it
166
trunk_tree.bzrdir.sprout('testbranch', stacked=True)
167
except unstackable_format_errors, e:
168
raise TestNotApplicable(e)
169
# Now serve the original mainline from a smart server
170
remote_transport = self.make_smart_server('mainline')
171
remote_bzrdir = bzrdir.BzrDir.open_from_transport(remote_transport)
172
# and make branch from the smart server which is stacked
173
new_dir = remote_bzrdir.sprout('newbranch', stacked=True)
175
self.assertRevisionNotInRepository('newbranch', trunk_revid)
176
tree = new_dir.open_branch().create_checkout('local')
177
new_branch_revid = tree.commit('something local')
115
178
self.assertRevisionNotInRepository('mainline', new_branch_revid)
116
179
self.assertRevisionInRepository('newbranch', new_branch_revid)
122
185
trunk_revid = trunk_tree.commit('revision on mainline')
123
186
# and make branch from it which is stacked
125
new_dir = trunk_tree.bzrdir.sprout('newbranch', stacked=True)
126
except (errors.UnstackableBranchFormat,
127
errors.UnstackableRepositoryFormat), e:
188
new_dir = trunk_tree.bzrdir.sprout(self.get_url('newbranch'),
190
except unstackable_format_errors, e:
128
191
raise TestNotApplicable(e)
129
192
# stacked repository
130
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
197
##newbranch_revid = new_dir.open_workingtree().commit('revision in '
131
199
# now when we unstack that should implicitly fetch, to make sure that
132
200
# the branch will still work
133
201
new_branch = new_dir.open_branch()
163
231
# same branch as the original.
165
233
stacked_bzrdir = self.make_stacked_bzrdir()
166
except (errors.UnstackableBranchFormat,
167
errors.UnstackableRepositoryFormat), e:
168
# not a testable combination.
234
except unstackable_format_errors, e:
169
235
raise TestNotApplicable(e)
170
236
cloned_bzrdir = stacked_bzrdir.clone('cloned', preserve_stacking=True)
172
238
self.assertEqual(
173
239
stacked_bzrdir.open_branch().get_stacked_on_url(),
174
240
cloned_bzrdir.open_branch().get_stacked_on_url())
175
except (errors.UnstackableBranchFormat,
176
errors.UnstackableRepositoryFormat):
241
except unstackable_format_errors, e:
179
244
def test_clone_from_branch_stacked_on_relative_url_preserve_stacking(self):
212
274
self.assertRaises((errors.NotStacked, errors.UnstackableBranchFormat),
213
275
cloned_bzrdir.open_branch().get_stacked_on_url)
277
def make_stacked_on_matching(self, source):
278
if source.repository.supports_rich_root():
279
if source.repository._format.supports_chks:
282
format = "1.9-rich-root"
285
return self.make_branch('stack-on', format)
215
287
def test_sprout_stacking_policy_handling(self):
216
288
"""Obey policy where possible, ignore otherwise."""
217
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)
218
293
parent_bzrdir = self.make_bzrdir('.', format='default')
219
294
parent_bzrdir.get_config().set_default_stack_on('stack-on')
220
source = self.make_branch('source')
221
295
target = source.bzrdir.sprout('target').open_branch()
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():
223
299
self.assertEqual('../stack-on', target.get_stacked_on_url())
224
except errors.UnstackableBranchFormat:
302
errors.UnstackableBranchFormat, target.get_stacked_on_url)
227
304
def test_clone_stacking_policy_handling(self):
228
305
"""Obey policy where possible, ignore otherwise."""
229
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)
230
310
parent_bzrdir = self.make_bzrdir('.', format='default')
231
311
parent_bzrdir.get_config().set_default_stack_on('stack-on')
232
source = self.make_branch('source')
233
312
target = source.bzrdir.clone('target').open_branch()
235
self.assertEqual('../stack-on', target.get_stacked_on_url())
236
except errors.UnstackableBranchFormat:
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():
316
self.assertEqual('../stack-on', target.get_stacked_on_url())
319
errors.UnstackableBranchFormat, target.get_stacked_on_url)
321
def test_sprout_to_smart_server_stacking_policy_handling(self):
322
"""Obey policy where possible, ignore otherwise."""
323
if isinstance(self.branch_format, branch.BzrBranchFormat4):
324
raise TestNotApplicable('Branch format 4 is not usable via HPSS.')
325
source = self.make_branch('source')
326
stack_on = self.make_stacked_on_matching(source)
327
parent_bzrdir = self.make_bzrdir('.', format='default')
328
parent_bzrdir.get_config().set_default_stack_on('stack-on')
329
url = self.make_smart_server('target').base
330
target = source.bzrdir.sprout(url).open_branch()
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():
334
self.assertEqual('../stack-on', target.get_stacked_on_url())
337
errors.UnstackableBranchFormat, target.get_stacked_on_url)
239
339
def prepare_stacked_on_fetch(self):
240
340
stack_on = self.make_branch_and_tree('stack-on')
241
341
stack_on.commit('first commit', rev_id='rev1')
243
343
stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
244
except (errors.UnstackableRepositoryFormat,
245
errors.UnstackableBranchFormat):
344
except unstackable_format_errors, e:
246
345
raise TestNotApplicable('Format does not support stacking.')
247
346
unstacked = self.make_repository('unstacked')
248
347
return stacked_dir.open_workingtree(), unstacked
255
354
def test_fetch_copies_from_stacked_on_and_stacked(self):
256
355
stacked, unstacked = self.prepare_stacked_on_fetch()
257
stacked.commit('second commit', rev_id='rev2')
356
tree = stacked.branch.create_checkout('local')
357
tree.commit('second commit', rev_id='rev2')
258
358
unstacked.fetch(stacked.branch.repository, 'rev2')
259
359
unstacked.get_revision('rev1')
260
360
unstacked.get_revision('rev2')
361
self.check_lines_added_or_present(stacked.branch, 'rev1')
362
self.check_lines_added_or_present(stacked.branch, 'rev2')
262
364
def test_autopack_when_stacked(self):
263
365
# in bzr.dev as of 20080730, autopack was reported to fail in stacked
265
367
# repository boundaries. however, i didn't actually get this test to
266
368
# fail on that code. -- mbp
267
369
# see https://bugs.launchpad.net/bzr/+bug/252821
268
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():
269
372
raise TestNotApplicable("%r does not support stacking"
270
373
% self.branch_format)
271
stack_on = self.make_branch_and_tree('stack-on')
272
374
text_lines = ['line %d blah blah blah\n' % i for i in range(20)]
273
375
self.build_tree_contents([('stack-on/a', ''.join(text_lines))])
274
376
stack_on.add('a')
275
377
stack_on.commit('base commit')
276
378
stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
277
stacked_tree = stacked_dir.open_workingtree()
379
stacked_branch = stacked_dir.open_branch()
380
local_tree = stack_on.bzrdir.sprout('local').open_workingtree()
278
381
for i in range(20):
279
382
text_lines[0] = 'changed in %d\n' % i
280
self.build_tree_contents([('stacked/a', ''.join(text_lines))])
281
stacked_tree.commit('commit %d' % i)
282
stacked_tree.branch.repository.pack()
283
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)
285
389
def test_pull_delta_when_stacked(self):
286
390
if not self.branch_format.supports_stacking():
299
403
other_tree = other_dir.open_workingtree()
300
404
text_lines[9] = 'changed in other\n'
301
405
self.build_tree_contents([('other/a', ''.join(text_lines))])
302
other_tree.commit('commit in other')
406
stacked_revid = other_tree.commit('commit in other')
303
407
# this should have generated a delta; try to pull that across
304
408
# bug 252821 caused a RevisionNotPresent here...
305
409
stacked_tree.pull(other_tree.branch)
306
410
stacked_tree.branch.repository.pack()
307
stacked_tree.branch.check()
411
check.check_dwim(stacked_tree.branch.base, False, True, True)
412
self.check_lines_added_or_present(stacked_tree.branch, stacked_revid)
309
414
def test_fetch_revisions_with_file_changes(self):
310
415
# Fetching revisions including file changes into a stacked branch
336
440
rtree.lock_read()
337
441
self.addCleanup(rtree.unlock)
338
442
self.assertEqual('new content', rtree.get_file_by_path('a').read())
443
self.check_lines_added_or_present(target, 'rev2')
445
def test_transform_fallback_location_hook(self):
446
# The 'transform_fallback_location' branch hook allows us to inspect
447
# and transform the URL of the fallback location for the branch.
448
stack_on = self.make_branch('stack-on')
449
stacked = self.make_branch('stacked')
451
stacked.set_stacked_on_url('../stack-on')
452
except unstackable_format_errors, e:
453
raise TestNotApplicable('Format does not support stacking.')
454
self.get_transport().rename('stack-on', 'new-stack-on')
456
def hook(stacked_branch, url):
457
hook_calls.append(url)
458
return '../new-stack-on'
459
branch.Branch.hooks.install_named_hook(
460
'transform_fallback_location', hook, None)
461
branch.Branch.open('stacked')
462
self.assertEqual(['../stack-on'], hook_calls)
464
def test_stack_on_repository_branch(self):
465
# Stacking should work when the repo isn't co-located with the
468
repo = self.make_repository('repo', shared=True)
469
except errors.IncompatibleFormat:
470
raise TestNotApplicable()
471
# Avoid make_branch, which produces standalone branches.
472
bzrdir = self.make_bzrdir('repo/stack-on')
474
b = bzrdir.create_branch()
475
except errors.UninitializableFormat:
476
raise TestNotApplicable()
477
transport = self.get_transport('stacked')
478
b.bzrdir.clone_on_transport(transport, stacked_on=b.base)
479
# Ensure that opening the branch doesn't raise.
480
branch.Branch.open(transport.base)
482
def test_revision_history_of_stacked(self):
483
# See <https://launchpad.net/bugs/380314>.
484
stack_on = self.make_branch_and_tree('stack-on')
485
stack_on.commit('first commit', rev_id='rev1')
487
stacked_dir = stack_on.bzrdir.sprout(
488
self.get_url('stacked'), stacked=True)
489
except unstackable_format_errors, e:
490
raise TestNotApplicable('Format does not support stacking.')
492
stacked = stacked_dir.open_workingtree()
493
except errors.NoWorkingTree:
494
stacked = stacked_dir.open_branch().create_checkout(
495
'stacked-checkout', lightweight=True)
496
tree = stacked.branch.create_checkout('local')
497
tree.commit('second commit', rev_id='rev2')
498
# Sanity check: stacked's repo should not contain rev1, otherwise this
499
# test isn't testing what it's supposed to.
500
repo = stacked.branch.repository.bzrdir.open_repository()
502
self.addCleanup(repo.unlock)
503
self.assertEqual({}, repo.get_parent_map(['rev1']))
504
# revision_history should work, even though the history is spread over
505
# multiple repositories.
506
self.assertLength(2, stacked.branch.revision_history())
509
class TestStackingConnections(
510
transport_util.TestCaseWithConnectionHookedTransport):
513
super(TestStackingConnections, self).setUp()
515
base_tree = self.make_branch_and_tree('base',
516
format=self.bzrdir_format)
517
except errors.UninitializableFormat, e:
518
raise TestNotApplicable(e)
519
stacked = self.make_branch('stacked', format=self.bzrdir_format)
521
stacked.set_stacked_on_url(base_tree.branch.base)
522
except unstackable_format_errors, e:
523
raise TestNotApplicable(e)
524
base_tree.commit('first', rev_id='rev-base')
525
stacked.set_last_revision_info(1, 'rev-base')
526
stacked_relative = self.make_branch('stacked_relative',
527
format=self.bzrdir_format)
528
stacked_relative.set_stacked_on_url('../base')
529
stacked.set_last_revision_info(1, 'rev-base')
530
self.start_logging_connections()
532
def test_open_stacked(self):
533
b = branch.Branch.open(self.get_url('stacked'))
534
rev = b.repository.get_revision('rev-base')
535
self.assertEqual(1, len(self.connections))
537
def test_open_stacked_relative(self):
538
b = branch.Branch.open(self.get_url('stacked_relative'))
539
rev = b.repository.get_revision('rev-base')
540
self.assertEqual(1, len(self.connections))