104
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
105
# does not have to be imported unless a remote format is involved.
107
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
108
"""Format representing bzrdirs accessed via a smart server"""
110
supports_workingtrees = False
113
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
114
# XXX: It's a bit ugly that the network name is here, because we'd
115
# like to believe that format objects are stateless or at least
116
# immutable, However, we do at least avoid mutating the name after
117
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
118
self._network_name = None
121
return "%s(_network_name=%r)" % (self.__class__.__name__,
124
def get_format_description(self):
125
if self._network_name:
127
real_format = controldir.network_format_registry.get(
132
return 'Remote: ' + real_format.get_format_description()
133
return 'bzr remote bzrdir'
135
def get_format_string(self):
136
raise NotImplementedError(self.get_format_string)
138
def network_name(self):
139
if self._network_name:
140
return self._network_name
142
raise AssertionError("No network name set.")
144
def initialize_on_transport(self, transport):
146
# hand off the request to the smart server
147
client_medium = transport.get_smart_medium()
148
except errors.NoSmartMedium:
149
# TODO: lookup the local format from a server hint.
150
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
151
return local_dir_format.initialize_on_transport(transport)
152
client = _SmartClient(client_medium)
153
path = client.remote_path_from_transport(transport)
155
response = client.call('BzrDirFormat.initialize', path)
156
except errors.ErrorFromSmartServer, err:
157
_translate_error(err, path=path)
158
if response[0] != 'ok':
159
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
160
format = RemoteBzrDirFormat()
161
self._supply_sub_formats_to(format)
162
return RemoteBzrDir(transport, format)
164
def parse_NoneTrueFalse(self, arg):
171
raise AssertionError("invalid arg %r" % arg)
173
def _serialize_NoneTrueFalse(self, arg):
180
def _serialize_NoneString(self, arg):
183
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
184
create_prefix=False, force_new_repo=False, stacked_on=None,
185
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
188
# hand off the request to the smart server
189
client_medium = transport.get_smart_medium()
190
except errors.NoSmartMedium:
193
# Decline to open it if the server doesn't support our required
194
# version (3) so that the VFS-based transport will do it.
195
if client_medium.should_probe():
197
server_version = client_medium.protocol_version()
198
if server_version != '2':
202
except errors.SmartProtocolError:
203
# Apparently there's no usable smart server there, even though
204
# the medium supports the smart protocol.
209
client = _SmartClient(client_medium)
210
path = client.remote_path_from_transport(transport)
211
if client_medium._is_remote_before((1, 16)):
214
# TODO: lookup the local format from a server hint.
215
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
216
self._supply_sub_formats_to(local_dir_format)
217
return local_dir_format.initialize_on_transport_ex(transport,
218
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
219
force_new_repo=force_new_repo, stacked_on=stacked_on,
220
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
221
make_working_trees=make_working_trees, shared_repo=shared_repo,
223
return self._initialize_on_transport_ex_rpc(client, path, transport,
224
use_existing_dir, create_prefix, force_new_repo, stacked_on,
225
stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
227
def _initialize_on_transport_ex_rpc(self, client, path, transport,
228
use_existing_dir, create_prefix, force_new_repo, stacked_on,
229
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
231
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
232
args.append(self._serialize_NoneTrueFalse(create_prefix))
233
args.append(self._serialize_NoneTrueFalse(force_new_repo))
234
args.append(self._serialize_NoneString(stacked_on))
235
# stack_on_pwd is often/usually our transport
238
stack_on_pwd = transport.relpath(stack_on_pwd)
241
except errors.PathNotChild:
243
args.append(self._serialize_NoneString(stack_on_pwd))
244
args.append(self._serialize_NoneString(repo_format_name))
245
args.append(self._serialize_NoneTrueFalse(make_working_trees))
246
args.append(self._serialize_NoneTrueFalse(shared_repo))
247
request_network_name = self._network_name or \
248
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
250
response = client.call('BzrDirFormat.initialize_ex_1.16',
251
request_network_name, path, *args)
252
except errors.UnknownSmartMethod:
253
client._medium._remember_remote_is_before((1,16))
254
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
255
self._supply_sub_formats_to(local_dir_format)
256
return local_dir_format.initialize_on_transport_ex(transport,
257
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
258
force_new_repo=force_new_repo, stacked_on=stacked_on,
259
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
260
make_working_trees=make_working_trees, shared_repo=shared_repo,
262
except errors.ErrorFromSmartServer, err:
263
_translate_error(err, path=path)
264
repo_path = response[0]
265
bzrdir_name = response[6]
266
require_stacking = response[7]
267
require_stacking = self.parse_NoneTrueFalse(require_stacking)
268
format = RemoteBzrDirFormat()
269
format._network_name = bzrdir_name
270
self._supply_sub_formats_to(format)
271
bzrdir = RemoteBzrDir(transport, format, _client=client)
273
repo_format = response_tuple_to_repo_format(response[1:])
277
repo_bzrdir_format = RemoteBzrDirFormat()
278
repo_bzrdir_format._network_name = response[5]
279
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
283
final_stack = response[8] or None
284
final_stack_pwd = response[9] or None
286
final_stack_pwd = urlutils.join(
287
transport.base, final_stack_pwd)
288
remote_repo = RemoteRepository(repo_bzr, repo_format)
289
if len(response) > 10:
290
# Updated server verb that locks remotely.
291
repo_lock_token = response[10] or None
292
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
294
remote_repo.dont_leave_lock_in_place()
296
remote_repo.lock_write()
297
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
298
final_stack_pwd, require_stacking)
299
policy.acquire_repository()
303
bzrdir._format.set_branch_format(self.get_branch_format())
305
# The repo has already been created, but we need to make sure that
306
# we'll make a stackable branch.
307
bzrdir._format.require_stacking(_skip_repo=True)
308
return remote_repo, bzrdir, require_stacking, policy
310
def _open(self, transport):
311
return RemoteBzrDir(transport, self)
313
def __eq__(self, other):
314
if not isinstance(other, RemoteBzrDirFormat):
316
return self.get_format_description() == other.get_format_description()
318
def __return_repository_format(self):
319
# Always return a RemoteRepositoryFormat object, but if a specific bzr
320
# repository format has been asked for, tell the RemoteRepositoryFormat
321
# that it should use that for init() etc.
322
result = RemoteRepositoryFormat()
323
custom_format = getattr(self, '_repository_format', None)
325
if isinstance(custom_format, RemoteRepositoryFormat):
328
# We will use the custom format to create repositories over the
329
# wire; expose its details like rich_root_data for code to
331
result._custom_format = custom_format
334
def get_branch_format(self):
335
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
336
if not isinstance(result, RemoteBranchFormat):
337
new_result = RemoteBranchFormat()
338
new_result._custom_format = result
340
self.set_branch_format(new_result)
344
repository_format = property(__return_repository_format,
345
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
348
class RemoteControlStore(config.IniFileStore):
349
"""Control store which attempts to use HPSS calls to retrieve control store.
351
Note that this is specific to bzr-based formats.
354
def __init__(self, bzrdir):
355
super(RemoteControlStore, self).__init__()
357
self._real_store = None
359
def lock_write(self, token=None):
361
return self._real_store.lock_write(token)
365
return self._real_store.unlock()
369
# We need to be able to override the undecorated implementation
370
self.save_without_locking()
372
def save_without_locking(self):
373
super(RemoteControlStore, self).save()
375
def _ensure_real(self):
376
self.bzrdir._ensure_real()
377
if self._real_store is None:
378
self._real_store = config.ControlStore(self.bzrdir)
380
def external_url(self):
381
return self.bzrdir.user_url
383
def _load_content(self):
384
medium = self.bzrdir._client._medium
385
path = self.bzrdir._path_for_remote_call(self.bzrdir._client)
387
response, handler = self.bzrdir._call_expecting_body(
388
'BzrDir.get_config_file', path)
389
except errors.UnknownSmartMethod:
391
return self._real_store._load_content()
392
if len(response) and response[0] != 'ok':
393
raise errors.UnexpectedSmartServerResponse(response)
394
return handler.read_body_bytes()
396
def _save_content(self, content):
397
# FIXME JRV 2011-11-22: Ideally this should use a
398
# HPSS call too, but at the moment it is not possible
399
# to write lock control directories.
401
return self._real_store._save_content(content)
404
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
87
# Note: RemoteBzrDirFormat is in bzrdir.py
89
class RemoteBzrDir(BzrDir, _RpcHelper):
405
90
"""Control directory on a remote server, accessed via bzr:// or similar."""
407
92
def __init__(self, transport, format, _client=None, _force_probe=False):
1672
1169
raise errors.UnexpectedSmartServerResponse(response)
1675
1171
def sprout(self, to_bzrdir, revision_id=None):
1676
"""Create a descendent repository for new development.
1678
Unlike clone, this does not copy the settings of the repository.
1680
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
1172
# TODO: Option to control what format is created?
1174
dest_repo = self._real_repository._format.initialize(to_bzrdir,
1681
1176
dest_repo.fetch(self, revision_id=revision_id)
1682
1177
return dest_repo
1684
def _create_sprouting_repo(self, a_bzrdir, shared):
1685
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
1686
# use target default format.
1687
dest_repo = a_bzrdir.create_repository()
1689
# Most control formats need the repository to be specifically
1690
# created, but on some old all-in-one formats it's not needed
1692
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
1693
except errors.UninitializableFormat:
1694
dest_repo = a_bzrdir.open_repository()
1697
1179
### These methods are just thin shims to the VFS object for now.
1700
1181
def revision_tree(self, revision_id):
1701
revision_id = _mod_revision.ensure_null(revision_id)
1702
if revision_id == _mod_revision.NULL_REVISION:
1703
return InventoryRevisionTree(self,
1704
Inventory(root_id=None), _mod_revision.NULL_REVISION)
1706
return list(self.revision_trees([revision_id]))[0]
1183
return self._real_repository.revision_tree(revision_id)
1708
1185
def get_serializer_format(self):
1709
path = self.bzrdir._path_for_remote_call(self._client)
1711
response = self._call('VersionedFileRepository.get_serializer_format',
1713
except errors.UnknownSmartMethod:
1715
return self._real_repository.get_serializer_format()
1716
if response[0] != 'ok':
1717
raise errors.UnexpectedSmartServerResponse(response)
1187
return self._real_repository.get_serializer_format()
1720
1189
def get_commit_builder(self, branch, parents, config, timestamp=None,
1721
1190
timezone=None, committer=None, revprops=None,
1722
revision_id=None, lossy=False):
1723
1192
# FIXME: It ought to be possible to call this without immediately
1724
1193
# triggering _ensure_real. For now it's the easiest thing to do.
1725
1194
self._ensure_real()
1726
1195
real_repo = self._real_repository
1727
1196
builder = real_repo.get_commit_builder(branch, parents,
1728
1197
config, timestamp=timestamp, timezone=timezone,
1729
committer=committer, revprops=revprops,
1730
revision_id=revision_id, lossy=lossy)
1198
committer=committer, revprops=revprops, revision_id=revision_id)
1733
1201
def add_fallback_repository(self, repository):
1842
1290
included_keys = result_set.intersection(result_parents)
1843
1291
start_keys = result_set.difference(included_keys)
1844
1292
exclude_keys = result_parents.difference(result_set)
1845
result = vf_search.SearchResult(start_keys, exclude_keys,
1293
result = graph.SearchResult(start_keys, exclude_keys,
1846
1294
len(result_set), result_set)
1849
1297
@needs_read_lock
1850
def search_missing_revision_ids(self, other,
1851
revision_id=symbol_versioning.DEPRECATED_PARAMETER,
1852
find_ghosts=True, revision_ids=None, if_present_ids=None,
1298
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1854
1299
"""Return the revision ids that other has that this does not.
1856
1301
These are returned in topological order.
1858
1303
revision_id: only return revision ids included by revision_id.
1860
if symbol_versioning.deprecated_passed(revision_id):
1861
symbol_versioning.warn(
1862
'search_missing_revision_ids(revision_id=...) was '
1863
'deprecated in 2.4. Use revision_ids=[...] instead.',
1864
DeprecationWarning, stacklevel=2)
1865
if revision_ids is not None:
1866
raise AssertionError(
1867
'revision_ids is mutually exclusive with revision_id')
1868
if revision_id is not None:
1869
revision_ids = [revision_id]
1870
inter_repo = _mod_repository.InterRepository.get(other, self)
1871
return inter_repo.search_missing_revision_ids(
1872
find_ghosts=find_ghosts, revision_ids=revision_ids,
1873
if_present_ids=if_present_ids, limit=limit)
1305
return repository.InterRepository.get(
1306
other, self).search_missing_revision_ids(revision_id, find_ghosts)
1875
def fetch(self, source, revision_id=None, find_ghosts=False,
1308
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1876
1309
fetch_spec=None):
1877
1310
# No base implementation to use as RemoteRepository is not a subclass
1878
1311
# of Repository; so this is a copy of Repository.fetch().
1923
1350
return self._real_repository._get_versioned_file_checker(
1924
1351
revisions, revision_versions_cache)
1926
def _iter_files_bytes_rpc(self, desired_files, absent):
1927
path = self.bzrdir._path_for_remote_call(self._client)
1930
for (file_id, revid, identifier) in desired_files:
1931
lines.append("%s\0%s" % (
1932
osutils.safe_file_id(file_id),
1933
osutils.safe_revision_id(revid)))
1934
identifiers.append(identifier)
1935
(response_tuple, response_handler) = (
1936
self._call_with_body_bytes_expecting_body(
1937
"Repository.iter_files_bytes", (path, ), "\n".join(lines)))
1938
if response_tuple != ('ok', ):
1939
response_handler.cancel_read_body()
1940
raise errors.UnexpectedSmartServerResponse(response_tuple)
1941
byte_stream = response_handler.read_streamed_body()
1942
def decompress_stream(start, byte_stream, unused):
1943
decompressor = zlib.decompressobj()
1944
yield decompressor.decompress(start)
1945
while decompressor.unused_data == "":
1947
data = byte_stream.next()
1948
except StopIteration:
1950
yield decompressor.decompress(data)
1951
yield decompressor.flush()
1952
unused.append(decompressor.unused_data)
1955
while not "\n" in unused:
1956
unused += byte_stream.next()
1957
header, rest = unused.split("\n", 1)
1958
args = header.split("\0")
1959
if args[0] == "absent":
1960
absent[identifiers[int(args[3])]] = (args[1], args[2])
1963
elif args[0] == "ok":
1966
raise errors.UnexpectedSmartServerResponse(args)
1968
yield (identifiers[idx],
1969
decompress_stream(rest, byte_stream, unused_chunks))
1970
unused = "".join(unused_chunks)
1972
1353
def iter_files_bytes(self, desired_files):
1973
1354
"""See Repository.iter_file_bytes.
1977
for (identifier, bytes_iterator) in self._iter_files_bytes_rpc(
1978
desired_files, absent):
1979
yield identifier, bytes_iterator
1980
for fallback in self._fallback_repositories:
1983
desired_files = [(key[0], key[1], identifier) for
1984
(identifier, key) in absent.iteritems()]
1985
for (identifier, bytes_iterator) in fallback.iter_files_bytes(desired_files):
1986
del absent[identifier]
1987
yield identifier, bytes_iterator
1989
# There may be more missing items, but raise an exception
1991
missing_identifier = absent.keys()[0]
1992
missing_key = absent[missing_identifier]
1993
raise errors.RevisionNotPresent(revision_id=missing_key[1],
1994
file_id=missing_key[0])
1995
except errors.UnknownSmartMethod:
1997
for (identifier, bytes_iterator) in (
1998
self._real_repository.iter_files_bytes(desired_files)):
1999
yield identifier, bytes_iterator
2001
def get_cached_parent_map(self, revision_ids):
2002
"""See bzrlib.CachingParentsProvider.get_cached_parent_map"""
2003
return self._unstacked_provider.get_cached_parent_map(revision_ids)
1357
return self._real_repository.iter_files_bytes(desired_files)
2005
1359
def get_parent_map(self, revision_ids):
2006
1360
"""See bzrlib.Graph.get_parent_map()."""
2318
1646
self._ensure_real()
2319
1647
return self._real_repository.texts
2321
def _iter_revisions_rpc(self, revision_ids):
2322
body = "\n".join(revision_ids)
2323
path = self.bzrdir._path_for_remote_call(self._client)
2324
response_tuple, response_handler = (
2325
self._call_with_body_bytes_expecting_body(
2326
"Repository.iter_revisions", (path, ), body))
2327
if response_tuple[0] != "ok":
2328
raise errors.UnexpectedSmartServerResponse(response_tuple)
2329
serializer_format = response_tuple[1]
2330
serializer = serializer_format_registry.get(serializer_format)
2331
byte_stream = response_handler.read_streamed_body()
2332
decompressor = zlib.decompressobj()
2334
for bytes in byte_stream:
2335
chunks.append(decompressor.decompress(bytes))
2336
if decompressor.unused_data != "":
2337
chunks.append(decompressor.flush())
2338
yield serializer.read_revision_from_string("".join(chunks))
2339
unused = decompressor.unused_data
2340
decompressor = zlib.decompressobj()
2341
chunks = [decompressor.decompress(unused)]
2342
chunks.append(decompressor.flush())
2343
text = "".join(chunks)
2345
yield serializer.read_revision_from_string("".join(chunks))
2347
1649
@needs_read_lock
2348
1650
def get_revisions(self, revision_ids):
2349
if revision_ids is None:
2350
revision_ids = self.all_revision_ids()
2352
for rev_id in revision_ids:
2353
if not rev_id or not isinstance(rev_id, basestring):
2354
raise errors.InvalidRevisionId(
2355
revision_id=rev_id, branch=self)
2357
missing = set(revision_ids)
2359
for rev in self._iter_revisions_rpc(revision_ids):
2360
missing.remove(rev.revision_id)
2361
revs[rev.revision_id] = rev
2362
except errors.UnknownSmartMethod:
2364
return self._real_repository.get_revisions(revision_ids)
2365
for fallback in self._fallback_repositories:
2368
for revid in list(missing):
2369
# XXX JRV 2011-11-20: It would be nice if there was a
2370
# public method on Repository that could be used to query
2371
# for revision objects *without* failing completely if one
2372
# was missing. There is VersionedFileRepository._iter_revisions,
2373
# but unfortunately that's private and not provided by
2374
# all repository implementations.
2376
revs[revid] = fallback.get_revision(revid)
2377
except errors.NoSuchRevision:
2380
missing.remove(revid)
2382
raise errors.NoSuchRevision(self, list(missing)[0])
2383
return [revs[revid] for revid in revision_ids]
1652
return self._real_repository.get_revisions(revision_ids)
2385
1654
def supports_rich_root(self):
2386
1655
return self._format.rich_root_data
2388
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
2389
1657
def iter_reverse_revision_history(self, revision_id):
2390
1658
self._ensure_real()
2391
1659
return self._real_repository.iter_reverse_revision_history(revision_id)
2394
1662
def _serializer(self):
2395
1663
return self._format._serializer
2398
1665
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
2399
signature = gpg_strategy.sign(plaintext)
2400
self.add_signature_text(revision_id, signature)
1667
return self._real_repository.store_revision_signature(
1668
gpg_strategy, plaintext, revision_id)
2402
1670
def add_signature_text(self, revision_id, signature):
2403
if self._real_repository:
2404
# If there is a real repository the write group will
2405
# be in the real repository as well, so use that:
2407
return self._real_repository.add_signature_text(
2408
revision_id, signature)
2409
path = self.bzrdir._path_for_remote_call(self._client)
2410
response, handler = self._call_with_body_bytes_expecting_body(
2411
'Repository.add_signature_text', (path, self._lock_token,
2412
revision_id) + tuple(self._write_group_tokens), signature)
2413
handler.cancel_read_body()
2415
if response[0] != 'ok':
2416
raise errors.UnexpectedSmartServerResponse(response)
2417
self._write_group_tokens = response[1:]
1672
return self._real_repository.add_signature_text(revision_id, signature)
2419
1674
def has_signature_for_revision_id(self, revision_id):
2420
path = self.bzrdir._path_for_remote_call(self._client)
2422
response = self._call('Repository.has_signature_for_revision_id',
2424
except errors.UnknownSmartMethod:
2426
return self._real_repository.has_signature_for_revision_id(
2428
if response[0] not in ('yes', 'no'):
2429
raise SmartProtocolError('unexpected response code %s' % (response,))
2430
if response[0] == 'yes':
2432
for fallback in self._fallback_repositories:
2433
if fallback.has_signature_for_revision_id(revision_id):
2438
def verify_revision_signature(self, revision_id, gpg_strategy):
2439
if not self.has_signature_for_revision_id(revision_id):
2440
return gpg.SIGNATURE_NOT_SIGNED, None
2441
signature = self.get_signature_text(revision_id)
2443
testament = _mod_testament.Testament.from_revision(self, revision_id)
2444
plaintext = testament.as_short_text()
2446
return gpg_strategy.verify(signature, plaintext)
1676
return self._real_repository.has_signature_for_revision_id(revision_id)
2448
1678
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
2449
1679
self._ensure_real()
2450
1680
return self._real_repository.item_keys_introduced_by(revision_ids,
2451
1681
_files_pb=_files_pb)
1683
def revision_graph_can_have_wrong_parents(self):
1684
# The answer depends on the remote repo format.
1686
return self._real_repository.revision_graph_can_have_wrong_parents()
2453
1688
def _find_inconsistent_revision_parents(self, revisions_iterator=None):
2454
1689
self._ensure_real()
2455
1690
return self._real_repository._find_inconsistent_revision_parents(
2801
2025
def network_name(self):
2802
2026
return self._network_name
2804
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2805
return a_bzrdir.open_branch(name=name,
2806
ignore_fallbacks=ignore_fallbacks)
2028
def open(self, a_bzrdir, ignore_fallbacks=False):
2029
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2808
def _vfs_initialize(self, a_bzrdir, name, append_revisions_only):
2031
def _vfs_initialize(self, a_bzrdir):
2809
2032
# Initialisation when using a local bzrdir object, or a non-vfs init
2810
2033
# method is not available on the server.
2811
2034
# self._custom_format is always set - the start of initialize ensures
2813
2036
if isinstance(a_bzrdir, RemoteBzrDir):
2814
2037
a_bzrdir._ensure_real()
2815
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2816
name, append_revisions_only=append_revisions_only)
2038
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2818
2040
# We assume the bzrdir is parameterised; it may not be.
2819
result = self._custom_format.initialize(a_bzrdir, name,
2820
append_revisions_only=append_revisions_only)
2041
result = self._custom_format.initialize(a_bzrdir)
2821
2042
if (isinstance(a_bzrdir, RemoteBzrDir) and
2822
2043
not isinstance(result, RemoteBranch)):
2823
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2044
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2827
def initialize(self, a_bzrdir, name=None, repository=None,
2828
append_revisions_only=None):
2047
def initialize(self, a_bzrdir):
2829
2048
# 1) get the network name to use.
2830
2049
if self._custom_format:
2831
2050
network_name = self._custom_format.network_name()
2833
2052
# Select the current bzrlib default and ask for that.
2834
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2053
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2835
2054
reference_format = reference_bzrdir_format.get_branch_format()
2836
2055
self._custom_format = reference_format
2837
2056
network_name = reference_format.network_name()
2838
2057
# Being asked to create on a non RemoteBzrDir:
2839
2058
if not isinstance(a_bzrdir, RemoteBzrDir):
2840
return self._vfs_initialize(a_bzrdir, name=name,
2841
append_revisions_only=append_revisions_only)
2059
return self._vfs_initialize(a_bzrdir)
2842
2060
medium = a_bzrdir._client._medium
2843
2061
if medium._is_remote_before((1, 13)):
2844
return self._vfs_initialize(a_bzrdir, name=name,
2845
append_revisions_only=append_revisions_only)
2062
return self._vfs_initialize(a_bzrdir)
2846
2063
# Creating on a remote bzr dir.
2847
2064
# 2) try direct creation via RPC
2848
2065
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2849
if name is not None:
2850
# XXX JRV20100304: Support creating colocated branches
2851
raise errors.NoColocatedBranchSupport(self)
2852
2066
verb = 'BzrDir.create_branch'
2854
2068
response = a_bzrdir._call(verb, path, network_name)
2855
2069
except errors.UnknownSmartMethod:
2856
2070
# Fallback - use vfs methods
2857
2071
medium._remember_remote_is_before((1, 13))
2858
return self._vfs_initialize(a_bzrdir, name=name,
2859
append_revisions_only=append_revisions_only)
2072
return self._vfs_initialize(a_bzrdir)
2860
2073
if response[0] != 'ok':
2861
2074
raise errors.UnexpectedSmartServerResponse(response)
2862
2075
# Turn the response into a RemoteRepository object.
2863
2076
format = RemoteBranchFormat(network_name=response[1])
2864
2077
repo_format = response_tuple_to_repo_format(response[3:])
2865
repo_path = response[2]
2866
if repository is not None:
2867
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2868
url_diff = urlutils.relative_url(repository.user_url,
2871
raise AssertionError(
2872
'repository.user_url %r does not match URL from server '
2873
'response (%r + %r)'
2874
% (repository.user_url, a_bzrdir.user_url, repo_path))
2875
remote_repo = repository
2078
if response[2] == '':
2079
repo_bzrdir = a_bzrdir
2878
repo_bzrdir = a_bzrdir
2880
repo_bzrdir = RemoteBzrDir(
2881
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2883
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)
2884
2085
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2885
format=format, setup_stacking=False, name=name)
2886
if append_revisions_only:
2887
remote_branch.set_append_revisions_only(append_revisions_only)
2086
format=format, setup_stacking=False)
2888
2087
# XXX: We know this is a new branch, so it must have revno 0, revid
2889
2088
# NULL_REVISION. Creating the branch locked would make this be unable
2890
2089
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2909
2108
self._ensure_real()
2910
2109
return self._custom_format.supports_set_append_revisions_only()
2912
def _use_default_local_heads_to_fetch(self):
2913
# If the branch format is a metadir format *and* its heads_to_fetch
2914
# implementation is not overridden vs the base class, we can use the
2915
# base class logic rather than use the heads_to_fetch RPC. This is
2916
# usually cheaper in terms of net round trips, as the last-revision and
2917
# tags info fetched is cached and would be fetched anyway.
2919
if isinstance(self._custom_format, branch.BranchFormatMetadir):
2920
branch_class = self._custom_format._branch_class()
2921
heads_to_fetch_impl = branch_class.heads_to_fetch.im_func
2922
if heads_to_fetch_impl is branch.Branch.heads_to_fetch.im_func:
2927
class RemoteBranchStore(config.IniFileStore):
2928
"""Branch store which attempts to use HPSS calls to retrieve branch store.
2930
Note that this is specific to bzr-based formats.
2933
def __init__(self, branch):
2934
super(RemoteBranchStore, self).__init__()
2935
self.branch = branch
2937
self._real_store = None
2939
def lock_write(self, token=None):
2940
return self.branch.lock_write(token)
2943
return self.branch.unlock()
2947
# We need to be able to override the undecorated implementation
2948
self.save_without_locking()
2950
def save_without_locking(self):
2951
super(RemoteBranchStore, self).save()
2953
def external_url(self):
2954
return self.branch.user_url
2956
def _load_content(self):
2957
path = self.branch._remote_path()
2959
response, handler = self.branch._call_expecting_body(
2960
'Branch.get_config_file', path)
2961
except errors.UnknownSmartMethod:
2963
return self._real_store._load_content()
2964
if len(response) and response[0] != 'ok':
2965
raise errors.UnexpectedSmartServerResponse(response)
2966
return handler.read_body_bytes()
2968
def _save_content(self, content):
2969
path = self.branch._remote_path()
2971
response, handler = self.branch._call_with_body_bytes_expecting_body(
2972
'Branch.put_config_file', (path,
2973
self.branch._lock_token, self.branch._repo_lock_token),
2975
except errors.UnknownSmartMethod:
2977
return self._real_store._save_content(content)
2978
handler.cancel_read_body()
2979
if response != ('ok', ):
2980
raise errors.UnexpectedSmartServerResponse(response)
2982
def _ensure_real(self):
2983
self.branch._ensure_real()
2984
if self._real_store is None:
2985
self._real_store = config.BranchStore(self.branch)
2988
2112
class RemoteBranch(branch.Branch, _RpcHelper, lock._RelockDebugMixin):
2989
2113
"""Branch stored on a server accessed by HPSS RPC.
3545
2611
_override_hook_target=self, **kwargs)
3547
2613
@needs_read_lock
3548
def push(self, target, overwrite=False, stop_revision=None, lossy=False):
2614
def push(self, target, overwrite=False, stop_revision=None):
3549
2615
self._ensure_real()
3550
2616
return self._real_branch.push(
3551
target, overwrite=overwrite, stop_revision=stop_revision, lossy=lossy,
2617
target, overwrite=overwrite, stop_revision=stop_revision,
3552
2618
_override_hook_source_branch=self)
3554
2620
def is_locked(self):
3555
2621
return self._lock_count >= 1
3557
2623
@needs_read_lock
3558
def revision_id_to_dotted_revno(self, revision_id):
3559
"""Given a revision id, return its dotted revno.
3561
:return: a tuple like (1,) or (400,1,3).
3564
response = self._call('Branch.revision_id_to_revno',
3565
self._remote_path(), revision_id)
3566
except errors.UnknownSmartMethod:
3568
return self._real_branch.revision_id_to_dotted_revno(revision_id)
3569
if response[0] == 'ok':
3570
return tuple([int(x) for x in response[1:]])
3572
raise errors.UnexpectedSmartServerResponse(response)
3575
2624
def revision_id_to_revno(self, revision_id):
3576
"""Given a revision id on the branch mainline, return its revno.
3581
response = self._call('Branch.revision_id_to_revno',
3582
self._remote_path(), revision_id)
3583
except errors.UnknownSmartMethod:
3585
return self._real_branch.revision_id_to_revno(revision_id)
3586
if response[0] == 'ok':
3587
if len(response) == 2:
3588
return int(response[1])
3589
raise NoSuchRevision(self, revision_id)
3591
raise errors.UnexpectedSmartServerResponse(response)
2626
return self._real_branch.revision_id_to_revno(revision_id)
3593
2628
@needs_write_lock
3594
2629
def set_last_revision_info(self, revno, revision_id):
3595
2630
# XXX: These should be returned by the set_last_revision_info verb
3596
2631
old_revno, old_revid = self.last_revision_info()
3597
2632
self._run_pre_change_branch_tip_hooks(revno, revision_id)
3598
if not revision_id or not isinstance(revision_id, basestring):
3599
raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2633
revision_id = ensure_null(revision_id)
3601
2635
response = self._call('Branch.set_last_revision_info',
3602
2636
self._remote_path(), self._lock_token, self._repo_lock_token,
3734
2731
medium = self._branch._client._medium
3735
2732
if medium._is_remote_before((1, 14)):
3736
2733
return self._vfs_set_option(value, name, section)
3737
if isinstance(value, dict):
3738
if medium._is_remote_before((2, 2)):
3739
return self._vfs_set_option(value, name, section)
3740
return self._set_config_option_dict(value, name, section)
3742
return self._set_config_option(value, name, section)
3744
def _set_config_option(self, value, name, section):
3746
2735
path = self._branch._remote_path()
3747
2736
response = self._branch._client.call('Branch.set_config_option',
3748
2737
path, self._branch._lock_token, self._branch._repo_lock_token,
3749
2738
value.encode('utf8'), name, section or '')
3750
2739
except errors.UnknownSmartMethod:
3751
medium = self._branch._client._medium
3752
2740
medium._remember_remote_is_before((1, 14))
3753
2741
return self._vfs_set_option(value, name, section)
3754
2742
if response != ():
3755
2743
raise errors.UnexpectedSmartServerResponse(response)
3757
def _serialize_option_dict(self, option_dict):
3759
for key, value in option_dict.items():
3760
if isinstance(key, unicode):
3761
key = key.encode('utf8')
3762
if isinstance(value, unicode):
3763
value = value.encode('utf8')
3764
utf8_dict[key] = value
3765
return bencode.bencode(utf8_dict)
3767
def _set_config_option_dict(self, value, name, section):
3769
path = self._branch._remote_path()
3770
serialised_dict = self._serialize_option_dict(value)
3771
response = self._branch._client.call(
3772
'Branch.set_config_option_dict',
3773
path, self._branch._lock_token, self._branch._repo_lock_token,
3774
serialised_dict, name, section or '')
3775
except errors.UnknownSmartMethod:
3776
medium = self._branch._client._medium
3777
medium._remember_remote_is_before((2, 2))
3778
return self._vfs_set_option(value, name, section)
3780
raise errors.UnexpectedSmartServerResponse(response)
3782
2745
def _real_object(self):
3783
2746
self._branch._ensure_real()
3784
2747
return self._branch._real_branch
3870
2830
'Missing key %r in context %r', key_err.args[0], context)
3874
translator = error_translators.get(err.error_verb)
3878
raise translator(err, find, get_path)
3880
translator = no_context_error_translators.get(err.error_verb)
3882
raise errors.UnknownErrorFromSmartServer(err)
3884
raise translator(err)
3887
error_translators.register('NoSuchRevision',
3888
lambda err, find, get_path: NoSuchRevision(
3889
find('branch'), err.error_args[0]))
3890
error_translators.register('nosuchrevision',
3891
lambda err, find, get_path: NoSuchRevision(
3892
find('repository'), err.error_args[0]))
3894
def _translate_nobranch_error(err, find, get_path):
3895
if len(err.error_args) >= 1:
3896
extra = err.error_args[0]
3899
return errors.NotBranchError(path=find('bzrdir').root_transport.base,
3902
error_translators.register('nobranch', _translate_nobranch_error)
3903
error_translators.register('norepository',
3904
lambda err, find, get_path: errors.NoRepositoryPresent(
3906
error_translators.register('UnlockableTransport',
3907
lambda err, find, get_path: errors.UnlockableTransport(
3908
find('bzrdir').root_transport))
3909
error_translators.register('TokenMismatch',
3910
lambda err, find, get_path: errors.TokenMismatch(
3911
find('token'), '(remote token)'))
3912
error_translators.register('Diverged',
3913
lambda err, find, get_path: errors.DivergedBranches(
3914
find('branch'), find('other_branch')))
3915
error_translators.register('NotStacked',
3916
lambda err, find, get_path: errors.NotStacked(branch=find('branch')))
3918
def _translate_PermissionDenied(err, find, get_path):
3920
if len(err.error_args) >= 2:
3921
extra = err.error_args[1]
3924
return errors.PermissionDenied(path, extra=extra)
3926
error_translators.register('PermissionDenied', _translate_PermissionDenied)
3927
error_translators.register('ReadError',
3928
lambda err, find, get_path: errors.ReadError(get_path()))
3929
error_translators.register('NoSuchFile',
3930
lambda err, find, get_path: errors.NoSuchFile(get_path()))
3931
error_translators.register('UnsuspendableWriteGroup',
3932
lambda err, find, get_path: errors.UnsuspendableWriteGroup(
3933
repository=find('repository')))
3934
error_translators.register('UnresumableWriteGroup',
3935
lambda err, find, get_path: errors.UnresumableWriteGroup(
3936
repository=find('repository'), write_groups=err.error_args[0],
3937
reason=err.error_args[1]))
3938
no_context_error_translators.register('IncompatibleRepositories',
3939
lambda err: errors.IncompatibleRepositories(
3940
err.error_args[0], err.error_args[1], err.error_args[2]))
3941
no_context_error_translators.register('LockContention',
3942
lambda err: errors.LockContention('(remote lock)'))
3943
no_context_error_translators.register('LockFailed',
3944
lambda err: errors.LockFailed(err.error_args[0], err.error_args[1]))
3945
no_context_error_translators.register('TipChangeRejected',
3946
lambda err: errors.TipChangeRejected(err.error_args[0].decode('utf8')))
3947
no_context_error_translators.register('UnstackableBranchFormat',
3948
lambda err: errors.UnstackableBranchFormat(*err.error_args))
3949
no_context_error_translators.register('UnstackableRepositoryFormat',
3950
lambda err: errors.UnstackableRepositoryFormat(*err.error_args))
3951
no_context_error_translators.register('FileExists',
3952
lambda err: errors.FileExists(err.error_args[0]))
3953
no_context_error_translators.register('DirectoryNotEmpty',
3954
lambda err: errors.DirectoryNotEmpty(err.error_args[0]))
3956
def _translate_short_readv_error(err):
3957
args = err.error_args
3958
return errors.ShortReadvError(args[0], int(args[1]), int(args[2]),
3961
no_context_error_translators.register('ShortReadvError',
3962
_translate_short_readv_error)
3964
def _translate_unicode_error(err):
2833
if err.error_verb == 'IncompatibleRepositories':
2834
raise errors.IncompatibleRepositories(err.error_args[0],
2835
err.error_args[1], err.error_args[2])
2836
elif err.error_verb == 'NoSuchRevision':
2837
raise NoSuchRevision(find('branch'), err.error_args[0])
2838
elif err.error_verb == 'nosuchrevision':
2839
raise NoSuchRevision(find('repository'), err.error_args[0])
2840
elif err.error_verb == 'nobranch':
2841
if len(err.error_args) >= 1:
2842
extra = err.error_args[0]
2845
raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2847
elif err.error_verb == 'norepository':
2848
raise errors.NoRepositoryPresent(find('bzrdir'))
2849
elif err.error_verb == 'LockContention':
2850
raise errors.LockContention('(remote lock)')
2851
elif err.error_verb == 'UnlockableTransport':
2852
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2853
elif err.error_verb == 'LockFailed':
2854
raise errors.LockFailed(err.error_args[0], err.error_args[1])
2855
elif err.error_verb == 'TokenMismatch':
2856
raise errors.TokenMismatch(find('token'), '(remote token)')
2857
elif err.error_verb == 'Diverged':
2858
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2859
elif err.error_verb == 'TipChangeRejected':
2860
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2861
elif err.error_verb == 'UnstackableBranchFormat':
2862
raise errors.UnstackableBranchFormat(*err.error_args)
2863
elif err.error_verb == 'UnstackableRepositoryFormat':
2864
raise errors.UnstackableRepositoryFormat(*err.error_args)
2865
elif err.error_verb == 'NotStacked':
2866
raise errors.NotStacked(branch=find('branch'))
2867
elif err.error_verb == 'PermissionDenied':
2869
if len(err.error_args) >= 2:
2870
extra = err.error_args[1]
2873
raise errors.PermissionDenied(path, extra=extra)
2874
elif err.error_verb == 'ReadError':
2876
raise errors.ReadError(path)
2877
elif err.error_verb == 'NoSuchFile':
2879
raise errors.NoSuchFile(path)
2880
elif err.error_verb == 'FileExists':
2881
raise errors.FileExists(err.error_args[0])
2882
elif err.error_verb == 'DirectoryNotEmpty':
2883
raise errors.DirectoryNotEmpty(err.error_args[0])
2884
elif err.error_verb == 'ShortReadvError':
2885
args = err.error_args
2886
raise errors.ShortReadvError(
2887
args[0], int(args[1]), int(args[2]), int(args[3]))
2888
elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
3965
2889
encoding = str(err.error_args[0]) # encoding must always be a string
3966
2890
val = err.error_args[1]
3967
2891
start = int(err.error_args[2])