89
# Note: RemoteBzrDirFormat is in bzrdir.py
91
class RemoteBzrDir(BzrDir, _RpcHelper):
97
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
98
# does not have to be imported unless a remote format is involved.
100
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
101
"""Format representing bzrdirs accessed via a smart server"""
103
supports_workingtrees = False
106
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
107
# XXX: It's a bit ugly that the network name is here, because we'd
108
# like to believe that format objects are stateless or at least
109
# immutable, However, we do at least avoid mutating the name after
110
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
111
self._network_name = None
114
return "%s(_network_name=%r)" % (self.__class__.__name__,
117
def get_format_description(self):
118
if self._network_name:
119
real_format = controldir.network_format_registry.get(self._network_name)
120
return 'Remote: ' + real_format.get_format_description()
121
return 'bzr remote bzrdir'
123
def get_format_string(self):
124
raise NotImplementedError(self.get_format_string)
126
def network_name(self):
127
if self._network_name:
128
return self._network_name
130
raise AssertionError("No network name set.")
132
def initialize_on_transport(self, transport):
134
# hand off the request to the smart server
135
client_medium = transport.get_smart_medium()
136
except errors.NoSmartMedium:
137
# TODO: lookup the local format from a server hint.
138
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
139
return local_dir_format.initialize_on_transport(transport)
140
client = _SmartClient(client_medium)
141
path = client.remote_path_from_transport(transport)
143
response = client.call('BzrDirFormat.initialize', path)
144
except errors.ErrorFromSmartServer, err:
145
_translate_error(err, path=path)
146
if response[0] != 'ok':
147
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
148
format = RemoteBzrDirFormat()
149
self._supply_sub_formats_to(format)
150
return RemoteBzrDir(transport, format)
152
def parse_NoneTrueFalse(self, arg):
159
raise AssertionError("invalid arg %r" % arg)
161
def _serialize_NoneTrueFalse(self, arg):
168
def _serialize_NoneString(self, arg):
171
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
172
create_prefix=False, force_new_repo=False, stacked_on=None,
173
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
176
# hand off the request to the smart server
177
client_medium = transport.get_smart_medium()
178
except errors.NoSmartMedium:
181
# Decline to open it if the server doesn't support our required
182
# version (3) so that the VFS-based transport will do it.
183
if client_medium.should_probe():
185
server_version = client_medium.protocol_version()
186
if server_version != '2':
190
except errors.SmartProtocolError:
191
# Apparently there's no usable smart server there, even though
192
# the medium supports the smart protocol.
197
client = _SmartClient(client_medium)
198
path = client.remote_path_from_transport(transport)
199
if client_medium._is_remote_before((1, 16)):
202
# TODO: lookup the local format from a server hint.
203
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
204
self._supply_sub_formats_to(local_dir_format)
205
return local_dir_format.initialize_on_transport_ex(transport,
206
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
207
force_new_repo=force_new_repo, stacked_on=stacked_on,
208
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
209
make_working_trees=make_working_trees, shared_repo=shared_repo,
211
return self._initialize_on_transport_ex_rpc(client, path, transport,
212
use_existing_dir, create_prefix, force_new_repo, stacked_on,
213
stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
215
def _initialize_on_transport_ex_rpc(self, client, path, transport,
216
use_existing_dir, create_prefix, force_new_repo, stacked_on,
217
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
219
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
220
args.append(self._serialize_NoneTrueFalse(create_prefix))
221
args.append(self._serialize_NoneTrueFalse(force_new_repo))
222
args.append(self._serialize_NoneString(stacked_on))
223
# stack_on_pwd is often/usually our transport
226
stack_on_pwd = transport.relpath(stack_on_pwd)
229
except errors.PathNotChild:
231
args.append(self._serialize_NoneString(stack_on_pwd))
232
args.append(self._serialize_NoneString(repo_format_name))
233
args.append(self._serialize_NoneTrueFalse(make_working_trees))
234
args.append(self._serialize_NoneTrueFalse(shared_repo))
235
request_network_name = self._network_name or \
236
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
238
response = client.call('BzrDirFormat.initialize_ex_1.16',
239
request_network_name, path, *args)
240
except errors.UnknownSmartMethod:
241
client._medium._remember_remote_is_before((1,16))
242
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
243
self._supply_sub_formats_to(local_dir_format)
244
return local_dir_format.initialize_on_transport_ex(transport,
245
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
246
force_new_repo=force_new_repo, stacked_on=stacked_on,
247
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
248
make_working_trees=make_working_trees, shared_repo=shared_repo,
250
except errors.ErrorFromSmartServer, err:
251
_translate_error(err, path=path)
252
repo_path = response[0]
253
bzrdir_name = response[6]
254
require_stacking = response[7]
255
require_stacking = self.parse_NoneTrueFalse(require_stacking)
256
format = RemoteBzrDirFormat()
257
format._network_name = bzrdir_name
258
self._supply_sub_formats_to(format)
259
bzrdir = RemoteBzrDir(transport, format, _client=client)
261
repo_format = response_tuple_to_repo_format(response[1:])
265
repo_bzrdir_format = RemoteBzrDirFormat()
266
repo_bzrdir_format._network_name = response[5]
267
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
271
final_stack = response[8] or None
272
final_stack_pwd = response[9] or None
274
final_stack_pwd = urlutils.join(
275
transport.base, final_stack_pwd)
276
remote_repo = RemoteRepository(repo_bzr, repo_format)
277
if len(response) > 10:
278
# Updated server verb that locks remotely.
279
repo_lock_token = response[10] or None
280
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
282
remote_repo.dont_leave_lock_in_place()
284
remote_repo.lock_write()
285
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
286
final_stack_pwd, require_stacking)
287
policy.acquire_repository()
291
bzrdir._format.set_branch_format(self.get_branch_format())
293
# The repo has already been created, but we need to make sure that
294
# we'll make a stackable branch.
295
bzrdir._format.require_stacking(_skip_repo=True)
296
return remote_repo, bzrdir, require_stacking, policy
298
def _open(self, transport):
299
return RemoteBzrDir(transport, self)
301
def __eq__(self, other):
302
if not isinstance(other, RemoteBzrDirFormat):
304
return self.get_format_description() == other.get_format_description()
306
def __return_repository_format(self):
307
# Always return a RemoteRepositoryFormat object, but if a specific bzr
308
# repository format has been asked for, tell the RemoteRepositoryFormat
309
# that it should use that for init() etc.
310
result = RemoteRepositoryFormat()
311
custom_format = getattr(self, '_repository_format', None)
313
if isinstance(custom_format, RemoteRepositoryFormat):
316
# We will use the custom format to create repositories over the
317
# wire; expose its details like rich_root_data for code to
319
result._custom_format = custom_format
322
def get_branch_format(self):
323
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
324
if not isinstance(result, RemoteBranchFormat):
325
new_result = RemoteBranchFormat()
326
new_result._custom_format = result
328
self.set_branch_format(new_result)
332
repository_format = property(__return_repository_format,
333
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
336
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
92
337
"""Control directory on a remote server, accessed via bzr:// or similar."""
94
339
def __init__(self, transport, format, _client=None, _force_probe=False):
1335
1660
@needs_read_lock
1336
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1661
def search_missing_revision_ids(self, other,
1662
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
1663
find_ghosts=True, revision_ids=None, if_present_ids=None,
1337
1665
"""Return the revision ids that other has that this does not.
1339
1667
These are returned in topological order.
1341
1669
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)
1671
if symbol_versioning.deprecated_passed(revision_id):
1672
symbol_versioning.warn(
1673
'search_missing_revision_ids(revision_id=...) was '
1674
'deprecated in 2.4. Use revision_ids=[...] instead.',
1675
DeprecationWarning, stacklevel=2)
1676
if revision_ids is not None:
1677
raise AssertionError(
1678
'revision_ids is mutually exclusive with revision_id')
1679
if revision_id is not None:
1680
revision_ids = [revision_id]
1681
inter_repo = _mod_repository.InterRepository.get(other, self)
1682
return inter_repo.search_missing_revision_ids(
1683
find_ghosts=find_ghosts, revision_ids=revision_ids,
1684
if_present_ids=if_present_ids, limit=limit)
1346
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1686
def fetch(self, source, revision_id=None, find_ghosts=False,
1347
1687
fetch_spec=None):
1348
1688
# No base implementation to use as RemoteRepository is not a subclass
1349
1689
# of Repository; so this is a copy of Repository.fetch().
1388
1730
return self._real_repository._get_versioned_file_checker(
1389
1731
revisions, revision_versions_cache)
1733
def _iter_files_bytes_rpc(self, desired_files, absent):
1734
path = self.bzrdir._path_for_remote_call(self._client)
1737
for (file_id, revid, identifier) in desired_files:
1738
lines.append("%s\0%s" % (file_id, revid))
1739
identifiers.append(identifier)
1740
(response_tuple, response_handler) = (
1741
self._call_with_body_bytes_expecting_body(
1742
"Repository.iter_files_bytes_bz2", (path, ), "\n".join(lines)))
1743
if response_tuple != ('ok', ):
1744
response_handler.cancel_read_body()
1745
raise errors.UnexpectedSmartServerResponse(response_tuple)
1746
byte_stream = response_handler.read_streamed_body()
1747
def decompress_stream(start, byte_stream, unused):
1748
decompressor = bz2.BZ2Decompressor()
1749
yield decompressor.decompress(start)
1750
while decompressor.unused_data == "":
1752
data = byte_stream.next()
1753
except StopIteration:
1756
yield decompressor.decompress(data)
1758
unused.extend([decompressor.unused_data, data])
1759
unused.append(decompressor.unused_data)
1762
while not "\n" in unused:
1763
unused += byte_stream.next()
1764
header, rest = unused.split("\n", 1)
1765
args = header.split("\0")
1766
if args[0] == "absent":
1767
absent[identifiers[int(args[3])]] = (args[1], args[2])
1770
elif args[0] == "ok":
1773
raise errors.UnexpectedSmartServerResponse(args)
1775
yield (identifiers[idx],
1776
decompress_stream(rest, byte_stream, unused))
1777
unused = "".join(unused)
1391
1779
def iter_files_bytes(self, desired_files):
1392
1780
"""See Repository.iter_file_bytes.
1395
return self._real_repository.iter_files_bytes(desired_files)
1784
for (identifier, bytes_iterator) in self._iter_files_bytes_rpc(
1785
desired_files, absent):
1786
yield identifier, bytes_iterator
1787
for fallback in self._fallback_repositories:
1790
desired_files = [(key[0], key[1], identifier) for
1791
(identifier, key) in absent.iteritems()]
1792
for (identifier, bytes_iterator) in fallback.iter_files_bytes(absent):
1793
del absent[identifier]
1794
yield identifier, bytes_iterator
1796
# There may be more missing items, but raise an exception
1798
missing_identifier = absent.keys()[0]
1799
missing_key = absent[missing_identifier]
1800
raise errors.RevisionNotPresent(missing_key[1], missing_key[0])
1801
except errors.UnknownSmartMethod:
1803
for (identifier, bytes_iterator) in (
1804
self._real_repository.iter_files_bytes(desired_files)):
1805
yield identifier, bytes_iterator
1807
def get_cached_parent_map(self, revision_ids):
1808
"""See bzrlib.CachingParentsProvider.get_cached_parent_map"""
1809
return self._unstacked_provider.get_cached_parent_map(revision_ids)
1397
1811
def get_parent_map(self, revision_ids):
1398
1812
"""See bzrlib.Graph.get_parent_map()."""
1696
2102
def _serializer(self):
1697
2103
return self._format._serializer
1699
2106
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1701
return self._real_repository.store_revision_signature(
1702
gpg_strategy, plaintext, revision_id)
2107
signature = gpg_strategy.sign(plaintext)
2108
self.add_signature_text(revision_id, signature)
1704
2110
def add_signature_text(self, revision_id, signature):
1705
2111
self._ensure_real()
1706
2112
return self._real_repository.add_signature_text(revision_id, signature)
1708
2114
def has_signature_for_revision_id(self, revision_id):
2115
path = self.bzrdir._path_for_remote_call(self._client)
2117
response = self._call('Repository.has_signature_for_revision_id',
2119
except errors.UnknownSmartMethod:
2121
return self._real_repository.has_signature_for_revision_id(
2123
if response[0] not in ('yes', 'no'):
2124
raise SmartProtocolError('unexpected response code %s' % (response,))
2125
return (response[0] == 'yes')
2127
def verify_revision_signature(self, revision_id, gpg_strategy):
1709
2128
self._ensure_real()
1710
return self._real_repository.has_signature_for_revision_id(revision_id)
2129
return self._real_repository.verify_revision_signature(
2130
revision_id, gpg_strategy)
1712
2132
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1713
2133
self._ensure_real()
1714
2134
return self._real_repository.item_keys_introduced_by(revision_ids,
1715
2135
_files_pb=_files_pb)
1717
def revision_graph_can_have_wrong_parents(self):
1718
# The answer depends on the remote repo format.
1720
return self._real_repository.revision_graph_can_have_wrong_parents()
1722
2137
def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1723
2138
self._ensure_real()
1724
2139
return self._real_repository._find_inconsistent_revision_parents(
2071
2493
if isinstance(a_bzrdir, RemoteBzrDir):
2072
2494
a_bzrdir._ensure_real()
2073
2495
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2496
name, append_revisions_only=append_revisions_only)
2076
2498
# We assume the bzrdir is parameterised; it may not be.
2077
result = self._custom_format.initialize(a_bzrdir, name)
2499
result = self._custom_format.initialize(a_bzrdir, name,
2500
append_revisions_only=append_revisions_only)
2078
2501
if (isinstance(a_bzrdir, RemoteBzrDir) and
2079
2502
not isinstance(result, RemoteBranch)):
2080
2503
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2084
def initialize(self, a_bzrdir, name=None):
2507
def initialize(self, a_bzrdir, name=None, repository=None,
2508
append_revisions_only=None):
2085
2509
# 1) get the network name to use.
2086
2510
if self._custom_format:
2087
2511
network_name = self._custom_format.network_name()
2089
2513
# Select the current bzrlib default and ask for that.
2090
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2514
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2091
2515
reference_format = reference_bzrdir_format.get_branch_format()
2092
2516
self._custom_format = reference_format
2093
2517
network_name = reference_format.network_name()
2094
2518
# Being asked to create on a non RemoteBzrDir:
2095
2519
if not isinstance(a_bzrdir, RemoteBzrDir):
2096
return self._vfs_initialize(a_bzrdir, name=name)
2520
return self._vfs_initialize(a_bzrdir, name=name,
2521
append_revisions_only=append_revisions_only)
2097
2522
medium = a_bzrdir._client._medium
2098
2523
if medium._is_remote_before((1, 13)):
2099
return self._vfs_initialize(a_bzrdir, name=name)
2524
return self._vfs_initialize(a_bzrdir, name=name,
2525
append_revisions_only=append_revisions_only)
2100
2526
# Creating on a remote bzr dir.
2101
2527
# 2) try direct creation via RPC
2102
2528
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2109
2535
except errors.UnknownSmartMethod:
2110
2536
# Fallback - use vfs methods
2111
2537
medium._remember_remote_is_before((1, 13))
2112
return self._vfs_initialize(a_bzrdir, name=name)
2538
return self._vfs_initialize(a_bzrdir, name=name,
2539
append_revisions_only=append_revisions_only)
2113
2540
if response[0] != 'ok':
2114
2541
raise errors.UnexpectedSmartServerResponse(response)
2115
2542
# Turn the response into a RemoteRepository object.
2116
2543
format = RemoteBranchFormat(network_name=response[1])
2117
2544
repo_format = response_tuple_to_repo_format(response[3:])
2118
if response[2] == '':
2119
repo_bzrdir = a_bzrdir
2545
repo_path = response[2]
2546
if repository is not None:
2547
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2548
url_diff = urlutils.relative_url(repository.user_url,
2551
raise AssertionError(
2552
'repository.user_url %r does not match URL from server '
2553
'response (%r + %r)'
2554
% (repository.user_url, a_bzrdir.user_url, repo_path))
2555
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)
2558
repo_bzrdir = a_bzrdir
2560
repo_bzrdir = RemoteBzrDir(
2561
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2563
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2125
2564
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2126
2565
format=format, setup_stacking=False, name=name)
2566
if append_revisions_only:
2567
remote_branch.set_append_revisions_only(append_revisions_only)
2127
2568
# XXX: We know this is a new branch, so it must have revno 0, revid
2128
2569
# NULL_REVISION. Creating the branch locked would make this be unable
2129
2570
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2774
3299
medium = self._branch._client._medium
2775
3300
if medium._is_remote_before((1, 14)):
2776
3301
return self._vfs_set_option(value, name, section)
3302
if isinstance(value, dict):
3303
if medium._is_remote_before((2, 2)):
3304
return self._vfs_set_option(value, name, section)
3305
return self._set_config_option_dict(value, name, section)
3307
return self._set_config_option(value, name, section)
3309
def _set_config_option(self, value, name, section):
2778
3311
path = self._branch._remote_path()
2779
3312
response = self._branch._client.call('Branch.set_config_option',
2780
3313
path, self._branch._lock_token, self._branch._repo_lock_token,
2781
3314
value.encode('utf8'), name, section or '')
2782
3315
except errors.UnknownSmartMethod:
3316
medium = self._branch._client._medium
2783
3317
medium._remember_remote_is_before((1, 14))
2784
3318
return self._vfs_set_option(value, name, section)
2785
3319
if response != ():
2786
3320
raise errors.UnexpectedSmartServerResponse(response)
3322
def _serialize_option_dict(self, option_dict):
3324
for key, value in option_dict.items():
3325
if isinstance(key, unicode):
3326
key = key.encode('utf8')
3327
if isinstance(value, unicode):
3328
value = value.encode('utf8')
3329
utf8_dict[key] = value
3330
return bencode.bencode(utf8_dict)
3332
def _set_config_option_dict(self, value, name, section):
3334
path = self._branch._remote_path()
3335
serialised_dict = self._serialize_option_dict(value)
3336
response = self._branch._client.call(
3337
'Branch.set_config_option_dict',
3338
path, self._branch._lock_token, self._branch._repo_lock_token,
3339
serialised_dict, name, section or '')
3340
except errors.UnknownSmartMethod:
3341
medium = self._branch._client._medium
3342
medium._remember_remote_is_before((2, 2))
3343
return self._vfs_set_option(value, name, section)
3345
raise errors.UnexpectedSmartServerResponse(response)
2788
3347
def _real_object(self):
2789
3348
self._branch._ensure_real()
2790
3349
return self._branch._real_branch