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):
87
# Note: RemoteBzrDirFormat is in bzrdir.py
89
class RemoteBzrDir(BzrDir, _RpcHelper):
334
90
"""Control directory on a remote server, accessed via bzr:// or similar."""
336
92
def __init__(self, transport, format, _client=None, _force_probe=False):
542
284
def _get_branch_reference(self):
543
285
path = self._path_for_remote_call(self._client)
544
286
medium = self._client._medium
546
('BzrDir.open_branchV3', (2, 1)),
547
('BzrDir.open_branchV2', (1, 13)),
548
('BzrDir.open_branch', None),
550
for verb, required_version in candidate_calls:
551
if required_version and medium._is_remote_before(required_version):
287
if not medium._is_remote_before((1, 13)):
554
response = self._call(verb, path)
289
response = self._call('BzrDir.open_branchV2', path)
290
if response[0] not in ('ref', 'branch'):
291
raise errors.UnexpectedSmartServerResponse(response)
555
293
except errors.UnknownSmartMethod:
556
if required_version is None:
558
medium._remember_remote_is_before(required_version)
561
if verb == 'BzrDir.open_branch':
562
if response[0] != 'ok':
563
raise errors.UnexpectedSmartServerResponse(response)
564
if response[1] != '':
565
return ('ref', response[1])
567
return ('branch', '')
568
if response[0] not in ('ref', 'branch'):
294
medium._remember_remote_is_before((1, 13))
295
response = self._call('BzrDir.open_branch', path)
296
if response[0] != 'ok':
569
297
raise errors.UnexpectedSmartServerResponse(response)
298
if response[1] != '':
299
return ('ref', response[1])
301
return ('branch', '')
572
def _get_tree_branch(self, name=None):
303
def _get_tree_branch(self):
573
304
"""See BzrDir._get_tree_branch()."""
574
return None, self.open_branch(name=name)
305
return None, self.open_branch()
576
def open_branch(self, name=None, unsupported=False,
577
ignore_fallbacks=False):
307
def open_branch(self, _unsupported=False, ignore_fallbacks=False):
579
309
raise NotImplementedError('unsupported flag support not implemented yet.')
580
310
if self._next_open_branch_result is not None:
581
311
# See create_branch for details.
1612
1278
@needs_read_lock
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,
1279
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1617
1280
"""Return the revision ids that other has that this does not.
1619
1282
These are returned in topological order.
1621
1284
revision_id: only return revision ids included by revision_id.
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)
1286
return repository.InterRepository.get(
1287
other, self).search_missing_revision_ids(revision_id, find_ghosts)
1638
def fetch(self, source, revision_id=None, find_ghosts=False,
1289
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1639
1290
fetch_spec=None):
1640
1291
# No base implementation to use as RemoteRepository is not a subclass
1641
1292
# of Repository; so this is a copy of Repository.fetch().
2349
2006
def network_name(self):
2350
2007
return self._network_name
2352
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2353
return a_bzrdir.open_branch(name=name,
2354
ignore_fallbacks=ignore_fallbacks)
2009
def open(self, a_bzrdir, ignore_fallbacks=False):
2010
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2356
def _vfs_initialize(self, a_bzrdir, name, append_revisions_only):
2012
def _vfs_initialize(self, a_bzrdir):
2357
2013
# Initialisation when using a local bzrdir object, or a non-vfs init
2358
2014
# method is not available on the server.
2359
2015
# self._custom_format is always set - the start of initialize ensures
2361
2017
if isinstance(a_bzrdir, RemoteBzrDir):
2362
2018
a_bzrdir._ensure_real()
2363
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2364
name, append_revisions_only=append_revisions_only)
2019
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2366
2021
# We assume the bzrdir is parameterised; it may not be.
2367
result = self._custom_format.initialize(a_bzrdir, name,
2368
append_revisions_only=append_revisions_only)
2022
result = self._custom_format.initialize(a_bzrdir)
2369
2023
if (isinstance(a_bzrdir, RemoteBzrDir) and
2370
2024
not isinstance(result, RemoteBranch)):
2371
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2025
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2375
def initialize(self, a_bzrdir, name=None, repository=None,
2376
append_revisions_only=None):
2028
def initialize(self, a_bzrdir):
2377
2029
# 1) get the network name to use.
2378
2030
if self._custom_format:
2379
2031
network_name = self._custom_format.network_name()
2381
2033
# Select the current bzrlib default and ask for that.
2382
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2034
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2383
2035
reference_format = reference_bzrdir_format.get_branch_format()
2384
2036
self._custom_format = reference_format
2385
2037
network_name = reference_format.network_name()
2386
2038
# Being asked to create on a non RemoteBzrDir:
2387
2039
if not isinstance(a_bzrdir, RemoteBzrDir):
2388
return self._vfs_initialize(a_bzrdir, name=name,
2389
append_revisions_only=append_revisions_only)
2040
return self._vfs_initialize(a_bzrdir)
2390
2041
medium = a_bzrdir._client._medium
2391
2042
if medium._is_remote_before((1, 13)):
2392
return self._vfs_initialize(a_bzrdir, name=name,
2393
append_revisions_only=append_revisions_only)
2043
return self._vfs_initialize(a_bzrdir)
2394
2044
# Creating on a remote bzr dir.
2395
2045
# 2) try direct creation via RPC
2396
2046
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2397
if name is not None:
2398
# XXX JRV20100304: Support creating colocated branches
2399
raise errors.NoColocatedBranchSupport(self)
2400
2047
verb = 'BzrDir.create_branch'
2402
2049
response = a_bzrdir._call(verb, path, network_name)
2403
2050
except errors.UnknownSmartMethod:
2404
2051
# Fallback - use vfs methods
2405
2052
medium._remember_remote_is_before((1, 13))
2406
return self._vfs_initialize(a_bzrdir, name=name,
2407
append_revisions_only=append_revisions_only)
2053
return self._vfs_initialize(a_bzrdir)
2408
2054
if response[0] != 'ok':
2409
2055
raise errors.UnexpectedSmartServerResponse(response)
2410
2056
# Turn the response into a RemoteRepository object.
2411
2057
format = RemoteBranchFormat(network_name=response[1])
2412
2058
repo_format = response_tuple_to_repo_format(response[3:])
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
2059
if response[2] == '':
2060
repo_bzrdir = a_bzrdir
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)
2062
repo_bzrdir = RemoteBzrDir(
2063
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2065
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2432
2066
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2433
format=format, setup_stacking=False, name=name)
2434
if append_revisions_only:
2435
remote_branch.set_append_revisions_only(append_revisions_only)
2067
format=format, setup_stacking=False)
2436
2068
# XXX: We know this is a new branch, so it must have revno 0, revid
2437
2069
# NULL_REVISION. Creating the branch locked would make this be unable
2438
2070
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
3167
2712
medium = self._branch._client._medium
3168
2713
if medium._is_remote_before((1, 14)):
3169
2714
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):
3179
2716
path = self._branch._remote_path()
3180
2717
response = self._branch._client.call('Branch.set_config_option',
3181
2718
path, self._branch._lock_token, self._branch._repo_lock_token,
3182
2719
value.encode('utf8'), name, section or '')
3183
2720
except errors.UnknownSmartMethod:
3184
medium = self._branch._client._medium
3185
2721
medium._remember_remote_is_before((1, 14))
3186
2722
return self._vfs_set_option(value, name, section)
3187
2723
if response != ():
3188
2724
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)
3215
2726
def _real_object(self):
3216
2727
self._branch._ensure_real()
3217
2728
return self._branch._real_branch
3300
2811
'Missing key %r in context %r', key_err.args[0], context)
3303
if err.error_verb == 'NoSuchRevision':
2814
if err.error_verb == 'IncompatibleRepositories':
2815
raise errors.IncompatibleRepositories(err.error_args[0],
2816
err.error_args[1], err.error_args[2])
2817
elif err.error_verb == 'NoSuchRevision':
3304
2818
raise NoSuchRevision(find('branch'), err.error_args[0])
3305
2819
elif err.error_verb == 'nosuchrevision':
3306
2820
raise NoSuchRevision(find('repository'), err.error_args[0])
3307
elif err.error_verb == 'nobranch':
3308
if len(err.error_args) >= 1:
3309
extra = err.error_args[0]
3312
raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2821
elif err.error_tuple == ('nobranch',):
2822
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
3314
2823
elif err.error_verb == 'norepository':
3315
2824
raise errors.NoRepositoryPresent(find('bzrdir'))
2825
elif err.error_verb == 'LockContention':
2826
raise errors.LockContention('(remote lock)')
3316
2827
elif err.error_verb == 'UnlockableTransport':
3317
2828
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2829
elif err.error_verb == 'LockFailed':
2830
raise errors.LockFailed(err.error_args[0], err.error_args[1])
3318
2831
elif err.error_verb == 'TokenMismatch':
3319
2832
raise errors.TokenMismatch(find('token'), '(remote token)')
3320
2833
elif err.error_verb == 'Diverged':
3321
2834
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2835
elif err.error_verb == 'TipChangeRejected':
2836
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2837
elif err.error_verb == 'UnstackableBranchFormat':
2838
raise errors.UnstackableBranchFormat(*err.error_args)
2839
elif err.error_verb == 'UnstackableRepositoryFormat':
2840
raise errors.UnstackableRepositoryFormat(*err.error_args)
3322
2841
elif err.error_verb == 'NotStacked':
3323
2842
raise errors.NotStacked(branch=find('branch'))
3324
2843
elif err.error_verb == 'PermissionDenied':