28
29
from bzrlib.osutils import abspath
29
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
31
from bzrlib.tests.blackbox import ExternalBase
32
from bzrlib.transport import register_transport, unregister_transport
33
from bzrlib.transport.memory import MemoryServer, MemoryTransport
31
34
from bzrlib.uncommit import uncommit
32
35
from bzrlib.urlutils import local_path_from_url
33
36
from bzrlib.workingtree import WorkingTree
59
62
# test push for failure without push location set
60
63
os.chdir('branch_a')
61
out = self.runbzr('push', retcode=3)
64
out = self.run_bzr('push', retcode=3)
62
65
self.assertEquals(out,
63
66
('','bzr: ERROR: No push location known or specified.\n'))
65
68
# test not remembered if cannot actually push
66
self.run_bzr('push', '../path/which/doesnt/exist', retcode=3)
69
self.run_bzr('push ../path/which/doesnt/exist', retcode=3)
67
70
out = self.run_bzr('push', retcode=3)
69
72
('', 'bzr: ERROR: No push location known or specified.\n'),
72
75
# test implicit --remember when no push location set, push fails
73
out = self.run_bzr('push', '../branch_b', retcode=3)
76
out = self.run_bzr('push ../branch_b', retcode=3)
74
77
self.assertEquals(out,
75
78
('','bzr: ERROR: These branches have diverged. '
76
79
'Try using "merge" and then "push".\n'))
91
94
self.assertEqual(path,
92
95
branch_b.bzrdir.root_transport.base)
93
96
# test explicit --remember
94
self.run_bzr('push', '../branch_c', '--remember')
97
self.run_bzr('push ../branch_c --remember')
95
98
self.assertEquals(branch_a.get_push_location(),
96
99
branch_c.bzrdir.root_transport.base)
98
101
def test_push_without_tree(self):
99
102
# bzr push from a branch that does not have a checkout should work.
100
103
b = self.make_branch('.')
101
out, err = self.run_bzr('push', 'pushed-location')
104
out, err = self.run_bzr('push pushed-location')
102
105
self.assertEqual('', out)
103
106
self.assertEqual('Created new branch.\n', err)
104
107
b2 = Branch.open('pushed-location')
150
153
# Now that we have a repository with shared files, make sure
151
154
# that things aren't copied out by a 'push'
152
155
os.chdir('repo/b')
153
self.run_bzr('push', '../../push-b')
156
self.run_bzr('push ../../push-b')
154
157
pushed_tree = WorkingTree.open('../../push-b')
155
158
pushed_repo = pushed_tree.branch.repository
156
159
self.assertFalse(pushed_repo.has_revision('a-1'))
163
166
self.build_tree(['filename'])
164
167
t.add('filename', 'funky-chars<>%&;"\'')
165
168
t.commit('commit filename')
166
self.run_bzr('push', '../new-tree')
169
self.run_bzr('push ../new-tree')
168
171
def test_push_dash_d(self):
169
172
t = self.make_branch_and_tree('from')
170
173
t.commit(allow_pointless=True,
171
174
message='first commit')
172
self.runbzr('push -d from to-one')
175
self.run_bzr('push -d from to-one')
173
176
self.failUnlessExists('to-one')
174
self.runbzr('push -d %s %s'
177
self.run_bzr('push -d %s %s'
175
178
% tuple(map(urlutils.local_path_to_url, ['from', 'to-two'])))
176
179
self.failUnlessExists('to-two')
187
190
tree = self.create_simple_tree()
189
192
self.run_bzr_error(['Parent directory of ../new/tree does not exist'],
190
'push', '../new/tree',
191
194
working_dir='tree')
192
self.run_bzr('push', '../new/tree', '--create-prefix',
195
self.run_bzr('push ../new/tree --create-prefix',
193
196
working_dir='tree')
194
197
new_tree = WorkingTree.open('new/tree')
195
198
self.assertEqual(tree.last_revision(), new_tree.last_revision())
206
209
self.run_bzr_error(['Target directory ../target already exists',
207
210
'Supply --use-existing-dir',
208
], 'push', '../target',
212
'push ../target', working_dir='tree')
211
self.run_bzr('push', '--use-existing-dir', '../target',
214
self.run_bzr('push --use-existing-dir ../target',
212
215
working_dir='tree')
214
217
new_tree = WorkingTree.open('target')
242
245
a_bzrdir = self.make_bzrdir('dir')
244
247
self.run_bzr_error(['At ../dir you have a valid .bzr control'],
246
249
working_dir='tree')
252
class RedirectingMemoryTransport(MemoryTransport):
254
def mkdir(self, path, mode=None):
255
path = self.abspath(path)[len(self._scheme):]
256
if path == '/source':
257
raise errors.RedirectRequested(
258
path, self._scheme + '/target', is_permanent=True)
259
elif path == '/infinite-loop':
260
raise errors.RedirectRequested(
261
path, self._scheme + '/infinite-loop', is_permanent=True)
263
return super(RedirectingMemoryTransport, self).mkdir(
267
class RedirectingMemoryServer(MemoryServer):
270
self._dirs = {'/': None}
273
self._scheme = 'redirecting-memory+%s:///' % id(self)
274
register_transport(self._scheme, self._memory_factory)
276
def _memory_factory(self, url):
277
result = RedirectingMemoryTransport(url)
278
result._dirs = self._dirs
279
result._files = self._files
280
result._locks = self._locks
284
unregister_transport(self._scheme, self._memory_factory)
287
class TestPushRedirect(ExternalBase):
290
ExternalBase.setUp(self)
291
self.memory_server = RedirectingMemoryServer()
292
self.memory_server.setUp()
293
self.addCleanup(self.memory_server.tearDown)
295
# Make the branch and tree that we'll be pushing.
296
t = self.make_branch_and_tree('tree')
297
self.build_tree(['tree/file'])
301
def test_push_redirects_on_mkdir(self):
302
"""If the push requires a mkdir, push respects redirect requests.
304
This is added primarily to handle lp:/ URI support, so that users can
305
push to new branches by specifying lp:/ URIs.
308
destination_url = self.memory_server.get_url() + 'source'
309
self.run_bzr('push %s' % destination_url)
312
local_revision = Branch.open('tree').last_revision()
313
remote_revision = Branch.open(
314
self.memory_server.get_url() + 'target').last_revision()
315
self.assertEqual(remote_revision, local_revision)
317
def test_push_gracefully_handles_too_many_redirects(self):
318
"""Push fails gracefully if the mkdir generates a large number of
322
destination_url = self.memory_server.get_url() + 'infinite-loop'
323
out, err = self.run_bzr_error(
324
['Too many redirections trying to make %s\\.\n'
325
% re.escape(destination_url)],
326
'push %s' % destination_url, retcode=3)
328
self.assertEqual('', out)