1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
30
repository as _mod_repository,
31
32
revision as _mod_revision,
34
36
from bzrlib.branch import BranchReferenceFormat
35
37
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
36
from bzrlib.decorators import needs_read_lock, needs_write_lock
38
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
37
39
from bzrlib.errors import (
39
41
SmartProtocolError,
89
91
class RemoteBzrDir(BzrDir, _RpcHelper):
90
92
"""Control directory on a remote server, accessed via bzr:// or similar."""
92
def __init__(self, transport, format, _client=None):
94
def __init__(self, transport, format, _client=None, _force_probe=False):
93
95
"""Construct a RemoteBzrDir.
95
97
:param _client: Private parameter for testing. Disables probing and the
99
101
# this object holds a delegated bzrdir that uses file-level operations
100
102
# to talk to the other side
101
103
self._real_bzrdir = None
104
self._has_working_tree = None
102
105
# 1-shot cache for the call pattern 'create_branch; open_branch' - see
103
106
# create_branch for details.
104
107
self._next_open_branch_result = None
108
111
self._client = client._SmartClient(medium)
110
113
self._client = _client
120
return '%s(%r)' % (self.__class__.__name__, self._client)
122
def _probe_bzrdir(self):
123
medium = self._client._medium
113
124
path = self._path_for_remote_call(self._client)
125
if medium._is_remote_before((2, 1)):
129
self._rpc_open_2_1(path)
131
except errors.UnknownSmartMethod:
132
medium._remember_remote_is_before((2, 1))
135
def _rpc_open_2_1(self, path):
136
response = self._call('BzrDir.open_2.1', path)
137
if response == ('no',):
138
raise errors.NotBranchError(path=self.root_transport.base)
139
elif response[0] == 'yes':
140
if response[1] == 'yes':
141
self._has_working_tree = True
142
elif response[1] == 'no':
143
self._has_working_tree = False
145
raise errors.UnexpectedSmartServerResponse(response)
147
raise errors.UnexpectedSmartServerResponse(response)
149
def _rpc_open(self, path):
114
150
response = self._call('BzrDir.open', path)
115
151
if response not in [('yes',), ('no',)]:
116
152
raise errors.UnexpectedSmartServerResponse(response)
117
153
if response == ('no',):
118
raise errors.NotBranchError(path=transport.base)
154
raise errors.NotBranchError(path=self.root_transport.base)
120
156
def _ensure_real(self):
121
157
"""Ensure that there is a _real_bzrdir set.
123
159
Used before calls to self._real_bzrdir.
125
161
if not self._real_bzrdir:
162
if 'hpssvfs' in debug.debug_flags:
164
warning('VFS BzrDir access triggered\n%s',
165
''.join(traceback.format_stack()))
126
166
self._real_bzrdir = BzrDir.open_from_transport(
127
167
self.root_transport, _server_formats=False)
128
168
self._format._network_name = \
204
244
self._ensure_real()
205
245
self._real_bzrdir.destroy_repository()
207
def create_branch(self):
247
def create_branch(self, name=None):
208
248
# as per meta1 formats - just delegate to the format object which may
209
249
# be parameterised.
210
real_branch = self._format.get_branch_format().initialize(self)
250
real_branch = self._format.get_branch_format().initialize(self,
211
252
if not isinstance(real_branch, RemoteBranch):
212
result = RemoteBranch(self, self.find_repository(), real_branch)
253
result = RemoteBranch(self, self.find_repository(), real_branch,
214
256
result = real_branch
215
257
# BzrDir.clone_on_transport() uses the result of create_branch but does
221
263
self._next_open_branch_result = result
224
def destroy_branch(self):
266
def destroy_branch(self, name=None):
225
267
"""See BzrDir.destroy_branch"""
226
268
self._ensure_real()
227
self._real_bzrdir.destroy_branch()
269
self._real_bzrdir.destroy_branch(name=name)
228
270
self._next_open_branch_result = None
230
272
def create_workingtree(self, revision_id=None, from_branch=None):
249
291
def _get_branch_reference(self):
250
292
path = self._path_for_remote_call(self._client)
251
293
medium = self._client._medium
252
if not medium._is_remote_before((1, 13)):
295
('BzrDir.open_branchV3', (2, 1)),
296
('BzrDir.open_branchV2', (1, 13)),
297
('BzrDir.open_branch', None),
299
for verb, required_version in candidate_calls:
300
if required_version and medium._is_remote_before(required_version):
254
response = self._call('BzrDir.open_branchV2', path)
255
if response[0] not in ('ref', 'branch'):
256
raise errors.UnexpectedSmartServerResponse(response)
303
response = self._call(verb, path)
258
304
except errors.UnknownSmartMethod:
259
medium._remember_remote_is_before((1, 13))
260
response = self._call('BzrDir.open_branch', path)
261
if response[0] != 'ok':
305
if required_version is None:
307
medium._remember_remote_is_before(required_version)
310
if verb == 'BzrDir.open_branch':
311
if response[0] != 'ok':
312
raise errors.UnexpectedSmartServerResponse(response)
313
if response[1] != '':
314
return ('ref', response[1])
316
return ('branch', '')
317
if response[0] not in ('ref', 'branch'):
262
318
raise errors.UnexpectedSmartServerResponse(response)
263
if response[1] != '':
264
return ('ref', response[1])
266
return ('branch', '')
268
321
def _get_tree_branch(self):
269
322
"""See BzrDir._get_tree_branch()."""
270
323
return None, self.open_branch()
272
def open_branch(self, _unsupported=False, ignore_fallbacks=False):
325
def open_branch(self, name=None, unsupported=False,
326
ignore_fallbacks=False):
274
328
raise NotImplementedError('unsupported flag support not implemented yet.')
275
329
if self._next_open_branch_result is not None:
276
330
# See create_branch for details.
281
335
if response[0] == 'ref':
282
336
# a branch reference, use the existing BranchReference logic.
283
337
format = BranchReferenceFormat()
284
return format.open(self, _found=True, location=response[1],
285
ignore_fallbacks=ignore_fallbacks)
338
return format.open(self, name=name, _found=True,
339
location=response[1], ignore_fallbacks=ignore_fallbacks)
286
340
branch_format_name = response[1]
287
341
if not branch_format_name:
288
342
branch_format_name = None
289
343
format = RemoteBranchFormat(network_name=branch_format_name)
290
344
return RemoteBranch(self, self.find_repository(), format=format,
291
setup_stacking=not ignore_fallbacks)
345
setup_stacking=not ignore_fallbacks, name=name)
293
347
def _open_repo_v1(self, path):
294
348
verb = 'BzrDir.find_repository'
356
410
raise errors.NoRepositoryPresent(self)
412
def has_workingtree(self):
413
if self._has_working_tree is None:
415
self._has_working_tree = self._real_bzrdir.has_workingtree()
416
return self._has_working_tree
358
418
def open_workingtree(self, recommend_upgrade=True):
360
if self._real_bzrdir.has_workingtree():
419
if self.has_workingtree():
361
420
raise errors.NotLocalUrl(self.root_transport)
363
422
raise errors.NoWorkingTree(self.root_transport.base)
366
425
"""Return the path to be used for this bzrdir in a remote call."""
367
426
return client.remote_path_from_transport(self.root_transport)
369
def get_branch_transport(self, branch_format):
428
def get_branch_transport(self, branch_format, name=None):
370
429
self._ensure_real()
371
return self._real_bzrdir.get_branch_transport(branch_format)
430
return self._real_bzrdir.get_branch_transport(branch_format, name=name)
373
432
def get_repository_transport(self, repository_format):
374
433
self._ensure_real()
583
643
return self._custom_format._serializer
586
class RemoteRepository(_RpcHelper):
646
class RemoteRepository(_RpcHelper, lock._RelockDebugMixin,
647
bzrdir.ControlComponent):
587
648
"""Repository accessed over rpc.
589
650
For the moment most operations are performed using local transport-backed
632
693
# Additional places to query for data.
633
694
self._fallback_repositories = []
697
def user_transport(self):
698
return self.bzrdir.user_transport
701
def control_transport(self):
702
# XXX: Normally you shouldn't directly get at the remote repository
703
# transport, but I'm not sure it's worth making this method
704
# optional -- mbp 2010-04-21
705
return self.bzrdir.get_repository_transport(None)
635
707
def __str__(self):
636
708
return "%s(%s)" % (self.__class__.__name__, self.base)
845
917
parents_provider = self._make_parents_provider(other_repository)
846
918
return graph.Graph(parents_provider)
921
def get_known_graph_ancestry(self, revision_ids):
922
"""Return the known graph for a set of revision ids and their ancestors.
924
st = static_tuple.StaticTuple
925
revision_keys = [st(r_id).intern() for r_id in revision_ids]
926
known_graph = self.revisions.get_known_graph_ancestry(revision_keys)
927
return graph.GraphThunkIdsToKeys(known_graph)
848
929
def gather_stats(self, revid=None, committers=None):
849
930
"""See Repository.gather_stats()."""
850
931
path = self.bzrdir._path_for_remote_call(self._client)
910
991
def is_write_locked(self):
911
992
return self._lock_mode == 'w'
994
def _warn_if_deprecated(self, branch=None):
995
# If we have a real repository, the check will be done there, if we
996
# don't the check will be done remotely.
913
999
def lock_read(self):
914
1000
# wrong eventually - want a local lock cache context
915
1001
if not self._lock_mode:
1002
self._note_lock('r')
916
1003
self._lock_mode = 'r'
917
1004
self._lock_count = 1
918
1005
self._unstacked_provider.enable_cache(cache_misses=True)
1151
1240
# state, so always add a lock here. If a caller passes us a locked
1152
1241
# repository, they are responsible for unlocking it later.
1153
1242
repository.lock_read()
1243
self._check_fallback_repository(repository)
1154
1244
self._fallback_repositories.append(repository)
1155
1245
# If self._real_repository was parameterised already (e.g. because a
1156
1246
# _real_branch had its get_stacked_on_url method called), then the
1157
1247
# repository to be added may already be in the _real_repositories list.
1158
1248
if self._real_repository is not None:
1159
fallback_locations = [repo.bzrdir.root_transport.base for repo in
1249
fallback_locations = [repo.user_url for repo in
1160
1250
self._real_repository._fallback_repositories]
1161
if repository.bzrdir.root_transport.base not in fallback_locations:
1251
if repository.user_url not in fallback_locations:
1162
1252
self._real_repository.add_fallback_repository(repository)
1254
def _check_fallback_repository(self, repository):
1255
"""Check that this repository can fallback to repository safely.
1257
Raise an error if not.
1259
:param repository: A repository to fallback to.
1261
return _mod_repository.InterRepository._assert_same_model(
1164
1264
def add_inventory(self, revid, inv, parents):
1165
1265
self._ensure_real()
1166
1266
return self._real_repository.add_inventory(revid, inv, parents)
1168
1268
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1269
parents, basis_inv=None, propagate_caches=False):
1170
1270
self._ensure_real()
1171
1271
return self._real_repository.add_inventory_by_delta(basis_revision_id,
1172
delta, new_revision_id, parents)
1272
delta, new_revision_id, parents, basis_inv=basis_inv,
1273
propagate_caches=propagate_caches)
1174
1275
def add_revision(self, rev_id, rev, inv=None, config=None):
1175
1276
self._ensure_real()
1434
1535
return self._real_repository.get_signature_text(revision_id)
1436
1537
@needs_read_lock
1437
def get_inventory_xml(self, revision_id):
1439
return self._real_repository.get_inventory_xml(revision_id)
1441
def deserialise_inventory(self, revision_id, xml):
1443
return self._real_repository.deserialise_inventory(revision_id, xml)
1538
def _get_inventory_xml(self, revision_id):
1540
return self._real_repository._get_inventory_xml(revision_id)
1445
1542
def reconcile(self, other=None, thorough=False):
1446
1543
self._ensure_real()
1522
1619
return self._real_repository.inventories
1524
1621
@needs_write_lock
1525
def pack(self, hint=None):
1622
def pack(self, hint=None, clean_obsolete_packs=False):
1526
1623
"""Compress the data within the repository.
1528
1625
This is not currently implemented within the smart server.
1530
1627
self._ensure_real()
1531
return self._real_repository.pack(hint=hint)
1628
return self._real_repository.pack(hint=hint, clean_obsolete_packs=clean_obsolete_packs)
1534
1631
def revisions(self):
1956
2053
self._network_name)
1958
2055
def get_format_description(self):
1959
return 'Remote BZR Branch'
2057
return 'Remote: ' + self._custom_format.get_format_description()
1961
2059
def network_name(self):
1962
2060
return self._network_name
1964
def open(self, a_bzrdir, ignore_fallbacks=False):
1965
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2062
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2063
return a_bzrdir.open_branch(name=name,
2064
ignore_fallbacks=ignore_fallbacks)
1967
def _vfs_initialize(self, a_bzrdir):
2066
def _vfs_initialize(self, a_bzrdir, name):
1968
2067
# Initialisation when using a local bzrdir object, or a non-vfs init
1969
2068
# method is not available on the server.
1970
2069
# self._custom_format is always set - the start of initialize ensures
1972
2071
if isinstance(a_bzrdir, RemoteBzrDir):
1973
2072
a_bzrdir._ensure_real()
1974
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2073
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
1976
2076
# We assume the bzrdir is parameterised; it may not be.
1977
result = self._custom_format.initialize(a_bzrdir)
2077
result = self._custom_format.initialize(a_bzrdir, name)
1978
2078
if (isinstance(a_bzrdir, RemoteBzrDir) and
1979
2079
not isinstance(result, RemoteBranch)):
1980
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2080
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
1983
def initialize(self, a_bzrdir):
2084
def initialize(self, a_bzrdir, name=None):
1984
2085
# 1) get the network name to use.
1985
2086
if self._custom_format:
1986
2087
network_name = self._custom_format.network_name()
1992
2093
network_name = reference_format.network_name()
1993
2094
# Being asked to create on a non RemoteBzrDir:
1994
2095
if not isinstance(a_bzrdir, RemoteBzrDir):
1995
return self._vfs_initialize(a_bzrdir)
2096
return self._vfs_initialize(a_bzrdir, name=name)
1996
2097
medium = a_bzrdir._client._medium
1997
2098
if medium._is_remote_before((1, 13)):
1998
return self._vfs_initialize(a_bzrdir)
2099
return self._vfs_initialize(a_bzrdir, name=name)
1999
2100
# Creating on a remote bzr dir.
2000
2101
# 2) try direct creation via RPC
2001
2102
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2103
if name is not None:
2104
# XXX JRV20100304: Support creating colocated branches
2105
raise errors.NoColocatedBranchSupport(self)
2002
2106
verb = 'BzrDir.create_branch'
2004
2108
response = a_bzrdir._call(verb, path, network_name)
2005
2109
except errors.UnknownSmartMethod:
2006
2110
# Fallback - use vfs methods
2007
2111
medium._remember_remote_is_before((1, 13))
2008
return self._vfs_initialize(a_bzrdir)
2112
return self._vfs_initialize(a_bzrdir, name=name)
2009
2113
if response[0] != 'ok':
2010
2114
raise errors.UnexpectedSmartServerResponse(response)
2011
2115
# Turn the response into a RemoteRepository object.
2019
2123
a_bzrdir._client)
2020
2124
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2021
2125
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2022
format=format, setup_stacking=False)
2126
format=format, setup_stacking=False, name=name)
2023
2127
# XXX: We know this is a new branch, so it must have revno 0, revid
2024
2128
# NULL_REVISION. Creating the branch locked would make this be unable
2025
2129
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2045
2149
return self._custom_format.supports_set_append_revisions_only()
2048
class RemoteBranch(branch.Branch, _RpcHelper):
2152
class RemoteBranch(branch.Branch, _RpcHelper, lock._RelockDebugMixin):
2049
2153
"""Branch stored on a server accessed by HPSS RPC.
2051
2155
At the moment most operations are mapped down to simple file operations.
2054
2158
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
2055
_client=None, format=None, setup_stacking=True):
2159
_client=None, format=None, setup_stacking=True, name=None):
2056
2160
"""Create a RemoteBranch instance.
2058
2162
:param real_branch: An optional local implementation of the branch
2064
2168
:param setup_stacking: If True make an RPC call to determine the
2065
2169
stacked (or not) status of the branch. If False assume the branch
2066
2170
is not stacked.
2171
:param name: Colocated branch name
2068
2173
# We intentionally don't call the parent class's __init__, because it
2069
2174
# will try to assign to self.tags, which is a property in this subclass.
2088
2193
self._real_branch = None
2089
2194
# Fill out expected attributes of branch for bzrlib API users.
2090
2195
self._clear_cached_state()
2091
self.base = self.bzrdir.root_transport.base
2196
# TODO: deprecate self.base in favor of user_url
2197
self.base = self.bzrdir.user_url
2092
2199
self._control_files = None
2093
2200
self._lock_mode = None
2094
2201
self._lock_token = None
2159
2266
'to use vfs implementation')
2160
2267
self.bzrdir._ensure_real()
2161
2268
self._real_branch = self.bzrdir._real_bzrdir.open_branch(
2162
ignore_fallbacks=self._real_ignore_fallbacks)
2269
ignore_fallbacks=self._real_ignore_fallbacks, name=self._name)
2163
2270
if self.repository._real_repository is None:
2164
2271
# Give the remote repository the matching real repo.
2165
2272
real_repo = self._real_branch.repository
2770
2880
raise NoSuchRevision(find('branch'), err.error_args[0])
2771
2881
elif err.error_verb == 'nosuchrevision':
2772
2882
raise NoSuchRevision(find('repository'), err.error_args[0])
2773
elif err.error_tuple == ('nobranch',):
2774
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2883
elif err.error_verb == 'nobranch':
2884
if len(err.error_args) >= 1:
2885
extra = err.error_args[0]
2888
raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2775
2890
elif err.error_verb == 'norepository':
2776
2891
raise errors.NoRepositoryPresent(find('bzrdir'))
2777
2892
elif err.error_verb == 'LockContention':