95
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
96
# does not have to be imported unless a remote format is involved.
98
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
99
"""Format representing bzrdirs accessed via a smart server"""
101
supports_workingtrees = False
104
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
105
# XXX: It's a bit ugly that the network name is here, because we'd
106
# like to believe that format objects are stateless or at least
107
# immutable, However, we do at least avoid mutating the name after
108
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
109
self._network_name = None
112
return "%s(_network_name=%r)" % (self.__class__.__name__,
115
def get_format_description(self):
116
if self._network_name:
117
real_format = controldir.network_format_registry.get(self._network_name)
118
return 'Remote: ' + real_format.get_format_description()
119
return 'bzr remote bzrdir'
121
def get_format_string(self):
122
raise NotImplementedError(self.get_format_string)
124
def network_name(self):
125
if self._network_name:
126
return self._network_name
128
raise AssertionError("No network name set.")
130
def initialize_on_transport(self, transport):
132
# hand off the request to the smart server
133
client_medium = transport.get_smart_medium()
134
except errors.NoSmartMedium:
135
# TODO: lookup the local format from a server hint.
136
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
137
return local_dir_format.initialize_on_transport(transport)
138
client = _SmartClient(client_medium)
139
path = client.remote_path_from_transport(transport)
141
response = client.call('BzrDirFormat.initialize', path)
142
except errors.ErrorFromSmartServer, err:
143
_translate_error(err, path=path)
144
if response[0] != 'ok':
145
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
146
format = RemoteBzrDirFormat()
147
self._supply_sub_formats_to(format)
148
return RemoteBzrDir(transport, format)
150
def parse_NoneTrueFalse(self, arg):
157
raise AssertionError("invalid arg %r" % arg)
159
def _serialize_NoneTrueFalse(self, arg):
166
def _serialize_NoneString(self, arg):
169
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
170
create_prefix=False, force_new_repo=False, stacked_on=None,
171
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
174
# hand off the request to the smart server
175
client_medium = transport.get_smart_medium()
176
except errors.NoSmartMedium:
179
# Decline to open it if the server doesn't support our required
180
# version (3) so that the VFS-based transport will do it.
181
if client_medium.should_probe():
183
server_version = client_medium.protocol_version()
184
if server_version != '2':
188
except errors.SmartProtocolError:
189
# Apparently there's no usable smart server there, even though
190
# the medium supports the smart protocol.
195
client = _SmartClient(client_medium)
196
path = client.remote_path_from_transport(transport)
197
if client_medium._is_remote_before((1, 16)):
200
# TODO: lookup the local format from a server hint.
201
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
202
self._supply_sub_formats_to(local_dir_format)
203
return local_dir_format.initialize_on_transport_ex(transport,
204
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
205
force_new_repo=force_new_repo, stacked_on=stacked_on,
206
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
207
make_working_trees=make_working_trees, shared_repo=shared_repo,
209
return self._initialize_on_transport_ex_rpc(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
def _initialize_on_transport_ex_rpc(self, client, path, transport,
214
use_existing_dir, create_prefix, force_new_repo, stacked_on,
215
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
217
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
218
args.append(self._serialize_NoneTrueFalse(create_prefix))
219
args.append(self._serialize_NoneTrueFalse(force_new_repo))
220
args.append(self._serialize_NoneString(stacked_on))
221
# stack_on_pwd is often/usually our transport
224
stack_on_pwd = transport.relpath(stack_on_pwd)
227
except errors.PathNotChild:
229
args.append(self._serialize_NoneString(stack_on_pwd))
230
args.append(self._serialize_NoneString(repo_format_name))
231
args.append(self._serialize_NoneTrueFalse(make_working_trees))
232
args.append(self._serialize_NoneTrueFalse(shared_repo))
233
request_network_name = self._network_name or \
234
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
236
response = client.call('BzrDirFormat.initialize_ex_1.16',
237
request_network_name, path, *args)
238
except errors.UnknownSmartMethod:
239
client._medium._remember_remote_is_before((1,16))
240
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
241
self._supply_sub_formats_to(local_dir_format)
242
return local_dir_format.initialize_on_transport_ex(transport,
243
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
244
force_new_repo=force_new_repo, stacked_on=stacked_on,
245
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
246
make_working_trees=make_working_trees, shared_repo=shared_repo,
248
except errors.ErrorFromSmartServer, err:
249
_translate_error(err, path=path)
250
repo_path = response[0]
251
bzrdir_name = response[6]
252
require_stacking = response[7]
253
require_stacking = self.parse_NoneTrueFalse(require_stacking)
254
format = RemoteBzrDirFormat()
255
format._network_name = bzrdir_name
256
self._supply_sub_formats_to(format)
257
bzrdir = RemoteBzrDir(transport, format, _client=client)
259
repo_format = response_tuple_to_repo_format(response[1:])
263
repo_bzrdir_format = RemoteBzrDirFormat()
264
repo_bzrdir_format._network_name = response[5]
265
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
269
final_stack = response[8] or None
270
final_stack_pwd = response[9] or None
272
final_stack_pwd = urlutils.join(
273
transport.base, final_stack_pwd)
274
remote_repo = RemoteRepository(repo_bzr, repo_format)
275
if len(response) > 10:
276
# Updated server verb that locks remotely.
277
repo_lock_token = response[10] or None
278
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
280
remote_repo.dont_leave_lock_in_place()
282
remote_repo.lock_write()
283
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
284
final_stack_pwd, require_stacking)
285
policy.acquire_repository()
289
bzrdir._format.set_branch_format(self.get_branch_format())
291
# The repo has already been created, but we need to make sure that
292
# we'll make a stackable branch.
293
bzrdir._format.require_stacking(_skip_repo=True)
294
return remote_repo, bzrdir, require_stacking, policy
296
def _open(self, transport):
297
return RemoteBzrDir(transport, self)
299
def __eq__(self, other):
300
if not isinstance(other, RemoteBzrDirFormat):
302
return self.get_format_description() == other.get_format_description()
304
def __return_repository_format(self):
305
# Always return a RemoteRepositoryFormat object, but if a specific bzr
306
# repository format has been asked for, tell the RemoteRepositoryFormat
307
# that it should use that for init() etc.
308
result = RemoteRepositoryFormat()
309
custom_format = getattr(self, '_repository_format', None)
311
if isinstance(custom_format, RemoteRepositoryFormat):
314
# We will use the custom format to create repositories over the
315
# wire; expose its details like rich_root_data for code to
317
result._custom_format = custom_format
320
def get_branch_format(self):
321
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
322
if not isinstance(result, RemoteBranchFormat):
323
new_result = RemoteBranchFormat()
324
new_result._custom_format = result
326
self.set_branch_format(new_result)
330
repository_format = property(__return_repository_format,
331
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
334
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
92
# Note: RemoteBzrDirFormat is in bzrdir.py
94
class RemoteBzrDir(BzrDir, _RpcHelper):
335
95
"""Control directory on a remote server, accessed via bzr:// or similar."""
337
97
def __init__(self, transport, format, _client=None, _force_probe=False):
2371
2105
if isinstance(a_bzrdir, RemoteBzrDir):
2372
2106
a_bzrdir._ensure_real()
2373
2107
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2374
name, append_revisions_only=append_revisions_only)
2376
2110
# We assume the bzrdir is parameterised; it may not be.
2377
result = self._custom_format.initialize(a_bzrdir, name,
2378
append_revisions_only=append_revisions_only)
2111
result = self._custom_format.initialize(a_bzrdir, name)
2379
2112
if (isinstance(a_bzrdir, RemoteBzrDir) and
2380
2113
not isinstance(result, RemoteBranch)):
2381
2114
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2385
def initialize(self, a_bzrdir, name=None, repository=None,
2386
append_revisions_only=None):
2118
def initialize(self, a_bzrdir, name=None, repository=None):
2387
2119
# 1) get the network name to use.
2388
2120
if self._custom_format:
2389
2121
network_name = self._custom_format.network_name()
2391
2123
# Select the current bzrlib default and ask for that.
2392
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2124
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2393
2125
reference_format = reference_bzrdir_format.get_branch_format()
2394
2126
self._custom_format = reference_format
2395
2127
network_name = reference_format.network_name()
2396
2128
# Being asked to create on a non RemoteBzrDir:
2397
2129
if not isinstance(a_bzrdir, RemoteBzrDir):
2398
return self._vfs_initialize(a_bzrdir, name=name,
2399
append_revisions_only=append_revisions_only)
2130
return self._vfs_initialize(a_bzrdir, name=name)
2400
2131
medium = a_bzrdir._client._medium
2401
2132
if medium._is_remote_before((1, 13)):
2402
return self._vfs_initialize(a_bzrdir, name=name,
2403
append_revisions_only=append_revisions_only)
2133
return self._vfs_initialize(a_bzrdir, name=name)
2404
2134
# Creating on a remote bzr dir.
2405
2135
# 2) try direct creation via RPC
2406
2136
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)