1
# Copyright (C) 2006, 2007 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.
20
from cStringIO import StringIO
28
from bzrlib.branch import Branch, BranchReferenceFormat
29
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
30
from bzrlib.config import BranchConfig, TreeConfig
31
from bzrlib.decorators import needs_read_lock, needs_write_lock
32
from bzrlib.errors import NoSuchRevision
33
from bzrlib.lockable_files import LockableFiles
34
from bzrlib.pack import ContainerReader
35
from bzrlib.revision import NULL_REVISION
36
from bzrlib.smart import client, vfs
37
from bzrlib.symbol_versioning import (
41
from bzrlib.trace import note
43
# Note: RemoteBzrDirFormat is in bzrdir.py
45
class RemoteBzrDir(BzrDir):
46
"""Control directory on a remote server, accessed via bzr:// or similar."""
48
def __init__(self, transport, _client=None):
49
"""Construct a RemoteBzrDir.
51
:param _client: Private parameter for testing. Disables probing and the
54
BzrDir.__init__(self, transport, RemoteBzrDirFormat())
55
# this object holds a delegated bzrdir that uses file-level operations
56
# to talk to the other side
57
self._real_bzrdir = None
60
self._shared_medium = transport.get_shared_medium()
61
self._client = client._SmartClient(self._shared_medium)
63
self._client = _client
64
self._shared_medium = None
67
path = self._path_for_remote_call(self._client)
68
response = self._client.call('BzrDir.open', path)
69
if response not in [('yes',), ('no',)]:
70
raise errors.UnexpectedSmartServerResponse(response)
71
if response == ('no',):
72
raise errors.NotBranchError(path=transport.base)
74
def _ensure_real(self):
75
"""Ensure that there is a _real_bzrdir set.
77
Used before calls to self._real_bzrdir.
79
if not self._real_bzrdir:
80
self._real_bzrdir = BzrDir.open_from_transport(
81
self.root_transport, _server_formats=False)
83
def create_repository(self, shared=False):
85
self._real_bzrdir.create_repository(shared=shared)
86
return self.open_repository()
88
def create_branch(self):
90
real_branch = self._real_bzrdir.create_branch()
91
return RemoteBranch(self, self.find_repository(), real_branch)
93
def create_workingtree(self, revision_id=None):
94
raise errors.NotLocalUrl(self.transport.base)
96
def find_branch_format(self):
97
"""Find the branch 'format' for this bzrdir.
99
This might be a synthetic object for e.g. RemoteBranch and SVN.
101
b = self.open_branch()
104
def get_branch_reference(self):
105
"""See BzrDir.get_branch_reference()."""
106
path = self._path_for_remote_call(self._client)
107
response = self._client.call('BzrDir.open_branch', path)
108
if response[0] == 'ok':
109
if response[1] == '':
110
# branch at this location.
113
# a branch reference, use the existing BranchReference logic.
115
elif response == ('nobranch',):
116
raise errors.NotBranchError(path=self.root_transport.base)
118
raise errors.UnexpectedSmartServerResponse(response)
120
def open_branch(self, _unsupported=False):
121
assert _unsupported == False, 'unsupported flag support not implemented yet.'
122
reference_url = self.get_branch_reference()
123
if reference_url is None:
124
# branch at this location.
125
return RemoteBranch(self, self.find_repository())
127
# a branch reference, use the existing BranchReference logic.
128
format = BranchReferenceFormat()
129
return format.open(self, _found=True, location=reference_url)
131
def open_repository(self):
132
path = self._path_for_remote_call(self._client)
133
response = self._client.call('BzrDir.find_repository', path)
134
assert response[0] in ('ok', 'norepository'), \
135
'unexpected response code %s' % (response,)
136
if response[0] == 'norepository':
137
raise errors.NoRepositoryPresent(self)
138
assert len(response) == 4, 'incorrect response length %s' % (response,)
139
if response[1] == '':
140
format = RemoteRepositoryFormat()
141
format.rich_root_data = (response[2] == 'yes')
142
format.supports_tree_reference = (response[3] == 'yes')
143
return RemoteRepository(self, format)
145
raise errors.NoRepositoryPresent(self)
147
def open_workingtree(self, recommend_upgrade=True):
149
if self._real_bzrdir.has_workingtree():
150
raise errors.NotLocalUrl(self.root_transport)
152
raise errors.NoWorkingTree(self.root_transport.base)
154
def _path_for_remote_call(self, client):
155
"""Return the path to be used for this bzrdir in a remote call."""
156
return client.remote_path_from_transport(self.root_transport)
158
def get_branch_transport(self, branch_format):
160
return self._real_bzrdir.get_branch_transport(branch_format)
162
def get_repository_transport(self, repository_format):
164
return self._real_bzrdir.get_repository_transport(repository_format)
166
def get_workingtree_transport(self, workingtree_format):
168
return self._real_bzrdir.get_workingtree_transport(workingtree_format)
170
def can_convert_format(self):
171
"""Upgrading of remote bzrdirs is not supported yet."""
174
def needs_format_conversion(self, format=None):
175
"""Upgrading of remote bzrdirs is not supported yet."""
178
def clone(self, url, revision_id=None, force_new_repo=False):
180
return self._real_bzrdir.clone(url, revision_id=revision_id,
181
force_new_repo=force_new_repo)
184
class RemoteRepositoryFormat(repository.RepositoryFormat):
185
"""Format for repositories accessed over a _SmartClient.
187
Instances of this repository are represented by RemoteRepository
190
The RemoteRepositoryFormat is parameterised during construction
191
to reflect the capabilities of the real, remote format. Specifically
192
the attributes rich_root_data and supports_tree_reference are set
193
on a per instance basis, and are not set (and should not be) at
197
_matchingbzrdir = RemoteBzrDirFormat
199
def initialize(self, a_bzrdir, shared=False):
200
assert isinstance(a_bzrdir, RemoteBzrDir), \
201
'%r is not a RemoteBzrDir' % (a_bzrdir,)
202
return a_bzrdir.create_repository(shared=shared)
204
def open(self, a_bzrdir):
205
assert isinstance(a_bzrdir, RemoteBzrDir)
206
return a_bzrdir.open_repository()
208
def get_format_description(self):
209
return 'bzr remote repository'
211
def __eq__(self, other):
212
return self.__class__ == other.__class__
214
def check_conversion_target(self, target_format):
215
if self.rich_root_data and not target_format.rich_root_data:
216
raise errors.BadConversionTarget(
217
'Does not support rich root data.', target_format)
218
if (self.supports_tree_reference and
219
not getattr(target_format, 'supports_tree_reference', False)):
220
raise errors.BadConversionTarget(
221
'Does not support nested trees', target_format)
224
class PackSource(object):
225
def __init__(self, chunked_stream):
226
self.chunked_stream = chunked_stream
228
def read(self, length):
229
if length is not None:
230
while len(self.buffer) < length:
231
self.buffer += self.chunked_stream.next()
232
bytes = self.buffer[:length]
233
self.buffer = self.buffer[length:]
236
for bytes in self.chunked_stream:
242
while '\n' not in self.buffer:
244
self.buffer += self.chunked_stream.next()
245
except StopIteration:
247
pos = self.buffer.find('\n')
252
bytes = self.buffer[:pos+1]
253
self.buffer = self.buffer[pos+1:]
257
class RemoteRepository(object):
258
"""Repository accessed over rpc.
260
For the moment most operations are performed using local transport-backed
264
def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
265
"""Create a RemoteRepository instance.
267
:param remote_bzrdir: The bzrdir hosting this repository.
268
:param format: The RemoteFormat object to use.
269
:param real_repository: If not None, a local implementation of the
270
repository logic for the repository, usually accessing the data
272
:param _client: Private testing parameter - override the smart client
273
to be used by the repository.
276
self._real_repository = real_repository
278
self._real_repository = None
279
self.bzrdir = remote_bzrdir
281
self._client = client._SmartClient(self.bzrdir._shared_medium)
283
self._client = _client
284
self._format = format
285
self._lock_mode = None
286
self._lock_token = None
288
self._leave_lock = False
290
self._reconcile_does_inventory_gc = True
292
def abort_write_group(self):
293
"""Complete a write group on the decorated repository.
295
Smart methods peform operations in a single step so this api
296
is not really applicable except as a compatibility thunk
297
for older plugins that don't use e.g. the CommitBuilder
301
return self._real_repository.abort_write_group()
303
def commit_write_group(self):
304
"""Complete a write group on the decorated repository.
306
Smart methods peform operations in a single step so this api
307
is not really applicable except as a compatibility thunk
308
for older plugins that don't use e.g. the CommitBuilder
312
return self._real_repository.commit_write_group()
314
def _ensure_real(self):
315
"""Ensure that there is a _real_repository set.
317
Used before calls to self._real_repository.
319
if not self._real_repository:
320
self.bzrdir._ensure_real()
321
#self._real_repository = self.bzrdir._real_bzrdir.open_repository()
322
self._set_real_repository(self.bzrdir._real_bzrdir.open_repository())
324
def get_revision_graph(self, revision_id=None):
325
"""See Repository.get_revision_graph()."""
326
if revision_id is None:
328
elif revision_id == NULL_REVISION:
331
path = self.bzrdir._path_for_remote_call(self._client)
332
assert type(revision_id) is str
333
response = self._client.call_expecting_body(
334
'Repository.get_revision_graph', path, revision_id)
335
if response[0][0] not in ['ok', 'nosuchrevision']:
336
raise errors.UnexpectedSmartServerResponse(response[0])
337
if response[0][0] == 'ok':
338
coded = response[1].read_body_bytes()
340
# no revisions in this repository!
342
lines = coded.split('\n')
345
d = tuple(line.split())
346
revision_graph[d[0]] = d[1:]
348
return revision_graph
350
response_body = response[1].read_body_bytes()
351
assert response_body == ''
352
raise NoSuchRevision(self, revision_id)
354
def has_revision(self, revision_id):
355
"""See Repository.has_revision()."""
356
if revision_id is None:
357
# The null revision is always present.
359
path = self.bzrdir._path_for_remote_call(self._client)
360
response = self._client.call('Repository.has_revision', path, revision_id)
361
assert response[0] in ('yes', 'no'), 'unexpected response code %s' % (response,)
362
return response[0] == 'yes'
364
def has_same_location(self, other):
365
return (self.__class__ == other.__class__ and
366
self.bzrdir.transport.base == other.bzrdir.transport.base)
368
def get_graph(self, other_repository=None):
369
"""Return the graph for this repository format"""
370
return self._real_repository.get_graph(other_repository)
372
def gather_stats(self, revid=None, committers=None):
373
"""See Repository.gather_stats()."""
374
path = self.bzrdir._path_for_remote_call(self._client)
375
if revid in (None, NULL_REVISION):
379
if committers is None or not committers:
380
fmt_committers = 'no'
382
fmt_committers = 'yes'
383
response = self._client.call_expecting_body(
384
'Repository.gather_stats', path, fmt_revid, fmt_committers)
385
assert response[0][0] == 'ok', \
386
'unexpected response code %s' % (response[0],)
388
body = response[1].read_body_bytes()
390
for line in body.split('\n'):
393
key, val_text = line.split(':')
394
if key in ('revisions', 'size', 'committers'):
395
result[key] = int(val_text)
396
elif key in ('firstrev', 'latestrev'):
397
values = val_text.split(' ')[1:]
398
result[key] = (float(values[0]), long(values[1]))
402
def get_physical_lock_status(self):
403
"""See Repository.get_physical_lock_status()."""
406
def is_in_write_group(self):
407
"""Return True if there is an open write group.
409
write groups are only applicable locally for the smart server..
411
if self._real_repository:
412
return self._real_repository.is_in_write_group()
415
return self._lock_count >= 1
418
"""See Repository.is_shared()."""
419
path = self.bzrdir._path_for_remote_call(self._client)
420
response = self._client.call('Repository.is_shared', path)
421
assert response[0] in ('yes', 'no'), 'unexpected response code %s' % (response,)
422
return response[0] == 'yes'
425
# wrong eventually - want a local lock cache context
426
if not self._lock_mode:
427
self._lock_mode = 'r'
429
if self._real_repository is not None:
430
self._real_repository.lock_read()
432
self._lock_count += 1
434
def _remote_lock_write(self, token):
435
path = self.bzrdir._path_for_remote_call(self._client)
438
response = self._client.call('Repository.lock_write', path, token)
439
if response[0] == 'ok':
442
elif response[0] == 'LockContention':
443
raise errors.LockContention('(remote lock)')
444
elif response[0] == 'UnlockableTransport':
445
raise errors.UnlockableTransport(self.bzrdir.root_transport)
447
raise errors.UnexpectedSmartServerResponse(response)
449
def lock_write(self, token=None):
450
if not self._lock_mode:
451
self._lock_token = self._remote_lock_write(token)
452
assert self._lock_token, 'Remote server did not return a token!'
453
if self._real_repository is not None:
454
self._real_repository.lock_write(token=self._lock_token)
455
if token is not None:
456
self._leave_lock = True
458
self._leave_lock = False
459
self._lock_mode = 'w'
461
elif self._lock_mode == 'r':
462
raise errors.ReadOnlyError(self)
464
self._lock_count += 1
465
return self._lock_token
467
def leave_lock_in_place(self):
468
self._leave_lock = True
470
def dont_leave_lock_in_place(self):
471
self._leave_lock = False
473
def _set_real_repository(self, repository):
474
"""Set the _real_repository for this repository.
476
:param repository: The repository to fallback to for non-hpss
477
implemented operations.
479
assert not isinstance(repository, RemoteRepository)
480
self._real_repository = repository
481
if self._lock_mode == 'w':
482
# if we are already locked, the real repository must be able to
483
# acquire the lock with our token.
484
self._real_repository.lock_write(self._lock_token)
485
elif self._lock_mode == 'r':
486
self._real_repository.lock_read()
488
def start_write_group(self):
489
"""Start a write group on the decorated repository.
491
Smart methods peform operations in a single step so this api
492
is not really applicable except as a compatibility thunk
493
for older plugins that don't use e.g. the CommitBuilder
497
return self._real_repository.start_write_group()
499
def _unlock(self, token):
500
path = self.bzrdir._path_for_remote_call(self._client)
501
response = self._client.call('Repository.unlock', path, token)
502
if response == ('ok',):
504
elif response[0] == 'TokenMismatch':
505
raise errors.TokenMismatch(token, '(remote token)')
507
raise errors.UnexpectedSmartServerResponse(response)
510
if self._lock_count == 1 and self._lock_mode == 'w':
511
# don't unlock if inside a write group.
512
if self.is_in_write_group():
513
raise errors.BzrError(
514
'Must end write groups before releasing write locks.')
515
self._lock_count -= 1
516
if not self._lock_count:
517
mode = self._lock_mode
518
self._lock_mode = None
519
if self._real_repository is not None:
520
self._real_repository.unlock()
522
# Only write-locked repositories need to make a remote method
523
# call to perfom the unlock.
525
assert self._lock_token, 'Locked, but no token!'
526
token = self._lock_token
527
self._lock_token = None
528
if not self._leave_lock:
531
def break_lock(self):
532
# should hand off to the network
534
return self._real_repository.break_lock()
536
def _get_tarball(self, compression):
537
"""Return a TemporaryFile containing a repository tarball"""
539
path = self.bzrdir._path_for_remote_call(self._client)
540
response, protocol = self._client.call_expecting_body(
541
'Repository.tarball', path, compression)
542
assert response[0] in ('ok', 'failure'), \
543
'unexpected response code %s' % (response,)
544
if response[0] == 'ok':
545
# Extract the tarball and return it
546
t = tempfile.NamedTemporaryFile()
547
# TODO: rpc layer should read directly into it...
548
t.write(protocol.read_body_bytes())
552
raise errors.SmartServerError(error_code=response)
554
def sprout(self, to_bzrdir, revision_id=None):
555
# TODO: Option to control what format is created?
556
dest_repo = to_bzrdir.create_repository()
557
dest_repo.fetch(self, revision_id=revision_id)
560
### These methods are just thin shims to the VFS object for now.
562
def revision_tree(self, revision_id):
564
return self._real_repository.revision_tree(revision_id)
566
def get_serializer_format(self):
568
return self._real_repository.get_serializer_format()
570
def get_commit_builder(self, branch, parents, config, timestamp=None,
571
timezone=None, committer=None, revprops=None,
573
# FIXME: It ought to be possible to call this without immediately
574
# triggering _ensure_real. For now it's the easiest thing to do.
576
builder = self._real_repository.get_commit_builder(branch, parents,
577
config, timestamp=timestamp, timezone=timezone,
578
committer=committer, revprops=revprops, revision_id=revision_id)
579
# Make the builder use this RemoteRepository rather than the real one.
580
builder.repository = self
584
def add_inventory(self, revid, inv, parents):
586
return self._real_repository.add_inventory(revid, inv, parents)
589
def add_revision(self, rev_id, rev, inv=None, config=None):
591
return self._real_repository.add_revision(
592
rev_id, rev, inv=inv, config=config)
595
def get_inventory(self, revision_id):
597
return self._real_repository.get_inventory(revision_id)
600
def get_revision(self, revision_id):
602
return self._real_repository.get_revision(revision_id)
605
def weave_store(self):
607
return self._real_repository.weave_store
609
def get_transaction(self):
611
return self._real_repository.get_transaction()
614
def clone(self, a_bzrdir, revision_id=None):
616
return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
618
def make_working_trees(self):
619
"""RemoteRepositories never create working trees by default."""
622
def fetch(self, source, revision_id=None, pb=None):
624
return self._real_repository.fetch(
625
source, revision_id=revision_id, pb=pb)
627
def create_bundle(self, target, base, fileobj, format=None):
629
self._real_repository.create_bundle(target, base, fileobj, format)
632
def control_weaves(self):
634
return self._real_repository.control_weaves
637
def get_ancestry(self, revision_id, topo_sorted=True):
639
return self._real_repository.get_ancestry(revision_id, topo_sorted)
642
def get_inventory_weave(self):
644
return self._real_repository.get_inventory_weave()
646
def fileids_altered_by_revision_ids(self, revision_ids):
648
return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
650
def iter_files_bytes(self, desired_files):
651
"""See Repository.iter_file_bytes.
654
return self._real_repository.iter_files_bytes(desired_files)
657
def get_signature_text(self, revision_id):
659
return self._real_repository.get_signature_text(revision_id)
662
def get_revision_graph_with_ghosts(self, revision_ids=None):
664
return self._real_repository.get_revision_graph_with_ghosts(
665
revision_ids=revision_ids)
668
def get_inventory_xml(self, revision_id):
670
return self._real_repository.get_inventory_xml(revision_id)
672
def deserialise_inventory(self, revision_id, xml):
674
return self._real_repository.deserialise_inventory(revision_id, xml)
676
def reconcile(self, other=None, thorough=False):
678
return self._real_repository.reconcile(other=other, thorough=thorough)
680
def all_revision_ids(self):
682
return self._real_repository.all_revision_ids()
685
def get_deltas_for_revisions(self, revisions):
687
return self._real_repository.get_deltas_for_revisions(revisions)
690
def get_revision_delta(self, revision_id):
692
return self._real_repository.get_revision_delta(revision_id)
695
def revision_trees(self, revision_ids):
697
return self._real_repository.revision_trees(revision_ids)
700
def get_revision_reconcile(self, revision_id):
702
return self._real_repository.get_revision_reconcile(revision_id)
705
def check(self, revision_ids):
707
return self._real_repository.check(revision_ids)
709
def copy_content_into(self, destination, revision_id=None):
711
return self._real_repository.copy_content_into(
712
destination, revision_id=revision_id)
714
def _copy_repository_tarball(self, destination, revision_id=None):
715
# get a tarball of the remote repository, and copy from that into the
717
from bzrlib import osutils
720
from StringIO import StringIO
721
# TODO: Maybe a progress bar while streaming the tarball?
722
note("Copying repository content as tarball...")
723
tar_file = self._get_tarball('bz2')
725
tar = tarfile.open('repository', fileobj=tar_file,
727
tmpdir = tempfile.mkdtemp()
729
_extract_tar(tar, tmpdir)
730
tmp_bzrdir = BzrDir.open(tmpdir)
731
tmp_repo = tmp_bzrdir.open_repository()
732
tmp_repo.copy_content_into(destination, revision_id)
734
osutils.rmtree(tmpdir)
737
# TODO: if the server doesn't support this operation, maybe do it the
738
# slow way using the _real_repository?
740
# TODO: Suggestion from john: using external tar is much faster than
741
# python's tarfile library, but it may not work on windows.
745
"""Compress the data within the repository.
747
This is not currently implemented within the smart server.
750
return self._real_repository.pack()
752
def set_make_working_trees(self, new_value):
753
raise NotImplementedError(self.set_make_working_trees)
756
def sign_revision(self, revision_id, gpg_strategy):
758
return self._real_repository.sign_revision(revision_id, gpg_strategy)
761
def get_revisions(self, revision_ids):
763
return self._real_repository.get_revisions(revision_ids)
765
def supports_rich_root(self):
767
return self._real_repository.supports_rich_root()
769
def iter_reverse_revision_history(self, revision_id):
771
return self._real_repository.iter_reverse_revision_history(revision_id)
774
def _serializer(self):
776
return self._real_repository._serializer
778
def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
780
return self._real_repository.store_revision_signature(
781
gpg_strategy, plaintext, revision_id)
783
def has_signature_for_revision_id(self, revision_id):
785
return self._real_repository.has_signature_for_revision_id(revision_id)
787
def get_data_stream(self, revision_ids):
788
path = self.bzrdir._path_for_remote_call(self._client)
789
response, protocol = self._client.call_expecting_body(
790
'Repository.stream_knit_data_for_revisions', path, *revision_ids)
792
if response == ('ok',):
793
#buffer = StringIO(protocol.read_body_bytes())
794
stream = protocol.read_streamed_body()
795
pack_source = PackSource(stream)
796
reader = ContainerReader(pack_source)
797
for record_names, read_bytes in reader.iter_records():
799
# These records should have only one name, and that name
800
# should be a one-element tuple.
801
[name_tuple] = record_names
803
raise errors.SmartProtocolError(
804
'Repository data stream had invalid record name %r'
806
yield name_tuple, read_bytes(None)
808
raise errors.UnexpectedSmartServerResponse(response)
810
def insert_data_stream(self, stream):
812
self._real_repository.insert_data_stream(stream)
814
def item_keys_introduced_by(self, revision_ids, _files_pb=None):
816
return self._real_repository.item_keys_introduced_by(revision_ids,
820
class RemoteBranchLockableFiles(LockableFiles):
821
"""A 'LockableFiles' implementation that talks to a smart server.
823
This is not a public interface class.
826
def __init__(self, bzrdir, _client):
828
self._client = _client
829
self._need_find_modes = True
830
LockableFiles.__init__(
831
self, bzrdir.get_branch_transport(None),
832
'lock', lockdir.LockDir)
834
def _find_modes(self):
835
# RemoteBranches don't let the client set the mode of control files.
836
self._dir_mode = None
837
self._file_mode = None
840
"""'get' a remote path as per the LockableFiles interface.
842
:param path: the file to 'get'. If this is 'branch.conf', we do not
843
just retrieve a file, instead we ask the smart server to generate
844
a configuration for us - which is retrieved as an INI file.
846
if path == 'branch.conf':
847
path = self.bzrdir._path_for_remote_call(self._client)
848
response = self._client.call_expecting_body(
849
'Branch.get_config_file', path)
850
assert response[0][0] == 'ok', \
851
'unexpected response code %s' % (response[0],)
852
return StringIO(response[1].read_body_bytes())
855
return LockableFiles.get(self, path)
858
class RemoteBranchFormat(branch.BranchFormat):
860
def __eq__(self, other):
861
return (isinstance(other, RemoteBranchFormat) and
862
self.__dict__ == other.__dict__)
864
def get_format_description(self):
865
return 'Remote BZR Branch'
867
def get_format_string(self):
868
return 'Remote BZR Branch'
870
def open(self, a_bzrdir):
871
assert isinstance(a_bzrdir, RemoteBzrDir)
872
return a_bzrdir.open_branch()
874
def initialize(self, a_bzrdir):
875
assert isinstance(a_bzrdir, RemoteBzrDir)
876
return a_bzrdir.create_branch()
878
def supports_tags(self):
879
# Remote branches might support tags, but we won't know until we
880
# access the real remote branch.
884
class RemoteBranch(branch.Branch):
885
"""Branch stored on a server accessed by HPSS RPC.
887
At the moment most operations are mapped down to simple file operations.
890
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
892
"""Create a RemoteBranch instance.
894
:param real_branch: An optional local implementation of the branch
895
format, usually accessing the data via the VFS.
896
:param _client: Private parameter for testing.
898
# We intentionally don't call the parent class's __init__, because it
899
# will try to assign to self.tags, which is a property in this subclass.
900
# And the parent's __init__ doesn't do much anyway.
901
self._revision_history_cache = None
902
self.bzrdir = remote_bzrdir
903
if _client is not None:
904
self._client = _client
906
self._client = client._SmartClient(self.bzrdir._shared_medium)
907
self.repository = remote_repository
908
if real_branch is not None:
909
self._real_branch = real_branch
910
# Give the remote repository the matching real repo.
911
real_repo = self._real_branch.repository
912
if isinstance(real_repo, RemoteRepository):
913
real_repo._ensure_real()
914
real_repo = real_repo._real_repository
915
self.repository._set_real_repository(real_repo)
916
# Give the branch the remote repository to let fast-pathing happen.
917
self._real_branch.repository = self.repository
919
self._real_branch = None
920
# Fill out expected attributes of branch for bzrlib api users.
921
self._format = RemoteBranchFormat()
922
self.base = self.bzrdir.root_transport.base
923
self._control_files = None
924
self._lock_mode = None
925
self._lock_token = None
927
self._leave_lock = False
930
return "%s(%s)" % (self.__class__.__name__, self.base)
934
def _ensure_real(self):
935
"""Ensure that there is a _real_branch set.
937
Used before calls to self._real_branch.
939
if not self._real_branch:
940
assert vfs.vfs_enabled()
941
self.bzrdir._ensure_real()
942
self._real_branch = self.bzrdir._real_bzrdir.open_branch()
943
# Give the remote repository the matching real repo.
944
real_repo = self._real_branch.repository
945
if isinstance(real_repo, RemoteRepository):
946
real_repo._ensure_real()
947
real_repo = real_repo._real_repository
948
self.repository._set_real_repository(real_repo)
949
# Give the branch the remote repository to let fast-pathing happen.
950
self._real_branch.repository = self.repository
951
# XXX: deal with _lock_mode == 'w'
952
if self._lock_mode == 'r':
953
self._real_branch.lock_read()
956
def control_files(self):
957
# Defer actually creating RemoteBranchLockableFiles until its needed,
958
# because it triggers an _ensure_real that we otherwise might not need.
959
if self._control_files is None:
960
self._control_files = RemoteBranchLockableFiles(
961
self.bzrdir, self._client)
962
return self._control_files
964
def _get_checkout_format(self):
966
return self._real_branch._get_checkout_format()
968
def get_physical_lock_status(self):
969
"""See Branch.get_physical_lock_status()."""
970
# should be an API call to the server, as branches must be lockable.
972
return self._real_branch.get_physical_lock_status()
975
if not self._lock_mode:
976
self._lock_mode = 'r'
978
if self._real_branch is not None:
979
self._real_branch.lock_read()
981
self._lock_count += 1
983
def _remote_lock_write(self, token):
985
branch_token = repo_token = ''
988
repo_token = self.repository.lock_write()
989
self.repository.unlock()
990
path = self.bzrdir._path_for_remote_call(self._client)
991
response = self._client.call('Branch.lock_write', path, branch_token,
993
if response[0] == 'ok':
994
ok, branch_token, repo_token = response
995
return branch_token, repo_token
996
elif response[0] == 'LockContention':
997
raise errors.LockContention('(remote lock)')
998
elif response[0] == 'TokenMismatch':
999
raise errors.TokenMismatch(token, '(remote token)')
1000
elif response[0] == 'UnlockableTransport':
1001
raise errors.UnlockableTransport(self.bzrdir.root_transport)
1002
elif response[0] == 'ReadOnlyError':
1003
raise errors.ReadOnlyError(self)
1005
raise errors.UnexpectedSmartServerResponse(response)
1007
def lock_write(self, token=None):
1008
if not self._lock_mode:
1009
remote_tokens = self._remote_lock_write(token)
1010
self._lock_token, self._repo_lock_token = remote_tokens
1011
assert self._lock_token, 'Remote server did not return a token!'
1012
# TODO: We really, really, really don't want to call _ensure_real
1013
# here, but it's the easiest way to ensure coherency between the
1014
# state of the RemoteBranch and RemoteRepository objects and the
1015
# physical locks. If we don't materialise the real objects here,
1016
# then getting everything in the right state later is complex, so
1017
# for now we just do it the lazy way.
1018
# -- Andrew Bennetts, 2007-02-22.
1020
if self._real_branch is not None:
1021
self._real_branch.repository.lock_write(
1022
token=self._repo_lock_token)
1024
self._real_branch.lock_write(token=self._lock_token)
1026
self._real_branch.repository.unlock()
1027
if token is not None:
1028
self._leave_lock = True
1030
# XXX: this case seems to be unreachable; token cannot be None.
1031
self._leave_lock = False
1032
self._lock_mode = 'w'
1033
self._lock_count = 1
1034
elif self._lock_mode == 'r':
1035
raise errors.ReadOnlyTransaction
1037
if token is not None:
1038
# A token was given to lock_write, and we're relocking, so check
1039
# that the given token actually matches the one we already have.
1040
if token != self._lock_token:
1041
raise errors.TokenMismatch(token, self._lock_token)
1042
self._lock_count += 1
1043
return self._lock_token
1045
def _unlock(self, branch_token, repo_token):
1046
path = self.bzrdir._path_for_remote_call(self._client)
1047
response = self._client.call('Branch.unlock', path, branch_token,
1049
if response == ('ok',):
1051
elif response[0] == 'TokenMismatch':
1052
raise errors.TokenMismatch(
1053
str((branch_token, repo_token)), '(remote tokens)')
1055
raise errors.UnexpectedSmartServerResponse(response)
1058
self._lock_count -= 1
1059
if not self._lock_count:
1060
self._clear_cached_state()
1061
mode = self._lock_mode
1062
self._lock_mode = None
1063
if self._real_branch is not None:
1064
if not self._leave_lock:
1065
# If this RemoteBranch will remove the physical lock for the
1066
# repository, make sure the _real_branch doesn't do it
1067
# first. (Because the _real_branch's repository is set to
1068
# be the RemoteRepository.)
1069
self._real_branch.repository.leave_lock_in_place()
1070
self._real_branch.unlock()
1072
# Only write-locked branched need to make a remote method call
1073
# to perfom the unlock.
1075
assert self._lock_token, 'Locked, but no token!'
1076
branch_token = self._lock_token
1077
repo_token = self._repo_lock_token
1078
self._lock_token = None
1079
self._repo_lock_token = None
1080
if not self._leave_lock:
1081
self._unlock(branch_token, repo_token)
1083
def break_lock(self):
1085
return self._real_branch.break_lock()
1087
def leave_lock_in_place(self):
1088
self._leave_lock = True
1090
def dont_leave_lock_in_place(self):
1091
self._leave_lock = False
1093
def last_revision_info(self):
1094
"""See Branch.last_revision_info()."""
1095
path = self.bzrdir._path_for_remote_call(self._client)
1096
response = self._client.call('Branch.last_revision_info', path)
1097
assert response[0] == 'ok', 'unexpected response code %s' % (response,)
1098
revno = int(response[1])
1099
last_revision = response[2]
1100
return (revno, last_revision)
1102
def _gen_revision_history(self):
1103
"""See Branch._gen_revision_history()."""
1104
path = self.bzrdir._path_for_remote_call(self._client)
1105
response = self._client.call_expecting_body(
1106
'Branch.revision_history', path)
1107
assert response[0][0] == 'ok', ('unexpected response code %s'
1109
result = response[1].read_body_bytes().split('\x00')
1115
def set_revision_history(self, rev_history):
1116
# Send just the tip revision of the history; the server will generate
1117
# the full history from that. If the revision doesn't exist in this
1118
# branch, NoSuchRevision will be raised.
1119
path = self.bzrdir._path_for_remote_call(self._client)
1120
if rev_history == []:
1123
rev_id = rev_history[-1]
1124
self._clear_cached_state()
1125
response = self._client.call('Branch.set_last_revision',
1126
path, self._lock_token, self._repo_lock_token, rev_id)
1127
if response[0] == 'NoSuchRevision':
1128
raise NoSuchRevision(self, rev_id)
1130
assert response == ('ok',), (
1131
'unexpected response code %r' % (response,))
1132
self._cache_revision_history(rev_history)
1134
def get_parent(self):
1136
return self._real_branch.get_parent()
1138
def set_parent(self, url):
1140
return self._real_branch.set_parent(url)
1142
def get_config(self):
1143
return RemoteBranchConfig(self)
1145
def sprout(self, to_bzrdir, revision_id=None):
1146
# Like Branch.sprout, except that it sprouts a branch in the default
1147
# format, because RemoteBranches can't be created at arbitrary URLs.
1148
# XXX: if to_bzrdir is a RemoteBranch, this should perhaps do
1149
# to_bzrdir.create_branch...
1150
result = branch.BranchFormat.get_default_format().initialize(to_bzrdir)
1151
self.copy_content_into(result, revision_id=revision_id)
1152
result.set_parent(self.bzrdir.root_transport.base)
1156
def pull(self, source, overwrite=False, stop_revision=None,
1158
# FIXME: This asks the real branch to run the hooks, which means
1159
# they're called with the wrong target branch parameter.
1160
# The test suite specifically allows this at present but it should be
1161
# fixed. It should get a _override_hook_target branch,
1162
# as push does. -- mbp 20070405
1164
self._real_branch.pull(
1165
source, overwrite=overwrite, stop_revision=stop_revision,
1169
def push(self, target, overwrite=False, stop_revision=None):
1171
return self._real_branch.push(
1172
target, overwrite=overwrite, stop_revision=stop_revision,
1173
_override_hook_source_branch=self)
1175
def is_locked(self):
1176
return self._lock_count >= 1
1178
def set_last_revision_info(self, revno, revision_id):
1180
self._clear_cached_state()
1181
return self._real_branch.set_last_revision_info(revno, revision_id)
1183
def generate_revision_history(self, revision_id, last_rev=None,
1186
return self._real_branch.generate_revision_history(
1187
revision_id, last_rev=last_rev, other_branch=other_branch)
1192
return self._real_branch.tags
1194
def set_push_location(self, location):
1196
return self._real_branch.set_push_location(location)
1198
def update_revisions(self, other, stop_revision=None):
1200
return self._real_branch.update_revisions(
1201
other, stop_revision=stop_revision)
1204
class RemoteBranchConfig(BranchConfig):
1207
self.branch._ensure_real()
1208
return self.branch._real_branch.get_config().username()
1210
def _get_branch_data_config(self):
1211
self.branch._ensure_real()
1212
if self._branch_data_config is None:
1213
self._branch_data_config = TreeConfig(self.branch._real_branch)
1214
return self._branch_data_config
1217
def _extract_tar(tar, to_dir):
1218
"""Extract all the contents of a tarfile object.
1220
A replacement for extractall, which is not present in python2.4
1223
tar.extract(tarinfo, to_dir)