89
# Note: RemoteBzrDirFormat is in bzrdir.py
91
class RemoteBzrDir(BzrDir, _RpcHelper):
94
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
95
# does not have to be imported unless a remote format is involved.
97
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
98
"""Format representing bzrdirs accessed via a smart server"""
100
supports_workingtrees = False
103
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
104
# XXX: It's a bit ugly that the network name is here, because we'd
105
# like to believe that format objects are stateless or at least
106
# immutable, However, we do at least avoid mutating the name after
107
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
108
self._network_name = None
111
return "%s(_network_name=%r)" % (self.__class__.__name__,
114
def get_format_description(self):
115
if self._network_name:
116
real_format = controldir.network_format_registry.get(self._network_name)
117
return 'Remote: ' + real_format.get_format_description()
118
return 'bzr remote bzrdir'
120
def get_format_string(self):
121
raise NotImplementedError(self.get_format_string)
123
def network_name(self):
124
if self._network_name:
125
return self._network_name
127
raise AssertionError("No network name set.")
129
def initialize_on_transport(self, transport):
131
# hand off the request to the smart server
132
client_medium = transport.get_smart_medium()
133
except errors.NoSmartMedium:
134
# TODO: lookup the local format from a server hint.
135
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
136
return local_dir_format.initialize_on_transport(transport)
137
client = _SmartClient(client_medium)
138
path = client.remote_path_from_transport(transport)
140
response = client.call('BzrDirFormat.initialize', path)
141
except errors.ErrorFromSmartServer, err:
142
_translate_error(err, path=path)
143
if response[0] != 'ok':
144
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
145
format = RemoteBzrDirFormat()
146
self._supply_sub_formats_to(format)
147
return RemoteBzrDir(transport, format)
149
def parse_NoneTrueFalse(self, arg):
156
raise AssertionError("invalid arg %r" % arg)
158
def _serialize_NoneTrueFalse(self, arg):
165
def _serialize_NoneString(self, arg):
168
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
169
create_prefix=False, force_new_repo=False, stacked_on=None,
170
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
173
# hand off the request to the smart server
174
client_medium = transport.get_smart_medium()
175
except errors.NoSmartMedium:
178
# Decline to open it if the server doesn't support our required
179
# version (3) so that the VFS-based transport will do it.
180
if client_medium.should_probe():
182
server_version = client_medium.protocol_version()
183
if server_version != '2':
187
except errors.SmartProtocolError:
188
# Apparently there's no usable smart server there, even though
189
# the medium supports the smart protocol.
194
client = _SmartClient(client_medium)
195
path = client.remote_path_from_transport(transport)
196
if client_medium._is_remote_before((1, 16)):
199
# TODO: lookup the local format from a server hint.
200
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
201
self._supply_sub_formats_to(local_dir_format)
202
return local_dir_format.initialize_on_transport_ex(transport,
203
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
204
force_new_repo=force_new_repo, stacked_on=stacked_on,
205
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
206
make_working_trees=make_working_trees, shared_repo=shared_repo,
208
return self._initialize_on_transport_ex_rpc(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
def _initialize_on_transport_ex_rpc(self, client, path, transport,
213
use_existing_dir, create_prefix, force_new_repo, stacked_on,
214
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
216
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
217
args.append(self._serialize_NoneTrueFalse(create_prefix))
218
args.append(self._serialize_NoneTrueFalse(force_new_repo))
219
args.append(self._serialize_NoneString(stacked_on))
220
# stack_on_pwd is often/usually our transport
223
stack_on_pwd = transport.relpath(stack_on_pwd)
226
except errors.PathNotChild:
228
args.append(self._serialize_NoneString(stack_on_pwd))
229
args.append(self._serialize_NoneString(repo_format_name))
230
args.append(self._serialize_NoneTrueFalse(make_working_trees))
231
args.append(self._serialize_NoneTrueFalse(shared_repo))
232
request_network_name = self._network_name or \
233
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
235
response = client.call('BzrDirFormat.initialize_ex_1.16',
236
request_network_name, path, *args)
237
except errors.UnknownSmartMethod:
238
client._medium._remember_remote_is_before((1,16))
239
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
240
self._supply_sub_formats_to(local_dir_format)
241
return local_dir_format.initialize_on_transport_ex(transport,
242
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
243
force_new_repo=force_new_repo, stacked_on=stacked_on,
244
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
245
make_working_trees=make_working_trees, shared_repo=shared_repo,
247
except errors.ErrorFromSmartServer, err:
248
_translate_error(err, path=path)
249
repo_path = response[0]
250
bzrdir_name = response[6]
251
require_stacking = response[7]
252
require_stacking = self.parse_NoneTrueFalse(require_stacking)
253
format = RemoteBzrDirFormat()
254
format._network_name = bzrdir_name
255
self._supply_sub_formats_to(format)
256
bzrdir = RemoteBzrDir(transport, format, _client=client)
258
repo_format = response_tuple_to_repo_format(response[1:])
262
repo_bzrdir_format = RemoteBzrDirFormat()
263
repo_bzrdir_format._network_name = response[5]
264
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
268
final_stack = response[8] or None
269
final_stack_pwd = response[9] or None
271
final_stack_pwd = urlutils.join(
272
transport.base, final_stack_pwd)
273
remote_repo = RemoteRepository(repo_bzr, repo_format)
274
if len(response) > 10:
275
# Updated server verb that locks remotely.
276
repo_lock_token = response[10] or None
277
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
279
remote_repo.dont_leave_lock_in_place()
281
remote_repo.lock_write()
282
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
283
final_stack_pwd, require_stacking)
284
policy.acquire_repository()
288
bzrdir._format.set_branch_format(self.get_branch_format())
290
# The repo has already been created, but we need to make sure that
291
# we'll make a stackable branch.
292
bzrdir._format.require_stacking(_skip_repo=True)
293
return remote_repo, bzrdir, require_stacking, policy
295
def _open(self, transport):
296
return RemoteBzrDir(transport, self)
298
def __eq__(self, other):
299
if not isinstance(other, RemoteBzrDirFormat):
301
return self.get_format_description() == other.get_format_description()
303
def __return_repository_format(self):
304
# Always return a RemoteRepositoryFormat object, but if a specific bzr
305
# repository format has been asked for, tell the RemoteRepositoryFormat
306
# that it should use that for init() etc.
307
result = RemoteRepositoryFormat()
308
custom_format = getattr(self, '_repository_format', None)
310
if isinstance(custom_format, RemoteRepositoryFormat):
313
# We will use the custom format to create repositories over the
314
# wire; expose its details like rich_root_data for code to
316
result._custom_format = custom_format
319
def get_branch_format(self):
320
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
321
if not isinstance(result, RemoteBranchFormat):
322
new_result = RemoteBranchFormat()
323
new_result._custom_format = result
325
self.set_branch_format(new_result)
329
repository_format = property(__return_repository_format,
330
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
333
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
92
334
"""Control directory on a remote server, accessed via bzr:// or similar."""
94
336
def __init__(self, transport, format, _client=None, _force_probe=False):
1335
1612
@needs_read_lock
1336
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1613
def search_missing_revision_ids(self, other,
1614
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
1615
find_ghosts=True, revision_ids=None, if_present_ids=None,
1337
1617
"""Return the revision ids that other has that this does not.
1339
1619
These are returned in topological order.
1341
1621
revision_id: only return revision ids included by revision_id.
1343
return repository.InterRepository.get(
1344
other, self).search_missing_revision_ids(revision_id, find_ghosts)
1623
if symbol_versioning.deprecated_passed(revision_id):
1624
symbol_versioning.warn(
1625
'search_missing_revision_ids(revision_id=...) was '
1626
'deprecated in 2.4. Use revision_ids=[...] instead.',
1627
DeprecationWarning, stacklevel=2)
1628
if revision_ids is not None:
1629
raise AssertionError(
1630
'revision_ids is mutually exclusive with revision_id')
1631
if revision_id is not None:
1632
revision_ids = [revision_id]
1633
inter_repo = _mod_repository.InterRepository.get(other, self)
1634
return inter_repo.search_missing_revision_ids(
1635
find_ghosts=find_ghosts, revision_ids=revision_ids,
1636
if_present_ids=if_present_ids, limit=limit)
1346
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1638
def fetch(self, source, revision_id=None, find_ghosts=False,
1347
1639
fetch_spec=None):
1348
1640
# No base implementation to use as RemoteRepository is not a subclass
1349
1641
# of Repository; so this is a copy of Repository.fetch().
2071
2361
if isinstance(a_bzrdir, RemoteBzrDir):
2072
2362
a_bzrdir._ensure_real()
2073
2363
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2364
name, append_revisions_only=append_revisions_only)
2076
2366
# We assume the bzrdir is parameterised; it may not be.
2077
result = self._custom_format.initialize(a_bzrdir, name)
2367
result = self._custom_format.initialize(a_bzrdir, name,
2368
append_revisions_only=append_revisions_only)
2078
2369
if (isinstance(a_bzrdir, RemoteBzrDir) and
2079
2370
not isinstance(result, RemoteBranch)):
2080
2371
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2084
def initialize(self, a_bzrdir, name=None):
2375
def initialize(self, a_bzrdir, name=None, repository=None,
2376
append_revisions_only=None):
2085
2377
# 1) get the network name to use.
2086
2378
if self._custom_format:
2087
2379
network_name = self._custom_format.network_name()
2089
2381
# Select the current bzrlib default and ask for that.
2090
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2382
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2091
2383
reference_format = reference_bzrdir_format.get_branch_format()
2092
2384
self._custom_format = reference_format
2093
2385
network_name = reference_format.network_name()
2094
2386
# Being asked to create on a non RemoteBzrDir:
2095
2387
if not isinstance(a_bzrdir, RemoteBzrDir):
2096
return self._vfs_initialize(a_bzrdir, name=name)
2388
return self._vfs_initialize(a_bzrdir, name=name,
2389
append_revisions_only=append_revisions_only)
2097
2390
medium = a_bzrdir._client._medium
2098
2391
if medium._is_remote_before((1, 13)):
2099
return self._vfs_initialize(a_bzrdir, name=name)
2392
return self._vfs_initialize(a_bzrdir, name=name,
2393
append_revisions_only=append_revisions_only)
2100
2394
# Creating on a remote bzr dir.
2101
2395
# 2) try direct creation via RPC
2102
2396
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2109
2403
except errors.UnknownSmartMethod:
2110
2404
# Fallback - use vfs methods
2111
2405
medium._remember_remote_is_before((1, 13))
2112
return self._vfs_initialize(a_bzrdir, name=name)
2406
return self._vfs_initialize(a_bzrdir, name=name,
2407
append_revisions_only=append_revisions_only)
2113
2408
if response[0] != 'ok':
2114
2409
raise errors.UnexpectedSmartServerResponse(response)
2115
2410
# Turn the response into a RemoteRepository object.
2116
2411
format = RemoteBranchFormat(network_name=response[1])
2117
2412
repo_format = response_tuple_to_repo_format(response[3:])
2118
if response[2] == '':
2119
repo_bzrdir = a_bzrdir
2413
repo_path = response[2]
2414
if repository is not None:
2415
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2416
url_diff = urlutils.relative_url(repository.user_url,
2419
raise AssertionError(
2420
'repository.user_url %r does not match URL from server '
2421
'response (%r + %r)'
2422
% (repository.user_url, a_bzrdir.user_url, repo_path))
2423
remote_repo = repository
2121
repo_bzrdir = RemoteBzrDir(
2122
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2124
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2426
repo_bzrdir = a_bzrdir
2428
repo_bzrdir = RemoteBzrDir(
2429
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2431
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2125
2432
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2126
2433
format=format, setup_stacking=False, name=name)
2434
if append_revisions_only:
2435
remote_branch.set_append_revisions_only(append_revisions_only)
2127
2436
# XXX: We know this is a new branch, so it must have revno 0, revid
2128
2437
# NULL_REVISION. Creating the branch locked would make this be unable
2129
2438
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2774
3167
medium = self._branch._client._medium
2775
3168
if medium._is_remote_before((1, 14)):
2776
3169
return self._vfs_set_option(value, name, section)
3170
if isinstance(value, dict):
3171
if medium._is_remote_before((2, 2)):
3172
return self._vfs_set_option(value, name, section)
3173
return self._set_config_option_dict(value, name, section)
3175
return self._set_config_option(value, name, section)
3177
def _set_config_option(self, value, name, section):
2778
3179
path = self._branch._remote_path()
2779
3180
response = self._branch._client.call('Branch.set_config_option',
2780
3181
path, self._branch._lock_token, self._branch._repo_lock_token,
2781
3182
value.encode('utf8'), name, section or '')
2782
3183
except errors.UnknownSmartMethod:
3184
medium = self._branch._client._medium
2783
3185
medium._remember_remote_is_before((1, 14))
2784
3186
return self._vfs_set_option(value, name, section)
2785
3187
if response != ():
2786
3188
raise errors.UnexpectedSmartServerResponse(response)
3190
def _serialize_option_dict(self, option_dict):
3192
for key, value in option_dict.items():
3193
if isinstance(key, unicode):
3194
key = key.encode('utf8')
3195
if isinstance(value, unicode):
3196
value = value.encode('utf8')
3197
utf8_dict[key] = value
3198
return bencode.bencode(utf8_dict)
3200
def _set_config_option_dict(self, value, name, section):
3202
path = self._branch._remote_path()
3203
serialised_dict = self._serialize_option_dict(value)
3204
response = self._branch._client.call(
3205
'Branch.set_config_option_dict',
3206
path, self._branch._lock_token, self._branch._repo_lock_token,
3207
serialised_dict, name, section or '')
3208
except errors.UnknownSmartMethod:
3209
medium = self._branch._client._medium
3210
medium._remember_remote_is_before((2, 2))
3211
return self._vfs_set_option(value, name, section)
3213
raise errors.UnexpectedSmartServerResponse(response)
2788
3215
def _real_object(self):
2789
3216
self._branch._ensure_real()
2790
3217
return self._branch._real_branch