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