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):
87
# Note: RemoteBzrDirFormat is in bzrdir.py
89
class RemoteBzrDir(BzrDir, _RpcHelper):
335
90
"""Control directory on a remote server, accessed via bzr:// or similar."""
337
92
def __init__(self, transport, format, _client=None, _force_probe=False):
1622
1297
@needs_read_lock
1623
def search_missing_revision_ids(self, other,
1624
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
1625
find_ghosts=True, revision_ids=None, if_present_ids=None,
1298
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1627
1299
"""Return the revision ids that other has that this does not.
1629
1301
These are returned in topological order.
1631
1303
revision_id: only return revision ids included by revision_id.
1633
if symbol_versioning.deprecated_passed(revision_id):
1634
symbol_versioning.warn(
1635
'search_missing_revision_ids(revision_id=...) was '
1636
'deprecated in 2.4. Use revision_ids=[...] instead.',
1637
DeprecationWarning, stacklevel=2)
1638
if revision_ids is not None:
1639
raise AssertionError(
1640
'revision_ids is mutually exclusive with revision_id')
1641
if revision_id is not None:
1642
revision_ids = [revision_id]
1643
inter_repo = _mod_repository.InterRepository.get(other, self)
1644
return inter_repo.search_missing_revision_ids(
1645
find_ghosts=find_ghosts, revision_ids=revision_ids,
1646
if_present_ids=if_present_ids, limit=limit)
1305
return repository.InterRepository.get(
1306
other, self).search_missing_revision_ids(revision_id, find_ghosts)
1648
def fetch(self, source, revision_id=None, find_ghosts=False,
1308
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1649
1309
fetch_spec=None):
1650
1310
# No base implementation to use as RemoteRepository is not a subclass
1651
1311
# of Repository; so this is a copy of Repository.fetch().
2359
2025
def network_name(self):
2360
2026
return self._network_name
2362
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2363
return a_bzrdir.open_branch(name=name,
2364
ignore_fallbacks=ignore_fallbacks)
2028
def open(self, a_bzrdir, ignore_fallbacks=False):
2029
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2366
def _vfs_initialize(self, a_bzrdir, name, append_revisions_only):
2031
def _vfs_initialize(self, a_bzrdir):
2367
2032
# Initialisation when using a local bzrdir object, or a non-vfs init
2368
2033
# method is not available on the server.
2369
2034
# self._custom_format is always set - the start of initialize ensures
2371
2036
if isinstance(a_bzrdir, RemoteBzrDir):
2372
2037
a_bzrdir._ensure_real()
2373
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2374
name, append_revisions_only=append_revisions_only)
2038
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2376
2040
# 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)
2041
result = self._custom_format.initialize(a_bzrdir)
2379
2042
if (isinstance(a_bzrdir, RemoteBzrDir) and
2380
2043
not isinstance(result, RemoteBranch)):
2381
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2044
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2385
def initialize(self, a_bzrdir, name=None, repository=None,
2386
append_revisions_only=None):
2047
def initialize(self, a_bzrdir):
2387
2048
# 1) get the network name to use.
2388
2049
if self._custom_format:
2389
2050
network_name = self._custom_format.network_name()
2391
2052
# Select the current bzrlib default and ask for that.
2392
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2053
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2393
2054
reference_format = reference_bzrdir_format.get_branch_format()
2394
2055
self._custom_format = reference_format
2395
2056
network_name = reference_format.network_name()
2396
2057
# Being asked to create on a non RemoteBzrDir:
2397
2058
if not isinstance(a_bzrdir, RemoteBzrDir):
2398
return self._vfs_initialize(a_bzrdir, name=name,
2399
append_revisions_only=append_revisions_only)
2059
return self._vfs_initialize(a_bzrdir)
2400
2060
medium = a_bzrdir._client._medium
2401
2061
if medium._is_remote_before((1, 13)):
2402
return self._vfs_initialize(a_bzrdir, name=name,
2403
append_revisions_only=append_revisions_only)
2062
return self._vfs_initialize(a_bzrdir)
2404
2063
# Creating on a remote bzr dir.
2405
2064
# 2) try direct creation via RPC
2406
2065
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2407
if name is not None:
2408
# XXX JRV20100304: Support creating colocated branches
2409
raise errors.NoColocatedBranchSupport(self)
2410
2066
verb = 'BzrDir.create_branch'
2412
2068
response = a_bzrdir._call(verb, path, network_name)
2413
2069
except errors.UnknownSmartMethod:
2414
2070
# Fallback - use vfs methods
2415
2071
medium._remember_remote_is_before((1, 13))
2416
return self._vfs_initialize(a_bzrdir, name=name,
2417
append_revisions_only=append_revisions_only)
2072
return self._vfs_initialize(a_bzrdir)
2418
2073
if response[0] != 'ok':
2419
2074
raise errors.UnexpectedSmartServerResponse(response)
2420
2075
# Turn the response into a RemoteRepository object.
2421
2076
format = RemoteBranchFormat(network_name=response[1])
2422
2077
repo_format = response_tuple_to_repo_format(response[3:])
2423
repo_path = response[2]
2424
if repository is not None:
2425
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2426
url_diff = urlutils.relative_url(repository.user_url,
2429
raise AssertionError(
2430
'repository.user_url %r does not match URL from server '
2431
'response (%r + %r)'
2432
% (repository.user_url, a_bzrdir.user_url, repo_path))
2433
remote_repo = repository
2078
if response[2] == '':
2079
repo_bzrdir = a_bzrdir
2436
repo_bzrdir = a_bzrdir
2438
repo_bzrdir = RemoteBzrDir(
2439
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2441
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2081
repo_bzrdir = RemoteBzrDir(
2082
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2084
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2442
2085
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2443
format=format, setup_stacking=False, name=name)
2444
if append_revisions_only:
2445
remote_branch.set_append_revisions_only(append_revisions_only)
2086
format=format, setup_stacking=False)
2446
2087
# XXX: We know this is a new branch, so it must have revno 0, revid
2447
2088
# NULL_REVISION. Creating the branch locked would make this be unable
2448
2089
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
3177
2731
medium = self._branch._client._medium
3178
2732
if medium._is_remote_before((1, 14)):
3179
2733
return self._vfs_set_option(value, name, section)
3180
if isinstance(value, dict):
3181
if medium._is_remote_before((2, 2)):
3182
return self._vfs_set_option(value, name, section)
3183
return self._set_config_option_dict(value, name, section)
3185
return self._set_config_option(value, name, section)
3187
def _set_config_option(self, value, name, section):
3189
2735
path = self._branch._remote_path()
3190
2736
response = self._branch._client.call('Branch.set_config_option',
3191
2737
path, self._branch._lock_token, self._branch._repo_lock_token,
3192
2738
value.encode('utf8'), name, section or '')
3193
2739
except errors.UnknownSmartMethod:
3194
medium = self._branch._client._medium
3195
2740
medium._remember_remote_is_before((1, 14))
3196
2741
return self._vfs_set_option(value, name, section)
3197
2742
if response != ():
3198
2743
raise errors.UnexpectedSmartServerResponse(response)
3200
def _serialize_option_dict(self, option_dict):
3202
for key, value in option_dict.items():
3203
if isinstance(key, unicode):
3204
key = key.encode('utf8')
3205
if isinstance(value, unicode):
3206
value = value.encode('utf8')
3207
utf8_dict[key] = value
3208
return bencode.bencode(utf8_dict)
3210
def _set_config_option_dict(self, value, name, section):
3212
path = self._branch._remote_path()
3213
serialised_dict = self._serialize_option_dict(value)
3214
response = self._branch._client.call(
3215
'Branch.set_config_option_dict',
3216
path, self._branch._lock_token, self._branch._repo_lock_token,
3217
serialised_dict, name, section or '')
3218
except errors.UnknownSmartMethod:
3219
medium = self._branch._client._medium
3220
medium._remember_remote_is_before((2, 2))
3221
return self._vfs_set_option(value, name, section)
3223
raise errors.UnexpectedSmartServerResponse(response)
3225
2745
def _real_object(self):
3226
2746
self._branch._ensure_real()
3227
2747
return self._branch._real_branch