92
# Note: RemoteBzrDirFormat is in bzrdir.py
94
class RemoteBzrDir(BzrDir, _RpcHelper):
90
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
91
# does not have to be imported unless a remote format is involved.
93
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
94
"""Format representing bzrdirs accessed via a smart server"""
96
supports_workingtrees = False
99
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
100
# XXX: It's a bit ugly that the network name is here, because we'd
101
# like to believe that format objects are stateless or at least
102
# immutable, However, we do at least avoid mutating the name after
103
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
104
self._network_name = None
107
return "%s(_network_name=%r)" % (self.__class__.__name__,
110
def get_format_description(self):
111
if self._network_name:
112
real_format = controldir.network_format_registry.get(self._network_name)
113
return 'Remote: ' + real_format.get_format_description()
114
return 'bzr remote bzrdir'
116
def get_format_string(self):
117
raise NotImplementedError(self.get_format_string)
119
def network_name(self):
120
if self._network_name:
121
return self._network_name
123
raise AssertionError("No network name set.")
125
def initialize_on_transport(self, transport):
127
# hand off the request to the smart server
128
client_medium = transport.get_smart_medium()
129
except errors.NoSmartMedium:
130
# TODO: lookup the local format from a server hint.
131
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
132
return local_dir_format.initialize_on_transport(transport)
133
client = _SmartClient(client_medium)
134
path = client.remote_path_from_transport(transport)
136
response = client.call('BzrDirFormat.initialize', path)
137
except errors.ErrorFromSmartServer, err:
138
_translate_error(err, path=path)
139
if response[0] != 'ok':
140
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
141
format = RemoteBzrDirFormat()
142
self._supply_sub_formats_to(format)
143
return RemoteBzrDir(transport, format)
145
def parse_NoneTrueFalse(self, arg):
152
raise AssertionError("invalid arg %r" % arg)
154
def _serialize_NoneTrueFalse(self, arg):
161
def _serialize_NoneString(self, arg):
164
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
165
create_prefix=False, force_new_repo=False, stacked_on=None,
166
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
169
# hand off the request to the smart server
170
client_medium = transport.get_smart_medium()
171
except errors.NoSmartMedium:
174
# Decline to open it if the server doesn't support our required
175
# version (3) so that the VFS-based transport will do it.
176
if client_medium.should_probe():
178
server_version = client_medium.protocol_version()
179
if server_version != '2':
183
except errors.SmartProtocolError:
184
# Apparently there's no usable smart server there, even though
185
# the medium supports the smart protocol.
190
client = _SmartClient(client_medium)
191
path = client.remote_path_from_transport(transport)
192
if client_medium._is_remote_before((1, 16)):
195
# TODO: lookup the local format from a server hint.
196
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
197
self._supply_sub_formats_to(local_dir_format)
198
return local_dir_format.initialize_on_transport_ex(transport,
199
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
200
force_new_repo=force_new_repo, stacked_on=stacked_on,
201
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
202
make_working_trees=make_working_trees, shared_repo=shared_repo,
204
return self._initialize_on_transport_ex_rpc(client, path, transport,
205
use_existing_dir, create_prefix, force_new_repo, stacked_on,
206
stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
208
def _initialize_on_transport_ex_rpc(self, client, path, transport,
209
use_existing_dir, create_prefix, force_new_repo, stacked_on,
210
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
212
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
213
args.append(self._serialize_NoneTrueFalse(create_prefix))
214
args.append(self._serialize_NoneTrueFalse(force_new_repo))
215
args.append(self._serialize_NoneString(stacked_on))
216
# stack_on_pwd is often/usually our transport
219
stack_on_pwd = transport.relpath(stack_on_pwd)
222
except errors.PathNotChild:
224
args.append(self._serialize_NoneString(stack_on_pwd))
225
args.append(self._serialize_NoneString(repo_format_name))
226
args.append(self._serialize_NoneTrueFalse(make_working_trees))
227
args.append(self._serialize_NoneTrueFalse(shared_repo))
228
request_network_name = self._network_name or \
229
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
231
response = client.call('BzrDirFormat.initialize_ex_1.16',
232
request_network_name, path, *args)
233
except errors.UnknownSmartMethod:
234
client._medium._remember_remote_is_before((1,16))
235
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
236
self._supply_sub_formats_to(local_dir_format)
237
return local_dir_format.initialize_on_transport_ex(transport,
238
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
239
force_new_repo=force_new_repo, stacked_on=stacked_on,
240
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
241
make_working_trees=make_working_trees, shared_repo=shared_repo,
243
except errors.ErrorFromSmartServer, err:
244
_translate_error(err, path=path)
245
repo_path = response[0]
246
bzrdir_name = response[6]
247
require_stacking = response[7]
248
require_stacking = self.parse_NoneTrueFalse(require_stacking)
249
format = RemoteBzrDirFormat()
250
format._network_name = bzrdir_name
251
self._supply_sub_formats_to(format)
252
bzrdir = RemoteBzrDir(transport, format, _client=client)
254
repo_format = response_tuple_to_repo_format(response[1:])
258
repo_bzrdir_format = RemoteBzrDirFormat()
259
repo_bzrdir_format._network_name = response[5]
260
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
264
final_stack = response[8] or None
265
final_stack_pwd = response[9] or None
267
final_stack_pwd = urlutils.join(
268
transport.base, final_stack_pwd)
269
remote_repo = RemoteRepository(repo_bzr, repo_format)
270
if len(response) > 10:
271
# Updated server verb that locks remotely.
272
repo_lock_token = response[10] or None
273
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
275
remote_repo.dont_leave_lock_in_place()
277
remote_repo.lock_write()
278
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
279
final_stack_pwd, require_stacking)
280
policy.acquire_repository()
284
bzrdir._format.set_branch_format(self.get_branch_format())
286
# The repo has already been created, but we need to make sure that
287
# we'll make a stackable branch.
288
bzrdir._format.require_stacking(_skip_repo=True)
289
return remote_repo, bzrdir, require_stacking, policy
291
def _open(self, transport):
292
return RemoteBzrDir(transport, self)
294
def __eq__(self, other):
295
if not isinstance(other, RemoteBzrDirFormat):
297
return self.get_format_description() == other.get_format_description()
299
def __return_repository_format(self):
300
# Always return a RemoteRepositoryFormat object, but if a specific bzr
301
# repository format has been asked for, tell the RemoteRepositoryFormat
302
# that it should use that for init() etc.
303
result = RemoteRepositoryFormat()
304
custom_format = getattr(self, '_repository_format', None)
306
if isinstance(custom_format, RemoteRepositoryFormat):
309
# We will use the custom format to create repositories over the
310
# wire; expose its details like rich_root_data for code to
312
result._custom_format = custom_format
315
def get_branch_format(self):
316
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
317
if not isinstance(result, RemoteBranchFormat):
318
new_result = RemoteBranchFormat()
319
new_result._custom_format = result
321
self.set_branch_format(new_result)
325
repository_format = property(__return_repository_format,
326
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
329
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
95
330
"""Control directory on a remote server, accessed via bzr:// or similar."""
97
332
def __init__(self, transport, format, _client=None, _force_probe=False):