1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# TODO: At some point, handle upgrades by just passing the whole request
18
# across to run on the server.
35
from bzrlib.branch import BranchReferenceFormat
36
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
37
from bzrlib.decorators import needs_read_lock, needs_write_lock
38
from bzrlib.errors import (
42
from bzrlib.lockable_files import LockableFiles
43
from bzrlib.smart import client, vfs
44
from bzrlib.revision import ensure_null, NULL_REVISION
45
from bzrlib.trace import mutter, note, warning
46
from bzrlib.versionedfile import record_to_fulltext_bytes
49
class _RpcHelper(object):
50
"""Mixin class that helps with issuing RPCs."""
52
def _call(self, method, *args, **err_context):
54
return self._client.call(method, *args)
55
except errors.ErrorFromSmartServer, err:
56
self._translate_error(err, **err_context)
58
def _call_expecting_body(self, method, *args, **err_context):
60
return self._client.call_expecting_body(method, *args)
61
except errors.ErrorFromSmartServer, err:
62
self._translate_error(err, **err_context)
64
def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
67
return self._client.call_with_body_bytes_expecting_body(
68
method, args, body_bytes)
69
except errors.ErrorFromSmartServer, err:
70
self._translate_error(err, **err_context)
72
# Note: RemoteBzrDirFormat is in bzrdir.py
74
class RemoteBzrDir(BzrDir, _RpcHelper):
75
"""Control directory on a remote server, accessed via bzr:// or similar."""
77
def __init__(self, transport, format, _client=None):
78
"""Construct a RemoteBzrDir.
80
:param _client: Private parameter for testing. Disables probing and the
83
BzrDir.__init__(self, transport, format)
84
# this object holds a delegated bzrdir that uses file-level operations
85
# to talk to the other side
86
self._real_bzrdir = None
89
medium = transport.get_smart_medium()
90
self._client = client._SmartClient(medium)
92
self._client = _client
95
path = self._path_for_remote_call(self._client)
96
response = self._call('BzrDir.open', path)
97
if response not in [('yes',), ('no',)]:
98
raise errors.UnexpectedSmartServerResponse(response)
99
if response == ('no',):
100
raise errors.NotBranchError(path=transport.base)
102
def _ensure_real(self):
103
"""Ensure that there is a _real_bzrdir set.
105
Used before calls to self._real_bzrdir.
107
if not self._real_bzrdir:
108
self._real_bzrdir = BzrDir.open_from_transport(
109
self.root_transport, _server_formats=False)
111
def _translate_error(self, err, **context):
112
_translate_error(err, bzrdir=self, **context)
114
def cloning_metadir(self, stacked=False):
116
return self._real_bzrdir.cloning_metadir(stacked)
118
def create_repository(self, shared=False):
119
# as per meta1 formats - just delegate to the format object which may
121
result = self._format.repository_format.initialize(self, shared)
122
if not isinstance(result, RemoteRepository):
123
return self.open_repository()
127
def destroy_repository(self):
128
"""See BzrDir.destroy_repository"""
130
self._real_bzrdir.destroy_repository()
132
def create_branch(self):
133
# as per meta1 formats - just delegate to the format object which may
135
real_branch = self._format.get_branch_format().initialize(self)
136
if not isinstance(real_branch, RemoteBranch):
137
return RemoteBranch(self, self.find_repository(), real_branch)
141
def destroy_branch(self):
142
"""See BzrDir.destroy_branch"""
144
self._real_bzrdir.destroy_branch()
146
def create_workingtree(self, revision_id=None, from_branch=None):
147
raise errors.NotLocalUrl(self.transport.base)
149
def find_branch_format(self):
150
"""Find the branch 'format' for this bzrdir.
152
This might be a synthetic object for e.g. RemoteBranch and SVN.
154
b = self.open_branch()
157
def get_branch_reference(self):
158
"""See BzrDir.get_branch_reference()."""
159
path = self._path_for_remote_call(self._client)
160
response = self._call('BzrDir.open_branch', path)
161
if response[0] == 'ok':
162
if response[1] == '':
163
# branch at this location.
166
# a branch reference, use the existing BranchReference logic.
169
raise errors.UnexpectedSmartServerResponse(response)
171
def _get_tree_branch(self):
172
"""See BzrDir._get_tree_branch()."""
173
return None, self.open_branch()
175
def open_branch(self, _unsupported=False):
177
raise NotImplementedError('unsupported flag support not implemented yet.')
178
reference_url = self.get_branch_reference()
179
if reference_url is None:
180
# branch at this location.
181
return RemoteBranch(self, self.find_repository())
183
# a branch reference, use the existing BranchReference logic.
184
format = BranchReferenceFormat()
185
return format.open(self, _found=True, location=reference_url)
187
def open_repository(self):
188
path = self._path_for_remote_call(self._client)
189
verb = 'BzrDir.find_repositoryV2'
191
response = self._call(verb, path)
192
except errors.UnknownSmartMethod:
193
verb = 'BzrDir.find_repository'
194
response = self._call(verb, path)
195
if response[0] != 'ok':
196
raise errors.UnexpectedSmartServerResponse(response)
197
if verb == 'BzrDir.find_repository':
198
# servers that don't support the V2 method don't support external
200
response = response + ('no', )
201
if not (len(response) == 5):
202
raise SmartProtocolError('incorrect response length %s' % (response,))
203
if response[1] == '':
204
format = RemoteRepositoryFormat()
205
format.rich_root_data = (response[2] == 'yes')
206
format.supports_tree_reference = (response[3] == 'yes')
207
# No wire format to check this yet.
208
format.supports_external_lookups = (response[4] == 'yes')
209
# Used to support creating a real format instance when needed.
210
format._creating_bzrdir = self
211
remote_repo = RemoteRepository(self, format)
212
format._creating_repo = remote_repo
215
raise errors.NoRepositoryPresent(self)
217
def open_workingtree(self, recommend_upgrade=True):
219
if self._real_bzrdir.has_workingtree():
220
raise errors.NotLocalUrl(self.root_transport)
222
raise errors.NoWorkingTree(self.root_transport.base)
224
def _path_for_remote_call(self, client):
225
"""Return the path to be used for this bzrdir in a remote call."""
226
return client.remote_path_from_transport(self.root_transport)
228
def get_branch_transport(self, branch_format):
230
return self._real_bzrdir.get_branch_transport(branch_format)
232
def get_repository_transport(self, repository_format):
234
return self._real_bzrdir.get_repository_transport(repository_format)
236
def get_workingtree_transport(self, workingtree_format):
238
return self._real_bzrdir.get_workingtree_transport(workingtree_format)
240
def can_convert_format(self):
241
"""Upgrading of remote bzrdirs is not supported yet."""
244
def needs_format_conversion(self, format=None):
245
"""Upgrading of remote bzrdirs is not supported yet."""
247
symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
248
% 'needs_format_conversion(format=None)')
251
def clone(self, url, revision_id=None, force_new_repo=False,
252
preserve_stacking=False):
254
return self._real_bzrdir.clone(url, revision_id=revision_id,
255
force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
257
def get_config(self):
259
return self._real_bzrdir.get_config()
262
class RemoteRepositoryFormat(repository.RepositoryFormat):
263
"""Format for repositories accessed over a _SmartClient.
265
Instances of this repository are represented by RemoteRepository
268
The RemoteRepositoryFormat is parameterized during construction
269
to reflect the capabilities of the real, remote format. Specifically
270
the attributes rich_root_data and supports_tree_reference are set
271
on a per instance basis, and are not set (and should not be) at
274
:ivar _custom_format: If set, a specific concrete repository format that
275
will be used when initializing a repository with this
276
RemoteRepositoryFormat.
277
:ivar _creating_repo: If set, the repository object that this
278
RemoteRepositoryFormat was created for: it can be called into
279
to obtain data like the network name.
282
_matchingbzrdir = RemoteBzrDirFormat()
285
repository.RepositoryFormat.__init__(self)
286
self._custom_format = None
287
self._network_name = None
288
self._creating_bzrdir = None
290
def _vfs_initialize(self, a_bzrdir, shared):
291
"""Helper for common code in initialize."""
292
if self._custom_format:
293
# Custom format requested
294
result = self._custom_format.initialize(a_bzrdir, shared=shared)
295
elif self._creating_bzrdir is not None:
296
# Use the format that the repository we were created to back
298
prior_repo = self._creating_bzrdir.open_repository()
299
prior_repo._ensure_real()
300
result = prior_repo._real_repository._format.initialize(
301
a_bzrdir, shared=shared)
303
# assume that a_bzr is a RemoteBzrDir but the smart server didn't
304
# support remote initialization.
305
# We delegate to a real object at this point (as RemoteBzrDir
306
# delegate to the repository format which would lead to infinite
307
# recursion if we just called a_bzrdir.create_repository.
308
a_bzrdir._ensure_real()
309
result = a_bzrdir._real_bzrdir.create_repository(shared=shared)
310
if not isinstance(result, RemoteRepository):
311
return self.open(a_bzrdir)
315
def initialize(self, a_bzrdir, shared=False):
316
# Being asked to create on a non RemoteBzrDir:
317
if not isinstance(a_bzrdir, RemoteBzrDir):
318
return self._vfs_initialize(a_bzrdir, shared)
319
medium = a_bzrdir._client._medium
320
if medium._is_remote_before((1, 13)):
321
return self._vfs_initialize(a_bzrdir, shared)
322
# Creating on a remote bzr dir.
323
# 1) get the network name to use.
324
if self._custom_format:
325
network_name = self._custom_format.network_name()
327
# Select the current bzrlib default and ask for that.
328
reference_bzrdir_format = bzrdir.format_registry.get('default')()
329
reference_format = reference_bzrdir_format.repository_format
330
network_name = reference_format.network_name()
331
# 2) try direct creation via RPC
332
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
333
verb = 'BzrDir.create_repository'
339
response = a_bzrdir._call(verb, path, network_name, shared_str)
340
except errors.UnknownSmartMethod:
341
# Fallback - use vfs methods
342
return self._vfs_initialize(a_bzrdir, shared)
344
# Turn the response into a RemoteRepository object.
345
format = RemoteRepositoryFormat()
346
format.rich_root_data = (response[1] == 'yes')
347
format.supports_tree_reference = (response[2] == 'yes')
348
format.supports_external_lookups = (response[3] == 'yes')
349
format._network_name = response[4]
350
# Used to support creating a real format instance when needed.
351
format._creating_bzrdir = a_bzrdir
352
remote_repo = RemoteRepository(a_bzrdir, format)
353
format._creating_repo = remote_repo
356
def open(self, a_bzrdir):
357
if not isinstance(a_bzrdir, RemoteBzrDir):
358
raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
359
return a_bzrdir.open_repository()
361
def get_format_description(self):
362
return 'bzr remote repository'
364
def __eq__(self, other):
365
return self.__class__ == other.__class__
367
def check_conversion_target(self, target_format):
368
if self.rich_root_data and not target_format.rich_root_data:
369
raise errors.BadConversionTarget(
370
'Does not support rich root data.', target_format)
371
if (self.supports_tree_reference and
372
not getattr(target_format, 'supports_tree_reference', False)):
373
raise errors.BadConversionTarget(
374
'Does not support nested trees', target_format)
376
def network_name(self):
377
if self._network_name:
378
return self._network_name
379
self._creating_repo._ensure_real()
380
return self._creating_repo._real_repository._format.network_name()
383
def _serializer(self):
384
# We should only be getting asked for the serializer for
385
# RemoteRepositoryFormat objects when the RemoteRepositoryFormat object
386
# is a concrete instance for a RemoteRepository. In this case we know
387
# the creating_repo and can use it to supply the serializer.
388
self._creating_repo._ensure_real()
389
return self._creating_repo._real_repository._format._serializer
392
class RemoteRepository(_RpcHelper):
393
"""Repository accessed over rpc.
395
For the moment most operations are performed using local transport-backed
399
def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
400
"""Create a RemoteRepository instance.
402
:param remote_bzrdir: The bzrdir hosting this repository.
403
:param format: The RemoteFormat object to use.
404
:param real_repository: If not None, a local implementation of the
405
repository logic for the repository, usually accessing the data
407
:param _client: Private testing parameter - override the smart client
408
to be used by the repository.
411
self._real_repository = real_repository
413
self._real_repository = None
414
self.bzrdir = remote_bzrdir
416
self._client = remote_bzrdir._client
418
self._client = _client
419
self._format = format
420
self._lock_mode = None
421
self._lock_token = None
423
self._leave_lock = False
424
self._unstacked_provider = graph.CachingParentsProvider(
425
get_parent_map=self._get_parent_map_rpc)
426
self._unstacked_provider.disable_cache()
428
# These depend on the actual remote format, so force them off for
429
# maximum compatibility. XXX: In future these should depend on the
430
# remote repository instance, but this is irrelevant until we perform
431
# reconcile via an RPC call.
432
self._reconcile_does_inventory_gc = False
433
self._reconcile_fixes_text_parents = False
434
self._reconcile_backsup_inventory = False
435
self.base = self.bzrdir.transport.base
436
# Additional places to query for data.
437
self._fallback_repositories = []
440
return "%s(%s)" % (self.__class__.__name__, self.base)
444
def abort_write_group(self, suppress_errors=False):
445
"""Complete a write group on the decorated repository.
447
Smart methods perform operations in a single step so this api
448
is not really applicable except as a compatibility thunk
449
for older plugins that don't use e.g. the CommitBuilder
452
:param suppress_errors: see Repository.abort_write_group.
455
return self._real_repository.abort_write_group(
456
suppress_errors=suppress_errors)
458
def commit_write_group(self):
459
"""Complete a write group on the decorated repository.
461
Smart methods perform operations in a single step so this api
462
is not really applicable except as a compatibility thunk
463
for older plugins that don't use e.g. the CommitBuilder
467
return self._real_repository.commit_write_group()
469
def resume_write_group(self, tokens):
471
return self._real_repository.resume_write_group(tokens)
473
def suspend_write_group(self):
475
return self._real_repository.suspend_write_group()
477
def _ensure_real(self):
478
"""Ensure that there is a _real_repository set.
480
Used before calls to self._real_repository.
482
if self._real_repository is None:
483
self.bzrdir._ensure_real()
484
self._set_real_repository(
485
self.bzrdir._real_bzrdir.open_repository())
487
def _translate_error(self, err, **context):
488
self.bzrdir._translate_error(err, repository=self, **context)
490
def find_text_key_references(self):
491
"""Find the text key references within the repository.
493
:return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
494
revision_ids. Each altered file-ids has the exact revision_ids that
495
altered it listed explicitly.
496
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
497
to whether they were referred to by the inventory of the
498
revision_id that they contain. The inventory texts from all present
499
revision ids are assessed to generate this report.
502
return self._real_repository.find_text_key_references()
504
def _generate_text_key_index(self):
505
"""Generate a new text key index for the repository.
507
This is an expensive function that will take considerable time to run.
509
:return: A dict mapping (file_id, revision_id) tuples to a list of
510
parents, also (file_id, revision_id) tuples.
513
return self._real_repository._generate_text_key_index()
515
@symbol_versioning.deprecated_method(symbol_versioning.one_four)
516
def get_revision_graph(self, revision_id=None):
517
"""See Repository.get_revision_graph()."""
518
return self._get_revision_graph(revision_id)
520
def _get_revision_graph(self, revision_id):
521
"""Private method for using with old (< 1.2) servers to fallback."""
522
if revision_id is None:
524
elif revision.is_null(revision_id):
527
path = self.bzrdir._path_for_remote_call(self._client)
528
response = self._call_expecting_body(
529
'Repository.get_revision_graph', path, revision_id)
530
response_tuple, response_handler = response
531
if response_tuple[0] != 'ok':
532
raise errors.UnexpectedSmartServerResponse(response_tuple)
533
coded = response_handler.read_body_bytes()
535
# no revisions in this repository!
537
lines = coded.split('\n')
540
d = tuple(line.split())
541
revision_graph[d[0]] = d[1:]
543
return revision_graph
546
"""See Repository._get_sink()."""
547
return RemoteStreamSink(self)
549
def has_revision(self, revision_id):
550
"""See Repository.has_revision()."""
551
if revision_id == NULL_REVISION:
552
# The null revision is always present.
554
path = self.bzrdir._path_for_remote_call(self._client)
555
response = self._call('Repository.has_revision', path, revision_id)
556
if response[0] not in ('yes', 'no'):
557
raise errors.UnexpectedSmartServerResponse(response)
558
if response[0] == 'yes':
560
for fallback_repo in self._fallback_repositories:
561
if fallback_repo.has_revision(revision_id):
565
def has_revisions(self, revision_ids):
566
"""See Repository.has_revisions()."""
567
# FIXME: This does many roundtrips, particularly when there are
568
# fallback repositories. -- mbp 20080905
570
for revision_id in revision_ids:
571
if self.has_revision(revision_id):
572
result.add(revision_id)
575
def has_same_location(self, other):
576
return (self.__class__ == other.__class__ and
577
self.bzrdir.transport.base == other.bzrdir.transport.base)
579
def get_graph(self, other_repository=None):
580
"""Return the graph for this repository format"""
581
parents_provider = self._make_parents_provider(other_repository)
582
return graph.Graph(parents_provider)
584
def gather_stats(self, revid=None, committers=None):
585
"""See Repository.gather_stats()."""
586
path = self.bzrdir._path_for_remote_call(self._client)
587
# revid can be None to indicate no revisions, not just NULL_REVISION
588
if revid is None or revision.is_null(revid):
592
if committers is None or not committers:
593
fmt_committers = 'no'
595
fmt_committers = 'yes'
596
response_tuple, response_handler = self._call_expecting_body(
597
'Repository.gather_stats', path, fmt_revid, fmt_committers)
598
if response_tuple[0] != 'ok':
599
raise errors.UnexpectedSmartServerResponse(response_tuple)
601
body = response_handler.read_body_bytes()
603
for line in body.split('\n'):
606
key, val_text = line.split(':')
607
if key in ('revisions', 'size', 'committers'):
608
result[key] = int(val_text)
609
elif key in ('firstrev', 'latestrev'):
610
values = val_text.split(' ')[1:]
611
result[key] = (float(values[0]), long(values[1]))
615
def find_branches(self, using=False):
616
"""See Repository.find_branches()."""
617
# should be an API call to the server.
619
return self._real_repository.find_branches(using=using)
621
def get_physical_lock_status(self):
622
"""See Repository.get_physical_lock_status()."""
623
# should be an API call to the server.
625
return self._real_repository.get_physical_lock_status()
627
def is_in_write_group(self):
628
"""Return True if there is an open write group.
630
write groups are only applicable locally for the smart server..
632
if self._real_repository:
633
return self._real_repository.is_in_write_group()
636
return self._lock_count >= 1
639
"""See Repository.is_shared()."""
640
path = self.bzrdir._path_for_remote_call(self._client)
641
response = self._call('Repository.is_shared', path)
642
if response[0] not in ('yes', 'no'):
643
raise SmartProtocolError('unexpected response code %s' % (response,))
644
return response[0] == 'yes'
646
def is_write_locked(self):
647
return self._lock_mode == 'w'
650
# wrong eventually - want a local lock cache context
651
if not self._lock_mode:
652
self._lock_mode = 'r'
654
self._unstacked_provider.enable_cache(cache_misses=False)
655
if self._real_repository is not None:
656
self._real_repository.lock_read()
658
self._lock_count += 1
660
def _remote_lock_write(self, token):
661
path = self.bzrdir._path_for_remote_call(self._client)
664
err_context = {'token': token}
665
response = self._call('Repository.lock_write', path, token,
667
if response[0] == 'ok':
671
raise errors.UnexpectedSmartServerResponse(response)
673
def lock_write(self, token=None, _skip_rpc=False):
674
if not self._lock_mode:
676
if self._lock_token is not None:
677
if token != self._lock_token:
678
raise errors.TokenMismatch(token, self._lock_token)
679
self._lock_token = token
681
self._lock_token = self._remote_lock_write(token)
682
# if self._lock_token is None, then this is something like packs or
683
# svn where we don't get to lock the repo, or a weave style repository
684
# where we cannot lock it over the wire and attempts to do so will
686
if self._real_repository is not None:
687
self._real_repository.lock_write(token=self._lock_token)
688
if token is not None:
689
self._leave_lock = True
691
self._leave_lock = False
692
self._lock_mode = 'w'
694
self._unstacked_provider.enable_cache(cache_misses=False)
695
elif self._lock_mode == 'r':
696
raise errors.ReadOnlyError(self)
698
self._lock_count += 1
699
return self._lock_token or None
701
def leave_lock_in_place(self):
702
if not self._lock_token:
703
raise NotImplementedError(self.leave_lock_in_place)
704
self._leave_lock = True
706
def dont_leave_lock_in_place(self):
707
if not self._lock_token:
708
raise NotImplementedError(self.dont_leave_lock_in_place)
709
self._leave_lock = False
711
def _set_real_repository(self, repository):
712
"""Set the _real_repository for this repository.
714
:param repository: The repository to fallback to for non-hpss
715
implemented operations.
717
if self._real_repository is not None:
718
raise AssertionError('_real_repository is already set')
719
if isinstance(repository, RemoteRepository):
720
raise AssertionError()
721
self._real_repository = repository
722
for fb in self._fallback_repositories:
723
self._real_repository.add_fallback_repository(fb)
724
if self._lock_mode == 'w':
725
# if we are already locked, the real repository must be able to
726
# acquire the lock with our token.
727
self._real_repository.lock_write(self._lock_token)
728
elif self._lock_mode == 'r':
729
self._real_repository.lock_read()
731
def start_write_group(self):
732
"""Start a write group on the decorated repository.
734
Smart methods perform operations in a single step so this api
735
is not really applicable except as a compatibility thunk
736
for older plugins that don't use e.g. the CommitBuilder
740
return self._real_repository.start_write_group()
742
def _unlock(self, token):
743
path = self.bzrdir._path_for_remote_call(self._client)
745
# with no token the remote repository is not persistently locked.
747
err_context = {'token': token}
748
response = self._call('Repository.unlock', path, token,
750
if response == ('ok',):
753
raise errors.UnexpectedSmartServerResponse(response)
756
self._lock_count -= 1
757
if self._lock_count > 0:
759
self._unstacked_provider.disable_cache()
760
old_mode = self._lock_mode
761
self._lock_mode = None
763
# The real repository is responsible at present for raising an
764
# exception if it's in an unfinished write group. However, it
765
# normally will *not* actually remove the lock from disk - that's
766
# done by the server on receiving the Repository.unlock call.
767
# This is just to let the _real_repository stay up to date.
768
if self._real_repository is not None:
769
self._real_repository.unlock()
771
# The rpc-level lock should be released even if there was a
772
# problem releasing the vfs-based lock.
774
# Only write-locked repositories need to make a remote method
775
# call to perform the unlock.
776
old_token = self._lock_token
777
self._lock_token = None
778
if not self._leave_lock:
779
self._unlock(old_token)
781
def break_lock(self):
782
# should hand off to the network
784
return self._real_repository.break_lock()
786
def _get_tarball(self, compression):
787
"""Return a TemporaryFile containing a repository tarball.
789
Returns None if the server does not support sending tarballs.
792
path = self.bzrdir._path_for_remote_call(self._client)
794
response, protocol = self._call_expecting_body(
795
'Repository.tarball', path, compression)
796
except errors.UnknownSmartMethod:
797
protocol.cancel_read_body()
799
if response[0] == 'ok':
800
# Extract the tarball and return it
801
t = tempfile.NamedTemporaryFile()
802
# TODO: rpc layer should read directly into it...
803
t.write(protocol.read_body_bytes())
806
raise errors.UnexpectedSmartServerResponse(response)
808
def sprout(self, to_bzrdir, revision_id=None):
809
# TODO: Option to control what format is created?
811
dest_repo = self._real_repository._format.initialize(to_bzrdir,
813
dest_repo.fetch(self, revision_id=revision_id)
816
### These methods are just thin shims to the VFS object for now.
818
def revision_tree(self, revision_id):
820
return self._real_repository.revision_tree(revision_id)
822
def get_serializer_format(self):
824
return self._real_repository.get_serializer_format()
826
def get_commit_builder(self, branch, parents, config, timestamp=None,
827
timezone=None, committer=None, revprops=None,
829
# FIXME: It ought to be possible to call this without immediately
830
# triggering _ensure_real. For now it's the easiest thing to do.
832
real_repo = self._real_repository
833
builder = real_repo.get_commit_builder(branch, parents,
834
config, timestamp=timestamp, timezone=timezone,
835
committer=committer, revprops=revprops, revision_id=revision_id)
838
def add_fallback_repository(self, repository):
839
"""Add a repository to use for looking up data not held locally.
841
:param repository: A repository.
843
# XXX: At the moment the RemoteRepository will allow fallbacks
844
# unconditionally - however, a _real_repository will usually exist,
845
# and may raise an error if it's not accommodated by the underlying
846
# format. Eventually we should check when opening the repository
847
# whether it's willing to allow them or not.
849
# We need to accumulate additional repositories here, to pass them in
851
self._fallback_repositories.append(repository)
852
# They are also seen by the fallback repository. If it doesn't exist
853
# yet they'll be added then. This implicitly copies them.
856
def add_inventory(self, revid, inv, parents):
858
return self._real_repository.add_inventory(revid, inv, parents)
860
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
863
return self._real_repository.add_inventory_by_delta(basis_revision_id,
864
delta, new_revision_id, parents)
866
def add_revision(self, rev_id, rev, inv=None, config=None):
868
return self._real_repository.add_revision(
869
rev_id, rev, inv=inv, config=config)
872
def get_inventory(self, revision_id):
874
return self._real_repository.get_inventory(revision_id)
876
def iter_inventories(self, revision_ids):
878
return self._real_repository.iter_inventories(revision_ids)
881
def get_revision(self, revision_id):
883
return self._real_repository.get_revision(revision_id)
885
def get_transaction(self):
887
return self._real_repository.get_transaction()
890
def clone(self, a_bzrdir, revision_id=None):
892
return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
894
def make_working_trees(self):
895
"""See Repository.make_working_trees"""
897
return self._real_repository.make_working_trees()
899
def revision_ids_to_search_result(self, result_set):
900
"""Convert a set of revision ids to a graph SearchResult."""
901
result_parents = set()
902
for parents in self.get_graph().get_parent_map(
903
result_set).itervalues():
904
result_parents.update(parents)
905
included_keys = result_set.intersection(result_parents)
906
start_keys = result_set.difference(included_keys)
907
exclude_keys = result_parents.difference(result_set)
908
result = graph.SearchResult(start_keys, exclude_keys,
909
len(result_set), result_set)
913
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
914
"""Return the revision ids that other has that this does not.
916
These are returned in topological order.
918
revision_id: only return revision ids included by revision_id.
920
return repository.InterRepository.get(
921
other, self).search_missing_revision_ids(revision_id, find_ghosts)
923
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
924
# Not delegated to _real_repository so that InterRepository.get has a
925
# chance to find an InterRepository specialised for RemoteRepository.
926
if self.has_same_location(source):
927
# check that last_revision is in 'from' and then return a
929
if (revision_id is not None and
930
not revision.is_null(revision_id)):
931
self.get_revision(revision_id)
933
inter = repository.InterRepository.get(source, self)
935
return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
936
except NotImplementedError:
937
raise errors.IncompatibleRepositories(source, self)
939
def create_bundle(self, target, base, fileobj, format=None):
941
self._real_repository.create_bundle(target, base, fileobj, format)
944
def get_ancestry(self, revision_id, topo_sorted=True):
946
return self._real_repository.get_ancestry(revision_id, topo_sorted)
948
def fileids_altered_by_revision_ids(self, revision_ids):
950
return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
952
def _get_versioned_file_checker(self, revisions, revision_versions_cache):
954
return self._real_repository._get_versioned_file_checker(
955
revisions, revision_versions_cache)
957
def iter_files_bytes(self, desired_files):
958
"""See Repository.iter_file_bytes.
961
return self._real_repository.iter_files_bytes(desired_files)
964
def _fetch_order(self):
965
"""Decorate the real repository for now.
967
In the long term getting this back from the remote repository as part
968
of open would be more efficient.
971
return self._real_repository._fetch_order
974
def _fetch_uses_deltas(self):
975
"""Decorate the real repository for now.
977
In the long term getting this back from the remote repository as part
978
of open would be more efficient.
981
return self._real_repository._fetch_uses_deltas
984
def _fetch_reconcile(self):
985
"""Decorate the real repository for now.
987
In the long term getting this back from the remote repository as part
988
of open would be more efficient.
991
return self._real_repository._fetch_reconcile
993
def get_parent_map(self, revision_ids):
994
"""See bzrlib.Graph.get_parent_map()."""
995
return self._make_parents_provider().get_parent_map(revision_ids)
997
def _get_parent_map_rpc(self, keys):
998
"""Helper for get_parent_map that performs the RPC."""
999
medium = self._client._medium
1000
if medium._is_remote_before((1, 2)):
1001
# We already found out that the server can't understand
1002
# Repository.get_parent_map requests, so just fetch the whole
1004
# XXX: Note that this will issue a deprecation warning. This is ok
1005
# :- its because we're working with a deprecated server anyway, and
1006
# the user will almost certainly have seen a warning about the
1007
# server version already.
1008
rg = self.get_revision_graph()
1009
# There is an api discrepancy between get_parent_map and
1010
# get_revision_graph. Specifically, a "key:()" pair in
1011
# get_revision_graph just means a node has no parents. For
1012
# "get_parent_map" it means the node is a ghost. So fix up the
1013
# graph to correct this.
1014
# https://bugs.launchpad.net/bzr/+bug/214894
1015
# There is one other "bug" which is that ghosts in
1016
# get_revision_graph() are not returned at all. But we won't worry
1017
# about that for now.
1018
for node_id, parent_ids in rg.iteritems():
1019
if parent_ids == ():
1020
rg[node_id] = (NULL_REVISION,)
1021
rg[NULL_REVISION] = ()
1026
raise ValueError('get_parent_map(None) is not valid')
1027
if NULL_REVISION in keys:
1028
keys.discard(NULL_REVISION)
1029
found_parents = {NULL_REVISION:()}
1031
return found_parents
1034
# TODO(Needs analysis): We could assume that the keys being requested
1035
# from get_parent_map are in a breadth first search, so typically they
1036
# will all be depth N from some common parent, and we don't have to
1037
# have the server iterate from the root parent, but rather from the
1038
# keys we're searching; and just tell the server the keyspace we
1039
# already have; but this may be more traffic again.
1041
# Transform self._parents_map into a search request recipe.
1042
# TODO: Manage this incrementally to avoid covering the same path
1043
# repeatedly. (The server will have to on each request, but the less
1044
# work done the better).
1045
parents_map = self._unstacked_provider.get_cached_map()
1046
if parents_map is None:
1047
# Repository is not locked, so there's no cache.
1049
start_set = set(parents_map)
1050
result_parents = set()
1051
for parents in parents_map.itervalues():
1052
result_parents.update(parents)
1053
stop_keys = result_parents.difference(start_set)
1054
included_keys = start_set.intersection(result_parents)
1055
start_set.difference_update(included_keys)
1056
recipe = (start_set, stop_keys, len(parents_map))
1057
body = self._serialise_search_recipe(recipe)
1058
path = self.bzrdir._path_for_remote_call(self._client)
1060
if type(key) is not str:
1062
"key %r not a plain string" % (key,))
1063
verb = 'Repository.get_parent_map'
1064
args = (path,) + tuple(keys)
1066
response = self._call_with_body_bytes_expecting_body(
1068
except errors.UnknownSmartMethod:
1069
# Server does not support this method, so get the whole graph.
1070
# Worse, we have to force a disconnection, because the server now
1071
# doesn't realise it has a body on the wire to consume, so the
1072
# only way to recover is to abandon the connection.
1074
'Server is too old for fast get_parent_map, reconnecting. '
1075
'(Upgrade the server to Bazaar 1.2 to avoid this)')
1077
# To avoid having to disconnect repeatedly, we keep track of the
1078
# fact the server doesn't understand remote methods added in 1.2.
1079
medium._remember_remote_is_before((1, 2))
1080
return self.get_revision_graph(None)
1081
response_tuple, response_handler = response
1082
if response_tuple[0] not in ['ok']:
1083
response_handler.cancel_read_body()
1084
raise errors.UnexpectedSmartServerResponse(response_tuple)
1085
if response_tuple[0] == 'ok':
1086
coded = bz2.decompress(response_handler.read_body_bytes())
1088
# no revisions found
1090
lines = coded.split('\n')
1093
d = tuple(line.split())
1095
revision_graph[d[0]] = d[1:]
1097
# No parents - so give the Graph result (NULL_REVISION,).
1098
revision_graph[d[0]] = (NULL_REVISION,)
1099
return revision_graph
1102
def get_signature_text(self, revision_id):
1104
return self._real_repository.get_signature_text(revision_id)
1107
@symbol_versioning.deprecated_method(symbol_versioning.one_three)
1108
def get_revision_graph_with_ghosts(self, revision_ids=None):
1110
return self._real_repository.get_revision_graph_with_ghosts(
1111
revision_ids=revision_ids)
1114
def get_inventory_xml(self, revision_id):
1116
return self._real_repository.get_inventory_xml(revision_id)
1118
def deserialise_inventory(self, revision_id, xml):
1120
return self._real_repository.deserialise_inventory(revision_id, xml)
1122
def reconcile(self, other=None, thorough=False):
1124
return self._real_repository.reconcile(other=other, thorough=thorough)
1126
def all_revision_ids(self):
1128
return self._real_repository.all_revision_ids()
1131
def get_deltas_for_revisions(self, revisions):
1133
return self._real_repository.get_deltas_for_revisions(revisions)
1136
def get_revision_delta(self, revision_id):
1138
return self._real_repository.get_revision_delta(revision_id)
1141
def revision_trees(self, revision_ids):
1143
return self._real_repository.revision_trees(revision_ids)
1146
def get_revision_reconcile(self, revision_id):
1148
return self._real_repository.get_revision_reconcile(revision_id)
1151
def check(self, revision_ids=None):
1153
return self._real_repository.check(revision_ids=revision_ids)
1155
def copy_content_into(self, destination, revision_id=None):
1157
return self._real_repository.copy_content_into(
1158
destination, revision_id=revision_id)
1160
def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
1161
# get a tarball of the remote repository, and copy from that into the
1163
from bzrlib import osutils
1165
# TODO: Maybe a progress bar while streaming the tarball?
1166
note("Copying repository content as tarball...")
1167
tar_file = self._get_tarball('bz2')
1168
if tar_file is None:
1170
destination = to_bzrdir.create_repository()
1172
tar = tarfile.open('repository', fileobj=tar_file,
1174
tmpdir = osutils.mkdtemp()
1176
_extract_tar(tar, tmpdir)
1177
tmp_bzrdir = BzrDir.open(tmpdir)
1178
tmp_repo = tmp_bzrdir.open_repository()
1179
tmp_repo.copy_content_into(destination, revision_id)
1181
osutils.rmtree(tmpdir)
1185
# TODO: Suggestion from john: using external tar is much faster than
1186
# python's tarfile library, but it may not work on windows.
1189
def inventories(self):
1190
"""Decorate the real repository for now.
1192
In the long term a full blown network facility is needed to
1193
avoid creating a real repository object locally.
1196
return self._real_repository.inventories
1200
"""Compress the data within the repository.
1202
This is not currently implemented within the smart server.
1205
return self._real_repository.pack()
1208
def revisions(self):
1209
"""Decorate the real repository for now.
1211
In the short term this should become a real object to intercept graph
1214
In the long term a full blown network facility is needed.
1217
return self._real_repository.revisions
1219
def set_make_working_trees(self, new_value):
1221
new_value_str = "True"
1223
new_value_str = "False"
1224
path = self.bzrdir._path_for_remote_call(self._client)
1226
response = self._call(
1227
'Repository.set_make_working_trees', path, new_value_str)
1228
except errors.UnknownSmartMethod:
1230
self._real_repository.set_make_working_trees(new_value)
1232
if response[0] != 'ok':
1233
raise errors.UnexpectedSmartServerResponse(response)
1236
def signatures(self):
1237
"""Decorate the real repository for now.
1239
In the long term a full blown network facility is needed to avoid
1240
creating a real repository object locally.
1243
return self._real_repository.signatures
1246
def sign_revision(self, revision_id, gpg_strategy):
1248
return self._real_repository.sign_revision(revision_id, gpg_strategy)
1252
"""Decorate the real repository for now.
1254
In the long term a full blown network facility is needed to avoid
1255
creating a real repository object locally.
1258
return self._real_repository.texts
1261
def get_revisions(self, revision_ids):
1263
return self._real_repository.get_revisions(revision_ids)
1265
def supports_rich_root(self):
1267
return self._real_repository.supports_rich_root()
1269
def iter_reverse_revision_history(self, revision_id):
1271
return self._real_repository.iter_reverse_revision_history(revision_id)
1274
def _serializer(self):
1276
return self._real_repository._serializer
1278
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1280
return self._real_repository.store_revision_signature(
1281
gpg_strategy, plaintext, revision_id)
1283
def add_signature_text(self, revision_id, signature):
1285
return self._real_repository.add_signature_text(revision_id, signature)
1287
def has_signature_for_revision_id(self, revision_id):
1289
return self._real_repository.has_signature_for_revision_id(revision_id)
1291
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1293
return self._real_repository.item_keys_introduced_by(revision_ids,
1294
_files_pb=_files_pb)
1296
def revision_graph_can_have_wrong_parents(self):
1297
# The answer depends on the remote repo format.
1299
return self._real_repository.revision_graph_can_have_wrong_parents()
1301
def _find_inconsistent_revision_parents(self):
1303
return self._real_repository._find_inconsistent_revision_parents()
1305
def _check_for_inconsistent_revision_parents(self):
1307
return self._real_repository._check_for_inconsistent_revision_parents()
1309
def _make_parents_provider(self, other=None):
1310
providers = [self._unstacked_provider]
1311
if other is not None:
1312
providers.insert(0, other)
1313
providers.extend(r._make_parents_provider() for r in
1314
self._fallback_repositories)
1315
return graph._StackedParentsProvider(providers)
1317
def _serialise_search_recipe(self, recipe):
1318
"""Serialise a graph search recipe.
1320
:param recipe: A search recipe (start, stop, count).
1321
:return: Serialised bytes.
1323
start_keys = ' '.join(recipe[0])
1324
stop_keys = ' '.join(recipe[1])
1325
count = str(recipe[2])
1326
return '\n'.join((start_keys, stop_keys, count))
1329
path = self.bzrdir._path_for_remote_call(self._client)
1331
response = self._call('PackRepository.autopack', path)
1332
except errors.UnknownSmartMethod:
1334
self._real_repository._pack_collection.autopack()
1336
if self._real_repository is not None:
1337
# Reset the real repository's cache of pack names.
1338
# XXX: At some point we may be able to skip this and just rely on
1339
# the automatic retry logic to do the right thing, but for now we
1340
# err on the side of being correct rather than being optimal.
1341
self._real_repository._pack_collection.reload_pack_names()
1342
if response[0] != 'ok':
1343
raise errors.UnexpectedSmartServerResponse(response)
1346
class RemoteStreamSink(repository.StreamSink):
1348
def _insert_real(self, stream, src_format):
1349
self.target_repo._ensure_real()
1350
sink = self.target_repo._real_repository._get_sink()
1351
return sink.insert_stream(stream, src_format)
1353
def insert_stream(self, stream, src_format):
1354
repo = self.target_repo
1355
# Until we can handle deltas in stack repositories we can't hand all
1356
# the processing off to a remote server.
1357
if self.target_repo._fallback_repositories:
1358
return self._insert_real(stream, src_format)
1359
client = repo._client
1360
medium = client._medium
1361
if medium._is_remote_before((1,13)):
1362
# No possible way this can work.
1363
return self._insert_real(stream, src_format)
1364
path = repo.bzrdir._path_for_remote_call(client)
1365
# XXX: Ugly but important for correctness, *will* be fixed during 1.13
1366
# cycle. Pushing a stream that is interrupted results in a fallback to
1367
# the _real_repositories sink *with a partial stream*. Thats bad
1368
# because we insert less data than bzr expected. To avoid this we do a
1369
# trial push to make sure the verb is accessible, and do not fallback
1370
# when actually pushing the stream. A cleanup patch is going to look at
1371
# rewinding/restarting the stream/partial buffering etc.
1372
byte_stream = self._stream_to_byte_stream([], src_format)
1374
response = client.call_with_body_stream(
1375
('Repository.insert_stream', path), byte_stream)
1376
except errors.UnknownSmartMethod:
1377
medium._remember_remote_is_before((1,13))
1378
return self._insert_real(stream, src_format)
1379
byte_stream = self._stream_to_byte_stream(stream, src_format)
1380
response = client.call_with_body_stream(
1381
('Repository.insert_stream', path), byte_stream)
1382
if response[0][0] not in ('ok', ):
1383
raise errors.UnexpectedSmartServerResponse(response)
1385
def _stream_to_byte_stream(self, stream, src_format):
1387
pack_writer = pack.ContainerWriter(bytes.append)
1389
pack_writer.add_bytes_record(src_format.network_name(), '')
1391
def get_adapter(adapter_key):
1393
return adapters[adapter_key]
1395
adapter_factory = adapter_registry.get(adapter_key)
1396
adapter = adapter_factory(self)
1397
adapters[adapter_key] = adapter
1399
for substream_type, substream in stream:
1400
for record in substream:
1401
if record.storage_kind in ('chunked', 'fulltext'):
1402
serialised = record_to_fulltext_bytes(record)
1404
serialised = record.get_bytes_as(record.storage_kind)
1405
pack_writer.add_bytes_record(serialised, [(substream_type,)])
1414
class RemoteBranchLockableFiles(LockableFiles):
1415
"""A 'LockableFiles' implementation that talks to a smart server.
1417
This is not a public interface class.
1420
def __init__(self, bzrdir, _client):
1421
self.bzrdir = bzrdir
1422
self._client = _client
1423
self._need_find_modes = True
1424
LockableFiles.__init__(
1425
self, bzrdir.get_branch_transport(None),
1426
'lock', lockdir.LockDir)
1428
def _find_modes(self):
1429
# RemoteBranches don't let the client set the mode of control files.
1430
self._dir_mode = None
1431
self._file_mode = None
1434
class RemoteBranchFormat(branch.BranchFormat):
1437
super(RemoteBranchFormat, self).__init__()
1438
self._matchingbzrdir = RemoteBzrDirFormat()
1439
self._matchingbzrdir.set_branch_format(self)
1441
def __eq__(self, other):
1442
return (isinstance(other, RemoteBranchFormat) and
1443
self.__dict__ == other.__dict__)
1445
def get_format_description(self):
1446
return 'Remote BZR Branch'
1448
def get_format_string(self):
1449
return 'Remote BZR Branch'
1451
def open(self, a_bzrdir):
1452
return a_bzrdir.open_branch()
1454
def initialize(self, a_bzrdir):
1455
# Delegate to a _real object here - the RemoteBzrDir format now
1456
# supports delegating to parameterised branch formats and as such
1457
# this RemoteBranchFormat method is only called when no specific format
1459
if not isinstance(a_bzrdir, RemoteBzrDir):
1460
result = a_bzrdir.create_branch()
1462
a_bzrdir._ensure_real()
1463
result = a_bzrdir._real_bzrdir.create_branch()
1464
if not isinstance(result, RemoteBranch):
1465
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
1468
def supports_tags(self):
1469
# Remote branches might support tags, but we won't know until we
1470
# access the real remote branch.
1474
class RemoteBranch(branch.Branch, _RpcHelper):
1475
"""Branch stored on a server accessed by HPSS RPC.
1477
At the moment most operations are mapped down to simple file operations.
1480
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
1482
"""Create a RemoteBranch instance.
1484
:param real_branch: An optional local implementation of the branch
1485
format, usually accessing the data via the VFS.
1486
:param _client: Private parameter for testing.
1488
# We intentionally don't call the parent class's __init__, because it
1489
# will try to assign to self.tags, which is a property in this subclass.
1490
# And the parent's __init__ doesn't do much anyway.
1491
self._revision_id_to_revno_cache = None
1492
self._partial_revision_id_to_revno_cache = {}
1493
self._revision_history_cache = None
1494
self._last_revision_info_cache = None
1495
self._merge_sorted_revisions_cache = None
1496
self.bzrdir = remote_bzrdir
1497
if _client is not None:
1498
self._client = _client
1500
self._client = remote_bzrdir._client
1501
self.repository = remote_repository
1502
if real_branch is not None:
1503
self._real_branch = real_branch
1504
# Give the remote repository the matching real repo.
1505
real_repo = self._real_branch.repository
1506
if isinstance(real_repo, RemoteRepository):
1507
real_repo._ensure_real()
1508
real_repo = real_repo._real_repository
1509
self.repository._set_real_repository(real_repo)
1510
# Give the branch the remote repository to let fast-pathing happen.
1511
self._real_branch.repository = self.repository
1513
self._real_branch = None
1514
# Fill out expected attributes of branch for bzrlib api users.
1515
self._format = RemoteBranchFormat()
1516
self.base = self.bzrdir.root_transport.base
1517
self._control_files = None
1518
self._lock_mode = None
1519
self._lock_token = None
1520
self._repo_lock_token = None
1521
self._lock_count = 0
1522
self._leave_lock = False
1523
# The base class init is not called, so we duplicate this:
1524
hooks = branch.Branch.hooks['open']
1527
self._setup_stacking()
1529
def _setup_stacking(self):
1530
# configure stacking into the remote repository, by reading it from
1533
fallback_url = self.get_stacked_on_url()
1534
except (errors.NotStacked, errors.UnstackableBranchFormat,
1535
errors.UnstackableRepositoryFormat), e:
1537
# it's relative to this branch...
1538
fallback_url = urlutils.join(self.base, fallback_url)
1539
transports = [self.bzrdir.root_transport]
1540
if self._real_branch is not None:
1541
transports.append(self._real_branch._transport)
1542
stacked_on = branch.Branch.open(fallback_url,
1543
possible_transports=transports)
1544
self.repository.add_fallback_repository(stacked_on.repository)
1546
def _get_real_transport(self):
1547
# if we try vfs access, return the real branch's vfs transport
1549
return self._real_branch._transport
1551
_transport = property(_get_real_transport)
1554
return "%s(%s)" % (self.__class__.__name__, self.base)
1558
def _ensure_real(self):
1559
"""Ensure that there is a _real_branch set.
1561
Used before calls to self._real_branch.
1563
if self._real_branch is None:
1564
if not vfs.vfs_enabled():
1565
raise AssertionError('smart server vfs must be enabled '
1566
'to use vfs implementation')
1567
self.bzrdir._ensure_real()
1568
self._real_branch = self.bzrdir._real_bzrdir.open_branch()
1569
if self.repository._real_repository is None:
1570
# Give the remote repository the matching real repo.
1571
real_repo = self._real_branch.repository
1572
if isinstance(real_repo, RemoteRepository):
1573
real_repo._ensure_real()
1574
real_repo = real_repo._real_repository
1575
self.repository._set_real_repository(real_repo)
1576
# Give the real branch the remote repository to let fast-pathing
1578
self._real_branch.repository = self.repository
1579
if self._lock_mode == 'r':
1580
self._real_branch.lock_read()
1581
elif self._lock_mode == 'w':
1582
self._real_branch.lock_write(token=self._lock_token)
1584
def _translate_error(self, err, **context):
1585
self.repository._translate_error(err, branch=self, **context)
1587
def _clear_cached_state(self):
1588
super(RemoteBranch, self)._clear_cached_state()
1589
if self._real_branch is not None:
1590
self._real_branch._clear_cached_state()
1592
def _clear_cached_state_of_remote_branch_only(self):
1593
"""Like _clear_cached_state, but doesn't clear the cache of
1596
This is useful when falling back to calling a method of
1597
self._real_branch that changes state. In that case the underlying
1598
branch changes, so we need to invalidate this RemoteBranch's cache of
1599
it. However, there's no need to invalidate the _real_branch's cache
1600
too, in fact doing so might harm performance.
1602
super(RemoteBranch, self)._clear_cached_state()
1605
def control_files(self):
1606
# Defer actually creating RemoteBranchLockableFiles until its needed,
1607
# because it triggers an _ensure_real that we otherwise might not need.
1608
if self._control_files is None:
1609
self._control_files = RemoteBranchLockableFiles(
1610
self.bzrdir, self._client)
1611
return self._control_files
1613
def _get_checkout_format(self):
1615
return self._real_branch._get_checkout_format()
1617
def get_physical_lock_status(self):
1618
"""See Branch.get_physical_lock_status()."""
1619
# should be an API call to the server, as branches must be lockable.
1621
return self._real_branch.get_physical_lock_status()
1623
def get_stacked_on_url(self):
1624
"""Get the URL this branch is stacked against.
1626
:raises NotStacked: If the branch is not stacked.
1627
:raises UnstackableBranchFormat: If the branch does not support
1629
:raises UnstackableRepositoryFormat: If the repository does not support
1633
# there may not be a repository yet, so we can't use
1634
# self._translate_error, so we can't use self._call either.
1635
response = self._client.call('Branch.get_stacked_on_url',
1636
self._remote_path())
1637
except errors.ErrorFromSmartServer, err:
1638
# there may not be a repository yet, so we can't call through
1639
# its _translate_error
1640
_translate_error(err, branch=self)
1641
except errors.UnknownSmartMethod, err:
1643
return self._real_branch.get_stacked_on_url()
1644
if response[0] != 'ok':
1645
raise errors.UnexpectedSmartServerResponse(response)
1648
def lock_read(self):
1649
self.repository.lock_read()
1650
if not self._lock_mode:
1651
self._lock_mode = 'r'
1652
self._lock_count = 1
1653
if self._real_branch is not None:
1654
self._real_branch.lock_read()
1656
self._lock_count += 1
1658
def _remote_lock_write(self, token):
1660
branch_token = repo_token = ''
1662
branch_token = token
1663
repo_token = self.repository.lock_write()
1664
self.repository.unlock()
1665
err_context = {'token': token}
1666
response = self._call(
1667
'Branch.lock_write', self._remote_path(), branch_token,
1668
repo_token or '', **err_context)
1669
if response[0] != 'ok':
1670
raise errors.UnexpectedSmartServerResponse(response)
1671
ok, branch_token, repo_token = response
1672
return branch_token, repo_token
1674
def lock_write(self, token=None):
1675
if not self._lock_mode:
1676
# Lock the branch and repo in one remote call.
1677
remote_tokens = self._remote_lock_write(token)
1678
self._lock_token, self._repo_lock_token = remote_tokens
1679
if not self._lock_token:
1680
raise SmartProtocolError('Remote server did not return a token!')
1681
# Tell the self.repository object that it is locked.
1682
self.repository.lock_write(
1683
self._repo_lock_token, _skip_rpc=True)
1685
if self._real_branch is not None:
1686
self._real_branch.lock_write(token=self._lock_token)
1687
if token is not None:
1688
self._leave_lock = True
1690
self._leave_lock = False
1691
self._lock_mode = 'w'
1692
self._lock_count = 1
1693
elif self._lock_mode == 'r':
1694
raise errors.ReadOnlyTransaction
1696
if token is not None:
1697
# A token was given to lock_write, and we're relocking, so
1698
# check that the given token actually matches the one we
1700
if token != self._lock_token:
1701
raise errors.TokenMismatch(token, self._lock_token)
1702
self._lock_count += 1
1703
# Re-lock the repository too.
1704
self.repository.lock_write(self._repo_lock_token)
1705
return self._lock_token or None
1707
def _unlock(self, branch_token, repo_token):
1708
err_context = {'token': str((branch_token, repo_token))}
1709
response = self._call(
1710
'Branch.unlock', self._remote_path(), branch_token,
1711
repo_token or '', **err_context)
1712
if response == ('ok',):
1714
raise errors.UnexpectedSmartServerResponse(response)
1718
self._lock_count -= 1
1719
if not self._lock_count:
1720
self._clear_cached_state()
1721
mode = self._lock_mode
1722
self._lock_mode = None
1723
if self._real_branch is not None:
1724
if (not self._leave_lock and mode == 'w' and
1725
self._repo_lock_token):
1726
# If this RemoteBranch will remove the physical lock
1727
# for the repository, make sure the _real_branch
1728
# doesn't do it first. (Because the _real_branch's
1729
# repository is set to be the RemoteRepository.)
1730
self._real_branch.repository.leave_lock_in_place()
1731
self._real_branch.unlock()
1733
# Only write-locked branched need to make a remote method
1734
# call to perform the unlock.
1736
if not self._lock_token:
1737
raise AssertionError('Locked, but no token!')
1738
branch_token = self._lock_token
1739
repo_token = self._repo_lock_token
1740
self._lock_token = None
1741
self._repo_lock_token = None
1742
if not self._leave_lock:
1743
self._unlock(branch_token, repo_token)
1745
self.repository.unlock()
1747
def break_lock(self):
1749
return self._real_branch.break_lock()
1751
def leave_lock_in_place(self):
1752
if not self._lock_token:
1753
raise NotImplementedError(self.leave_lock_in_place)
1754
self._leave_lock = True
1756
def dont_leave_lock_in_place(self):
1757
if not self._lock_token:
1758
raise NotImplementedError(self.dont_leave_lock_in_place)
1759
self._leave_lock = False
1761
def _last_revision_info(self):
1762
response = self._call('Branch.last_revision_info', self._remote_path())
1763
if response[0] != 'ok':
1764
raise SmartProtocolError('unexpected response code %s' % (response,))
1765
revno = int(response[1])
1766
last_revision = response[2]
1767
return (revno, last_revision)
1769
def _gen_revision_history(self):
1770
"""See Branch._gen_revision_history()."""
1771
response_tuple, response_handler = self._call_expecting_body(
1772
'Branch.revision_history', self._remote_path())
1773
if response_tuple[0] != 'ok':
1774
raise errors.UnexpectedSmartServerResponse(response_tuple)
1775
result = response_handler.read_body_bytes().split('\x00')
1780
def _remote_path(self):
1781
return self.bzrdir._path_for_remote_call(self._client)
1783
def _set_last_revision_descendant(self, revision_id, other_branch,
1784
allow_diverged=False, allow_overwrite_descendant=False):
1785
# This performs additional work to meet the hook contract; while its
1786
# undesirable, we have to synthesise the revno to call the hook, and
1787
# not calling the hook is worse as it means changes can't be prevented.
1788
# Having calculated this though, we can't just call into
1789
# set_last_revision_info as a simple call, because there is a set_rh
1790
# hook that some folk may still be using.
1791
old_revno, old_revid = self.last_revision_info()
1792
history = self._lefthand_history(revision_id)
1793
self._run_pre_change_branch_tip_hooks(len(history), revision_id)
1794
err_context = {'other_branch': other_branch}
1795
response = self._call('Branch.set_last_revision_ex',
1796
self._remote_path(), self._lock_token, self._repo_lock_token,
1797
revision_id, int(allow_diverged), int(allow_overwrite_descendant),
1799
self._clear_cached_state()
1800
if len(response) != 3 and response[0] != 'ok':
1801
raise errors.UnexpectedSmartServerResponse(response)
1802
new_revno, new_revision_id = response[1:]
1803
self._last_revision_info_cache = new_revno, new_revision_id
1804
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1805
if self._real_branch is not None:
1806
cache = new_revno, new_revision_id
1807
self._real_branch._last_revision_info_cache = cache
1809
def _set_last_revision(self, revision_id):
1810
old_revno, old_revid = self.last_revision_info()
1811
# This performs additional work to meet the hook contract; while its
1812
# undesirable, we have to synthesise the revno to call the hook, and
1813
# not calling the hook is worse as it means changes can't be prevented.
1814
# Having calculated this though, we can't just call into
1815
# set_last_revision_info as a simple call, because there is a set_rh
1816
# hook that some folk may still be using.
1817
history = self._lefthand_history(revision_id)
1818
self._run_pre_change_branch_tip_hooks(len(history), revision_id)
1819
self._clear_cached_state()
1820
response = self._call('Branch.set_last_revision',
1821
self._remote_path(), self._lock_token, self._repo_lock_token,
1823
if response != ('ok',):
1824
raise errors.UnexpectedSmartServerResponse(response)
1825
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1828
def set_revision_history(self, rev_history):
1829
# Send just the tip revision of the history; the server will generate
1830
# the full history from that. If the revision doesn't exist in this
1831
# branch, NoSuchRevision will be raised.
1832
if rev_history == []:
1835
rev_id = rev_history[-1]
1836
self._set_last_revision(rev_id)
1837
for hook in branch.Branch.hooks['set_rh']:
1838
hook(self, rev_history)
1839
self._cache_revision_history(rev_history)
1841
def get_parent(self):
1843
return self._real_branch.get_parent()
1845
def _get_parent_location(self):
1846
# Used by tests, when checking normalisation of given vs stored paths.
1848
return self._real_branch._get_parent_location()
1850
def set_parent(self, url):
1852
return self._real_branch.set_parent(url)
1854
def _set_parent_location(self, url):
1855
# Used by tests, to poke bad urls into branch configurations
1857
self.set_parent(url)
1860
return self._real_branch._set_parent_location(url)
1862
def set_stacked_on_url(self, stacked_location):
1863
"""Set the URL this branch is stacked against.
1865
:raises UnstackableBranchFormat: If the branch does not support
1867
:raises UnstackableRepositoryFormat: If the repository does not support
1871
return self._real_branch.set_stacked_on_url(stacked_location)
1873
def sprout(self, to_bzrdir, revision_id=None):
1874
branch_format = to_bzrdir._format._branch_format
1875
if (branch_format is None or
1876
isinstance(branch_format, RemoteBranchFormat)):
1877
# The to_bzrdir specifies RemoteBranchFormat (or no format, which
1878
# implies the same thing), but RemoteBranches can't be created at
1879
# arbitrary URLs. So create a branch in the same format as
1880
# _real_branch instead.
1881
# XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
1882
# to_bzrdir.create_branch to create a RemoteBranch after all...
1884
result = self._real_branch._format.initialize(to_bzrdir)
1885
self.copy_content_into(result, revision_id=revision_id)
1886
result.set_parent(self.bzrdir.root_transport.base)
1888
result = branch.Branch.sprout(
1889
self, to_bzrdir, revision_id=revision_id)
1893
def pull(self, source, overwrite=False, stop_revision=None,
1895
self._clear_cached_state_of_remote_branch_only()
1897
return self._real_branch.pull(
1898
source, overwrite=overwrite, stop_revision=stop_revision,
1899
_override_hook_target=self, **kwargs)
1902
def push(self, target, overwrite=False, stop_revision=None):
1904
return self._real_branch.push(
1905
target, overwrite=overwrite, stop_revision=stop_revision,
1906
_override_hook_source_branch=self)
1908
def is_locked(self):
1909
return self._lock_count >= 1
1912
def revision_id_to_revno(self, revision_id):
1914
return self._real_branch.revision_id_to_revno(revision_id)
1917
def set_last_revision_info(self, revno, revision_id):
1918
# XXX: These should be returned by the set_last_revision_info verb
1919
old_revno, old_revid = self.last_revision_info()
1920
self._run_pre_change_branch_tip_hooks(revno, revision_id)
1921
revision_id = ensure_null(revision_id)
1923
response = self._call('Branch.set_last_revision_info',
1924
self._remote_path(), self._lock_token, self._repo_lock_token,
1925
str(revno), revision_id)
1926
except errors.UnknownSmartMethod:
1928
self._clear_cached_state_of_remote_branch_only()
1929
self._real_branch.set_last_revision_info(revno, revision_id)
1930
self._last_revision_info_cache = revno, revision_id
1932
if response == ('ok',):
1933
self._clear_cached_state()
1934
self._last_revision_info_cache = revno, revision_id
1935
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1936
# Update the _real_branch's cache too.
1937
if self._real_branch is not None:
1938
cache = self._last_revision_info_cache
1939
self._real_branch._last_revision_info_cache = cache
1941
raise errors.UnexpectedSmartServerResponse(response)
1944
def generate_revision_history(self, revision_id, last_rev=None,
1946
medium = self._client._medium
1947
if not medium._is_remote_before((1, 6)):
1948
# Use a smart method for 1.6 and above servers
1950
self._set_last_revision_descendant(revision_id, other_branch,
1951
allow_diverged=True, allow_overwrite_descendant=True)
1953
except errors.UnknownSmartMethod:
1954
medium._remember_remote_is_before((1, 6))
1955
self._clear_cached_state_of_remote_branch_only()
1956
self.set_revision_history(self._lefthand_history(revision_id,
1957
last_rev=last_rev,other_branch=other_branch))
1962
return self._real_branch.tags
1964
def set_push_location(self, location):
1966
return self._real_branch.set_push_location(location)
1969
def update_revisions(self, other, stop_revision=None, overwrite=False,
1971
"""See Branch.update_revisions."""
1974
if stop_revision is None:
1975
stop_revision = other.last_revision()
1976
if revision.is_null(stop_revision):
1977
# if there are no commits, we're done.
1979
self.fetch(other, stop_revision)
1982
# Just unconditionally set the new revision. We don't care if
1983
# the branches have diverged.
1984
self._set_last_revision(stop_revision)
1986
medium = self._client._medium
1987
if not medium._is_remote_before((1, 6)):
1989
self._set_last_revision_descendant(stop_revision, other)
1991
except errors.UnknownSmartMethod:
1992
medium._remember_remote_is_before((1, 6))
1993
# Fallback for pre-1.6 servers: check for divergence
1994
# client-side, then do _set_last_revision.
1995
last_rev = revision.ensure_null(self.last_revision())
1997
graph = self.repository.get_graph()
1998
if self._check_if_descendant_or_diverged(
1999
stop_revision, last_rev, graph, other):
2000
# stop_revision is a descendant of last_rev, but we aren't
2001
# overwriting, so we're done.
2003
self._set_last_revision(stop_revision)
2008
def _extract_tar(tar, to_dir):
2009
"""Extract all the contents of a tarfile object.
2011
A replacement for extractall, which is not present in python2.4
2014
tar.extract(tarinfo, to_dir)
2017
def _translate_error(err, **context):
2018
"""Translate an ErrorFromSmartServer into a more useful error.
2020
Possible context keys:
2028
If the error from the server doesn't match a known pattern, then
2029
UnknownErrorFromSmartServer is raised.
2033
return context[name]
2034
except KeyError, key_err:
2035
mutter('Missing key %r in context %r', key_err.args[0], context)
2038
"""Get the path from the context if present, otherwise use first error
2042
return context['path']
2043
except KeyError, key_err:
2045
return err.error_args[0]
2046
except IndexError, idx_err:
2048
'Missing key %r in context %r', key_err.args[0], context)
2051
if err.error_verb == 'NoSuchRevision':
2052
raise NoSuchRevision(find('branch'), err.error_args[0])
2053
elif err.error_verb == 'nosuchrevision':
2054
raise NoSuchRevision(find('repository'), err.error_args[0])
2055
elif err.error_tuple == ('nobranch',):
2056
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2057
elif err.error_verb == 'norepository':
2058
raise errors.NoRepositoryPresent(find('bzrdir'))
2059
elif err.error_verb == 'LockContention':
2060
raise errors.LockContention('(remote lock)')
2061
elif err.error_verb == 'UnlockableTransport':
2062
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2063
elif err.error_verb == 'LockFailed':
2064
raise errors.LockFailed(err.error_args[0], err.error_args[1])
2065
elif err.error_verb == 'TokenMismatch':
2066
raise errors.TokenMismatch(find('token'), '(remote token)')
2067
elif err.error_verb == 'Diverged':
2068
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2069
elif err.error_verb == 'TipChangeRejected':
2070
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2071
elif err.error_verb == 'UnstackableBranchFormat':
2072
raise errors.UnstackableBranchFormat(*err.error_args)
2073
elif err.error_verb == 'UnstackableRepositoryFormat':
2074
raise errors.UnstackableRepositoryFormat(*err.error_args)
2075
elif err.error_verb == 'NotStacked':
2076
raise errors.NotStacked(branch=find('branch'))
2077
elif err.error_verb == 'PermissionDenied':
2079
if len(err.error_args) >= 2:
2080
extra = err.error_args[1]
2083
raise errors.PermissionDenied(path, extra=extra)
2084
elif err.error_verb == 'ReadError':
2086
raise errors.ReadError(path)
2087
elif err.error_verb == 'NoSuchFile':
2089
raise errors.NoSuchFile(path)
2090
elif err.error_verb == 'FileExists':
2091
raise errors.FileExists(err.error_args[0])
2092
elif err.error_verb == 'DirectoryNotEmpty':
2093
raise errors.DirectoryNotEmpty(err.error_args[0])
2094
elif err.error_verb == 'ShortReadvError':
2095
args = err.error_args
2096
raise errors.ShortReadvError(
2097
args[0], int(args[1]), int(args[2]), int(args[3]))
2098
elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
2099
encoding = str(err.error_args[0]) # encoding must always be a string
2100
val = err.error_args[1]
2101
start = int(err.error_args[2])
2102
end = int(err.error_args[3])
2103
reason = str(err.error_args[4]) # reason must always be a string
2104
if val.startswith('u:'):
2105
val = val[2:].decode('utf-8')
2106
elif val.startswith('s:'):
2107
val = val[2:].decode('base64')
2108
if err.error_verb == 'UnicodeDecodeError':
2109
raise UnicodeDecodeError(encoding, val, start, end, reason)
2110
elif err.error_verb == 'UnicodeEncodeError':
2111
raise UnicodeEncodeError(encoding, val, start, end, reason)
2112
elif err.error_verb == 'ReadOnlyError':
2113
raise errors.TransportNotPossible('readonly transport')
2114
raise errors.UnknownErrorFromSmartServer(err)