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.
21
from cStringIO import StringIO
34
from bzrlib.branch import BranchReferenceFormat
35
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
36
from bzrlib.decorators import needs_read_lock, needs_write_lock
37
from bzrlib.errors import (
41
from bzrlib.lockable_files import LockableFiles
42
from bzrlib.smart import client, vfs
43
from bzrlib.revision import ensure_null, NULL_REVISION
44
from bzrlib.trace import mutter, note, warning
45
from bzrlib.versionedfile import VersionedFiles
48
class _RpcHelper(object):
49
"""Mixin class that helps with issuing RPCs."""
51
def _call(self, method, *args, **err_context):
53
return self._client.call(method, *args)
54
except errors.ErrorFromSmartServer, err:
55
self._translate_error(err, **err_context)
57
def _call_expecting_body(self, method, *args, **err_context):
59
return self._client.call_expecting_body(method, *args)
60
except errors.ErrorFromSmartServer, err:
61
self._translate_error(err, **err_context)
63
def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
66
return self._client.call_with_body_bytes_expecting_body(
67
method, args, body_bytes)
68
except errors.ErrorFromSmartServer, err:
69
self._translate_error(err, **err_context)
71
# Note: RemoteBzrDirFormat is in bzrdir.py
73
class RemoteBzrDir(BzrDir, _RpcHelper):
74
"""Control directory on a remote server, accessed via bzr:// or similar."""
76
def __init__(self, transport, _client=None):
77
"""Construct a RemoteBzrDir.
79
:param _client: Private parameter for testing. Disables probing and the
82
BzrDir.__init__(self, transport, RemoteBzrDirFormat())
83
# this object holds a delegated bzrdir that uses file-level operations
84
# to talk to the other side
85
self._real_bzrdir = None
88
medium = transport.get_smart_medium()
89
self._client = client._SmartClient(medium)
91
self._client = _client
94
path = self._path_for_remote_call(self._client)
95
response = self._call('BzrDir.open', path)
96
if response not in [('yes',), ('no',)]:
97
raise errors.UnexpectedSmartServerResponse(response)
98
if response == ('no',):
99
raise errors.NotBranchError(path=transport.base)
101
def _ensure_real(self):
102
"""Ensure that there is a _real_bzrdir set.
104
Used before calls to self._real_bzrdir.
106
if not self._real_bzrdir:
107
self._real_bzrdir = BzrDir.open_from_transport(
108
self.root_transport, _server_formats=False)
110
def _translate_error(self, err, **context):
111
_translate_error(err, bzrdir=self, **context)
113
def cloning_metadir(self, stacked=False):
115
return self._real_bzrdir.cloning_metadir(stacked)
117
def create_repository(self, shared=False):
119
self._real_bzrdir.create_repository(shared=shared)
120
return self.open_repository()
122
def destroy_repository(self):
123
"""See BzrDir.destroy_repository"""
125
self._real_bzrdir.destroy_repository()
127
def create_branch(self):
129
real_branch = self._real_bzrdir.create_branch()
130
return RemoteBranch(self, self.find_repository(), real_branch)
132
def destroy_branch(self):
133
"""See BzrDir.destroy_branch"""
135
self._real_bzrdir.destroy_branch()
137
def create_workingtree(self, revision_id=None, from_branch=None):
138
raise errors.NotLocalUrl(self.transport.base)
140
def find_branch_format(self):
141
"""Find the branch 'format' for this bzrdir.
143
This might be a synthetic object for e.g. RemoteBranch and SVN.
145
b = self.open_branch()
148
def get_branch_reference(self):
149
"""See BzrDir.get_branch_reference()."""
150
path = self._path_for_remote_call(self._client)
151
response = self._call('BzrDir.open_branch', path)
152
if response[0] == 'ok':
153
if response[1] == '':
154
# branch at this location.
157
# a branch reference, use the existing BranchReference logic.
160
raise errors.UnexpectedSmartServerResponse(response)
162
def _get_tree_branch(self):
163
"""See BzrDir._get_tree_branch()."""
164
return None, self.open_branch()
166
def open_branch(self, _unsupported=False):
168
raise NotImplementedError('unsupported flag support not implemented yet.')
169
reference_url = self.get_branch_reference()
170
if reference_url is None:
171
# branch at this location.
172
return RemoteBranch(self, self.find_repository())
174
# a branch reference, use the existing BranchReference logic.
175
format = BranchReferenceFormat()
176
return format.open(self, _found=True, location=reference_url)
178
def open_repository(self):
179
path = self._path_for_remote_call(self._client)
180
verb = 'BzrDir.find_repositoryV2'
182
response = self._call(verb, path)
183
except errors.UnknownSmartMethod:
184
verb = 'BzrDir.find_repository'
185
response = self._call(verb, path)
186
if response[0] != 'ok':
187
raise errors.UnexpectedSmartServerResponse(response)
188
if verb == 'BzrDir.find_repository':
189
# servers that don't support the V2 method don't support external
191
response = response + ('no', )
192
if not (len(response) == 5):
193
raise SmartProtocolError('incorrect response length %s' % (response,))
194
if response[1] == '':
195
format = RemoteRepositoryFormat()
196
format.rich_root_data = (response[2] == 'yes')
197
format.supports_tree_reference = (response[3] == 'yes')
198
# No wire format to check this yet.
199
format.supports_external_lookups = (response[4] == 'yes')
200
# Used to support creating a real format instance when needed.
201
format._creating_bzrdir = self
202
return RemoteRepository(self, format)
204
raise errors.NoRepositoryPresent(self)
206
def open_workingtree(self, recommend_upgrade=True):
208
if self._real_bzrdir.has_workingtree():
209
raise errors.NotLocalUrl(self.root_transport)
211
raise errors.NoWorkingTree(self.root_transport.base)
213
def _path_for_remote_call(self, client):
214
"""Return the path to be used for this bzrdir in a remote call."""
215
return client.remote_path_from_transport(self.root_transport)
217
def get_branch_transport(self, branch_format):
219
return self._real_bzrdir.get_branch_transport(branch_format)
221
def get_repository_transport(self, repository_format):
223
return self._real_bzrdir.get_repository_transport(repository_format)
225
def get_workingtree_transport(self, workingtree_format):
227
return self._real_bzrdir.get_workingtree_transport(workingtree_format)
229
def can_convert_format(self):
230
"""Upgrading of remote bzrdirs is not supported yet."""
233
def needs_format_conversion(self, format=None):
234
"""Upgrading of remote bzrdirs is not supported yet."""
237
def clone(self, url, revision_id=None, force_new_repo=False,
238
preserve_stacking=False):
240
return self._real_bzrdir.clone(url, revision_id=revision_id,
241
force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
243
def get_config(self):
245
return self._real_bzrdir.get_config()
248
class RemoteRepositoryFormat(repository.RepositoryFormat):
249
"""Format for repositories accessed over a _SmartClient.
251
Instances of this repository are represented by RemoteRepository
254
The RemoteRepositoryFormat is parameterized during construction
255
to reflect the capabilities of the real, remote format. Specifically
256
the attributes rich_root_data and supports_tree_reference are set
257
on a per instance basis, and are not set (and should not be) at
261
_matchingbzrdir = RemoteBzrDirFormat()
263
def initialize(self, a_bzrdir, shared=False):
264
if not isinstance(a_bzrdir, RemoteBzrDir):
265
prior_repo = self._creating_bzrdir.open_repository()
266
prior_repo._ensure_real()
267
return prior_repo._real_repository._format.initialize(
268
a_bzrdir, shared=shared)
269
return a_bzrdir.create_repository(shared=shared)
271
def open(self, a_bzrdir):
272
if not isinstance(a_bzrdir, RemoteBzrDir):
273
raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
274
return a_bzrdir.open_repository()
276
def get_format_description(self):
277
return 'bzr remote repository'
279
def __eq__(self, other):
280
return self.__class__ == other.__class__
282
def check_conversion_target(self, target_format):
283
if self.rich_root_data and not target_format.rich_root_data:
284
raise errors.BadConversionTarget(
285
'Does not support rich root data.', target_format)
286
if (self.supports_tree_reference and
287
not getattr(target_format, 'supports_tree_reference', False)):
288
raise errors.BadConversionTarget(
289
'Does not support nested trees', target_format)
292
class _UnstackedParentsProvider(object):
293
"""ParentsProvider for RemoteRepository that ignores stacking."""
295
def __init__(self, remote_repository):
296
self._remote_repository = remote_repository
298
def get_parent_map(self, revision_ids):
299
"""See RemoteRepository.get_parent_map."""
300
return self._remote_repository._get_parent_map(revision_ids)
303
class RemoteRepository(_RpcHelper):
304
"""Repository accessed over rpc.
306
For the moment most operations are performed using local transport-backed
310
def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
311
"""Create a RemoteRepository instance.
313
:param remote_bzrdir: The bzrdir hosting this repository.
314
:param format: The RemoteFormat object to use.
315
:param real_repository: If not None, a local implementation of the
316
repository logic for the repository, usually accessing the data
318
:param _client: Private testing parameter - override the smart client
319
to be used by the repository.
322
self._real_repository = real_repository
324
self._real_repository = None
325
self.bzrdir = remote_bzrdir
327
self._client = remote_bzrdir._client
329
self._client = _client
330
self._format = format
331
self._lock_mode = None
332
self._lock_token = None
334
self._leave_lock = False
335
# A cache of looked up revision parent data; reset at unlock time.
336
self._parents_map = None
337
if 'hpss' in debug.debug_flags:
338
self._requested_parents = None
340
# These depend on the actual remote format, so force them off for
341
# maximum compatibility. XXX: In future these should depend on the
342
# remote repository instance, but this is irrelevant until we perform
343
# reconcile via an RPC call.
344
self._reconcile_does_inventory_gc = False
345
self._reconcile_fixes_text_parents = False
346
self._reconcile_backsup_inventory = False
347
self.base = self.bzrdir.transport.base
348
# Additional places to query for data.
349
self._fallback_repositories = []
350
self.texts = RemoteVersionedFiles(self, 'texts')
351
self.inventories = RemoteVersionedFiles(self, 'inventories')
352
self.signatures = RemoteVersionedFiles(self, 'signatures')
353
self.revisions = RemoteVersionedFiles(self, 'revisions')
355
self.texts, self.inventories, self.signatures, self.revisions]
358
return "%s(%s)" % (self.__class__.__name__, self.base)
362
def abort_write_group(self, suppress_errors=False):
363
"""Complete a write group on the decorated repository.
365
Smart methods peform operations in a single step so this api
366
is not really applicable except as a compatibility thunk
367
for older plugins that don't use e.g. the CommitBuilder
370
:param suppress_errors: see Repository.abort_write_group.
373
return self._real_repository.abort_write_group(
374
suppress_errors=suppress_errors)
376
def commit_write_group(self):
377
"""Complete a write group on the decorated repository.
379
Smart methods peform operations in a single step so this api
380
is not really applicable except as a compatibility thunk
381
for older plugins that don't use e.g. the CommitBuilder
385
return self._real_repository.commit_write_group()
387
def _ensure_real(self):
388
"""Ensure that there is a _real_repository set.
390
Used before calls to self._real_repository.
392
if self._real_repository is None:
393
self.bzrdir._ensure_real()
394
self._set_real_repository(
395
self.bzrdir._real_bzrdir.open_repository())
397
def _translate_error(self, err, **context):
398
self.bzrdir._translate_error(err, repository=self, **context)
400
def find_text_key_references(self):
401
"""Find the text key references within the repository.
403
:return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
404
revision_ids. Each altered file-ids has the exact revision_ids that
405
altered it listed explicitly.
406
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
407
to whether they were referred to by the inventory of the
408
revision_id that they contain. The inventory texts from all present
409
revision ids are assessed to generate this report.
412
return self._real_repository.find_text_key_references()
414
def _generate_text_key_index(self):
415
"""Generate a new text key index for the repository.
417
This is an expensive function that will take considerable time to run.
419
:return: A dict mapping (file_id, revision_id) tuples to a list of
420
parents, also (file_id, revision_id) tuples.
423
return self._real_repository._generate_text_key_index()
425
@symbol_versioning.deprecated_method(symbol_versioning.one_four)
426
def get_revision_graph(self, revision_id=None):
427
"""See Repository.get_revision_graph()."""
428
return self._get_revision_graph(revision_id)
430
def _get_revision_graph(self, revision_id):
431
"""Private method for using with old (< 1.2) servers to fallback."""
432
if revision_id is None:
434
elif revision.is_null(revision_id):
437
path = self.bzrdir._path_for_remote_call(self._client)
438
response = self._call_expecting_body(
439
'Repository.get_revision_graph', path, revision_id)
440
response_tuple, response_handler = response
441
if response_tuple[0] != 'ok':
442
raise errors.UnexpectedSmartServerResponse(response_tuple)
443
coded = response_handler.read_body_bytes()
445
# no revisions in this repository!
447
lines = coded.split('\n')
450
d = tuple(line.split())
451
revision_graph[d[0]] = d[1:]
453
return revision_graph
455
def has_revision(self, revision_id):
456
"""See Repository.has_revision()."""
457
if revision_id == NULL_REVISION:
458
# The null revision is always present.
460
path = self.bzrdir._path_for_remote_call(self._client)
461
response = self._call('Repository.has_revision', path, revision_id)
462
if response[0] not in ('yes', 'no'):
463
raise errors.UnexpectedSmartServerResponse(response)
464
if response[0] == 'yes':
466
for fallback_repo in self._fallback_repositories:
467
if fallback_repo.has_revision(revision_id):
471
def has_revisions(self, revision_ids):
472
"""See Repository.has_revisions()."""
473
# FIXME: This does many roundtrips, particularly when there are
474
# fallback repositories. -- mbp 20080905
476
for revision_id in revision_ids:
477
if self.has_revision(revision_id):
478
result.add(revision_id)
481
def has_same_location(self, other):
482
return (self.__class__ == other.__class__ and
483
self.bzrdir.transport.base == other.bzrdir.transport.base)
485
def get_graph(self, other_repository=None):
486
"""Return the graph for this repository format"""
487
parents_provider = self._make_parents_provider()
488
if (other_repository is not None and
489
other_repository.bzrdir.transport.base !=
490
self.bzrdir.transport.base):
491
parents_provider = graph._StackedParentsProvider(
492
[parents_provider, other_repository._make_parents_provider()])
493
return graph.Graph(parents_provider)
495
def gather_stats(self, revid=None, committers=None):
496
"""See Repository.gather_stats()."""
497
path = self.bzrdir._path_for_remote_call(self._client)
498
# revid can be None to indicate no revisions, not just NULL_REVISION
499
if revid is None or revision.is_null(revid):
503
if committers is None or not committers:
504
fmt_committers = 'no'
506
fmt_committers = 'yes'
507
response_tuple, response_handler = self._call_expecting_body(
508
'Repository.gather_stats', path, fmt_revid, fmt_committers)
509
if response_tuple[0] != 'ok':
510
raise errors.UnexpectedSmartServerResponse(response_tuple)
512
body = response_handler.read_body_bytes()
514
for line in body.split('\n'):
517
key, val_text = line.split(':')
518
if key in ('revisions', 'size', 'committers'):
519
result[key] = int(val_text)
520
elif key in ('firstrev', 'latestrev'):
521
values = val_text.split(' ')[1:]
522
result[key] = (float(values[0]), long(values[1]))
526
def find_branches(self, using=False):
527
"""See Repository.find_branches()."""
528
# should be an API call to the server.
530
return self._real_repository.find_branches(using=using)
532
def get_physical_lock_status(self):
533
"""See Repository.get_physical_lock_status()."""
534
# should be an API call to the server.
536
return self._real_repository.get_physical_lock_status()
538
def is_in_write_group(self):
539
"""Return True if there is an open write group.
541
write groups are only applicable locally for the smart server..
543
if self._real_repository:
544
return self._real_repository.is_in_write_group()
547
return self._lock_count >= 1
550
"""See Repository.is_shared()."""
551
path = self.bzrdir._path_for_remote_call(self._client)
552
response = self._call('Repository.is_shared', path)
553
if response[0] not in ('yes', 'no'):
554
raise SmartProtocolError('unexpected response code %s' % (response,))
555
return response[0] == 'yes'
557
def is_write_locked(self):
558
return self._lock_mode == 'w'
561
# wrong eventually - want a local lock cache context
562
if not self._lock_mode:
563
self._lock_mode = 'r'
565
self._parents_map = {}
566
for vf in self._vf_objs:
568
if 'hpss' in debug.debug_flags:
569
self._requested_parents = set()
570
if self._real_repository is not None:
571
self._real_repository.lock_read()
573
self._lock_count += 1
575
def _remote_lock_write(self, token):
576
path = self.bzrdir._path_for_remote_call(self._client)
579
err_context = {'token': token}
580
response = self._call('Repository.lock_write', path, token,
582
if response[0] == 'ok':
586
raise errors.UnexpectedSmartServerResponse(response)
588
def lock_write(self, token=None, _skip_rpc=False):
589
if not self._lock_mode:
591
if self._lock_token is not None:
592
if token != self._lock_token:
593
raise errors.TokenMismatch(token, self._lock_token)
594
self._lock_token = token
596
self._lock_token = self._remote_lock_write(token)
597
# if self._lock_token is None, then this is something like packs or
598
# svn where we don't get to lock the repo, or a weave style repository
599
# where we cannot lock it over the wire and attempts to do so will
601
if self._real_repository is not None:
602
self._real_repository.lock_write(token=self._lock_token)
603
if token is not None:
604
self._leave_lock = True
606
self._leave_lock = False
607
self._lock_mode = 'w'
609
self._parents_map = {}
610
for vf in self._vf_objs:
612
if 'hpss' in debug.debug_flags:
613
self._requested_parents = set()
614
elif self._lock_mode == 'r':
615
raise errors.ReadOnlyError(self)
617
self._lock_count += 1
618
return self._lock_token or None
620
def leave_lock_in_place(self):
621
if not self._lock_token:
622
raise NotImplementedError(self.leave_lock_in_place)
623
self._leave_lock = True
625
def dont_leave_lock_in_place(self):
626
if not self._lock_token:
627
raise NotImplementedError(self.dont_leave_lock_in_place)
628
self._leave_lock = False
630
def _set_real_repository(self, repository):
631
"""Set the _real_repository for this repository.
633
:param repository: The repository to fallback to for non-hpss
634
implemented operations.
636
if self._real_repository is not None:
637
raise AssertionError('_real_repository is already set')
638
if isinstance(repository, RemoteRepository):
639
raise AssertionError()
640
self._real_repository = repository
641
for fb in self._fallback_repositories:
642
self._real_repository.add_fallback_repository(fb)
643
if self._lock_mode == 'w':
644
# if we are already locked, the real repository must be able to
645
# acquire the lock with our token.
646
self._real_repository.lock_write(self._lock_token)
647
elif self._lock_mode == 'r':
648
self._real_repository.lock_read()
650
def start_write_group(self):
651
"""Start a write group on the decorated repository.
653
Smart methods peform operations in a single step so this api
654
is not really applicable except as a compatibility thunk
655
for older plugins that don't use e.g. the CommitBuilder
659
return self._real_repository.start_write_group()
661
def _unlock(self, token):
662
path = self.bzrdir._path_for_remote_call(self._client)
664
# with no token the remote repository is not persistently locked.
666
err_context = {'token': token}
667
response = self._call('Repository.unlock', path, token,
669
if response == ('ok',):
672
raise errors.UnexpectedSmartServerResponse(response)
675
self._lock_count -= 1
676
if self._lock_count > 0:
678
self._parents_map = None
679
for vf in self._vf_objs:
680
vf._parents_map = None
681
if 'hpss' in debug.debug_flags:
682
self._requested_parents = None
683
old_mode = self._lock_mode
684
self._lock_mode = None
686
# The real repository is responsible at present for raising an
687
# exception if it's in an unfinished write group. However, it
688
# normally will *not* actually remove the lock from disk - that's
689
# done by the server on receiving the Repository.unlock call.
690
# This is just to let the _real_repository stay up to date.
691
if self._real_repository is not None:
692
self._real_repository.unlock()
694
# The rpc-level lock should be released even if there was a
695
# problem releasing the vfs-based lock.
697
# Only write-locked repositories need to make a remote method
698
# call to perfom the unlock.
699
old_token = self._lock_token
700
self._lock_token = None
701
if not self._leave_lock:
702
self._unlock(old_token)
704
def break_lock(self):
705
# should hand off to the network
707
return self._real_repository.break_lock()
709
def _get_tarball(self, compression):
710
"""Return a TemporaryFile containing a repository tarball.
712
Returns None if the server does not support sending tarballs.
715
path = self.bzrdir._path_for_remote_call(self._client)
717
response, protocol = self._call_expecting_body(
718
'Repository.tarball', path, compression)
719
except errors.UnknownSmartMethod:
720
protocol.cancel_read_body()
722
if response[0] == 'ok':
723
# Extract the tarball and return it
724
t = tempfile.NamedTemporaryFile()
725
# TODO: rpc layer should read directly into it...
726
t.write(protocol.read_body_bytes())
729
raise errors.UnexpectedSmartServerResponse(response)
731
def sprout(self, to_bzrdir, revision_id=None):
732
# TODO: Option to control what format is created?
734
dest_repo = self._real_repository._format.initialize(to_bzrdir,
736
dest_repo.fetch(self, revision_id=revision_id)
739
### These methods are just thin shims to the VFS object for now.
741
def revision_tree(self, revision_id):
743
return self._real_repository.revision_tree(revision_id)
745
def get_serializer_format(self):
747
return self._real_repository.get_serializer_format()
749
def get_commit_builder(self, branch, parents, config, timestamp=None,
750
timezone=None, committer=None, revprops=None,
752
# FIXME: It ought to be possible to call this without immediately
753
# triggering _ensure_real. For now it's the easiest thing to do.
755
real_repo = self._real_repository
756
builder = real_repo.get_commit_builder(branch, parents,
757
config, timestamp=timestamp, timezone=timezone,
758
committer=committer, revprops=revprops, revision_id=revision_id)
761
def add_fallback_repository(self, repository):
762
"""Add a repository to use for looking up data not held locally.
764
:param repository: A repository.
766
# XXX: At the moment the RemoteRepository will allow fallbacks
767
# unconditionally - however, a _real_repository will usually exist,
768
# and may raise an error if it's not accommodated by the underlying
769
# format. Eventually we should check when opening the repository
770
# whether it's willing to allow them or not.
772
# We need to accumulate additional repositories here, to pass them in
774
self._fallback_repositories.append(repository)
775
# They are also seen by the fallback repository. If it doesn't exist
776
# yet they'll be added then. This implicitly copies them.
779
def add_inventory(self, revid, inv, parents):
781
return self._real_repository.add_inventory(revid, inv, parents)
783
def add_revision(self, rev_id, rev, inv=None, config=None):
785
return self._real_repository.add_revision(
786
rev_id, rev, inv=inv, config=config)
789
def get_inventory(self, revision_id):
791
return self._real_repository.get_inventory(revision_id)
793
def iter_inventories(self, revision_ids):
795
return self._real_repository.iter_inventories(revision_ids)
798
def get_revision(self, revision_id):
800
return self._real_repository.get_revision(revision_id)
802
def get_transaction(self):
804
return self._real_repository.get_transaction()
807
def clone(self, a_bzrdir, revision_id=None):
809
return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
811
def make_working_trees(self):
812
"""See Repository.make_working_trees"""
814
return self._real_repository.make_working_trees()
816
def revision_ids_to_search_result(self, result_set):
817
"""Convert a set of revision ids to a graph SearchResult."""
818
result_parents = set()
819
for parents in self.get_graph().get_parent_map(
820
result_set).itervalues():
821
result_parents.update(parents)
822
included_keys = result_set.intersection(result_parents)
823
start_keys = result_set.difference(included_keys)
824
exclude_keys = result_parents.difference(result_set)
825
result = graph.SearchResult(start_keys, exclude_keys,
826
len(result_set), result_set)
830
def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
831
"""Return the revision ids that other has that this does not.
833
These are returned in topological order.
835
revision_id: only return revision ids included by revision_id.
837
return repository.InterRepository.get(
838
other, self).search_missing_revision_ids(revision_id, find_ghosts)
840
def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
841
# Not delegated to _real_repository so that InterRepository.get has a
842
# chance to find an InterRepository specialised for RemoteRepository.
843
if self.has_same_location(source):
844
# check that last_revision is in 'from' and then return a
846
if (revision_id is not None and
847
not revision.is_null(revision_id)):
848
self.get_revision(revision_id)
850
inter = repository.InterRepository.get(source, self)
852
return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
853
except NotImplementedError:
854
raise errors.IncompatibleRepositories(source, self)
856
def create_bundle(self, target, base, fileobj, format=None):
858
self._real_repository.create_bundle(target, base, fileobj, format)
861
def get_ancestry(self, revision_id, topo_sorted=True):
863
return self._real_repository.get_ancestry(revision_id, topo_sorted)
865
def fileids_altered_by_revision_ids(self, revision_ids):
867
return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
869
def _get_versioned_file_checker(self, revisions, revision_versions_cache):
871
return self._real_repository._get_versioned_file_checker(
872
revisions, revision_versions_cache)
874
def iter_files_bytes(self, desired_files):
875
"""See Repository.iter_file_bytes.
878
return self._real_repository.iter_files_bytes(desired_files)
881
def _fetch_order(self):
882
"""Decorate the real repository for now.
884
In the long term getting this back from the remote repository as part
885
of open would be more efficient.
888
return self._real_repository._fetch_order
891
def _fetch_uses_deltas(self):
892
"""Decorate the real repository for now.
894
In the long term getting this back from the remote repository as part
895
of open would be more efficient.
898
return self._real_repository._fetch_uses_deltas
901
def _fetch_reconcile(self):
902
"""Decorate the real repository for now.
904
In the long term getting this back from the remote repository as part
905
of open would be more efficient.
908
return self._real_repository._fetch_reconcile
910
def get_parent_map(self, revision_ids):
911
"""See bzrlib.Graph.get_parent_map()."""
912
return self._make_parents_provider().get_parent_map(revision_ids)
914
def _get_parent_map(self, keys):
915
"""Implementation of get_parent_map() that ignores fallbacks."""
916
# Hack to build up the caching logic.
917
ancestry = self._parents_map
919
# Repository is not locked, so there's no cache.
920
missing_revisions = set(keys)
923
missing_revisions = set(key for key in keys if key not in ancestry)
924
if missing_revisions:
925
parent_map = self._get_parent_map_rpc(missing_revisions)
926
if 'hpss' in debug.debug_flags:
927
mutter('retransmitted revisions: %d of %d',
928
len(set(ancestry).intersection(parent_map)),
930
ancestry.update(parent_map)
931
present_keys = [k for k in keys if k in ancestry]
932
if 'hpss' in debug.debug_flags:
933
if self._requested_parents is not None and len(ancestry) != 0:
934
self._requested_parents.update(present_keys)
935
mutter('Current RemoteRepository graph hit rate: %d%%',
936
100.0 * len(self._requested_parents) / len(ancestry))
937
return dict((k, ancestry[k]) for k in present_keys)
939
def _get_parent_map_rpc(self, keys):
940
"""Helper for get_parent_map that performs the RPC."""
941
medium = self._client._medium
942
if medium._is_remote_before((1, 2)):
943
# We already found out that the server can't understand
944
# Repository.get_parent_map requests, so just fetch the whole
946
# XXX: Note that this will issue a deprecation warning. This is ok
947
# :- its because we're working with a deprecated server anyway, and
948
# the user will almost certainly have seen a warning about the
949
# server version already.
950
rg = self.get_revision_graph()
951
# There is an api discrepency between get_parent_map and
952
# get_revision_graph. Specifically, a "key:()" pair in
953
# get_revision_graph just means a node has no parents. For
954
# "get_parent_map" it means the node is a ghost. So fix up the
955
# graph to correct this.
956
# https://bugs.launchpad.net/bzr/+bug/214894
957
# There is one other "bug" which is that ghosts in
958
# get_revision_graph() are not returned at all. But we won't worry
959
# about that for now.
960
for node_id, parent_ids in rg.iteritems():
962
rg[node_id] = (NULL_REVISION,)
963
rg[NULL_REVISION] = ()
968
raise ValueError('get_parent_map(None) is not valid')
969
if NULL_REVISION in keys:
970
keys.discard(NULL_REVISION)
971
found_parents = {NULL_REVISION:()}
976
# TODO(Needs analysis): We could assume that the keys being requested
977
# from get_parent_map are in a breadth first search, so typically they
978
# will all be depth N from some common parent, and we don't have to
979
# have the server iterate from the root parent, but rather from the
980
# keys we're searching; and just tell the server the keyspace we
981
# already have; but this may be more traffic again.
983
# Transform self._parents_map into a search request recipe.
984
# TODO: Manage this incrementally to avoid covering the same path
985
# repeatedly. (The server will have to on each request, but the less
986
# work done the better).
987
parents_map = self._parents_map
988
if parents_map is None:
989
# Repository is not locked, so there's no cache.
991
start_set = set(parents_map)
992
result_parents = set()
993
for parents in parents_map.itervalues():
994
result_parents.update(parents)
995
stop_keys = result_parents.difference(start_set)
996
included_keys = start_set.intersection(result_parents)
997
start_set.difference_update(included_keys)
998
recipe = (start_set, stop_keys, len(parents_map))
999
path = self.bzrdir._path_for_remote_call(self._client)
1001
if type(key) is not str:
1003
"key %r not a plain string" % (key,))
1004
verb = 'Repository.get_parent_map'
1005
args = (path,) + tuple(keys)
1007
response = self._call_with_body_bytes_expecting_body(
1008
verb, args, _serialise_search_recipe(recipe))
1009
except errors.UnknownSmartMethod:
1010
# Server does not support this method, so get the whole graph.
1011
# Worse, we have to force a disconnection, because the server now
1012
# doesn't realise it has a body on the wire to consume, so the
1013
# only way to recover is to abandon the connection.
1015
'Server is too old for fast get_parent_map, reconnecting. '
1016
'(Upgrade the server to Bazaar 1.2 to avoid this)')
1018
# To avoid having to disconnect repeatedly, we keep track of the
1019
# fact the server doesn't understand remote methods added in 1.2.
1020
medium._remember_remote_is_before((1, 2))
1021
return self.get_revision_graph(None)
1022
response_tuple, response_handler = response
1023
if response_tuple[0] not in ['ok']:
1024
response_handler.cancel_read_body()
1025
raise errors.UnexpectedSmartServerResponse(response_tuple)
1026
if response_tuple[0] == 'ok':
1027
coded = bz2.decompress(response_handler.read_body_bytes())
1029
# no revisions found
1031
lines = coded.split('\n')
1034
d = tuple(line.split())
1036
revision_graph[d[0]] = d[1:]
1038
# No parents - so give the Graph result (NULL_REVISION,).
1039
revision_graph[d[0]] = (NULL_REVISION,)
1040
return revision_graph
1043
def get_signature_text(self, revision_id):
1045
return self._real_repository.get_signature_text(revision_id)
1048
@symbol_versioning.deprecated_method(symbol_versioning.one_three)
1049
def get_revision_graph_with_ghosts(self, revision_ids=None):
1051
return self._real_repository.get_revision_graph_with_ghosts(
1052
revision_ids=revision_ids)
1055
def get_inventory_xml(self, revision_id):
1057
return self._real_repository.get_inventory_xml(revision_id)
1059
def deserialise_inventory(self, revision_id, xml):
1061
return self._real_repository.deserialise_inventory(revision_id, xml)
1063
def reconcile(self, other=None, thorough=False):
1065
return self._real_repository.reconcile(other=other, thorough=thorough)
1067
def all_revision_ids(self):
1069
return self._real_repository.all_revision_ids()
1072
def get_deltas_for_revisions(self, revisions):
1074
return self._real_repository.get_deltas_for_revisions(revisions)
1077
def get_revision_delta(self, revision_id):
1079
return self._real_repository.get_revision_delta(revision_id)
1082
def revision_trees(self, revision_ids):
1084
return self._real_repository.revision_trees(revision_ids)
1087
def get_revision_reconcile(self, revision_id):
1089
return self._real_repository.get_revision_reconcile(revision_id)
1092
def check(self, revision_ids=None):
1094
return self._real_repository.check(revision_ids=revision_ids)
1096
def copy_content_into(self, destination, revision_id=None):
1098
return self._real_repository.copy_content_into(
1099
destination, revision_id=revision_id)
1101
def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
1102
# get a tarball of the remote repository, and copy from that into the
1104
from bzrlib import osutils
1106
# TODO: Maybe a progress bar while streaming the tarball?
1107
note("Copying repository content as tarball...")
1108
tar_file = self._get_tarball('bz2')
1109
if tar_file is None:
1111
destination = to_bzrdir.create_repository()
1113
tar = tarfile.open('repository', fileobj=tar_file,
1115
tmpdir = osutils.mkdtemp()
1117
_extract_tar(tar, tmpdir)
1118
tmp_bzrdir = BzrDir.open(tmpdir)
1119
tmp_repo = tmp_bzrdir.open_repository()
1120
tmp_repo.copy_content_into(destination, revision_id)
1122
osutils.rmtree(tmpdir)
1126
# TODO: Suggestion from john: using external tar is much faster than
1127
# python's tarfile library, but it may not work on windows.
1131
"""Compress the data within the repository.
1133
This is not currently implemented within the smart server.
1136
return self._real_repository.pack()
1138
def set_make_working_trees(self, new_value):
1140
self._real_repository.set_make_working_trees(new_value)
1143
def sign_revision(self, revision_id, gpg_strategy):
1145
return self._real_repository.sign_revision(revision_id, gpg_strategy)
1148
def get_revisions(self, revision_ids):
1150
return self._real_repository.get_revisions(revision_ids)
1152
def supports_rich_root(self):
1154
return self._real_repository.supports_rich_root()
1156
def iter_reverse_revision_history(self, revision_id):
1158
return self._real_repository.iter_reverse_revision_history(revision_id)
1161
def _serializer(self):
1163
return self._real_repository._serializer
1165
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1167
return self._real_repository.store_revision_signature(
1168
gpg_strategy, plaintext, revision_id)
1170
def add_signature_text(self, revision_id, signature):
1172
return self._real_repository.add_signature_text(revision_id, signature)
1174
def has_signature_for_revision_id(self, revision_id):
1176
return self._real_repository.has_signature_for_revision_id(revision_id)
1178
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1180
return self._real_repository.item_keys_introduced_by(revision_ids,
1181
_files_pb=_files_pb)
1183
def revision_graph_can_have_wrong_parents(self):
1184
# The answer depends on the remote repo format.
1186
return self._real_repository.revision_graph_can_have_wrong_parents()
1188
def _find_inconsistent_revision_parents(self):
1190
return self._real_repository._find_inconsistent_revision_parents()
1192
def _check_for_inconsistent_revision_parents(self):
1194
return self._real_repository._check_for_inconsistent_revision_parents()
1196
def _make_parents_provider(self):
1197
providers = [_UnstackedParentsProvider(self)]
1198
providers.extend(r._make_parents_provider() for r in
1199
self._fallback_repositories)
1200
return graph._StackedParentsProvider(providers)
1202
def _serialise_search_recipe(recipe):
1203
"""Serialise a graph search recipe.
1205
:param recipe: A search recipe (start, stop, count).
1206
:return: Serialised bytes.
1208
start_keys = ' '.join(recipe[0])
1209
stop_keys = ' '.join(recipe[1])
1210
count = str(recipe[2])
1211
return '\n'.join((start_keys, stop_keys, count))
1213
def _serialise_search_tuple_key_recipe((start, stop, count)):
1214
"""Serialise a graph search recipe.
1216
:param recipe: A search recipe (start, stop, count).
1217
:return: Serialised bytes.
1220
# recipe = keys NEWLINE keys NEWLINE count
1222
# keys = key 0x00 key 0x00 key ...
1223
# key = key_elem SPACE key_elem SPACE key_elem ...
1225
start_keys = (' '.join(key) for key in start)
1226
buf.write('\0'.join(start_keys))
1228
stop_keys = (' '.join(key) for key in stop)
1229
buf.write('\0'.join(stop_keys))
1231
buf.write(str(count))
1232
return buf.getvalue()
1235
def _deserialise_search_result(bytes):
1237
for line in bytes.split('\n'):
1238
keys_bytes = line.split('\0')
1239
keys = [tuple(key_bytes.split(' ')) for key_bytes in keys_bytes]
1240
parents = tuple(keys[1:])
1241
if parents == (('',),):
1243
graph_dict[keys[0]] = parents
1247
class RemoteVersionedFiles(VersionedFiles):
1249
def __init__(self, remote_repo, vf_name):
1250
self.remote_repo = remote_repo
1251
self.vf_name = vf_name
1252
self._parents_map = None
1253
if 'hpss' in debug.debug_flags:
1254
self._requested_parents = None
1256
def _get_real_vf(self):
1257
self.remote_repo._ensure_real()
1258
return getattr(self.remote_repo._real_repository, self.vf_name)
1260
def add_lines(self, version_id, parents, lines, parent_texts=None,
1261
left_matching_blocks=None, nostore_sha=None, random_id=False,
1262
check_content=True):
1263
real_vf = self._get_real_vf()
1264
return real_vf.add_lines(version_id, parents, lines,
1265
parent_texts=parent_texts,
1266
left_matching_blocks=left_matching_blocks,
1267
nostore_sha=nostore_sha, random_id=random_id,
1268
check_content=check_content)
1270
def add_mpdiffs(self, records):
1271
real_vf = self._get_real_vf()
1272
return real_vf.add_mpdiffs(records)
1274
def annotate(self, key):
1275
real_vf = self._get_real_vf()
1276
return real_vf.annotate(key)
1278
def check(self, progress_bar=None):
1279
real_vf = self._get_real_vf()
1280
return real_vf.check(progress_bar=progress_bar)
1282
def get_parent_map(self, keys):
1283
# XXX: lots of duplication with RemoteRepository.get_parent_map.
1284
client = self.remote_repo._client
1285
medium = client._medium
1286
if medium._is_remote_before((1, 8)):
1287
real_vf = self._get_real_vf()
1288
return real_vf.get_parent_map(keys)
1289
# Hack to build up the caching logic.
1290
ancestry = self._parents_map
1291
if ancestry is None:
1292
# Repository is not locked, so there's no cache.
1293
missing_revisions = set(keys)
1296
missing_revisions = set(key for key in keys if key not in ancestry)
1297
if missing_revisions:
1299
parent_map = self._get_parent_map(missing_revisions)
1300
except errors.UnknownSmartMethod:
1301
medium._remember_remote_is_before((1, 8))
1302
real_vf = self._get_real_vf()
1303
return real_vf.get_parent_map(keys)
1304
if 'hpss' in debug.debug_flags:
1305
mutter('retransmitted revisions: %d of %d',
1306
len(set(ancestry).intersection(parent_map)),
1308
ancestry.update(parent_map)
1309
present_keys = [k for k in keys if k in ancestry]
1310
if 'hpss' in debug.debug_flags:
1311
if self._requested_parents is not None and len(ancestry) != 0:
1312
self._requested_parents.update(present_keys)
1313
mutter('Current RemoteRepository graph hit rate: %d%%',
1314
100.0 * len(self._requested_parents) / len(ancestry))
1315
return dict((k, ancestry[k]) for k in present_keys)
1317
def _get_parent_map(self, keys):
1318
# XXX: lots of duplication with RemoteRepository._get_parent_map.
1320
raise ValueError('get_parent_map(None) is not valid')
1321
if NULL_REVISION in keys:
1322
keys.discard(NULL_REVISION)
1323
found_parents = {NULL_REVISION:()}
1325
return found_parents
1328
# TODO(Needs analysis): We could assume that the keys being requested
1329
# from get_parent_map are in a breadth first search, so typically they
1330
# will all be depth N from some common parent, and we don't have to
1331
# have the server iterate from the root parent, but rather from the
1332
# keys we're searching; and just tell the server the keyspace we
1333
# already have; but this may be more traffic again.
1335
# Transform self._parents_map into a search request recipe.
1336
# TODO: Manage this incrementally to avoid covering the same path
1337
# repeatedly. (The server will have to on each request, but the less
1338
# work done the better).
1339
parents_map = self._parents_map
1340
if parents_map is None:
1341
# Repository is not locked, so there's no cache.
1343
start_set = set(parents_map)
1344
result_parents = set()
1345
for parents in parents_map.itervalues():
1346
result_parents.update(parents)
1347
stop_keys = result_parents.difference(start_set)
1348
included_keys = start_set.intersection(result_parents)
1349
start_set.difference_update(included_keys)
1350
recipe = (start_set, stop_keys, len(parents_map))
1351
client = self.remote_repo._client
1352
path = self.remote_repo.bzrdir._path_for_remote_call(client)
1354
if type(key) is not tuple:
1356
"key %r not a tuple of strs" % (key,))
1357
for key_elem in key:
1358
if type(key_elem) is not str:
1360
"key %r not a tuple of strs" % (key,))
1361
verb = 'VersionedFiles.get_parent_map'
1362
args = (path, self.vf_name) + tuple(keys)
1363
response = client.call_with_body_bytes_expecting_body(
1364
verb, args, _serialise_search_tuple_key_recipe(recipe))
1365
response_tuple, response_handler = response
1366
if response_tuple[0] not in ['ok']:
1367
response_handler.cancel_read_body()
1368
raise errors.UnexpectedSmartServerResponse(response_tuple)
1369
if response_tuple[0] == 'ok':
1370
coded = bz2.decompress(response_handler.read_body_bytes())
1371
revision_graph = _deserialise_search_result(coded)
1372
# The server doesn't return explicit results for keys it doesn't
1373
# have, so add empty entries for the missing keys to the result
1375
missing = keys.difference(revision_graph)
1376
for missing_key in missing:
1377
revision_graph[missing_key] = ()
1378
return revision_graph
1380
def get_record_stream(self, keys, ordering, include_delta_closure):
1381
real_vf = self._get_real_vf()
1382
return real_vf.get_record_stream(keys, ordering, include_delta_closure)
1384
def get_sha1s(self, keys):
1385
real_vf = self._get_real_vf()
1386
return real_vf.get_sha1s(keys)
1388
def insert_record_stream(self, stream):
1389
real_vf = self._get_real_vf()
1390
return real_vf.insert_record_stream(stream)
1392
def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1393
real_vf = self._get_real_vf()
1394
return real_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
1397
real_vf = self._get_real_vf()
1398
return real_vf.keys()
1400
def make_mpdiffs(self, keys):
1401
real_vf = self._get_real_vf()
1402
return real_vf.make_mpdiffs(keys)
1405
path = self.bzrdir._path_for_remote_call(self._client)
1407
response = self._call('PackRepository.autopack', path)
1408
except errors.UnknownSmartMethod:
1410
self._real_repository._pack_collection.autopack()
1412
if self._real_repository is not None:
1413
# Reset the real repository's cache of pack names.
1414
# XXX: At some point we may be able to skip this and just rely on
1415
# the automatic retry logic to do the right thing, but for now we
1416
# err on the side of being correct rather than being optimal.
1417
self._real_repository._pack_collection.reload_pack_names()
1418
if response[0] != 'ok':
1419
raise errors.UnexpectedSmartServerResponse(response)
1422
class RemoteBranchLockableFiles(LockableFiles):
1423
"""A 'LockableFiles' implementation that talks to a smart server.
1425
This is not a public interface class.
1428
def __init__(self, bzrdir, _client):
1429
self.bzrdir = bzrdir
1430
self._client = _client
1431
self._need_find_modes = True
1432
LockableFiles.__init__(
1433
self, bzrdir.get_branch_transport(None),
1434
'lock', lockdir.LockDir)
1436
def _find_modes(self):
1437
# RemoteBranches don't let the client set the mode of control files.
1438
self._dir_mode = None
1439
self._file_mode = None
1442
class RemoteBranchFormat(branch.BranchFormat):
1444
def __eq__(self, other):
1445
return (isinstance(other, RemoteBranchFormat) and
1446
self.__dict__ == other.__dict__)
1448
def get_format_description(self):
1449
return 'Remote BZR Branch'
1451
def get_format_string(self):
1452
return 'Remote BZR Branch'
1454
def open(self, a_bzrdir):
1455
return a_bzrdir.open_branch()
1457
def initialize(self, a_bzrdir):
1458
return a_bzrdir.create_branch()
1460
def supports_tags(self):
1461
# Remote branches might support tags, but we won't know until we
1462
# access the real remote branch.
1466
class RemoteBranch(branch.Branch, _RpcHelper):
1467
"""Branch stored on a server accessed by HPSS RPC.
1469
At the moment most operations are mapped down to simple file operations.
1472
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
1474
"""Create a RemoteBranch instance.
1476
:param real_branch: An optional local implementation of the branch
1477
format, usually accessing the data via the VFS.
1478
:param _client: Private parameter for testing.
1480
# We intentionally don't call the parent class's __init__, because it
1481
# will try to assign to self.tags, which is a property in this subclass.
1482
# And the parent's __init__ doesn't do much anyway.
1483
self._revision_id_to_revno_cache = None
1484
self._revision_history_cache = None
1485
self._last_revision_info_cache = None
1486
self.bzrdir = remote_bzrdir
1487
if _client is not None:
1488
self._client = _client
1490
self._client = remote_bzrdir._client
1491
self.repository = remote_repository
1492
if real_branch is not None:
1493
self._real_branch = real_branch
1494
# Give the remote repository the matching real repo.
1495
real_repo = self._real_branch.repository
1496
if isinstance(real_repo, RemoteRepository):
1497
real_repo._ensure_real()
1498
real_repo = real_repo._real_repository
1499
self.repository._set_real_repository(real_repo)
1500
# Give the branch the remote repository to let fast-pathing happen.
1501
self._real_branch.repository = self.repository
1503
self._real_branch = None
1504
# Fill out expected attributes of branch for bzrlib api users.
1505
self._format = RemoteBranchFormat()
1506
self.base = self.bzrdir.root_transport.base
1507
self._control_files = None
1508
self._lock_mode = None
1509
self._lock_token = None
1510
self._repo_lock_token = None
1511
self._lock_count = 0
1512
self._leave_lock = False
1513
# The base class init is not called, so we duplicate this:
1514
hooks = branch.Branch.hooks['open']
1517
self._setup_stacking()
1519
def _setup_stacking(self):
1520
# configure stacking into the remote repository, by reading it from
1523
fallback_url = self.get_stacked_on_url()
1524
except (errors.NotStacked, errors.UnstackableBranchFormat,
1525
errors.UnstackableRepositoryFormat), e:
1527
# it's relative to this branch...
1528
fallback_url = urlutils.join(self.base, fallback_url)
1529
transports = [self.bzrdir.root_transport]
1530
if self._real_branch is not None:
1531
transports.append(self._real_branch._transport)
1532
stacked_on = branch.Branch.open(fallback_url,
1533
possible_transports=transports)
1534
self.repository.add_fallback_repository(stacked_on.repository)
1536
def _get_real_transport(self):
1537
# if we try vfs access, return the real branch's vfs transport
1539
return self._real_branch._transport
1541
_transport = property(_get_real_transport)
1544
return "%s(%s)" % (self.__class__.__name__, self.base)
1548
def _ensure_real(self):
1549
"""Ensure that there is a _real_branch set.
1551
Used before calls to self._real_branch.
1553
if self._real_branch is None:
1554
if not vfs.vfs_enabled():
1555
raise AssertionError('smart server vfs must be enabled '
1556
'to use vfs implementation')
1557
self.bzrdir._ensure_real()
1558
self._real_branch = self.bzrdir._real_bzrdir.open_branch()
1559
if self.repository._real_repository is None:
1560
# Give the remote repository the matching real repo.
1561
real_repo = self._real_branch.repository
1562
if isinstance(real_repo, RemoteRepository):
1563
real_repo._ensure_real()
1564
real_repo = real_repo._real_repository
1565
self.repository._set_real_repository(real_repo)
1566
# Give the real branch the remote repository to let fast-pathing
1568
self._real_branch.repository = self.repository
1569
if self._lock_mode == 'r':
1570
self._real_branch.lock_read()
1571
elif self._lock_mode == 'w':
1572
self._real_branch.lock_write(token=self._lock_token)
1574
def _translate_error(self, err, **context):
1575
self.repository._translate_error(err, branch=self, **context)
1577
def _clear_cached_state(self):
1578
super(RemoteBranch, self)._clear_cached_state()
1579
if self._real_branch is not None:
1580
self._real_branch._clear_cached_state()
1582
def _clear_cached_state_of_remote_branch_only(self):
1583
"""Like _clear_cached_state, but doesn't clear the cache of
1586
This is useful when falling back to calling a method of
1587
self._real_branch that changes state. In that case the underlying
1588
branch changes, so we need to invalidate this RemoteBranch's cache of
1589
it. However, there's no need to invalidate the _real_branch's cache
1590
too, in fact doing so might harm performance.
1592
super(RemoteBranch, self)._clear_cached_state()
1595
def control_files(self):
1596
# Defer actually creating RemoteBranchLockableFiles until its needed,
1597
# because it triggers an _ensure_real that we otherwise might not need.
1598
if self._control_files is None:
1599
self._control_files = RemoteBranchLockableFiles(
1600
self.bzrdir, self._client)
1601
return self._control_files
1603
def _get_checkout_format(self):
1605
return self._real_branch._get_checkout_format()
1607
def get_physical_lock_status(self):
1608
"""See Branch.get_physical_lock_status()."""
1609
# should be an API call to the server, as branches must be lockable.
1611
return self._real_branch.get_physical_lock_status()
1613
def get_stacked_on_url(self):
1614
"""Get the URL this branch is stacked against.
1616
:raises NotStacked: If the branch is not stacked.
1617
:raises UnstackableBranchFormat: If the branch does not support
1619
:raises UnstackableRepositoryFormat: If the repository does not support
1623
# there may not be a repository yet, so we can't use
1624
# self._translate_error, so we can't use self._call either.
1625
response = self._client.call('Branch.get_stacked_on_url',
1626
self._remote_path())
1627
except errors.ErrorFromSmartServer, err:
1628
# there may not be a repository yet, so we can't call through
1629
# its _translate_error
1630
_translate_error(err, branch=self)
1631
except errors.UnknownSmartMethod, err:
1633
return self._real_branch.get_stacked_on_url()
1634
if response[0] != 'ok':
1635
raise errors.UnexpectedSmartServerResponse(response)
1638
def lock_read(self):
1639
self.repository.lock_read()
1640
if not self._lock_mode:
1641
self._lock_mode = 'r'
1642
self._lock_count = 1
1643
if self._real_branch is not None:
1644
self._real_branch.lock_read()
1646
self._lock_count += 1
1648
def _remote_lock_write(self, token):
1650
branch_token = repo_token = ''
1652
branch_token = token
1653
repo_token = self.repository.lock_write()
1654
self.repository.unlock()
1655
err_context = {'token': token}
1656
response = self._call(
1657
'Branch.lock_write', self._remote_path(), branch_token,
1658
repo_token or '', **err_context)
1659
if response[0] != 'ok':
1660
raise errors.UnexpectedSmartServerResponse(response)
1661
ok, branch_token, repo_token = response
1662
return branch_token, repo_token
1664
def lock_write(self, token=None):
1665
if not self._lock_mode:
1666
# Lock the branch and repo in one remote call.
1667
remote_tokens = self._remote_lock_write(token)
1668
self._lock_token, self._repo_lock_token = remote_tokens
1669
if not self._lock_token:
1670
raise SmartProtocolError('Remote server did not return a token!')
1671
# Tell the self.repository object that it is locked.
1672
self.repository.lock_write(
1673
self._repo_lock_token, _skip_rpc=True)
1675
if self._real_branch is not None:
1676
self._real_branch.lock_write(token=self._lock_token)
1677
if token is not None:
1678
self._leave_lock = True
1680
self._leave_lock = False
1681
self._lock_mode = 'w'
1682
self._lock_count = 1
1683
elif self._lock_mode == 'r':
1684
raise errors.ReadOnlyTransaction
1686
if token is not None:
1687
# A token was given to lock_write, and we're relocking, so
1688
# check that the given token actually matches the one we
1690
if token != self._lock_token:
1691
raise errors.TokenMismatch(token, self._lock_token)
1692
self._lock_count += 1
1693
# Re-lock the repository too.
1694
self.repository.lock_write(self._repo_lock_token)
1695
return self._lock_token or None
1697
def _unlock(self, branch_token, repo_token):
1698
err_context = {'token': str((branch_token, repo_token))}
1699
response = self._call(
1700
'Branch.unlock', self._remote_path(), branch_token,
1701
repo_token or '', **err_context)
1702
if response == ('ok',):
1704
raise errors.UnexpectedSmartServerResponse(response)
1708
self._lock_count -= 1
1709
if not self._lock_count:
1710
self._clear_cached_state()
1711
mode = self._lock_mode
1712
self._lock_mode = None
1713
if self._real_branch is not None:
1714
if (not self._leave_lock and mode == 'w' and
1715
self._repo_lock_token):
1716
# If this RemoteBranch will remove the physical lock
1717
# for the repository, make sure the _real_branch
1718
# doesn't do it first. (Because the _real_branch's
1719
# repository is set to be the RemoteRepository.)
1720
self._real_branch.repository.leave_lock_in_place()
1721
self._real_branch.unlock()
1723
# Only write-locked branched need to make a remote method
1724
# call to perfom the unlock.
1726
if not self._lock_token:
1727
raise AssertionError('Locked, but no token!')
1728
branch_token = self._lock_token
1729
repo_token = self._repo_lock_token
1730
self._lock_token = None
1731
self._repo_lock_token = None
1732
if not self._leave_lock:
1733
self._unlock(branch_token, repo_token)
1735
self.repository.unlock()
1737
def break_lock(self):
1739
return self._real_branch.break_lock()
1741
def leave_lock_in_place(self):
1742
if not self._lock_token:
1743
raise NotImplementedError(self.leave_lock_in_place)
1744
self._leave_lock = True
1746
def dont_leave_lock_in_place(self):
1747
if not self._lock_token:
1748
raise NotImplementedError(self.dont_leave_lock_in_place)
1749
self._leave_lock = False
1751
def _last_revision_info(self):
1752
response = self._call('Branch.last_revision_info', self._remote_path())
1753
if response[0] != 'ok':
1754
raise SmartProtocolError('unexpected response code %s' % (response,))
1755
revno = int(response[1])
1756
last_revision = response[2]
1757
return (revno, last_revision)
1759
def _gen_revision_history(self):
1760
"""See Branch._gen_revision_history()."""
1761
response_tuple, response_handler = self._call_expecting_body(
1762
'Branch.revision_history', self._remote_path())
1763
if response_tuple[0] != 'ok':
1764
raise errors.UnexpectedSmartServerResponse(response_tuple)
1765
result = response_handler.read_body_bytes().split('\x00')
1770
def _remote_path(self):
1771
return self.bzrdir._path_for_remote_call(self._client)
1773
def _set_last_revision_descendant(self, revision_id, other_branch,
1774
allow_diverged=False, allow_overwrite_descendant=False):
1775
err_context = {'other_branch': other_branch}
1776
response = self._call('Branch.set_last_revision_ex',
1777
self._remote_path(), self._lock_token, self._repo_lock_token,
1778
revision_id, int(allow_diverged), int(allow_overwrite_descendant),
1780
self._clear_cached_state()
1781
if len(response) != 3 and response[0] != 'ok':
1782
raise errors.UnexpectedSmartServerResponse(response)
1783
new_revno, new_revision_id = response[1:]
1784
self._last_revision_info_cache = new_revno, new_revision_id
1785
if self._real_branch is not None:
1786
cache = new_revno, new_revision_id
1787
self._real_branch._last_revision_info_cache = cache
1789
def _set_last_revision(self, revision_id):
1790
self._clear_cached_state()
1791
response = self._call('Branch.set_last_revision',
1792
self._remote_path(), self._lock_token, self._repo_lock_token,
1794
if response != ('ok',):
1795
raise errors.UnexpectedSmartServerResponse(response)
1798
def set_revision_history(self, rev_history):
1799
# Send just the tip revision of the history; the server will generate
1800
# the full history from that. If the revision doesn't exist in this
1801
# branch, NoSuchRevision will be raised.
1802
if rev_history == []:
1805
rev_id = rev_history[-1]
1806
self._set_last_revision(rev_id)
1807
self._cache_revision_history(rev_history)
1809
def get_parent(self):
1811
return self._real_branch.get_parent()
1813
def set_parent(self, url):
1815
return self._real_branch.set_parent(url)
1817
def set_stacked_on_url(self, stacked_location):
1818
"""Set the URL this branch is stacked against.
1820
:raises UnstackableBranchFormat: If the branch does not support
1822
:raises UnstackableRepositoryFormat: If the repository does not support
1826
return self._real_branch.set_stacked_on_url(stacked_location)
1828
def sprout(self, to_bzrdir, revision_id=None):
1829
branch_format = to_bzrdir._format._branch_format
1830
if (branch_format is None or
1831
isinstance(branch_format, RemoteBranchFormat)):
1832
# The to_bzrdir specifies RemoteBranchFormat (or no format, which
1833
# implies the same thing), but RemoteBranches can't be created at
1834
# arbitrary URLs. So create a branch in the same format as
1835
# _real_branch instead.
1836
# XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
1837
# to_bzrdir.create_branch to create a RemoteBranch after all...
1839
result = self._real_branch._format.initialize(to_bzrdir)
1840
self.copy_content_into(result, revision_id=revision_id)
1841
result.set_parent(self.bzrdir.root_transport.base)
1843
result = branch.Branch.sprout(
1844
self, to_bzrdir, revision_id=revision_id)
1848
def pull(self, source, overwrite=False, stop_revision=None,
1850
self._clear_cached_state_of_remote_branch_only()
1852
return self._real_branch.pull(
1853
source, overwrite=overwrite, stop_revision=stop_revision,
1854
_override_hook_target=self, **kwargs)
1857
def push(self, target, overwrite=False, stop_revision=None):
1859
return self._real_branch.push(
1860
target, overwrite=overwrite, stop_revision=stop_revision,
1861
_override_hook_source_branch=self)
1863
def is_locked(self):
1864
return self._lock_count >= 1
1867
def revision_id_to_revno(self, revision_id):
1869
return self._real_branch.revision_id_to_revno(revision_id)
1872
def set_last_revision_info(self, revno, revision_id):
1873
revision_id = ensure_null(revision_id)
1875
response = self._call('Branch.set_last_revision_info',
1876
self._remote_path(), self._lock_token, self._repo_lock_token,
1877
str(revno), revision_id)
1878
except errors.UnknownSmartMethod:
1880
self._clear_cached_state_of_remote_branch_only()
1881
self._real_branch.set_last_revision_info(revno, revision_id)
1882
self._last_revision_info_cache = revno, revision_id
1884
if response == ('ok',):
1885
self._clear_cached_state()
1886
self._last_revision_info_cache = revno, revision_id
1887
# Update the _real_branch's cache too.
1888
if self._real_branch is not None:
1889
cache = self._last_revision_info_cache
1890
self._real_branch._last_revision_info_cache = cache
1892
raise errors.UnexpectedSmartServerResponse(response)
1895
def generate_revision_history(self, revision_id, last_rev=None,
1897
medium = self._client._medium
1898
if not medium._is_remote_before((1, 6)):
1900
self._set_last_revision_descendant(revision_id, other_branch,
1901
allow_diverged=True, allow_overwrite_descendant=True)
1903
except errors.UnknownSmartMethod:
1904
medium._remember_remote_is_before((1, 6))
1905
self._clear_cached_state_of_remote_branch_only()
1907
self._real_branch.generate_revision_history(
1908
revision_id, last_rev=last_rev, other_branch=other_branch)
1913
return self._real_branch.tags
1915
def set_push_location(self, location):
1917
return self._real_branch.set_push_location(location)
1920
def update_revisions(self, other, stop_revision=None, overwrite=False,
1922
"""See Branch.update_revisions."""
1925
if stop_revision is None:
1926
stop_revision = other.last_revision()
1927
if revision.is_null(stop_revision):
1928
# if there are no commits, we're done.
1930
self.fetch(other, stop_revision)
1933
# Just unconditionally set the new revision. We don't care if
1934
# the branches have diverged.
1935
self._set_last_revision(stop_revision)
1937
medium = self._client._medium
1938
if not medium._is_remote_before((1, 6)):
1940
self._set_last_revision_descendant(stop_revision, other)
1942
except errors.UnknownSmartMethod:
1943
medium._remember_remote_is_before((1, 6))
1944
# Fallback for pre-1.6 servers: check for divergence
1945
# client-side, then do _set_last_revision.
1946
last_rev = revision.ensure_null(self.last_revision())
1948
graph = self.repository.get_graph()
1949
if self._check_if_descendant_or_diverged(
1950
stop_revision, last_rev, graph, other):
1951
# stop_revision is a descendant of last_rev, but we aren't
1952
# overwriting, so we're done.
1954
self._set_last_revision(stop_revision)
1959
def _extract_tar(tar, to_dir):
1960
"""Extract all the contents of a tarfile object.
1962
A replacement for extractall, which is not present in python2.4
1965
tar.extract(tarinfo, to_dir)
1968
def _translate_error(err, **context):
1969
"""Translate an ErrorFromSmartServer into a more useful error.
1971
Possible context keys:
1979
If the error from the server doesn't match a known pattern, then
1980
UnknownErrorFromSmartServer is raised.
1984
return context[name]
1985
except KeyError, key_err:
1986
mutter('Missing key %r in context %r', key_err.args[0], context)
1989
"""Get the path from the context if present, otherwise use first error
1993
return context['path']
1994
except KeyError, key_err:
1996
return err.error_args[0]
1997
except IndexError, idx_err:
1999
'Missing key %r in context %r', key_err.args[0], context)
2002
if err.error_verb == 'NoSuchRevision':
2003
raise NoSuchRevision(find('branch'), err.error_args[0])
2004
elif err.error_verb == 'nosuchrevision':
2005
raise NoSuchRevision(find('repository'), err.error_args[0])
2006
elif err.error_tuple == ('nobranch',):
2007
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2008
elif err.error_verb == 'norepository':
2009
raise errors.NoRepositoryPresent(find('bzrdir'))
2010
elif err.error_verb == 'LockContention':
2011
raise errors.LockContention('(remote lock)')
2012
elif err.error_verb == 'UnlockableTransport':
2013
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2014
elif err.error_verb == 'LockFailed':
2015
raise errors.LockFailed(err.error_args[0], err.error_args[1])
2016
elif err.error_verb == 'TokenMismatch':
2017
raise errors.TokenMismatch(find('token'), '(remote token)')
2018
elif err.error_verb == 'Diverged':
2019
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2020
elif err.error_verb == 'TipChangeRejected':
2021
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2022
elif err.error_verb == 'UnstackableBranchFormat':
2023
raise errors.UnstackableBranchFormat(*err.error_args)
2024
elif err.error_verb == 'UnstackableRepositoryFormat':
2025
raise errors.UnstackableRepositoryFormat(*err.error_args)
2026
elif err.error_verb == 'NotStacked':
2027
raise errors.NotStacked(branch=find('branch'))
2028
elif err.error_verb == 'PermissionDenied':
2030
if len(err.error_args) >= 2:
2031
extra = err.error_args[1]
2034
raise errors.PermissionDenied(path, extra=extra)
2035
elif err.error_verb == 'ReadError':
2037
raise errors.ReadError(path)
2038
elif err.error_verb == 'NoSuchFile':
2040
raise errors.NoSuchFile(path)
2041
elif err.error_verb == 'FileExists':
2042
raise errors.FileExists(err.error_args[0])
2043
elif err.error_verb == 'DirectoryNotEmpty':
2044
raise errors.DirectoryNotEmpty(err.error_args[0])
2045
elif err.error_verb == 'ShortReadvError':
2046
args = err.error_args
2047
raise errors.ShortReadvError(
2048
args[0], int(args[1]), int(args[2]), int(args[3]))
2049
elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
2050
encoding = str(err.error_args[0]) # encoding must always be a string
2051
val = err.error_args[1]
2052
start = int(err.error_args[2])
2053
end = int(err.error_args[3])
2054
reason = str(err.error_args[4]) # reason must always be a string
2055
if val.startswith('u:'):
2056
val = val[2:].decode('utf-8')
2057
elif val.startswith('s:'):
2058
val = val[2:].decode('base64')
2059
if err.error_verb == 'UnicodeDecodeError':
2060
raise UnicodeDecodeError(encoding, val, start, end, reason)
2061
elif err.error_verb == 'UnicodeEncodeError':
2062
raise UnicodeEncodeError(encoding, val, start, end, reason)
2063
elif err.error_verb == 'ReadOnlyError':
2064
raise errors.TransportNotPossible('readonly transport')
2065
raise errors.UnknownErrorFromSmartServer(err)