246
242
a_bzrdir = self.make_bzrdir('dir')
248
244
self.run_bzr_error(['At ../dir you have a valid .bzr control'],
250
246
working_dir='tree')
252
def test_push_with_revisionspec(self):
253
"""We should be able to push a revision older than the tip."""
254
tree_from = self.make_branch_and_tree('from')
255
tree_from.commit("One.", rev_id="from-1")
256
tree_from.commit("Two.", rev_id="from-2")
258
self.run_bzr('push -r1 ../to', working_dir='from')
260
tree_to = WorkingTree.open('to')
261
repo_to = tree_to.branch.repository
262
self.assertTrue(repo_to.has_revision('from-1'))
263
self.assertFalse(repo_to.has_revision('from-2'))
264
self.assertEqual(tree_to.branch.last_revision_info()[1], 'from-1')
267
"bzr: ERROR: bzr push --revision takes one value.\n",
268
'push -r0..2 ../to', working_dir='from')
270
def create_trunk_and_feature_branch(self):
272
trunk_tree = self.make_branch_and_tree('target',
273
format='development')
274
trunk_tree.commit('mainline')
275
# and a branch from it
276
branch_tree = self.make_branch_and_tree('branch',
277
format='development')
278
branch_tree.pull(trunk_tree.branch)
279
branch_tree.branch.set_parent(trunk_tree.branch.base)
280
# with some work on it
281
branch_tree.commit('moar work plz')
282
return trunk_tree, branch_tree
284
def assertPublished(self, branch_revid, stacked_on):
285
"""Assert that the branch 'published' has been published correctly."""
286
published_branch = Branch.open('published')
287
# The published branch refers to the mainline
288
self.assertEqual(stacked_on, published_branch.get_stacked_on_url())
289
# and the branch's work was pushed
290
self.assertTrue(published_branch.repository.has_revision(branch_revid))
292
def test_push_new_branch_stacked_on(self):
293
"""Pushing a new branch with --stacked-on creates a stacked branch."""
294
trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
295
# we publish branch_tree with a reference to the mainline.
296
out, err = self.run_bzr(['push', '--stacked-on', trunk_tree.branch.base,
297
self.get_url('published')], working_dir='branch')
298
self.assertEqual('', out)
299
self.assertEqual('Created new stacked branch referring to %s.\n' %
300
trunk_tree.branch.base, err)
301
self.assertPublished(branch_tree.last_revision(),
302
trunk_tree.branch.base)
304
def test_push_new_branch_stacked_uses_parent_when_no_public_url(self):
305
"""When the parent has no public url the parent is used as-is."""
306
trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
307
# now we do a stacked push, which should determine the public location
309
out, err = self.run_bzr(['push', '--stacked',
310
self.get_url('published')], working_dir='branch')
311
self.assertEqual('', out)
312
self.assertEqual('Created new stacked branch referring to %s.\n' %
313
trunk_tree.branch.base, err)
314
self.assertPublished(branch_tree.last_revision(), trunk_tree.branch.base)
316
def test_push_new_branch_stacked_uses_parent_public(self):
317
"""Pushing a new branch with --stacked creates a stacked branch."""
318
trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
319
# the trunk is published on a web server
320
self.transport_readonly_server = HttpServer
321
trunk_public = self.make_branch('public_trunk', format='development')
322
trunk_public.pull(trunk_tree.branch)
323
trunk_public_url = self.get_readonly_url('public_trunk')
324
trunk_tree.branch.set_public_branch(trunk_public_url)
325
# now we do a stacked push, which should determine the public location
327
out, err = self.run_bzr(['push', '--stacked',
328
self.get_url('published')], working_dir='branch')
329
self.assertEqual('', out)
330
self.assertEqual('Created new stacked branch referring to %s.\n' %
331
trunk_public_url, err)
332
self.assertPublished(branch_tree.last_revision(), trunk_public_url)
334
def test_push_new_branch_stacked_no_parent(self):
335
"""Pushing with --stacked and no parent branch errors."""
336
branch = self.make_branch_and_tree('branch', format='development')
337
# now we do a stacked push, which should fail as the place to refer too
338
# cannot be determined.
339
out, err = self.run_bzr_error(
340
['Could not determine branch to refer to\\.'], ['push', '--stacked',
341
self.get_url('published')], working_dir='branch')
342
self.assertEqual('', out)
343
self.assertFalse(self.get_transport('published').has('.'))
345
def test_push_notifies_default_stacking(self):
346
self.make_branch('stack_on', format='development1')
347
self.make_bzrdir('.').get_config().set_default_stack_on('stack_on')
348
self.make_branch('from', format='development1')
349
out, err = self.run_bzr('push -d from to')
350
self.assertContainsRe(err,
351
'Using default stacking branch stack_on at .*')
354
class RedirectingMemoryTransport(MemoryTransport):
356
def mkdir(self, path, mode=None):
357
path = self.abspath(path)[len(self._scheme):]
358
if path == '/source':
359
raise errors.RedirectRequested(
360
path, self._scheme + '/target', is_permanent=True)
361
elif path == '/infinite-loop':
362
raise errors.RedirectRequested(
363
path, self._scheme + '/infinite-loop', is_permanent=True)
365
return super(RedirectingMemoryTransport, self).mkdir(
369
class RedirectingMemoryServer(MemoryServer):
372
self._dirs = {'/': None}
375
self._scheme = 'redirecting-memory+%s:///' % id(self)
376
register_transport(self._scheme, self._memory_factory)
378
def _memory_factory(self, url):
379
result = RedirectingMemoryTransport(url)
380
result._dirs = self._dirs
381
result._files = self._files
382
result._locks = self._locks
386
unregister_transport(self._scheme, self._memory_factory)
389
class TestPushRedirect(ExternalBase):
392
ExternalBase.setUp(self)
393
self.memory_server = RedirectingMemoryServer()
394
self.memory_server.setUp()
395
self.addCleanup(self.memory_server.tearDown)
397
# Make the branch and tree that we'll be pushing.
398
t = self.make_branch_and_tree('tree')
399
self.build_tree(['tree/file'])
403
def test_push_redirects_on_mkdir(self):
404
"""If the push requires a mkdir, push respects redirect requests.
406
This is added primarily to handle lp:/ URI support, so that users can
407
push to new branches by specifying lp:/ URIs.
410
destination_url = self.memory_server.get_url() + 'source'
411
self.run_bzr('push %s' % destination_url)
414
local_revision = Branch.open('tree').last_revision()
415
remote_revision = Branch.open(
416
self.memory_server.get_url() + 'target').last_revision()
417
self.assertEqual(remote_revision, local_revision)
419
def test_push_gracefully_handles_too_many_redirects(self):
420
"""Push fails gracefully if the mkdir generates a large number of
424
destination_url = self.memory_server.get_url() + 'infinite-loop'
425
out, err = self.run_bzr_error(
426
['Too many redirections trying to make %s\\.\n'
427
% re.escape(destination_url)],
428
'push %s' % destination_url, retcode=3)
430
self.assertEqual('', out)