34
29
class SmartServerRequestOpenBzrDir(SmartServerRequest):
36
31
def do(self, path):
32
from bzrlib.bzrdir import BzrDirFormat
33
t = self._backing_transport.clone(path)
34
default_format = BzrDirFormat.get_default_format()
35
real_bzrdir = default_format.open(t, _found=True)
38
t = self.transport_from_client_path(path)
39
except errors.PathNotChild:
40
# The client is trying to ask about a path that they have no access
42
# Ideally we'd return a FailedSmartServerResponse here rather than
43
# a "successful" negative, but we want to be compatibile with
44
# clients that don't anticipate errors from this method.
37
real_bzrdir._format.probe_transport(t)
38
except (errors.NotBranchError, errors.UnknownFormatError):
47
default_format = BzrDirFormat.get_default_format()
48
real_bzrdir = default_format.open(t, _found=True)
50
real_bzrdir._format.probe_transport(t)
51
except (errors.NotBranchError, errors.UnknownFormatError):
55
42
return SuccessfulSmartServerResponse((answer,))
58
class SmartServerRequestOpenBzrDir_2_1(SmartServerRequest):
45
class SmartServerRequestFindRepository(SmartServerRequest):
60
47
def do(self, path):
61
"""Is there a BzrDir present, and if so does it have a working tree?
66
t = self.transport_from_client_path(path)
67
except errors.PathNotChild:
68
# The client is trying to ask about a path that they have no access
70
return SuccessfulSmartServerResponse(('no',))
72
bd = BzrDir.open_from_transport(t)
73
except errors.NotBranchError:
77
if bd.has_workingtree():
81
return SuccessfulSmartServerResponse(answer)
84
class SmartServerRequestBzrDir(SmartServerRequest):
86
def do(self, path, *args):
87
"""Open a BzrDir at path, and return self.do_bzrdir_request(*args)."""
89
self._bzrdir = BzrDir.open_from_transport(
90
self.transport_from_client_path(path))
91
except errors.NotBranchError, e:
92
return FailedSmartServerResponse(('nobranch',))
93
return self.do_bzrdir_request(*args)
95
def _boolean_to_yes_no(self, a_boolean):
101
def _format_to_capabilities(self, repo_format):
102
rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
103
tree_ref = self._boolean_to_yes_no(
104
repo_format.supports_tree_reference)
105
external_lookup = self._boolean_to_yes_no(
106
repo_format.supports_external_lookups)
107
return rich_root, tree_ref, external_lookup
109
def _repo_relpath(self, current_transport, repository):
110
"""Get the relative path for repository from current_transport."""
111
# the relpath of the bzrdir in the found repository gives us the
112
# path segments to pop-out.
113
relpath = repository.bzrdir.root_transport.relpath(
114
current_transport.base)
116
segments = ['..'] * len(relpath.split('/'))
119
return '/'.join(segments)
122
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
124
def do_bzrdir_request(self, require_stacking):
125
"""Get the format that should be used when cloning from this dir.
129
:return: on success, a 3-tuple of network names for (control,
130
repository, branch) directories, where '' signifies "not present".
131
If this BzrDir contains a branch reference then this will fail with
132
BranchReference; clients should resolve branch references before
136
branch_ref = self._bzrdir.get_branch_reference()
137
except errors.NotBranchError:
139
if branch_ref is not None:
140
# The server shouldn't try to resolve references, and it quite
141
# possibly can't reach them anyway. The client needs to resolve
142
# the branch reference to determine the cloning_metadir.
143
return FailedSmartServerResponse(('BranchReference',))
144
if require_stacking == "True":
145
require_stacking = True
147
require_stacking = False
148
control_format = self._bzrdir.cloning_metadir(
149
require_stacking=require_stacking)
150
control_name = control_format.network_name()
151
# XXX: There should be a method that tells us that the format does/does
152
# not have subformats.
153
if isinstance(control_format, BzrDirMetaFormat1):
154
branch_name = ('branch',
155
control_format.get_branch_format().network_name())
156
repository_name = control_format.repository_format.network_name()
158
# Only MetaDir has delegated formats today.
159
branch_name = ('branch', '')
161
return SuccessfulSmartServerResponse((control_name, repository_name,
165
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
167
def do(self, path, network_name):
168
"""Create a branch in the bzr dir at path.
170
This operates precisely like 'bzrdir.create_branch'.
48
"""try to find a repository from path upwards
50
This operates precisely like 'bzrdir.find_repository'.
172
52
If a bzrdir is not present, an exception is propogated
173
rather than 'no branch' because these are different conditions (and
174
this method should only be called after establishing that a bzr dir
177
This is the initial version of this method introduced to the smart
180
:param path: The path to the bzrdir.
181
:param network_name: The network name of the branch type to create.
182
:return: (ok, network_name)
184
bzrdir = BzrDir.open_from_transport(
185
self.transport_from_client_path(path))
186
format = branch.network_format_registry.get(network_name)
187
bzrdir.branch_format = format
188
result = format.initialize(bzrdir)
189
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
190
result.repository._format)
191
branch_format = result._format.network_name()
192
repo_format = result.repository._format.network_name()
193
repo_path = self._repo_relpath(bzrdir.root_transport,
195
# branch format, repo relpath, rich_root, tree_ref, external_lookup,
197
return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
198
rich_root, tree_ref, external_lookup, repo_format))
201
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
203
def do(self, path, network_name, shared):
204
"""Create a repository in the bzr dir at path.
206
This operates precisely like 'bzrdir.create_repository'.
208
If a bzrdir is not present, an exception is propagated
209
rather than 'no branch' because these are different conditions (and
210
this method should only be called after establishing that a bzr dir
213
This is the initial version of this method introduced to the smart
216
:param path: The path to the bzrdir.
217
:param network_name: The network name of the repository type to create.
218
:param shared: The value to pass create_repository for the shared
220
:return: (ok, rich_root, tree_ref, external_lookup, network_name)
222
bzrdir = BzrDir.open_from_transport(
223
self.transport_from_client_path(path))
224
shared = shared == 'True'
225
format = repository.network_format_registry.get(network_name)
226
bzrdir.repository_format = format
227
result = format.initialize(bzrdir, shared=shared)
228
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
230
return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
231
external_lookup, result._format.network_name()))
234
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
236
def _find(self, path):
237
"""try to find a repository from path upwards
239
This operates precisely like 'bzrdir.find_repository'.
241
:return: (relpath, rich_root, tree_ref, external_lookup, network_name).
242
All are strings, relpath is a / prefixed path, the next three are
243
either 'yes' or 'no', and the last is a repository format network
245
:raises errors.NoRepositoryPresent: When there is no repository
248
bzrdir = BzrDir.open_from_transport(
249
self.transport_from_client_path(path))
250
repository = bzrdir.find_repository()
251
path = self._repo_relpath(bzrdir.root_transport, repository)
252
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
254
network_name = repository._format.network_name()
255
return path, rich_root, tree_ref, external_lookup, network_name
258
class SmartServerRequestFindRepositoryV1(SmartServerRequestFindRepository):
261
"""try to find a repository from path upwards
263
This operates precisely like 'bzrdir.find_repository'.
265
If a bzrdir is not present, an exception is propagated
266
53
rather than 'no branch' because these are different conditions.
268
This is the initial version of this method introduced with the smart
269
server. Modern clients will try the V2 method that adds support for the
270
supports_external_lookups attribute.
272
55
:return: norepository or ok, relpath.
275
path, rich_root, tree_ref, external_lookup, name = self._find(path)
276
return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
277
except errors.NoRepositoryPresent:
278
return FailedSmartServerResponse(('norepository', ))
281
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
284
"""try to find a repository from path upwards
286
This operates precisely like 'bzrdir.find_repository'.
288
If a bzrdir is not present, an exception is propagated
289
rather than 'no branch' because these are different conditions.
291
This is the second edition of this method introduced in bzr 1.3, which
292
returns information about the supports_external_lookups format
295
:return: norepository or ok, relpath, rich_root, tree_ref,
299
path, rich_root, tree_ref, external_lookup, name = self._find(path)
300
return SuccessfulSmartServerResponse(
301
('ok', path, rich_root, tree_ref, external_lookup))
302
except errors.NoRepositoryPresent:
303
return FailedSmartServerResponse(('norepository', ))
306
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
309
"""try to find a repository from path upwards
311
This operates precisely like 'bzrdir.find_repository'.
313
If a bzrdir is not present, an exception is propogated
314
rather than 'no branch' because these are different conditions.
316
This is the third edition of this method introduced in bzr 1.13, which
317
returns information about the network name of the repository format.
319
:return: norepository or ok, relpath, rich_root, tree_ref,
320
external_lookup, network_name.
323
path, rich_root, tree_ref, external_lookup, name = self._find(path)
324
return SuccessfulSmartServerResponse(
325
('ok', path, rich_root, tree_ref, external_lookup, name))
326
except errors.NoRepositoryPresent:
327
return FailedSmartServerResponse(('norepository', ))
330
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
332
def do_bzrdir_request(self):
333
"""Get the configuration bytes for a config file in bzrdir.
335
The body is not utf8 decoded - it is the literal bytestream from disk.
337
config = self._bzrdir._get_config()
341
content = config._get_config_file().read()
342
return SuccessfulSmartServerResponse((), content)
57
bzrdir = BzrDir.open_from_transport(self._backing_transport.clone(path))
59
repository = bzrdir.find_repository()
60
# the relpath of the bzrdir in the found repository gives us the
61
# path segments to pop-out.
62
relpath = repository.bzrdir.root_transport.relpath(bzrdir.root_transport.base)
64
segments = ['..'] * len(relpath.split('/'))
67
if repository.supports_rich_root():
71
if repository._format.supports_tree_reference:
75
return SuccessfulSmartServerResponse(('ok', '/'.join(segments), rich_root, tree_ref))
76
except errors.NoRepositoryPresent:
77
return FailedSmartServerResponse(('norepository', ))
345
80
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
350
85
The default format of the server is used.
351
86
:return: SmartServerResponse(('ok', ))
353
target_transport = self.transport_from_client_path(path)
88
target_transport = self._backing_transport.clone(path)
354
89
BzrDirFormat.get_default_format().initialize_on_transport(target_transport)
355
90
return SuccessfulSmartServerResponse(('ok', ))
358
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
360
def parse_NoneTrueFalse(self, arg):
367
raise AssertionError("invalid arg %r" % arg)
369
def parse_NoneString(self, arg):
372
def _serialize_NoneTrueFalse(self, arg):
379
def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
380
force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
381
make_working_trees, shared_repo):
382
"""Initialize a bzrdir at path as per
383
BzrDirFormat.initialize_on_transport_ex.
385
New in 1.16. (Replaces BzrDirFormat.initialize_ex verb from 1.15).
387
:return: return SuccessfulSmartServerResponse((repo_path, rich_root,
388
tree_ref, external_lookup, repo_network_name,
389
repo_bzrdir_network_name, bzrdir_format_network_name,
390
NoneTrueFalse(stacking), final_stack, final_stack_pwd,
93
class SmartServerRequestOpenBranch(SmartServerRequest):
96
"""try to open a branch at path and return ok/nobranch.
98
If a bzrdir is not present, an exception is propogated
99
rather than 'no branch' because these are different conditions.
393
target_transport = self.transport_from_client_path(path)
394
format = network_format_registry.get(bzrdir_network_name)
395
use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
396
create_prefix = self.parse_NoneTrueFalse(create_prefix)
397
force_new_repo = self.parse_NoneTrueFalse(force_new_repo)
398
stacked_on = self.parse_NoneString(stacked_on)
399
stack_on_pwd = self.parse_NoneString(stack_on_pwd)
400
make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
401
shared_repo = self.parse_NoneTrueFalse(shared_repo)
402
if stack_on_pwd == '.':
403
stack_on_pwd = target_transport.base
404
repo_format_name = self.parse_NoneString(repo_format_name)
405
repo, bzrdir, stacking, repository_policy = \
406
format.initialize_on_transport_ex(target_transport,
407
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
408
force_new_repo=force_new_repo, stacked_on=stacked_on,
409
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
410
make_working_trees=make_working_trees, shared_repo=shared_repo)
414
rich_root = tree_ref = external_lookup = ''
415
repo_bzrdir_name = ''
417
final_stack_pwd = None
420
repo_path = self._repo_relpath(bzrdir.root_transport, repo)
423
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
425
repo_name = repo._format.network_name()
426
repo_bzrdir_name = repo.bzrdir._format.network_name()
427
final_stack = repository_policy._stack_on
428
final_stack_pwd = repository_policy._stack_on_pwd
429
# It is returned locked, but we need to do the lock to get the lock
432
repo_lock_token = repo.lock_write() or ''
434
repo.leave_lock_in_place()
436
final_stack = final_stack or ''
437
final_stack_pwd = final_stack_pwd or ''
439
# We want this to be relative to the bzrdir.
441
final_stack_pwd = urlutils.relative_url(
442
target_transport.base, final_stack_pwd)
444
# Can't meaningfully return a root path.
445
if final_stack.startswith('/'):
446
client_path = self._root_client_path + final_stack[1:]
447
final_stack = urlutils.relative_url(
448
self._root_client_path, client_path)
449
final_stack_pwd = '.'
451
return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
452
external_lookup, repo_name, repo_bzrdir_name,
453
bzrdir._format.network_name(),
454
self._serialize_NoneTrueFalse(stacking), final_stack,
455
final_stack_pwd, repo_lock_token))
458
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
460
def do_bzrdir_request(self):
461
"""open a branch at path and return the branch reference or branch."""
101
bzrdir = BzrDir.open_from_transport(self._backing_transport.clone(path))
463
reference_url = self._bzrdir.get_branch_reference()
103
reference_url = bzrdir.get_branch_reference()
464
104
if reference_url is None:
465
105
return SuccessfulSmartServerResponse(('ok', ''))
467
107
return SuccessfulSmartServerResponse(('ok', reference_url))
468
except errors.NotBranchError, e:
469
return FailedSmartServerResponse(('nobranch',))
472
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
474
def do_bzrdir_request(self):
475
"""open a branch at path and return the reference or format."""
477
reference_url = self._bzrdir.get_branch_reference()
478
if reference_url is None:
479
br = self._bzrdir.open_branch(ignore_fallbacks=True)
480
format = br._format.network_name()
481
return SuccessfulSmartServerResponse(('branch', format))
483
return SuccessfulSmartServerResponse(('ref', reference_url))
484
except errors.NotBranchError, e:
485
return FailedSmartServerResponse(('nobranch',))
488
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
490
def do_bzrdir_request(self):
491
"""Open a branch at path and return the reference or format.
493
This version introduced in 2.1.
495
Differences to SmartServerRequestOpenBranchV2:
496
* can return 2-element ('nobranch', extra), where 'extra' is a string
497
with an explanation like 'location is a repository'. Previously
498
a 'nobranch' response would never have more than one element.
501
reference_url = self._bzrdir.get_branch_reference()
502
if reference_url is None:
503
br = self._bzrdir.open_branch(ignore_fallbacks=True)
504
format = br._format.network_name()
505
return SuccessfulSmartServerResponse(('branch', format))
507
return SuccessfulSmartServerResponse(('ref', reference_url))
508
except errors.NotBranchError, e:
509
# Stringify the exception so that its .detail attribute will be
515
if detail.startswith(': '):
518
return FailedSmartServerResponse(resp)
108
except errors.NotBranchError:
109
return FailedSmartServerResponse(('nobranch', ))