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 peform 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 peform 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 peform 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 perfom 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 discrepency 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
path = repo.bzrdir._path_for_remote_call(client)
1361
byte_stream = self._stream_to_byte_stream(stream, src_format)
1363
response = client.call_with_body_stream(
1364
('Repository.insert_stream', path), byte_stream)
1365
except errors.UnknownSmartMethod:
1366
return self._insert_real(stream, src_format)
1368
def _stream_to_byte_stream(self, stream, src_format):
1370
pack_writer = pack.ContainerWriter(bytes.append)
1372
pack_writer.add_bytes_record(src_format.network_name(), '')
1374
def get_adapter(adapter_key):
1376
return adapters[adapter_key]
1378
adapter_factory = adapter_registry.get(adapter_key)
1379
adapter = adapter_factory(self)
1380
adapters[adapter_key] = adapter
1382
for substream_type, substream in stream:
1383
for record in substream:
1384
if record.storage_kind in ('chunked', 'fulltext'):
1385
serialised = record_to_fulltext_bytes(record)
1387
serialised = record.get_bytes_as(record.storage_kind)
1388
pack_writer.add_bytes_record(serialised, [(substream_type,)])
1397
class RemoteBranchLockableFiles(LockableFiles):
1398
"""A 'LockableFiles' implementation that talks to a smart server.
1400
This is not a public interface class.
1403
def __init__(self, bzrdir, _client):
1404
self.bzrdir = bzrdir
1405
self._client = _client
1406
self._need_find_modes = True
1407
LockableFiles.__init__(
1408
self, bzrdir.get_branch_transport(None),
1409
'lock', lockdir.LockDir)
1411
def _find_modes(self):
1412
# RemoteBranches don't let the client set the mode of control files.
1413
self._dir_mode = None
1414
self._file_mode = None
1417
class RemoteBranchFormat(branch.BranchFormat):
1420
super(RemoteBranchFormat, self).__init__()
1421
self._matchingbzrdir = RemoteBzrDirFormat()
1422
self._matchingbzrdir.set_branch_format(self)
1424
def __eq__(self, other):
1425
return (isinstance(other, RemoteBranchFormat) and
1426
self.__dict__ == other.__dict__)
1428
def get_format_description(self):
1429
return 'Remote BZR Branch'
1431
def get_format_string(self):
1432
return 'Remote BZR Branch'
1434
def open(self, a_bzrdir):
1435
return a_bzrdir.open_branch()
1437
def initialize(self, a_bzrdir):
1438
# Delegate to a _real object here - the RemoteBzrDir format now
1439
# supports delegating to parameterised branch formats and as such
1440
# this RemoteBranchFormat method is only called when no specific format
1442
if not isinstance(a_bzrdir, RemoteBzrDir):
1443
result = a_bzrdir.create_branch()
1445
a_bzrdir._ensure_real()
1446
result = a_bzrdir._real_bzrdir.create_branch()
1447
if not isinstance(result, RemoteBranch):
1448
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
1451
def supports_tags(self):
1452
# Remote branches might support tags, but we won't know until we
1453
# access the real remote branch.
1457
class RemoteBranch(branch.Branch, _RpcHelper):
1458
"""Branch stored on a server accessed by HPSS RPC.
1460
At the moment most operations are mapped down to simple file operations.
1463
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
1465
"""Create a RemoteBranch instance.
1467
:param real_branch: An optional local implementation of the branch
1468
format, usually accessing the data via the VFS.
1469
:param _client: Private parameter for testing.
1471
# We intentionally don't call the parent class's __init__, because it
1472
# will try to assign to self.tags, which is a property in this subclass.
1473
# And the parent's __init__ doesn't do much anyway.
1474
self._revision_id_to_revno_cache = None
1475
self._partial_revision_id_to_revno_cache = {}
1476
self._revision_history_cache = None
1477
self._last_revision_info_cache = None
1478
self._merge_sorted_revisions_cache = None
1479
self.bzrdir = remote_bzrdir
1480
if _client is not None:
1481
self._client = _client
1483
self._client = remote_bzrdir._client
1484
self.repository = remote_repository
1485
if real_branch is not None:
1486
self._real_branch = real_branch
1487
# Give the remote repository the matching real repo.
1488
real_repo = self._real_branch.repository
1489
if isinstance(real_repo, RemoteRepository):
1490
real_repo._ensure_real()
1491
real_repo = real_repo._real_repository
1492
self.repository._set_real_repository(real_repo)
1493
# Give the branch the remote repository to let fast-pathing happen.
1494
self._real_branch.repository = self.repository
1496
self._real_branch = None
1497
# Fill out expected attributes of branch for bzrlib api users.
1498
self._format = RemoteBranchFormat()
1499
self.base = self.bzrdir.root_transport.base
1500
self._control_files = None
1501
self._lock_mode = None
1502
self._lock_token = None
1503
self._repo_lock_token = None
1504
self._lock_count = 0
1505
self._leave_lock = False
1506
# The base class init is not called, so we duplicate this:
1507
hooks = branch.Branch.hooks['open']
1510
self._setup_stacking()
1512
def _setup_stacking(self):
1513
# configure stacking into the remote repository, by reading it from
1516
fallback_url = self.get_stacked_on_url()
1517
except (errors.NotStacked, errors.UnstackableBranchFormat,
1518
errors.UnstackableRepositoryFormat), e:
1520
# it's relative to this branch...
1521
fallback_url = urlutils.join(self.base, fallback_url)
1522
transports = [self.bzrdir.root_transport]
1523
if self._real_branch is not None:
1524
transports.append(self._real_branch._transport)
1525
stacked_on = branch.Branch.open(fallback_url,
1526
possible_transports=transports)
1527
self.repository.add_fallback_repository(stacked_on.repository)
1529
def _get_real_transport(self):
1530
# if we try vfs access, return the real branch's vfs transport
1532
return self._real_branch._transport
1534
_transport = property(_get_real_transport)
1537
return "%s(%s)" % (self.__class__.__name__, self.base)
1541
def _ensure_real(self):
1542
"""Ensure that there is a _real_branch set.
1544
Used before calls to self._real_branch.
1546
if self._real_branch is None:
1547
if not vfs.vfs_enabled():
1548
raise AssertionError('smart server vfs must be enabled '
1549
'to use vfs implementation')
1550
self.bzrdir._ensure_real()
1551
self._real_branch = self.bzrdir._real_bzrdir.open_branch()
1552
if self.repository._real_repository is None:
1553
# Give the remote repository the matching real repo.
1554
real_repo = self._real_branch.repository
1555
if isinstance(real_repo, RemoteRepository):
1556
real_repo._ensure_real()
1557
real_repo = real_repo._real_repository
1558
self.repository._set_real_repository(real_repo)
1559
# Give the real branch the remote repository to let fast-pathing
1561
self._real_branch.repository = self.repository
1562
if self._lock_mode == 'r':
1563
self._real_branch.lock_read()
1564
elif self._lock_mode == 'w':
1565
self._real_branch.lock_write(token=self._lock_token)
1567
def _translate_error(self, err, **context):
1568
self.repository._translate_error(err, branch=self, **context)
1570
def _clear_cached_state(self):
1571
super(RemoteBranch, self)._clear_cached_state()
1572
if self._real_branch is not None:
1573
self._real_branch._clear_cached_state()
1575
def _clear_cached_state_of_remote_branch_only(self):
1576
"""Like _clear_cached_state, but doesn't clear the cache of
1579
This is useful when falling back to calling a method of
1580
self._real_branch that changes state. In that case the underlying
1581
branch changes, so we need to invalidate this RemoteBranch's cache of
1582
it. However, there's no need to invalidate the _real_branch's cache
1583
too, in fact doing so might harm performance.
1585
super(RemoteBranch, self)._clear_cached_state()
1588
def control_files(self):
1589
# Defer actually creating RemoteBranchLockableFiles until its needed,
1590
# because it triggers an _ensure_real that we otherwise might not need.
1591
if self._control_files is None:
1592
self._control_files = RemoteBranchLockableFiles(
1593
self.bzrdir, self._client)
1594
return self._control_files
1596
def _get_checkout_format(self):
1598
return self._real_branch._get_checkout_format()
1600
def get_physical_lock_status(self):
1601
"""See Branch.get_physical_lock_status()."""
1602
# should be an API call to the server, as branches must be lockable.
1604
return self._real_branch.get_physical_lock_status()
1606
def get_stacked_on_url(self):
1607
"""Get the URL this branch is stacked against.
1609
:raises NotStacked: If the branch is not stacked.
1610
:raises UnstackableBranchFormat: If the branch does not support
1612
:raises UnstackableRepositoryFormat: If the repository does not support
1616
# there may not be a repository yet, so we can't use
1617
# self._translate_error, so we can't use self._call either.
1618
response = self._client.call('Branch.get_stacked_on_url',
1619
self._remote_path())
1620
except errors.ErrorFromSmartServer, err:
1621
# there may not be a repository yet, so we can't call through
1622
# its _translate_error
1623
_translate_error(err, branch=self)
1624
except errors.UnknownSmartMethod, err:
1626
return self._real_branch.get_stacked_on_url()
1627
if response[0] != 'ok':
1628
raise errors.UnexpectedSmartServerResponse(response)
1631
def lock_read(self):
1632
self.repository.lock_read()
1633
if not self._lock_mode:
1634
self._lock_mode = 'r'
1635
self._lock_count = 1
1636
if self._real_branch is not None:
1637
self._real_branch.lock_read()
1639
self._lock_count += 1
1641
def _remote_lock_write(self, token):
1643
branch_token = repo_token = ''
1645
branch_token = token
1646
repo_token = self.repository.lock_write()
1647
self.repository.unlock()
1648
err_context = {'token': token}
1649
response = self._call(
1650
'Branch.lock_write', self._remote_path(), branch_token,
1651
repo_token or '', **err_context)
1652
if response[0] != 'ok':
1653
raise errors.UnexpectedSmartServerResponse(response)
1654
ok, branch_token, repo_token = response
1655
return branch_token, repo_token
1657
def lock_write(self, token=None):
1658
if not self._lock_mode:
1659
# Lock the branch and repo in one remote call.
1660
remote_tokens = self._remote_lock_write(token)
1661
self._lock_token, self._repo_lock_token = remote_tokens
1662
if not self._lock_token:
1663
raise SmartProtocolError('Remote server did not return a token!')
1664
# Tell the self.repository object that it is locked.
1665
self.repository.lock_write(
1666
self._repo_lock_token, _skip_rpc=True)
1668
if self._real_branch is not None:
1669
self._real_branch.lock_write(token=self._lock_token)
1670
if token is not None:
1671
self._leave_lock = True
1673
self._leave_lock = False
1674
self._lock_mode = 'w'
1675
self._lock_count = 1
1676
elif self._lock_mode == 'r':
1677
raise errors.ReadOnlyTransaction
1679
if token is not None:
1680
# A token was given to lock_write, and we're relocking, so
1681
# check that the given token actually matches the one we
1683
if token != self._lock_token:
1684
raise errors.TokenMismatch(token, self._lock_token)
1685
self._lock_count += 1
1686
# Re-lock the repository too.
1687
self.repository.lock_write(self._repo_lock_token)
1688
return self._lock_token or None
1690
def _unlock(self, branch_token, repo_token):
1691
err_context = {'token': str((branch_token, repo_token))}
1692
response = self._call(
1693
'Branch.unlock', self._remote_path(), branch_token,
1694
repo_token or '', **err_context)
1695
if response == ('ok',):
1697
raise errors.UnexpectedSmartServerResponse(response)
1701
self._lock_count -= 1
1702
if not self._lock_count:
1703
self._clear_cached_state()
1704
mode = self._lock_mode
1705
self._lock_mode = None
1706
if self._real_branch is not None:
1707
if (not self._leave_lock and mode == 'w' and
1708
self._repo_lock_token):
1709
# If this RemoteBranch will remove the physical lock
1710
# for the repository, make sure the _real_branch
1711
# doesn't do it first. (Because the _real_branch's
1712
# repository is set to be the RemoteRepository.)
1713
self._real_branch.repository.leave_lock_in_place()
1714
self._real_branch.unlock()
1716
# Only write-locked branched need to make a remote method
1717
# call to perfom the unlock.
1719
if not self._lock_token:
1720
raise AssertionError('Locked, but no token!')
1721
branch_token = self._lock_token
1722
repo_token = self._repo_lock_token
1723
self._lock_token = None
1724
self._repo_lock_token = None
1725
if not self._leave_lock:
1726
self._unlock(branch_token, repo_token)
1728
self.repository.unlock()
1730
def break_lock(self):
1732
return self._real_branch.break_lock()
1734
def leave_lock_in_place(self):
1735
if not self._lock_token:
1736
raise NotImplementedError(self.leave_lock_in_place)
1737
self._leave_lock = True
1739
def dont_leave_lock_in_place(self):
1740
if not self._lock_token:
1741
raise NotImplementedError(self.dont_leave_lock_in_place)
1742
self._leave_lock = False
1744
def _last_revision_info(self):
1745
response = self._call('Branch.last_revision_info', self._remote_path())
1746
if response[0] != 'ok':
1747
raise SmartProtocolError('unexpected response code %s' % (response,))
1748
revno = int(response[1])
1749
last_revision = response[2]
1750
return (revno, last_revision)
1752
def _gen_revision_history(self):
1753
"""See Branch._gen_revision_history()."""
1754
response_tuple, response_handler = self._call_expecting_body(
1755
'Branch.revision_history', self._remote_path())
1756
if response_tuple[0] != 'ok':
1757
raise errors.UnexpectedSmartServerResponse(response_tuple)
1758
result = response_handler.read_body_bytes().split('\x00')
1763
def _remote_path(self):
1764
return self.bzrdir._path_for_remote_call(self._client)
1766
def _set_last_revision_descendant(self, revision_id, other_branch,
1767
allow_diverged=False, allow_overwrite_descendant=False):
1768
# This performs additional work to meet the hook contract; while its
1769
# undesirable, we have to synthesise the revno to call the hook, and
1770
# not calling the hook is worse as it means changes can't be prevented.
1771
# Having calculated this though, we can't just call into
1772
# set_last_revision_info as a simple call, because there is a set_rh
1773
# hook that some folk may still be using.
1774
old_revno, old_revid = self.last_revision_info()
1775
history = self._lefthand_history(revision_id)
1776
self._run_pre_change_branch_tip_hooks(len(history), revision_id)
1777
err_context = {'other_branch': other_branch}
1778
response = self._call('Branch.set_last_revision_ex',
1779
self._remote_path(), self._lock_token, self._repo_lock_token,
1780
revision_id, int(allow_diverged), int(allow_overwrite_descendant),
1782
self._clear_cached_state()
1783
if len(response) != 3 and response[0] != 'ok':
1784
raise errors.UnexpectedSmartServerResponse(response)
1785
new_revno, new_revision_id = response[1:]
1786
self._last_revision_info_cache = new_revno, new_revision_id
1787
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1788
if self._real_branch is not None:
1789
cache = new_revno, new_revision_id
1790
self._real_branch._last_revision_info_cache = cache
1792
def _set_last_revision(self, revision_id):
1793
old_revno, old_revid = self.last_revision_info()
1794
# This performs additional work to meet the hook contract; while its
1795
# undesirable, we have to synthesise the revno to call the hook, and
1796
# not calling the hook is worse as it means changes can't be prevented.
1797
# Having calculated this though, we can't just call into
1798
# set_last_revision_info as a simple call, because there is a set_rh
1799
# hook that some folk may still be using.
1800
history = self._lefthand_history(revision_id)
1801
self._run_pre_change_branch_tip_hooks(len(history), revision_id)
1802
self._clear_cached_state()
1803
response = self._call('Branch.set_last_revision',
1804
self._remote_path(), self._lock_token, self._repo_lock_token,
1806
if response != ('ok',):
1807
raise errors.UnexpectedSmartServerResponse(response)
1808
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1811
def set_revision_history(self, rev_history):
1812
# Send just the tip revision of the history; the server will generate
1813
# the full history from that. If the revision doesn't exist in this
1814
# branch, NoSuchRevision will be raised.
1815
if rev_history == []:
1818
rev_id = rev_history[-1]
1819
self._set_last_revision(rev_id)
1820
for hook in branch.Branch.hooks['set_rh']:
1821
hook(self, rev_history)
1822
self._cache_revision_history(rev_history)
1824
def get_parent(self):
1826
return self._real_branch.get_parent()
1828
def _get_parent_location(self):
1829
# Used by tests, when checking normalisation of given vs stored paths.
1831
return self._real_branch._get_parent_location()
1833
def set_parent(self, url):
1835
return self._real_branch.set_parent(url)
1837
def _set_parent_location(self, url):
1838
# Used by tests, to poke bad urls into branch configurations
1840
self.set_parent(url)
1843
return self._real_branch._set_parent_location(url)
1845
def set_stacked_on_url(self, stacked_location):
1846
"""Set the URL this branch is stacked against.
1848
:raises UnstackableBranchFormat: If the branch does not support
1850
:raises UnstackableRepositoryFormat: If the repository does not support
1854
return self._real_branch.set_stacked_on_url(stacked_location)
1856
def sprout(self, to_bzrdir, revision_id=None):
1857
branch_format = to_bzrdir._format._branch_format
1858
if (branch_format is None or
1859
isinstance(branch_format, RemoteBranchFormat)):
1860
# The to_bzrdir specifies RemoteBranchFormat (or no format, which
1861
# implies the same thing), but RemoteBranches can't be created at
1862
# arbitrary URLs. So create a branch in the same format as
1863
# _real_branch instead.
1864
# XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
1865
# to_bzrdir.create_branch to create a RemoteBranch after all...
1867
result = self._real_branch._format.initialize(to_bzrdir)
1868
self.copy_content_into(result, revision_id=revision_id)
1869
result.set_parent(self.bzrdir.root_transport.base)
1871
result = branch.Branch.sprout(
1872
self, to_bzrdir, revision_id=revision_id)
1876
def pull(self, source, overwrite=False, stop_revision=None,
1878
self._clear_cached_state_of_remote_branch_only()
1880
return self._real_branch.pull(
1881
source, overwrite=overwrite, stop_revision=stop_revision,
1882
_override_hook_target=self, **kwargs)
1885
def push(self, target, overwrite=False, stop_revision=None):
1887
return self._real_branch.push(
1888
target, overwrite=overwrite, stop_revision=stop_revision,
1889
_override_hook_source_branch=self)
1891
def is_locked(self):
1892
return self._lock_count >= 1
1895
def revision_id_to_revno(self, revision_id):
1897
return self._real_branch.revision_id_to_revno(revision_id)
1900
def set_last_revision_info(self, revno, revision_id):
1901
# XXX: These should be returned by the set_last_revision_info verb
1902
old_revno, old_revid = self.last_revision_info()
1903
self._run_pre_change_branch_tip_hooks(revno, revision_id)
1904
revision_id = ensure_null(revision_id)
1906
response = self._call('Branch.set_last_revision_info',
1907
self._remote_path(), self._lock_token, self._repo_lock_token,
1908
str(revno), revision_id)
1909
except errors.UnknownSmartMethod:
1911
self._clear_cached_state_of_remote_branch_only()
1912
self._real_branch.set_last_revision_info(revno, revision_id)
1913
self._last_revision_info_cache = revno, revision_id
1915
if response == ('ok',):
1916
self._clear_cached_state()
1917
self._last_revision_info_cache = revno, revision_id
1918
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
1919
# Update the _real_branch's cache too.
1920
if self._real_branch is not None:
1921
cache = self._last_revision_info_cache
1922
self._real_branch._last_revision_info_cache = cache
1924
raise errors.UnexpectedSmartServerResponse(response)
1927
def generate_revision_history(self, revision_id, last_rev=None,
1929
medium = self._client._medium
1930
if not medium._is_remote_before((1, 6)):
1931
# Use a smart method for 1.6 and above servers
1933
self._set_last_revision_descendant(revision_id, other_branch,
1934
allow_diverged=True, allow_overwrite_descendant=True)
1936
except errors.UnknownSmartMethod:
1937
medium._remember_remote_is_before((1, 6))
1938
self._clear_cached_state_of_remote_branch_only()
1939
self.set_revision_history(self._lefthand_history(revision_id,
1940
last_rev=last_rev,other_branch=other_branch))
1945
return self._real_branch.tags
1947
def set_push_location(self, location):
1949
return self._real_branch.set_push_location(location)
1952
def update_revisions(self, other, stop_revision=None, overwrite=False,
1954
"""See Branch.update_revisions."""
1957
if stop_revision is None:
1958
stop_revision = other.last_revision()
1959
if revision.is_null(stop_revision):
1960
# if there are no commits, we're done.
1962
self.fetch(other, stop_revision)
1965
# Just unconditionally set the new revision. We don't care if
1966
# the branches have diverged.
1967
self._set_last_revision(stop_revision)
1969
medium = self._client._medium
1970
if not medium._is_remote_before((1, 6)):
1972
self._set_last_revision_descendant(stop_revision, other)
1974
except errors.UnknownSmartMethod:
1975
medium._remember_remote_is_before((1, 6))
1976
# Fallback for pre-1.6 servers: check for divergence
1977
# client-side, then do _set_last_revision.
1978
last_rev = revision.ensure_null(self.last_revision())
1980
graph = self.repository.get_graph()
1981
if self._check_if_descendant_or_diverged(
1982
stop_revision, last_rev, graph, other):
1983
# stop_revision is a descendant of last_rev, but we aren't
1984
# overwriting, so we're done.
1986
self._set_last_revision(stop_revision)
1991
def _extract_tar(tar, to_dir):
1992
"""Extract all the contents of a tarfile object.
1994
A replacement for extractall, which is not present in python2.4
1997
tar.extract(tarinfo, to_dir)
2000
def _translate_error(err, **context):
2001
"""Translate an ErrorFromSmartServer into a more useful error.
2003
Possible context keys:
2011
If the error from the server doesn't match a known pattern, then
2012
UnknownErrorFromSmartServer is raised.
2016
return context[name]
2017
except KeyError, key_err:
2018
mutter('Missing key %r in context %r', key_err.args[0], context)
2021
"""Get the path from the context if present, otherwise use first error
2025
return context['path']
2026
except KeyError, key_err:
2028
return err.error_args[0]
2029
except IndexError, idx_err:
2031
'Missing key %r in context %r', key_err.args[0], context)
2034
if err.error_verb == 'NoSuchRevision':
2035
raise NoSuchRevision(find('branch'), err.error_args[0])
2036
elif err.error_verb == 'nosuchrevision':
2037
raise NoSuchRevision(find('repository'), err.error_args[0])
2038
elif err.error_tuple == ('nobranch',):
2039
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2040
elif err.error_verb == 'norepository':
2041
raise errors.NoRepositoryPresent(find('bzrdir'))
2042
elif err.error_verb == 'LockContention':
2043
raise errors.LockContention('(remote lock)')
2044
elif err.error_verb == 'UnlockableTransport':
2045
raise errors.UnlockableTransport(find('bzrdir').root_transport)
2046
elif err.error_verb == 'LockFailed':
2047
raise errors.LockFailed(err.error_args[0], err.error_args[1])
2048
elif err.error_verb == 'TokenMismatch':
2049
raise errors.TokenMismatch(find('token'), '(remote token)')
2050
elif err.error_verb == 'Diverged':
2051
raise errors.DivergedBranches(find('branch'), find('other_branch'))
2052
elif err.error_verb == 'TipChangeRejected':
2053
raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2054
elif err.error_verb == 'UnstackableBranchFormat':
2055
raise errors.UnstackableBranchFormat(*err.error_args)
2056
elif err.error_verb == 'UnstackableRepositoryFormat':
2057
raise errors.UnstackableRepositoryFormat(*err.error_args)
2058
elif err.error_verb == 'NotStacked':
2059
raise errors.NotStacked(branch=find('branch'))
2060
elif err.error_verb == 'PermissionDenied':
2062
if len(err.error_args) >= 2:
2063
extra = err.error_args[1]
2066
raise errors.PermissionDenied(path, extra=extra)
2067
elif err.error_verb == 'ReadError':
2069
raise errors.ReadError(path)
2070
elif err.error_verb == 'NoSuchFile':
2072
raise errors.NoSuchFile(path)
2073
elif err.error_verb == 'FileExists':
2074
raise errors.FileExists(err.error_args[0])
2075
elif err.error_verb == 'DirectoryNotEmpty':
2076
raise errors.DirectoryNotEmpty(err.error_args[0])
2077
elif err.error_verb == 'ShortReadvError':
2078
args = err.error_args
2079
raise errors.ShortReadvError(
2080
args[0], int(args[1]), int(args[2]), int(args[3]))
2081
elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
2082
encoding = str(err.error_args[0]) # encoding must always be a string
2083
val = err.error_args[1]
2084
start = int(err.error_args[2])
2085
end = int(err.error_args[3])
2086
reason = str(err.error_args[4]) # reason must always be a string
2087
if val.startswith('u:'):
2088
val = val[2:].decode('utf-8')
2089
elif val.startswith('s:'):
2090
val = val[2:].decode('base64')
2091
if err.error_verb == 'UnicodeDecodeError':
2092
raise UnicodeDecodeError(encoding, val, start, end, reason)
2093
elif err.error_verb == 'UnicodeEncodeError':
2094
raise UnicodeEncodeError(encoding, val, start, end, reason)
2095
elif err.error_verb == 'ReadOnlyError':
2096
raise errors.TransportNotPossible('readonly transport')
2097
raise errors.UnknownErrorFromSmartServer(err)