76
65
method, args, body_bytes)
77
66
except errors.ErrorFromSmartServer, err:
78
67
self._translate_error(err, **err_context)
81
def response_tuple_to_repo_format(response):
82
"""Convert a response tuple describing a repository format to a format."""
83
format = RemoteRepositoryFormat()
84
format._rich_root_data = (response[0] == 'yes')
85
format._supports_tree_reference = (response[1] == 'yes')
86
format._supports_external_lookups = (response[2] == 'yes')
87
format._network_name = response[3]
91
69
# Note: RemoteBzrDirFormat is in bzrdir.py
93
71
class RemoteBzrDir(BzrDir, _RpcHelper):
94
72
"""Control directory on a remote server, accessed via bzr:// or similar."""
96
def __init__(self, transport, format, _client=None, _force_probe=False):
74
def __init__(self, transport, _client=None):
97
75
"""Construct a RemoteBzrDir.
99
77
:param _client: Private parameter for testing. Disables probing and the
100
78
use of a real bzrdir.
102
BzrDir.__init__(self, transport, format)
80
BzrDir.__init__(self, transport, RemoteBzrDirFormat())
103
81
# this object holds a delegated bzrdir that uses file-level operations
104
82
# to talk to the other side
105
83
self._real_bzrdir = None
106
self._has_working_tree = None
107
# 1-shot cache for the call pattern 'create_branch; open_branch' - see
108
# create_branch for details.
109
self._next_open_branch_result = None
111
85
if _client is None:
112
86
medium = transport.get_smart_medium()
113
87
self._client = client._SmartClient(medium)
115
89
self._client = _client
122
return '%s(%r)' % (self.__class__.__name__, self._client)
124
def _probe_bzrdir(self):
125
medium = self._client._medium
126
92
path = self._path_for_remote_call(self._client)
127
if medium._is_remote_before((2, 1)):
131
self._rpc_open_2_1(path)
133
except errors.UnknownSmartMethod:
134
medium._remember_remote_is_before((2, 1))
137
def _rpc_open_2_1(self, path):
138
response = self._call('BzrDir.open_2.1', path)
139
if response == ('no',):
140
raise errors.NotBranchError(path=self.root_transport.base)
141
elif response[0] == 'yes':
142
if response[1] == 'yes':
143
self._has_working_tree = True
144
elif response[1] == 'no':
145
self._has_working_tree = False
147
raise errors.UnexpectedSmartServerResponse(response)
149
raise errors.UnexpectedSmartServerResponse(response)
151
def _rpc_open(self, path):
152
93
response = self._call('BzrDir.open', path)
153
94
if response not in [('yes',), ('no',)]:
154
95
raise errors.UnexpectedSmartServerResponse(response)
155
96
if response == ('no',):
156
raise errors.NotBranchError(path=self.root_transport.base)
97
raise errors.NotBranchError(path=transport.base)
158
99
def _ensure_real(self):
159
100
"""Ensure that there is a _real_bzrdir set.
161
102
Used before calls to self._real_bzrdir.
163
104
if not self._real_bzrdir:
164
if 'hpssvfs' in debug.debug_flags:
166
warning('VFS BzrDir access triggered\n%s',
167
''.join(traceback.format_stack()))
168
105
self._real_bzrdir = BzrDir.open_from_transport(
169
106
self.root_transport, _server_formats=False)
170
self._format._network_name = \
171
self._real_bzrdir._format.network_name()
173
108
def _translate_error(self, err, **context):
174
109
_translate_error(err, bzrdir=self, **context)
176
def break_lock(self):
177
# Prevent aliasing problems in the next_open_branch_result cache.
178
# See create_branch for rationale.
179
self._next_open_branch_result = None
180
return BzrDir.break_lock(self)
182
def _vfs_cloning_metadir(self, require_stacking=False):
111
def cloning_metadir(self, stacked=False):
183
112
self._ensure_real()
184
return self._real_bzrdir.cloning_metadir(
185
require_stacking=require_stacking)
187
def cloning_metadir(self, require_stacking=False):
188
medium = self._client._medium
189
if medium._is_remote_before((1, 13)):
190
return self._vfs_cloning_metadir(require_stacking=require_stacking)
191
verb = 'BzrDir.cloning_metadir'
196
path = self._path_for_remote_call(self._client)
198
response = self._call(verb, path, stacking)
199
except errors.UnknownSmartMethod:
200
medium._remember_remote_is_before((1, 13))
201
return self._vfs_cloning_metadir(require_stacking=require_stacking)
202
except errors.UnknownErrorFromSmartServer, err:
203
if err.error_tuple != ('BranchReference',):
205
# We need to resolve the branch reference to determine the
206
# cloning_metadir. This causes unnecessary RPCs to open the
207
# referenced branch (and bzrdir, etc) but only when the caller
208
# didn't already resolve the branch reference.
209
referenced_branch = self.open_branch()
210
return referenced_branch.bzrdir.cloning_metadir()
211
if len(response) != 3:
212
raise errors.UnexpectedSmartServerResponse(response)
213
control_name, repo_name, branch_info = response
214
if len(branch_info) != 2:
215
raise errors.UnexpectedSmartServerResponse(response)
216
branch_ref, branch_name = branch_info
217
format = bzrdir.network_format_registry.get(control_name)
219
format.repository_format = repository.network_format_registry.get(
221
if branch_ref == 'ref':
222
# XXX: we need possible_transports here to avoid reopening the
223
# connection to the referenced location
224
ref_bzrdir = BzrDir.open(branch_name)
225
branch_format = ref_bzrdir.cloning_metadir().get_branch_format()
226
format.set_branch_format(branch_format)
227
elif branch_ref == 'branch':
229
format.set_branch_format(
230
branch.network_format_registry.get(branch_name))
232
raise errors.UnexpectedSmartServerResponse(response)
113
return self._real_bzrdir.cloning_metadir(stacked)
235
115
def create_repository(self, shared=False):
236
# as per meta1 formats - just delegate to the format object which may
238
result = self._format.repository_format.initialize(self, shared)
239
if not isinstance(result, RemoteRepository):
240
return self.open_repository()
117
self._real_bzrdir.create_repository(shared=shared)
118
return self.open_repository()
244
120
def destroy_repository(self):
245
121
"""See BzrDir.destroy_repository"""
246
122
self._ensure_real()
247
123
self._real_bzrdir.destroy_repository()
249
def create_branch(self, name=None):
250
# as per meta1 formats - just delegate to the format object which may
252
real_branch = self._format.get_branch_format().initialize(self,
254
if not isinstance(real_branch, RemoteBranch):
255
result = RemoteBranch(self, self.find_repository(), real_branch,
259
# BzrDir.clone_on_transport() uses the result of create_branch but does
260
# not return it to its callers; we save approximately 8% of our round
261
# trips by handing the branch we created back to the first caller to
262
# open_branch rather than probing anew. Long term we need a API in
263
# bzrdir that doesn't discard result objects (like result_branch).
265
self._next_open_branch_result = result
125
def create_branch(self):
127
real_branch = self._real_bzrdir.create_branch()
128
return RemoteBranch(self, self.find_repository(), real_branch)
268
def destroy_branch(self, name=None):
130
def destroy_branch(self):
269
131
"""See BzrDir.destroy_branch"""
270
132
self._ensure_real()
271
self._real_bzrdir.destroy_branch(name=name)
272
self._next_open_branch_result = None
133
self._real_bzrdir.destroy_branch()
274
135
def create_workingtree(self, revision_id=None, from_branch=None):
275
136
raise errors.NotLocalUrl(self.transport.base)
277
def find_branch_format(self, name=None):
138
def find_branch_format(self):
278
139
"""Find the branch 'format' for this bzrdir.
280
141
This might be a synthetic object for e.g. RemoteBranch and SVN.
282
b = self.open_branch(name=name)
143
b = self.open_branch()
285
def get_branch_reference(self, name=None):
146
def get_branch_reference(self):
286
147
"""See BzrDir.get_branch_reference()."""
288
# XXX JRV20100304: Support opening colocated branches
289
raise errors.NoColocatedBranchSupport(self)
290
response = self._get_branch_reference()
291
if response[0] == 'ref':
148
path = self._path_for_remote_call(self._client)
149
response = self._call('BzrDir.open_branch', path)
150
if response[0] == 'ok':
151
if response[1] == '':
152
# branch at this location.
155
# a branch reference, use the existing BranchReference logic.
296
def _get_branch_reference(self):
297
path = self._path_for_remote_call(self._client)
298
medium = self._client._medium
300
('BzrDir.open_branchV3', (2, 1)),
301
('BzrDir.open_branchV2', (1, 13)),
302
('BzrDir.open_branch', None),
304
for verb, required_version in candidate_calls:
305
if required_version and medium._is_remote_before(required_version):
308
response = self._call(verb, path)
309
except errors.UnknownSmartMethod:
310
if required_version is None:
312
medium._remember_remote_is_before(required_version)
315
if verb == 'BzrDir.open_branch':
316
if response[0] != 'ok':
317
raise errors.UnexpectedSmartServerResponse(response)
318
if response[1] != '':
319
return ('ref', response[1])
321
return ('branch', '')
322
if response[0] not in ('ref', 'branch'):
323
158
raise errors.UnexpectedSmartServerResponse(response)
326
def _get_tree_branch(self, name=None):
160
def _get_tree_branch(self):
327
161
"""See BzrDir._get_tree_branch()."""
328
return None, self.open_branch(name=name)
162
return None, self.open_branch()
330
def open_branch(self, name=None, unsupported=False,
331
ignore_fallbacks=False):
164
def open_branch(self, _unsupported=False):
333
166
raise NotImplementedError('unsupported flag support not implemented yet.')
334
if self._next_open_branch_result is not None:
335
# See create_branch for details.
336
result = self._next_open_branch_result
337
self._next_open_branch_result = None
339
response = self._get_branch_reference()
340
if response[0] == 'ref':
167
reference_url = self.get_branch_reference()
168
if reference_url is None:
169
# branch at this location.
170
return RemoteBranch(self, self.find_repository())
341
172
# a branch reference, use the existing BranchReference logic.
342
173
format = BranchReferenceFormat()
343
return format.open(self, name=name, _found=True,
344
location=response[1], ignore_fallbacks=ignore_fallbacks)
345
branch_format_name = response[1]
346
if not branch_format_name:
347
branch_format_name = None
348
format = RemoteBranchFormat(network_name=branch_format_name)
349
return RemoteBranch(self, self.find_repository(), format=format,
350
setup_stacking=not ignore_fallbacks, name=name)
352
def _open_repo_v1(self, path):
353
verb = 'BzrDir.find_repository'
354
response = self._call(verb, path)
355
if response[0] != 'ok':
356
raise errors.UnexpectedSmartServerResponse(response)
357
# servers that only support the v1 method don't support external
360
repo = self._real_bzrdir.open_repository()
361
response = response + ('no', repo._format.network_name())
362
return response, repo
364
def _open_repo_v2(self, path):
174
return format.open(self, _found=True, location=reference_url)
176
def open_repository(self):
177
path = self._path_for_remote_call(self._client)
365
178
verb = 'BzrDir.find_repositoryV2'
366
response = self._call(verb, path)
367
if response[0] != 'ok':
368
raise errors.UnexpectedSmartServerResponse(response)
370
repo = self._real_bzrdir.open_repository()
371
response = response + (repo._format.network_name(),)
372
return response, repo
374
def _open_repo_v3(self, path):
375
verb = 'BzrDir.find_repositoryV3'
376
medium = self._client._medium
377
if medium._is_remote_before((1, 13)):
378
raise errors.UnknownSmartMethod(verb)
380
180
response = self._call(verb, path)
381
181
except errors.UnknownSmartMethod:
382
medium._remember_remote_is_before((1, 13))
384
if response[0] != 'ok':
385
raise errors.UnexpectedSmartServerResponse(response)
386
return response, None
388
def open_repository(self):
389
path = self._path_for_remote_call(self._client)
391
for probe in [self._open_repo_v3, self._open_repo_v2,
394
response, real_repo = probe(path)
396
except errors.UnknownSmartMethod:
399
raise errors.UnknownSmartMethod('BzrDir.find_repository{3,2,}')
400
if response[0] != 'ok':
401
raise errors.UnexpectedSmartServerResponse(response)
402
if len(response) != 6:
182
verb = 'BzrDir.find_repository'
183
response = self._call(verb, path)
184
if response[0] != 'ok':
185
raise errors.UnexpectedSmartServerResponse(response)
186
if verb == 'BzrDir.find_repository':
187
# servers that don't support the V2 method don't support external
189
response = response + ('no', )
190
if not (len(response) == 5):
403
191
raise SmartProtocolError('incorrect response length %s' % (response,))
404
192
if response[1] == '':
405
# repo is at this dir.
406
format = response_tuple_to_repo_format(response[2:])
193
format = RemoteRepositoryFormat()
194
format.rich_root_data = (response[2] == 'yes')
195
format.supports_tree_reference = (response[3] == 'yes')
196
# No wire format to check this yet.
197
format.supports_external_lookups = (response[4] == 'yes')
407
198
# Used to support creating a real format instance when needed.
408
199
format._creating_bzrdir = self
409
remote_repo = RemoteRepository(self, format)
410
format._creating_repo = remote_repo
411
if real_repo is not None:
412
remote_repo._set_real_repository(real_repo)
200
return RemoteRepository(self, format)
415
202
raise errors.NoRepositoryPresent(self)
417
def has_workingtree(self):
418
if self._has_working_tree is None:
420
self._has_working_tree = self._real_bzrdir.has_workingtree()
421
return self._has_working_tree
423
204
def open_workingtree(self, recommend_upgrade=True):
424
if self.has_workingtree():
206
if self._real_bzrdir.has_workingtree():
425
207
raise errors.NotLocalUrl(self.root_transport)
427
209
raise errors.NoWorkingTree(self.root_transport.base)
474
257
the attributes rich_root_data and supports_tree_reference are set
475
258
on a per instance basis, and are not set (and should not be) at
478
:ivar _custom_format: If set, a specific concrete repository format that
479
will be used when initializing a repository with this
480
RemoteRepositoryFormat.
481
:ivar _creating_repo: If set, the repository object that this
482
RemoteRepositoryFormat was created for: it can be called into
483
to obtain data like the network name.
486
262
_matchingbzrdir = RemoteBzrDirFormat()
489
repository.RepositoryFormat.__init__(self)
490
self._custom_format = None
491
self._network_name = None
492
self._creating_bzrdir = None
493
self._supports_chks = None
494
self._supports_external_lookups = None
495
self._supports_tree_reference = None
496
self._rich_root_data = None
499
return "%s(_network_name=%r)" % (self.__class__.__name__,
503
def fast_deltas(self):
505
return self._custom_format.fast_deltas
508
def rich_root_data(self):
509
if self._rich_root_data is None:
511
self._rich_root_data = self._custom_format.rich_root_data
512
return self._rich_root_data
515
def supports_chks(self):
516
if self._supports_chks is None:
518
self._supports_chks = self._custom_format.supports_chks
519
return self._supports_chks
522
def supports_external_lookups(self):
523
if self._supports_external_lookups is None:
525
self._supports_external_lookups = \
526
self._custom_format.supports_external_lookups
527
return self._supports_external_lookups
530
def supports_tree_reference(self):
531
if self._supports_tree_reference is None:
533
self._supports_tree_reference = \
534
self._custom_format.supports_tree_reference
535
return self._supports_tree_reference
537
def _vfs_initialize(self, a_bzrdir, shared):
538
"""Helper for common code in initialize."""
539
if self._custom_format:
540
# Custom format requested
541
result = self._custom_format.initialize(a_bzrdir, shared=shared)
542
elif self._creating_bzrdir is not None:
543
# Use the format that the repository we were created to back
264
def initialize(self, a_bzrdir, shared=False):
265
if not isinstance(a_bzrdir, RemoteBzrDir):
545
266
prior_repo = self._creating_bzrdir.open_repository()
546
267
prior_repo._ensure_real()
547
result = prior_repo._real_repository._format.initialize(
268
return prior_repo._real_repository._format.initialize(
548
269
a_bzrdir, shared=shared)
550
# assume that a_bzr is a RemoteBzrDir but the smart server didn't
551
# support remote initialization.
552
# We delegate to a real object at this point (as RemoteBzrDir
553
# delegate to the repository format which would lead to infinite
554
# recursion if we just called a_bzrdir.create_repository.
555
a_bzrdir._ensure_real()
556
result = a_bzrdir._real_bzrdir.create_repository(shared=shared)
557
if not isinstance(result, RemoteRepository):
558
return self.open(a_bzrdir)
562
def initialize(self, a_bzrdir, shared=False):
563
# Being asked to create on a non RemoteBzrDir:
564
if not isinstance(a_bzrdir, RemoteBzrDir):
565
return self._vfs_initialize(a_bzrdir, shared)
566
medium = a_bzrdir._client._medium
567
if medium._is_remote_before((1, 13)):
568
return self._vfs_initialize(a_bzrdir, shared)
569
# Creating on a remote bzr dir.
570
# 1) get the network name to use.
571
if self._custom_format:
572
network_name = self._custom_format.network_name()
573
elif self._network_name:
574
network_name = self._network_name
576
# Select the current bzrlib default and ask for that.
577
reference_bzrdir_format = bzrdir.format_registry.get('default')()
578
reference_format = reference_bzrdir_format.repository_format
579
network_name = reference_format.network_name()
580
# 2) try direct creation via RPC
581
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
582
verb = 'BzrDir.create_repository'
588
response = a_bzrdir._call(verb, path, network_name, shared_str)
589
except errors.UnknownSmartMethod:
590
# Fallback - use vfs methods
591
medium._remember_remote_is_before((1, 13))
592
return self._vfs_initialize(a_bzrdir, shared)
594
# Turn the response into a RemoteRepository object.
595
format = response_tuple_to_repo_format(response[1:])
596
# Used to support creating a real format instance when needed.
597
format._creating_bzrdir = a_bzrdir
598
remote_repo = RemoteRepository(a_bzrdir, format)
599
format._creating_repo = remote_repo
270
return a_bzrdir.create_repository(shared=shared)
602
272
def open(self, a_bzrdir):
603
273
if not isinstance(a_bzrdir, RemoteBzrDir):
604
274
raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
605
275
return a_bzrdir.open_repository()
607
def _ensure_real(self):
608
if self._custom_format is None:
609
self._custom_format = repository.network_format_registry.get(
613
def _fetch_order(self):
615
return self._custom_format._fetch_order
618
def _fetch_uses_deltas(self):
620
return self._custom_format._fetch_uses_deltas
623
def _fetch_reconcile(self):
625
return self._custom_format._fetch_reconcile
627
277
def get_format_description(self):
629
return 'Remote: ' + self._custom_format.get_format_description()
278
return 'bzr remote repository'
631
280
def __eq__(self, other):
632
return self.__class__ is other.__class__
634
def network_name(self):
635
if self._network_name:
636
return self._network_name
637
self._creating_repo._ensure_real()
638
return self._creating_repo._real_repository._format.network_name()
641
def pack_compresses(self):
643
return self._custom_format.pack_compresses
646
def _serializer(self):
648
return self._custom_format._serializer
651
class RemoteRepository(_RpcHelper, lock._RelockDebugMixin,
652
controldir.ControlComponent):
281
return self.__class__ == other.__class__
283
def check_conversion_target(self, target_format):
284
if self.rich_root_data and not target_format.rich_root_data:
285
raise errors.BadConversionTarget(
286
'Does not support rich root data.', target_format)
287
if (self.supports_tree_reference and
288
not getattr(target_format, 'supports_tree_reference', False)):
289
raise errors.BadConversionTarget(
290
'Does not support nested trees', target_format)
293
class RemoteRepository(_RpcHelper):
653
294
"""Repository accessed over rpc.
655
296
For the moment most operations are performed using local transport-backed
749
367
self._ensure_real()
750
368
return self._real_repository.commit_write_group()
752
def resume_write_group(self, tokens):
754
return self._real_repository.resume_write_group(tokens)
756
def suspend_write_group(self):
758
return self._real_repository.suspend_write_group()
760
def get_missing_parent_inventories(self, check_for_missing_texts=True):
762
return self._real_repository.get_missing_parent_inventories(
763
check_for_missing_texts=check_for_missing_texts)
765
def _get_rev_id_for_revno_vfs(self, revno, known_pair):
767
return self._real_repository.get_rev_id_for_revno(
770
def get_rev_id_for_revno(self, revno, known_pair):
771
"""See Repository.get_rev_id_for_revno."""
772
path = self.bzrdir._path_for_remote_call(self._client)
774
if self._client._medium._is_remote_before((1, 17)):
775
return self._get_rev_id_for_revno_vfs(revno, known_pair)
776
response = self._call(
777
'Repository.get_rev_id_for_revno', path, revno, known_pair)
778
except errors.UnknownSmartMethod:
779
self._client._medium._remember_remote_is_before((1, 17))
780
return self._get_rev_id_for_revno_vfs(revno, known_pair)
781
if response[0] == 'ok':
782
return True, response[1]
783
elif response[0] == 'history-incomplete':
784
known_pair = response[1:3]
785
for fallback in self._fallback_repositories:
786
found, result = fallback.get_rev_id_for_revno(revno, known_pair)
791
# Not found in any fallbacks
792
return False, known_pair
794
raise errors.UnexpectedSmartServerResponse(response)
796
370
def _ensure_real(self):
797
371
"""Ensure that there is a _real_repository set.
799
373
Used before calls to self._real_repository.
801
Note that _ensure_real causes many roundtrips to the server which are
802
not desirable, and prevents the use of smart one-roundtrip RPC's to
803
perform complex operations (such as accessing parent data, streaming
804
revisions etc). Adding calls to _ensure_real should only be done when
805
bringing up new functionality, adding fallbacks for smart methods that
806
require a fallback path, and never to replace an existing smart method
807
invocation. If in doubt chat to the bzr network team.
809
375
if self._real_repository is None:
810
if 'hpssvfs' in debug.debug_flags:
812
warning('VFS Repository access triggered\n%s',
813
''.join(traceback.format_stack()))
814
self._unstacked_provider.missing_keys.clear()
815
376
self.bzrdir._ensure_real()
816
377
self._set_real_repository(
817
378
self.bzrdir._real_bzrdir.open_repository())
866
432
for line in lines:
867
433
d = tuple(line.split())
868
434
revision_graph[d[0]] = d[1:]
870
436
return revision_graph
873
"""See Repository._get_sink()."""
874
return RemoteStreamSink(self)
876
def _get_source(self, to_format):
877
"""Return a source for streaming from this repository."""
878
return RemoteStreamSource(self, to_format)
881
438
def has_revision(self, revision_id):
882
"""True if this repository has a copy of the revision."""
883
# Copy of bzrlib.repository.Repository.has_revision
884
return revision_id in self.has_revisions((revision_id,))
439
"""See Repository.has_revision()."""
440
if revision_id == NULL_REVISION:
441
# The null revision is always present.
443
path = self.bzrdir._path_for_remote_call(self._client)
444
response = self._call('Repository.has_revision', path, revision_id)
445
if response[0] not in ('yes', 'no'):
446
raise errors.UnexpectedSmartServerResponse(response)
447
if response[0] == 'yes':
449
for fallback_repo in self._fallback_repositories:
450
if fallback_repo.has_revision(revision_id):
887
454
def has_revisions(self, revision_ids):
888
"""Probe to find out the presence of multiple revisions.
890
:param revision_ids: An iterable of revision_ids.
891
:return: A set of the revision_ids that were present.
893
# Copy of bzrlib.repository.Repository.has_revisions
894
parent_map = self.get_parent_map(revision_ids)
895
result = set(parent_map)
896
if _mod_revision.NULL_REVISION in revision_ids:
897
result.add(_mod_revision.NULL_REVISION)
455
"""See Repository.has_revisions()."""
456
# FIXME: This does many roundtrips, particularly when there are
457
# fallback repositories. -- mbp 20080905
459
for revision_id in revision_ids:
460
if self.has_revision(revision_id):
461
result.add(revision_id)
900
def _has_same_fallbacks(self, other_repo):
901
"""Returns true if the repositories have the same fallbacks."""
902
# XXX: copied from Repository; it should be unified into a base class
903
# <https://bugs.launchpad.net/bzr/+bug/401622>
904
my_fb = self._fallback_repositories
905
other_fb = other_repo._fallback_repositories
906
if len(my_fb) != len(other_fb):
908
for f, g in zip(my_fb, other_fb):
909
if not f.has_same_location(g):
913
464
def has_same_location(self, other):
914
# TODO: Move to RepositoryBase and unify with the regular Repository
915
# one; unfortunately the tests rely on slightly different behaviour at
916
# present -- mbp 20090710
917
return (self.__class__ is other.__class__ and
465
return (self.__class__ == other.__class__ and
918
466
self.bzrdir.transport.base == other.bzrdir.transport.base)
920
468
def get_graph(self, other_repository=None):
1237
727
def add_fallback_repository(self, repository):
1238
728
"""Add a repository to use for looking up data not held locally.
1240
730
:param repository: A repository.
1242
if not self._format.supports_external_lookups:
1243
raise errors.UnstackableRepositoryFormat(
1244
self._format.network_name(), self.base)
732
# XXX: At the moment the RemoteRepository will allow fallbacks
733
# unconditionally - however, a _real_repository will usually exist,
734
# and may raise an error if it's not accommodated by the underlying
735
# format. Eventually we should check when opening the repository
736
# whether it's willing to allow them or not.
1245
738
# We need to accumulate additional repositories here, to pass them in
1246
739
# on various RPC's.
1248
if self.is_locked():
1249
# We will call fallback.unlock() when we transition to the unlocked
1250
# state, so always add a lock here. If a caller passes us a locked
1251
# repository, they are responsible for unlocking it later.
1252
repository.lock_read()
1253
self._check_fallback_repository(repository)
1254
740
self._fallback_repositories.append(repository)
1255
# If self._real_repository was parameterised already (e.g. because a
1256
# _real_branch had its get_stacked_on_url method called), then the
1257
# repository to be added may already be in the _real_repositories list.
1258
if self._real_repository is not None:
1259
fallback_locations = [repo.user_url for repo in
1260
self._real_repository._fallback_repositories]
1261
if repository.user_url not in fallback_locations:
1262
self._real_repository.add_fallback_repository(repository)
1264
def _check_fallback_repository(self, repository):
1265
"""Check that this repository can fallback to repository safely.
1267
Raise an error if not.
1269
:param repository: A repository to fallback to.
1271
return _mod_repository.InterRepository._assert_same_model(
741
# They are also seen by the fallback repository. If it doesn't exist
742
# yet they'll be added then. This implicitly copies them.
1274
745
def add_inventory(self, revid, inv, parents):
1275
746
self._ensure_real()
1276
747
return self._real_repository.add_inventory(revid, inv, parents)
1278
749
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1279
parents, basis_inv=None, propagate_caches=False):
1280
751
self._ensure_real()
1281
752
return self._real_repository.add_inventory_by_delta(basis_revision_id,
1282
delta, new_revision_id, parents, basis_inv=basis_inv,
1283
propagate_caches=propagate_caches)
753
delta, new_revision_id, parents)
1285
755
def add_revision(self, rev_id, rev, inv=None, config=None):
1286
756
self._ensure_real()
1774
1210
self._ensure_real()
1775
1211
self._real_repository._pack_collection.autopack()
1213
if self._real_repository is not None:
1214
# Reset the real repository's cache of pack names.
1215
# XXX: At some point we may be able to skip this and just rely on
1216
# the automatic retry logic to do the right thing, but for now we
1217
# err on the side of being correct rather than being optimal.
1218
self._real_repository._pack_collection.reload_pack_names()
1778
1219
if response[0] != 'ok':
1779
1220
raise errors.UnexpectedSmartServerResponse(response)
1782
class RemoteStreamSink(repository.StreamSink):
1784
def _insert_real(self, stream, src_format, resume_tokens):
1785
self.target_repo._ensure_real()
1786
sink = self.target_repo._real_repository._get_sink()
1787
result = sink.insert_stream(stream, src_format, resume_tokens)
1789
self.target_repo.autopack()
1792
def insert_stream(self, stream, src_format, resume_tokens):
1793
target = self.target_repo
1794
target._unstacked_provider.missing_keys.clear()
1795
candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
1796
if target._lock_token:
1797
candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
1798
lock_args = (target._lock_token or '',)
1800
candidate_calls.append(('Repository.insert_stream', (1, 13)))
1802
client = target._client
1803
medium = client._medium
1804
path = target.bzrdir._path_for_remote_call(client)
1805
# Probe for the verb to use with an empty stream before sending the
1806
# real stream to it. We do this both to avoid the risk of sending a
1807
# large request that is then rejected, and because we don't want to
1808
# implement a way to buffer, rewind, or restart the stream.
1810
for verb, required_version in candidate_calls:
1811
if medium._is_remote_before(required_version):
1814
# We've already done the probing (and set _is_remote_before) on
1815
# a previous insert.
1818
byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1820
response = client.call_with_body_stream(
1821
(verb, path, '') + lock_args, byte_stream)
1822
except errors.UnknownSmartMethod:
1823
medium._remember_remote_is_before(required_version)
1829
return self._insert_real(stream, src_format, resume_tokens)
1830
self._last_inv_record = None
1831
self._last_substream = None
1832
if required_version < (1, 19):
1833
# Remote side doesn't support inventory deltas. Wrap the stream to
1834
# make sure we don't send any. If the stream contains inventory
1835
# deltas we'll interrupt the smart insert_stream request and
1837
stream = self._stop_stream_if_inventory_delta(stream)
1838
byte_stream = smart_repo._stream_to_byte_stream(
1840
resume_tokens = ' '.join(resume_tokens)
1841
response = client.call_with_body_stream(
1842
(verb, path, resume_tokens) + lock_args, byte_stream)
1843
if response[0][0] not in ('ok', 'missing-basis'):
1844
raise errors.UnexpectedSmartServerResponse(response)
1845
if self._last_substream is not None:
1846
# The stream included an inventory-delta record, but the remote
1847
# side isn't new enough to support them. So we need to send the
1848
# rest of the stream via VFS.
1849
self.target_repo.refresh_data()
1850
return self._resume_stream_with_vfs(response, src_format)
1851
if response[0][0] == 'missing-basis':
1852
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1853
resume_tokens = tokens
1854
return resume_tokens, set(missing_keys)
1856
self.target_repo.refresh_data()
1859
def _resume_stream_with_vfs(self, response, src_format):
1860
"""Resume sending a stream via VFS, first resending the record and
1861
substream that couldn't be sent via an insert_stream verb.
1863
if response[0][0] == 'missing-basis':
1864
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1865
# Ignore missing_keys, we haven't finished inserting yet
1868
def resume_substream():
1869
# Yield the substream that was interrupted.
1870
for record in self._last_substream:
1872
self._last_substream = None
1873
def resume_stream():
1874
# Finish sending the interrupted substream
1875
yield ('inventory-deltas', resume_substream())
1876
# Then simply continue sending the rest of the stream.
1877
for substream_kind, substream in self._last_stream:
1878
yield substream_kind, substream
1879
return self._insert_real(resume_stream(), src_format, tokens)
1881
def _stop_stream_if_inventory_delta(self, stream):
1882
"""Normally this just lets the original stream pass-through unchanged.
1884
However if any 'inventory-deltas' substream occurs it will stop
1885
streaming, and store the interrupted substream and stream in
1886
self._last_substream and self._last_stream so that the stream can be
1887
resumed by _resume_stream_with_vfs.
1890
stream_iter = iter(stream)
1891
for substream_kind, substream in stream_iter:
1892
if substream_kind == 'inventory-deltas':
1893
self._last_substream = substream
1894
self._last_stream = stream_iter
1897
yield substream_kind, substream
1900
class RemoteStreamSource(repository.StreamSource):
1901
"""Stream data from a remote server."""
1903
def get_stream(self, search):
1904
if (self.from_repository._fallback_repositories and
1905
self.to_format._fetch_order == 'topological'):
1906
return self._real_stream(self.from_repository, search)
1909
repos = [self.from_repository]
1915
repos.extend(repo._fallback_repositories)
1916
sources.append(repo)
1917
return self.missing_parents_chain(search, sources)
1919
def get_stream_for_missing_keys(self, missing_keys):
1920
self.from_repository._ensure_real()
1921
real_repo = self.from_repository._real_repository
1922
real_source = real_repo._get_source(self.to_format)
1923
return real_source.get_stream_for_missing_keys(missing_keys)
1925
def _real_stream(self, repo, search):
1926
"""Get a stream for search from repo.
1928
This never called RemoteStreamSource.get_stream, and is a heler
1929
for RemoteStreamSource._get_stream to allow getting a stream
1930
reliably whether fallback back because of old servers or trying
1931
to stream from a non-RemoteRepository (which the stacked support
1934
source = repo._get_source(self.to_format)
1935
if isinstance(source, RemoteStreamSource):
1937
source = repo._real_repository._get_source(self.to_format)
1938
return source.get_stream(search)
1940
def _get_stream(self, repo, search):
1941
"""Core worker to get a stream from repo for search.
1943
This is used by both get_stream and the stacking support logic. It
1944
deliberately gets a stream for repo which does not need to be
1945
self.from_repository. In the event that repo is not Remote, or
1946
cannot do a smart stream, a fallback is made to the generic
1947
repository._get_stream() interface, via self._real_stream.
1949
In the event of stacking, streams from _get_stream will not
1950
contain all the data for search - this is normal (see get_stream).
1952
:param repo: A repository.
1953
:param search: A search.
1955
# Fallbacks may be non-smart
1956
if not isinstance(repo, RemoteRepository):
1957
return self._real_stream(repo, search)
1958
client = repo._client
1959
medium = client._medium
1960
path = repo.bzrdir._path_for_remote_call(client)
1961
search_bytes = repo._serialise_search_result(search)
1962
args = (path, self.to_format.network_name())
1964
('Repository.get_stream_1.19', (1, 19)),
1965
('Repository.get_stream', (1, 13))]
1967
for verb, version in candidate_verbs:
1968
if medium._is_remote_before(version):
1971
response = repo._call_with_body_bytes_expecting_body(
1972
verb, args, search_bytes)
1973
except errors.UnknownSmartMethod:
1974
medium._remember_remote_is_before(version)
1976
response_tuple, response_handler = response
1980
return self._real_stream(repo, search)
1981
if response_tuple[0] != 'ok':
1982
raise errors.UnexpectedSmartServerResponse(response_tuple)
1983
byte_stream = response_handler.read_streamed_body()
1984
src_format, stream = smart_repo._byte_stream_to_stream(byte_stream,
1985
self._record_counter)
1986
if src_format.network_name() != repo._format.network_name():
1987
raise AssertionError(
1988
"Mismatched RemoteRepository and stream src %r, %r" % (
1989
src_format.network_name(), repo._format.network_name()))
1992
def missing_parents_chain(self, search, sources):
1993
"""Chain multiple streams together to handle stacking.
1995
:param search: The overall search to satisfy with streams.
1996
:param sources: A list of Repository objects to query.
1998
self.from_serialiser = self.from_repository._format._serializer
1999
self.seen_revs = set()
2000
self.referenced_revs = set()
2001
# If there are heads in the search, or the key count is > 0, we are not
2003
while not search.is_empty() and len(sources) > 1:
2004
source = sources.pop(0)
2005
stream = self._get_stream(source, search)
2006
for kind, substream in stream:
2007
if kind != 'revisions':
2008
yield kind, substream
2010
yield kind, self.missing_parents_rev_handler(substream)
2011
search = search.refine(self.seen_revs, self.referenced_revs)
2012
self.seen_revs = set()
2013
self.referenced_revs = set()
2014
if not search.is_empty():
2015
for kind, stream in self._get_stream(sources[0], search):
2018
def missing_parents_rev_handler(self, substream):
2019
for content in substream:
2020
revision_bytes = content.get_bytes_as('fulltext')
2021
revision = self.from_serialiser.read_revision_from_string(
2023
self.seen_revs.add(content.key[-1])
2024
self.referenced_revs.update(revision.parent_ids)
2028
1223
class RemoteBranchLockableFiles(LockableFiles):
2029
1224
"""A 'LockableFiles' implementation that talks to a smart server.
2031
1226
This is not a public interface class.
2048
1243
class RemoteBranchFormat(branch.BranchFormat):
2050
def __init__(self, network_name=None):
2051
1246
super(RemoteBranchFormat, self).__init__()
2052
1247
self._matchingbzrdir = RemoteBzrDirFormat()
2053
1248
self._matchingbzrdir.set_branch_format(self)
2054
self._custom_format = None
2055
self._network_name = network_name
2057
1250
def __eq__(self, other):
2058
return (isinstance(other, RemoteBranchFormat) and
1251
return (isinstance(other, RemoteBranchFormat) and
2059
1252
self.__dict__ == other.__dict__)
2061
def _ensure_real(self):
2062
if self._custom_format is None:
2063
self._custom_format = branch.network_format_registry.get(
2066
1254
def get_format_description(self):
2068
return 'Remote: ' + self._custom_format.get_format_description()
2070
def network_name(self):
2071
return self._network_name
2073
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2074
return a_bzrdir.open_branch(name=name,
2075
ignore_fallbacks=ignore_fallbacks)
2077
def _vfs_initialize(self, a_bzrdir, name):
2078
# Initialisation when using a local bzrdir object, or a non-vfs init
2079
# method is not available on the server.
2080
# self._custom_format is always set - the start of initialize ensures
2082
if isinstance(a_bzrdir, RemoteBzrDir):
2083
a_bzrdir._ensure_real()
2084
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2087
# We assume the bzrdir is parameterised; it may not be.
2088
result = self._custom_format.initialize(a_bzrdir, name)
2089
if (isinstance(a_bzrdir, RemoteBzrDir) and
2090
not isinstance(result, RemoteBranch)):
2091
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2095
def initialize(self, a_bzrdir, name=None):
2096
# 1) get the network name to use.
2097
if self._custom_format:
2098
network_name = self._custom_format.network_name()
2100
# Select the current bzrlib default and ask for that.
2101
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2102
reference_format = reference_bzrdir_format.get_branch_format()
2103
self._custom_format = reference_format
2104
network_name = reference_format.network_name()
2105
# Being asked to create on a non RemoteBzrDir:
2106
if not isinstance(a_bzrdir, RemoteBzrDir):
2107
return self._vfs_initialize(a_bzrdir, name=name)
2108
medium = a_bzrdir._client._medium
2109
if medium._is_remote_before((1, 13)):
2110
return self._vfs_initialize(a_bzrdir, name=name)
2111
# Creating on a remote bzr dir.
2112
# 2) try direct creation via RPC
2113
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2114
if name is not None:
2115
# XXX JRV20100304: Support creating colocated branches
2116
raise errors.NoColocatedBranchSupport(self)
2117
verb = 'BzrDir.create_branch'
2119
response = a_bzrdir._call(verb, path, network_name)
2120
except errors.UnknownSmartMethod:
2121
# Fallback - use vfs methods
2122
medium._remember_remote_is_before((1, 13))
2123
return self._vfs_initialize(a_bzrdir, name=name)
2124
if response[0] != 'ok':
2125
raise errors.UnexpectedSmartServerResponse(response)
2126
# Turn the response into a RemoteRepository object.
2127
format = RemoteBranchFormat(network_name=response[1])
2128
repo_format = response_tuple_to_repo_format(response[3:])
2129
if response[2] == '':
2130
repo_bzrdir = a_bzrdir
2132
repo_bzrdir = RemoteBzrDir(
2133
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2135
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2136
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2137
format=format, setup_stacking=False, name=name)
2138
# XXX: We know this is a new branch, so it must have revno 0, revid
2139
# NULL_REVISION. Creating the branch locked would make this be unable
2140
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2141
remote_branch._last_revision_info_cache = 0, NULL_REVISION
2142
return remote_branch
2144
def make_tags(self, branch):
2146
return self._custom_format.make_tags(branch)
1255
return 'Remote BZR Branch'
1257
def get_format_string(self):
1258
return 'Remote BZR Branch'
1260
def open(self, a_bzrdir):
1261
return a_bzrdir.open_branch()
1263
def initialize(self, a_bzrdir):
1264
return a_bzrdir.create_branch()
2148
1266
def supports_tags(self):
2149
1267
# Remote branches might support tags, but we won't know until we
2150
1268
# access the real remote branch.
2152
return self._custom_format.supports_tags()
2154
def supports_stacking(self):
2156
return self._custom_format.supports_stacking()
2158
def supports_set_append_revisions_only(self):
2160
return self._custom_format.supports_set_append_revisions_only()
2163
class RemoteBranch(branch.Branch, _RpcHelper, lock._RelockDebugMixin):
1272
class RemoteBranch(branch.Branch, _RpcHelper):
2164
1273
"""Branch stored on a server accessed by HPSS RPC.
2166
1275
At the moment most operations are mapped down to simple file operations.
2169
1278
def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
2170
_client=None, format=None, setup_stacking=True, name=None):
2171
1280
"""Create a RemoteBranch instance.
2173
1282
:param real_branch: An optional local implementation of the branch
2174
1283
format, usually accessing the data via the VFS.
2175
1284
:param _client: Private parameter for testing.
2176
:param format: A RemoteBranchFormat object, None to create one
2177
automatically. If supplied it should have a network_name already
2179
:param setup_stacking: If True make an RPC call to determine the
2180
stacked (or not) status of the branch. If False assume the branch
2182
:param name: Colocated branch name
2184
1286
# We intentionally don't call the parent class's __init__, because it
2185
1287
# will try to assign to self.tags, which is a property in this subclass.
2186
1288
# And the parent's __init__ doesn't do much anyway.
1289
self._revision_id_to_revno_cache = None
1290
self._partial_revision_id_to_revno_cache = {}
1291
self._revision_history_cache = None
1292
self._last_revision_info_cache = None
1293
self._merge_sorted_revisions_cache = None
2187
1294
self.bzrdir = remote_bzrdir
2188
1295
if _client is not None:
2189
1296
self._client = _client
2357
1443
raise errors.UnexpectedSmartServerResponse(response)
2358
1444
return response[1]
2360
def set_stacked_on_url(self, url):
2361
branch.Branch.set_stacked_on_url(self, url)
2363
self._is_stacked = False
2365
self._is_stacked = True
2367
def _vfs_get_tags_bytes(self):
2369
return self._real_branch._get_tags_bytes()
2371
def _get_tags_bytes(self):
2372
medium = self._client._medium
2373
if medium._is_remote_before((1, 13)):
2374
return self._vfs_get_tags_bytes()
2376
response = self._call('Branch.get_tags_bytes', self._remote_path())
2377
except errors.UnknownSmartMethod:
2378
medium._remember_remote_is_before((1, 13))
2379
return self._vfs_get_tags_bytes()
2382
def _vfs_set_tags_bytes(self, bytes):
2384
return self._real_branch._set_tags_bytes(bytes)
2386
def _set_tags_bytes(self, bytes):
2387
medium = self._client._medium
2388
if medium._is_remote_before((1, 18)):
2389
self._vfs_set_tags_bytes(bytes)
2393
self._remote_path(), self._lock_token, self._repo_lock_token)
2394
response = self._call_with_body_bytes(
2395
'Branch.set_tags_bytes', args, bytes)
2396
except errors.UnknownSmartMethod:
2397
medium._remember_remote_is_before((1, 18))
2398
self._vfs_set_tags_bytes(bytes)
2400
1446
def lock_read(self):
2401
"""Lock the branch for read operations.
2403
:return: A bzrlib.lock.LogicalLockResult.
2405
1447
self.repository.lock_read()
2406
1448
if not self._lock_mode:
2407
self._note_lock('r')
2408
1449
self._lock_mode = 'r'
2409
1450
self._lock_count = 1
2410
1451
if self._real_branch is not None:
2411
1452
self._real_branch.lock_read()
2413
1454
self._lock_count += 1
2414
return lock.LogicalLockResult(self.unlock)
2416
1456
def _remote_lock_write(self, token):
2417
1457
if token is None:
2418
1458
branch_token = repo_token = ''
2420
1460
branch_token = token
2421
repo_token = self.repository.lock_write().repository_token
1461
repo_token = self.repository.lock_write()
2422
1462
self.repository.unlock()
2423
1463
err_context = {'token': token}
2425
response = self._call(
2426
'Branch.lock_write', self._remote_path(), branch_token,
2427
repo_token or '', **err_context)
2428
except errors.LockContention, e:
2429
# The LockContention from the server doesn't have any
2430
# information about the lock_url. We re-raise LockContention
2431
# with valid lock_url.
2432
raise errors.LockContention('(remote lock)',
2433
self.repository.base.split('.bzr/')[0])
1464
response = self._call(
1465
'Branch.lock_write', self._remote_path(), branch_token,
1466
repo_token or '', **err_context)
2434
1467
if response[0] != 'ok':
2435
1468
raise errors.UnexpectedSmartServerResponse(response)
2436
1469
ok, branch_token, repo_token = response
2437
1470
return branch_token, repo_token
2439
1472
def lock_write(self, token=None):
2440
1473
if not self._lock_mode:
2441
self._note_lock('w')
2442
1474
# Lock the branch and repo in one remote call.
2443
1475
remote_tokens = self._remote_lock_write(token)
2444
1476
self._lock_token, self._repo_lock_token = remote_tokens
2623
1613
rev_id = rev_history[-1]
2624
1614
self._set_last_revision(rev_id)
2625
for hook in branch.Branch.hooks['set_rh']:
2626
hook(self, rev_history)
2627
1615
self._cache_revision_history(rev_history)
2629
def _get_parent_location(self):
2630
medium = self._client._medium
2631
if medium._is_remote_before((1, 13)):
2632
return self._vfs_get_parent_location()
2634
response = self._call('Branch.get_parent', self._remote_path())
2635
except errors.UnknownSmartMethod:
2636
medium._remember_remote_is_before((1, 13))
2637
return self._vfs_get_parent_location()
2638
if len(response) != 1:
2639
raise errors.UnexpectedSmartServerResponse(response)
2640
parent_location = response[0]
2641
if parent_location == '':
2643
return parent_location
2645
def _vfs_get_parent_location(self):
2647
return self._real_branch._get_parent_location()
2649
def _set_parent_location(self, url):
2650
medium = self._client._medium
2651
if medium._is_remote_before((1, 15)):
2652
return self._vfs_set_parent_location(url)
2654
call_url = url or ''
2655
if type(call_url) is not str:
2656
raise AssertionError('url must be a str or None (%s)' % url)
2657
response = self._call('Branch.set_parent_location',
2658
self._remote_path(), self._lock_token, self._repo_lock_token,
2660
except errors.UnknownSmartMethod:
2661
medium._remember_remote_is_before((1, 15))
2662
return self._vfs_set_parent_location(url)
2664
raise errors.UnexpectedSmartServerResponse(response)
2666
def _vfs_set_parent_location(self, url):
2668
return self._real_branch._set_parent_location(url)
1617
def get_parent(self):
1619
return self._real_branch.get_parent()
1621
def set_parent(self, url):
1623
return self._real_branch.set_parent(url)
1625
def set_stacked_on_url(self, stacked_location):
1626
"""Set the URL this branch is stacked against.
1628
:raises UnstackableBranchFormat: If the branch does not support
1630
:raises UnstackableRepositoryFormat: If the repository does not support
1634
return self._real_branch.set_stacked_on_url(stacked_location)
1636
def sprout(self, to_bzrdir, revision_id=None):
1637
branch_format = to_bzrdir._format._branch_format
1638
if (branch_format is None or
1639
isinstance(branch_format, RemoteBranchFormat)):
1640
# The to_bzrdir specifies RemoteBranchFormat (or no format, which
1641
# implies the same thing), but RemoteBranches can't be created at
1642
# arbitrary URLs. So create a branch in the same format as
1643
# _real_branch instead.
1644
# XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
1645
# to_bzrdir.create_branch to create a RemoteBranch after all...
1647
result = self._real_branch._format.initialize(to_bzrdir)
1648
self.copy_content_into(result, revision_id=revision_id)
1649
result.set_parent(self.bzrdir.root_transport.base)
1651
result = branch.Branch.sprout(
1652
self, to_bzrdir, revision_id=revision_id)
2670
1655
@needs_write_lock
2671
1656
def pull(self, source, overwrite=False, stop_revision=None,
2731
1711
except errors.UnknownSmartMethod:
2732
1712
medium._remember_remote_is_before((1, 6))
2733
1713
self._clear_cached_state_of_remote_branch_only()
2734
self.set_revision_history(self._lefthand_history(revision_id,
2735
last_rev=last_rev,other_branch=other_branch))
1715
self._real_branch.generate_revision_history(
1716
revision_id, last_rev=last_rev, other_branch=other_branch)
1721
return self._real_branch.tags
2737
1723
def set_push_location(self, location):
2738
1724
self._ensure_real()
2739
1725
return self._real_branch.set_push_location(location)
2742
class RemoteConfig(object):
2743
"""A Config that reads and writes from smart verbs.
2745
It is a low-level object that considers config data to be name/value pairs
2746
that may be associated with a section. Assigning meaning to the these
2747
values is done at higher levels like bzrlib.config.TreeConfig.
2750
def get_option(self, name, section=None, default=None):
2751
"""Return the value associated with a named option.
2753
:param name: The name of the value
2754
:param section: The section the option is in (if any)
2755
:param default: The value to return if the value is not set
2756
:return: The value or default value
1728
def update_revisions(self, other, stop_revision=None, overwrite=False,
1730
"""See Branch.update_revisions."""
2759
configobj = self._get_configobj()
2761
section_obj = configobj
1733
if stop_revision is None:
1734
stop_revision = other.last_revision()
1735
if revision.is_null(stop_revision):
1736
# if there are no commits, we're done.
1738
self.fetch(other, stop_revision)
1741
# Just unconditionally set the new revision. We don't care if
1742
# the branches have diverged.
1743
self._set_last_revision(stop_revision)
2764
section_obj = configobj[section]
2767
return section_obj.get(name, default)
2768
except errors.UnknownSmartMethod:
2769
return self._vfs_get_option(name, section, default)
2771
def _response_to_configobj(self, response):
2772
if len(response[0]) and response[0][0] != 'ok':
2773
raise errors.UnexpectedSmartServerResponse(response)
2774
lines = response[1].read_body_bytes().splitlines()
2775
return config.ConfigObj(lines, encoding='utf-8')
2778
class RemoteBranchConfig(RemoteConfig):
2779
"""A RemoteConfig for Branches."""
2781
def __init__(self, branch):
2782
self._branch = branch
2784
def _get_configobj(self):
2785
path = self._branch._remote_path()
2786
response = self._branch._client.call_expecting_body(
2787
'Branch.get_config_file', path)
2788
return self._response_to_configobj(response)
2790
def set_option(self, value, name, section=None):
2791
"""Set the value associated with a named option.
2793
:param value: The value to set
2794
:param name: The name of the value to set
2795
:param section: The section the option is in (if any)
2797
medium = self._branch._client._medium
2798
if medium._is_remote_before((1, 14)):
2799
return self._vfs_set_option(value, name, section)
2800
if isinstance(value, dict):
2801
if medium._is_remote_before((2, 2)):
2802
return self._vfs_set_option(value, name, section)
2803
return self._set_config_option_dict(value, name, section)
2805
return self._set_config_option(value, name, section)
2807
def _set_config_option(self, value, name, section):
2809
path = self._branch._remote_path()
2810
response = self._branch._client.call('Branch.set_config_option',
2811
path, self._branch._lock_token, self._branch._repo_lock_token,
2812
value.encode('utf8'), name, section or '')
2813
except errors.UnknownSmartMethod:
2814
medium = self._branch._client._medium
2815
medium._remember_remote_is_before((1, 14))
2816
return self._vfs_set_option(value, name, section)
2818
raise errors.UnexpectedSmartServerResponse(response)
2820
def _serialize_option_dict(self, option_dict):
2822
for key, value in option_dict.items():
2823
if isinstance(key, unicode):
2824
key = key.encode('utf8')
2825
if isinstance(value, unicode):
2826
value = value.encode('utf8')
2827
utf8_dict[key] = value
2828
return bencode.bencode(utf8_dict)
2830
def _set_config_option_dict(self, value, name, section):
2832
path = self._branch._remote_path()
2833
serialised_dict = self._serialize_option_dict(value)
2834
response = self._branch._client.call(
2835
'Branch.set_config_option_dict',
2836
path, self._branch._lock_token, self._branch._repo_lock_token,
2837
serialised_dict, name, section or '')
2838
except errors.UnknownSmartMethod:
2839
medium = self._branch._client._medium
2840
medium._remember_remote_is_before((2, 2))
2841
return self._vfs_set_option(value, name, section)
2843
raise errors.UnexpectedSmartServerResponse(response)
2845
def _real_object(self):
2846
self._branch._ensure_real()
2847
return self._branch._real_branch
2849
def _vfs_set_option(self, value, name, section=None):
2850
return self._real_object()._get_config().set_option(
2851
value, name, section)
2854
class RemoteBzrDirConfig(RemoteConfig):
2855
"""A RemoteConfig for BzrDirs."""
2857
def __init__(self, bzrdir):
2858
self._bzrdir = bzrdir
2860
def _get_configobj(self):
2861
medium = self._bzrdir._client._medium
2862
verb = 'BzrDir.get_config_file'
2863
if medium._is_remote_before((1, 15)):
2864
raise errors.UnknownSmartMethod(verb)
2865
path = self._bzrdir._path_for_remote_call(self._bzrdir._client)
2866
response = self._bzrdir._call_expecting_body(
2868
return self._response_to_configobj(response)
2870
def _vfs_get_option(self, name, section, default):
2871
return self._real_object()._get_config().get_option(
2872
name, section, default)
2874
def set_option(self, value, name, section=None):
2875
"""Set the value associated with a named option.
2877
:param value: The value to set
2878
:param name: The name of the value to set
2879
:param section: The section the option is in (if any)
2881
return self._real_object()._get_config().set_option(
2882
value, name, section)
2884
def _real_object(self):
2885
self._bzrdir._ensure_real()
2886
return self._bzrdir._real_bzrdir
1745
medium = self._client._medium
1746
if not medium._is_remote_before((1, 6)):
1748
self._set_last_revision_descendant(stop_revision, other)
1750
except errors.UnknownSmartMethod:
1751
medium._remember_remote_is_before((1, 6))
1752
# Fallback for pre-1.6 servers: check for divergence
1753
# client-side, then do _set_last_revision.
1754
last_rev = revision.ensure_null(self.last_revision())
1756
graph = self.repository.get_graph()
1757
if self._check_if_descendant_or_diverged(
1758
stop_revision, last_rev, graph, other):
1759
# stop_revision is a descendant of last_rev, but we aren't
1760
# overwriting, so we're done.
1762
self._set_last_revision(stop_revision)
2890
1767
def _extract_tar(tar, to_dir):