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):
87
# Note: RemoteBzrDirFormat is in bzrdir.py
89
class RemoteBzrDir(BzrDir, _RpcHelper):
331
90
"""Control directory on a remote server, accessed via bzr:// or similar."""
333
92
def __init__(self, transport, format, _client=None, _force_probe=False):
537
280
def _get_branch_reference(self):
538
281
path = self._path_for_remote_call(self._client)
539
282
medium = self._client._medium
541
('BzrDir.open_branchV3', (2, 1)),
542
('BzrDir.open_branchV2', (1, 13)),
543
('BzrDir.open_branch', None),
545
for verb, required_version in candidate_calls:
546
if required_version and medium._is_remote_before(required_version):
283
if not medium._is_remote_before((1, 13)):
549
response = self._call(verb, path)
285
response = self._call('BzrDir.open_branchV2', path)
286
if response[0] not in ('ref', 'branch'):
287
raise errors.UnexpectedSmartServerResponse(response)
550
289
except errors.UnknownSmartMethod:
551
if required_version is None:
553
medium._remember_remote_is_before(required_version)
556
if verb == 'BzrDir.open_branch':
557
if response[0] != 'ok':
558
raise errors.UnexpectedSmartServerResponse(response)
559
if response[1] != '':
560
return ('ref', response[1])
562
return ('branch', '')
563
if response[0] not in ('ref', 'branch'):
290
medium._remember_remote_is_before((1, 13))
291
response = self._call('BzrDir.open_branch', path)
292
if response[0] != 'ok':
564
293
raise errors.UnexpectedSmartServerResponse(response)
294
if response[1] != '':
295
return ('ref', response[1])
297
return ('branch', '')
567
def _get_tree_branch(self, name=None):
299
def _get_tree_branch(self):
568
300
"""See BzrDir._get_tree_branch()."""
569
return None, self.open_branch(name=name)
301
return None, self.open_branch()
571
def open_branch(self, name=None, unsupported=False,
572
ignore_fallbacks=False):
303
def open_branch(self, _unsupported=False, ignore_fallbacks=False):
574
305
raise NotImplementedError('unsupported flag support not implemented yet.')
575
306
if self._next_open_branch_result is not None:
576
307
# See create_branch for details.
1510
1190
# state, so always add a lock here. If a caller passes us a locked
1511
1191
# repository, they are responsible for unlocking it later.
1512
1192
repository.lock_read()
1513
self._check_fallback_repository(repository)
1514
1193
self._fallback_repositories.append(repository)
1515
1194
# If self._real_repository was parameterised already (e.g. because a
1516
1195
# _real_branch had its get_stacked_on_url method called), then the
1517
1196
# repository to be added may already be in the _real_repositories list.
1518
1197
if self._real_repository is not None:
1519
fallback_locations = [repo.user_url for repo in
1198
fallback_locations = [repo.bzrdir.root_transport.base for repo in
1520
1199
self._real_repository._fallback_repositories]
1521
if repository.user_url not in fallback_locations:
1200
if repository.bzrdir.root_transport.base not in fallback_locations:
1522
1201
self._real_repository.add_fallback_repository(repository)
1524
def _check_fallback_repository(self, repository):
1525
"""Check that this repository can fallback to repository safely.
1527
Raise an error if not.
1529
:param repository: A repository to fallback to.
1531
return _mod_repository.InterRepository._assert_same_model(
1534
1203
def add_inventory(self, revid, inv, parents):
1535
1204
self._ensure_real()
1536
1205
return self._real_repository.add_inventory(revid, inv, parents)
1538
1207
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1539
parents, basis_inv=None, propagate_caches=False):
1540
1209
self._ensure_real()
1541
1210
return self._real_repository.add_inventory_by_delta(basis_revision_id,
1542
delta, new_revision_id, parents, basis_inv=basis_inv,
1543
propagate_caches=propagate_caches)
1211
delta, new_revision_id, parents)
1545
1213
def add_revision(self, rev_id, rev, inv=None, config=None):
1546
1214
self._ensure_real()
1605
1273
@needs_read_lock
1606
def search_missing_revision_ids(self, other,
1607
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
1608
find_ghosts=True, revision_ids=None, if_present_ids=None,
1274
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1610
1275
"""Return the revision ids that other has that this does not.
1612
1277
These are returned in topological order.
1614
1279
revision_id: only return revision ids included by revision_id.
1616
if symbol_versioning.deprecated_passed(revision_id):
1617
symbol_versioning.warn(
1618
'search_missing_revision_ids(revision_id=...) was '
1619
'deprecated in 2.4. Use revision_ids=[...] instead.',
1620
DeprecationWarning, stacklevel=2)
1621
if revision_ids is not None:
1622
raise AssertionError(
1623
'revision_ids is mutually exclusive with revision_id')
1624
if revision_id is not None:
1625
revision_ids = [revision_id]
1626
inter_repo = _mod_repository.InterRepository.get(other, self)
1627
return inter_repo.search_missing_revision_ids(
1628
find_ghosts=find_ghosts, revision_ids=revision_ids,
1629
if_present_ids=if_present_ids, limit=limit)
1281
return repository.InterRepository.get(
1282
other, self).search_missing_revision_ids(revision_id, find_ghosts)
1631
def fetch(self, source, revision_id=None, find_ghosts=False,
1284
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1632
1285
fetch_spec=None):
1633
1286
# No base implementation to use as RemoteRepository is not a subclass
1634
1287
# of Repository; so this is a copy of Repository.fetch().
2343
1995
self._network_name)
2345
1997
def get_format_description(self):
2347
return 'Remote: ' + self._custom_format.get_format_description()
1998
return 'Remote BZR Branch'
2349
2000
def network_name(self):
2350
2001
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)
2003
def open(self, a_bzrdir, ignore_fallbacks=False):
2004
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2356
def _vfs_initialize(self, a_bzrdir, name):
2006
def _vfs_initialize(self, a_bzrdir):
2357
2007
# Initialisation when using a local bzrdir object, or a non-vfs init
2358
2008
# method is not available on the server.
2359
2009
# self._custom_format is always set - the start of initialize ensures
2361
2011
if isinstance(a_bzrdir, RemoteBzrDir):
2362
2012
a_bzrdir._ensure_real()
2363
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2013
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2366
2015
# We assume the bzrdir is parameterised; it may not be.
2367
result = self._custom_format.initialize(a_bzrdir, name)
2016
result = self._custom_format.initialize(a_bzrdir)
2368
2017
if (isinstance(a_bzrdir, RemoteBzrDir) and
2369
2018
not isinstance(result, RemoteBranch)):
2370
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2019
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2374
def initialize(self, a_bzrdir, name=None, repository=None):
2022
def initialize(self, a_bzrdir):
2375
2023
# 1) get the network name to use.
2376
2024
if self._custom_format:
2377
2025
network_name = self._custom_format.network_name()
2379
2027
# Select the current bzrlib default and ask for that.
2380
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2028
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2381
2029
reference_format = reference_bzrdir_format.get_branch_format()
2382
2030
self._custom_format = reference_format
2383
2031
network_name = reference_format.network_name()
2384
2032
# Being asked to create on a non RemoteBzrDir:
2385
2033
if not isinstance(a_bzrdir, RemoteBzrDir):
2386
return self._vfs_initialize(a_bzrdir, name=name)
2034
return self._vfs_initialize(a_bzrdir)
2387
2035
medium = a_bzrdir._client._medium
2388
2036
if medium._is_remote_before((1, 13)):
2389
return self._vfs_initialize(a_bzrdir, name=name)
2037
return self._vfs_initialize(a_bzrdir)
2390
2038
# Creating on a remote bzr dir.
2391
2039
# 2) try direct creation via RPC
2392
2040
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2393
if name is not None:
2394
# XXX JRV20100304: Support creating colocated branches
2395
raise errors.NoColocatedBranchSupport(self)
2396
2041
verb = 'BzrDir.create_branch'
2398
2043
response = a_bzrdir._call(verb, path, network_name)
2399
2044
except errors.UnknownSmartMethod:
2400
2045
# Fallback - use vfs methods
2401
2046
medium._remember_remote_is_before((1, 13))
2402
return self._vfs_initialize(a_bzrdir, name=name)
2047
return self._vfs_initialize(a_bzrdir)
2403
2048
if response[0] != 'ok':
2404
2049
raise errors.UnexpectedSmartServerResponse(response)
2405
2050
# Turn the response into a RemoteRepository object.
2406
2051
format = RemoteBranchFormat(network_name=response[1])
2407
2052
repo_format = response_tuple_to_repo_format(response[3:])
2408
repo_path = response[2]
2409
if repository is not None:
2410
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2411
url_diff = urlutils.relative_url(repository.user_url,
2414
raise AssertionError(
2415
'repository.user_url %r does not match URL from server '
2416
'response (%r + %r)'
2417
% (repository.user_url, a_bzrdir.user_url, repo_path))
2418
remote_repo = repository
2053
if response[2] == '':
2054
repo_bzrdir = a_bzrdir
2421
repo_bzrdir = a_bzrdir
2423
repo_bzrdir = RemoteBzrDir(
2424
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2426
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2056
repo_bzrdir = RemoteBzrDir(
2057
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2059
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2427
2060
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2428
format=format, setup_stacking=False, name=name)
2061
format=format, setup_stacking=False)
2429
2062
# XXX: We know this is a new branch, so it must have revno 0, revid
2430
2063
# NULL_REVISION. Creating the branch locked would make this be unable
2431
2064
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
3153
2705
medium = self._branch._client._medium
3154
2706
if medium._is_remote_before((1, 14)):
3155
2707
return self._vfs_set_option(value, name, section)
3156
if isinstance(value, dict):
3157
if medium._is_remote_before((2, 2)):
3158
return self._vfs_set_option(value, name, section)
3159
return self._set_config_option_dict(value, name, section)
3161
return self._set_config_option(value, name, section)
3163
def _set_config_option(self, value, name, section):
3165
2709
path = self._branch._remote_path()
3166
2710
response = self._branch._client.call('Branch.set_config_option',
3167
2711
path, self._branch._lock_token, self._branch._repo_lock_token,
3168
2712
value.encode('utf8'), name, section or '')
3169
2713
except errors.UnknownSmartMethod:
3170
medium = self._branch._client._medium
3171
2714
medium._remember_remote_is_before((1, 14))
3172
2715
return self._vfs_set_option(value, name, section)
3173
2716
if response != ():
3174
2717
raise errors.UnexpectedSmartServerResponse(response)
3176
def _serialize_option_dict(self, option_dict):
3178
for key, value in option_dict.items():
3179
if isinstance(key, unicode):
3180
key = key.encode('utf8')
3181
if isinstance(value, unicode):
3182
value = value.encode('utf8')
3183
utf8_dict[key] = value
3184
return bencode.bencode(utf8_dict)
3186
def _set_config_option_dict(self, value, name, section):
3188
path = self._branch._remote_path()
3189
serialised_dict = self._serialize_option_dict(value)
3190
response = self._branch._client.call(
3191
'Branch.set_config_option_dict',
3192
path, self._branch._lock_token, self._branch._repo_lock_token,
3193
serialised_dict, name, section or '')
3194
except errors.UnknownSmartMethod:
3195
medium = self._branch._client._medium
3196
medium._remember_remote_is_before((2, 2))
3197
return self._vfs_set_option(value, name, section)
3199
raise errors.UnexpectedSmartServerResponse(response)
3201
2719
def _real_object(self):
3202
2720
self._branch._ensure_real()
3203
2721
return self._branch._real_branch
3286
2804
'Missing key %r in context %r', key_err.args[0], context)
3289
if err.error_verb == 'NoSuchRevision':
2807
if err.error_verb == 'IncompatibleRepositories':
2808
raise errors.IncompatibleRepositories(err.error_args[0],
2809
err.error_args[1], err.error_args[2])
2810
elif err.error_verb == 'NoSuchRevision':
3290
2811
raise NoSuchRevision(find('branch'), err.error_args[0])
3291
2812
elif err.error_verb == 'nosuchrevision':
3292
2813
raise NoSuchRevision(find('repository'), err.error_args[0])
3293
elif err.error_verb == 'nobranch':
3294
if len(err.error_args) >= 1:
3295
extra = err.error_args[0]
3298
raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2814
elif err.error_tuple == ('nobranch',):
2815
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
3300
2816
elif err.error_verb == 'norepository':
3301
2817
raise errors.NoRepositoryPresent(find('bzrdir'))
2818
elif err.error_verb == 'LockContention':
2819
raise errors.LockContention('(remote lock)')
3302
2820
elif err.error_verb == 'UnlockableTransport':
3303
2821
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2822
elif err.error_verb == 'LockFailed':
2823
raise errors.LockFailed(err.error_args[0], err.error_args[1])
3304
2824
elif err.error_verb == 'TokenMismatch':
3305
2825
raise errors.TokenMismatch(find('token'), '(remote token)')
3306
2826
elif err.error_verb == 'Diverged':
3307
2827
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2828
elif err.error_verb == 'TipChangeRejected':
2829
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2830
elif err.error_verb == 'UnstackableBranchFormat':
2831
raise errors.UnstackableBranchFormat(*err.error_args)
2832
elif err.error_verb == 'UnstackableRepositoryFormat':
2833
raise errors.UnstackableRepositoryFormat(*err.error_args)
3308
2834
elif err.error_verb == 'NotStacked':
3309
2835
raise errors.NotStacked(branch=find('branch'))
3310
2836
elif err.error_verb == 'PermissionDenied':