1
# Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Server-side bzrdir related request implmentations."""
20
from bzrlib import branch, errors, repository
21
from bzrlib.bzrdir import (
25
network_format_registry,
27
from bzrlib.smart.request import (
28
FailedSmartServerResponse,
30
SuccessfulSmartServerResponse,
34
class SmartServerRequestOpenBzrDir(SmartServerRequest):
37
from bzrlib.bzrdir import BzrDirFormat
39
t = self.transport_from_client_path(path)
40
except errors.PathNotChild:
41
# The client is trying to ask about a path that they have no access
43
# Ideally we'd return a FailedSmartServerResponse here rather than
44
# a "successful" negative, but we want to be compatibile with
45
# clients that don't anticipate errors from this method.
48
default_format = BzrDirFormat.get_default_format()
49
real_bzrdir = default_format.open(t, _found=True)
51
real_bzrdir._format.probe_transport(t)
52
except (errors.NotBranchError, errors.UnknownFormatError):
56
return SuccessfulSmartServerResponse((answer,))
59
class SmartServerRequestBzrDir(SmartServerRequest):
61
def do(self, path, *args):
62
"""Open a BzrDir at path, and return self.do_bzrdir_request(*args)."""
64
self._bzrdir = BzrDir.open_from_transport(
65
self.transport_from_client_path(path))
66
except errors.NotBranchError:
67
return FailedSmartServerResponse(('nobranch', ))
68
return self.do_bzrdir_request(*args)
70
def _boolean_to_yes_no(self, a_boolean):
76
def _format_to_capabilities(self, repo_format):
77
rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
78
tree_ref = self._boolean_to_yes_no(
79
repo_format.supports_tree_reference)
80
external_lookup = self._boolean_to_yes_no(
81
repo_format.supports_external_lookups)
82
return rich_root, tree_ref, external_lookup
84
def _repo_relpath(self, current_transport, repository):
85
"""Get the relative path for repository from current_transport."""
86
# the relpath of the bzrdir in the found repository gives us the
87
# path segments to pop-out.
88
relpath = repository.bzrdir.root_transport.relpath(
89
current_transport.base)
91
segments = ['..'] * len(relpath.split('/'))
94
return '/'.join(segments)
97
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
99
def do_bzrdir_request(self, require_stacking):
100
"""Get the format that should be used when cloning from this dir.
104
:return: on success, a 3-tuple of network names for (control,
105
repository, branch) directories, where '' signifies "not present".
106
If this BzrDir contains a branch reference then this will fail with
107
BranchReference; clients should resolve branch references before
111
branch_ref = self._bzrdir.get_branch_reference()
112
except errors.NotBranchError:
114
if branch_ref is not None:
115
# The server shouldn't try to resolve references, and it quite
116
# possibly can't reach them anyway. The client needs to resolve
117
# the branch reference to determine the cloning_metadir.
118
return FailedSmartServerResponse(('BranchReference',))
119
if require_stacking == "True":
120
require_stacking = True
122
require_stacking = False
123
control_format = self._bzrdir.cloning_metadir(
124
require_stacking=require_stacking)
125
control_name = control_format.network_name()
126
# XXX: There should be a method that tells us that the format does/does
127
# not have subformats.
128
if isinstance(control_format, BzrDirMetaFormat1):
129
branch_name = ('branch',
130
control_format.get_branch_format().network_name())
131
repository_name = control_format.repository_format.network_name()
133
# Only MetaDir has delegated formats today.
134
branch_name = ('branch', '')
136
return SuccessfulSmartServerResponse((control_name, repository_name,
140
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
142
def do(self, path, network_name):
143
"""Create a branch in the bzr dir at path.
145
This operates precisely like 'bzrdir.create_branch'.
147
If a bzrdir is not present, an exception is propogated
148
rather than 'no branch' because these are different conditions (and
149
this method should only be called after establishing that a bzr dir
152
This is the initial version of this method introduced to the smart
155
:param path: The path to the bzrdir.
156
:param network_name: The network name of the branch type to create.
157
:return: (ok, network_name)
159
bzrdir = BzrDir.open_from_transport(
160
self.transport_from_client_path(path))
161
format = branch.network_format_registry.get(network_name)
162
bzrdir.branch_format = format
163
result = format.initialize(bzrdir)
164
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
165
result.repository._format)
166
branch_format = result._format.network_name()
167
repo_format = result.repository._format.network_name()
168
repo_path = self._repo_relpath(bzrdir.root_transport,
170
# branch format, repo relpath, rich_root, tree_ref, external_lookup,
172
return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
173
rich_root, tree_ref, external_lookup, repo_format))
176
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
178
def do(self, path, network_name, shared):
179
"""Create a repository in the bzr dir at path.
181
This operates precisely like 'bzrdir.create_repository'.
183
If a bzrdir is not present, an exception is propagated
184
rather than 'no branch' because these are different conditions (and
185
this method should only be called after establishing that a bzr dir
188
This is the initial version of this method introduced to the smart
191
:param path: The path to the bzrdir.
192
:param network_name: The network name of the repository type to create.
193
:param shared: The value to pass create_repository for the shared
195
:return: (ok, rich_root, tree_ref, external_lookup, network_name)
197
bzrdir = BzrDir.open_from_transport(
198
self.transport_from_client_path(path))
199
shared = shared == 'True'
200
format = repository.network_format_registry.get(network_name)
201
bzrdir.repository_format = format
202
result = format.initialize(bzrdir, shared=shared)
203
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
205
return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
206
external_lookup, result._format.network_name()))
209
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
211
def _find(self, path):
212
"""try to find a repository from path upwards
214
This operates precisely like 'bzrdir.find_repository'.
216
:return: (relpath, rich_root, tree_ref, external_lookup, network_name).
217
All are strings, relpath is a / prefixed path, the next three are
218
either 'yes' or 'no', and the last is a repository format network
220
:raises errors.NoRepositoryPresent: When there is no repository
223
bzrdir = BzrDir.open_from_transport(
224
self.transport_from_client_path(path))
225
repository = bzrdir.find_repository()
226
path = self._repo_relpath(bzrdir.root_transport, repository)
227
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
229
network_name = repository._format.network_name()
230
return path, rich_root, tree_ref, external_lookup, network_name
233
class SmartServerRequestFindRepositoryV1(SmartServerRequestFindRepository):
236
"""try to find a repository from path upwards
238
This operates precisely like 'bzrdir.find_repository'.
240
If a bzrdir is not present, an exception is propagated
241
rather than 'no branch' because these are different conditions.
243
This is the initial version of this method introduced with the smart
244
server. Modern clients will try the V2 method that adds support for the
245
supports_external_lookups attribute.
247
:return: norepository or ok, relpath.
250
path, rich_root, tree_ref, external_lookup, name = self._find(path)
251
return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
252
except errors.NoRepositoryPresent:
253
return FailedSmartServerResponse(('norepository', ))
256
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
259
"""try to find a repository from path upwards
261
This operates precisely like 'bzrdir.find_repository'.
263
If a bzrdir is not present, an exception is propagated
264
rather than 'no branch' because these are different conditions.
266
This is the second edition of this method introduced in bzr 1.3, which
267
returns information about the supports_external_lookups format
270
:return: norepository or ok, relpath, rich_root, tree_ref,
274
path, rich_root, tree_ref, external_lookup, name = self._find(path)
275
return SuccessfulSmartServerResponse(
276
('ok', path, rich_root, tree_ref, external_lookup))
277
except errors.NoRepositoryPresent:
278
return FailedSmartServerResponse(('norepository', ))
281
class SmartServerRequestFindRepositoryV3(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 propogated
289
rather than 'no branch' because these are different conditions.
291
This is the third edition of this method introduced in bzr 1.13, which
292
returns information about the network name of the repository format.
294
:return: norepository or ok, relpath, rich_root, tree_ref,
295
external_lookup, network_name.
298
path, rich_root, tree_ref, external_lookup, name = self._find(path)
299
return SuccessfulSmartServerResponse(
300
('ok', path, rich_root, tree_ref, external_lookup, name))
301
except errors.NoRepositoryPresent:
302
return FailedSmartServerResponse(('norepository', ))
305
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
307
def do_bzrdir_request(self):
308
"""Get the configuration bytes for a config file in bzrdir.
310
The body is not utf8 decoded - it is the literal bytestream from disk.
312
config = self._bzrdir._get_config()
316
content = config._get_config_file().read()
317
return SuccessfulSmartServerResponse((), content)
320
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
323
"""Initialize a bzrdir at path.
325
The default format of the server is used.
326
:return: SmartServerResponse(('ok', ))
328
target_transport = self.transport_from_client_path(path)
329
BzrDirFormat.get_default_format().initialize_on_transport(target_transport)
330
return SuccessfulSmartServerResponse(('ok', ))
333
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
335
def parse_NoneTrueFalse(self, arg):
342
raise AssertionError("invalid arg %r" % arg)
344
def parse_NoneString(self, arg):
347
def _serialize_NoneTrueFalse(self, arg):
354
def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
355
force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
356
make_working_trees, shared_repo):
357
"""Initialize a bzrdir at path as per BzrDirFormat.initialize_ex
359
:return: SmartServerResponse()
361
target_transport = self.transport_from_client_path(path)
362
format = network_format_registry.get(bzrdir_network_name)
363
use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
364
create_prefix = self.parse_NoneTrueFalse(create_prefix)
365
force_new_repo = self.parse_NoneTrueFalse(force_new_repo)
366
stacked_on = self.parse_NoneString(stacked_on)
367
stack_on_pwd = self.parse_NoneString(stack_on_pwd)
368
make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
369
shared_repo = self.parse_NoneTrueFalse(shared_repo)
370
if stack_on_pwd == '.':
371
stack_on_pwd = target_transport.base
372
repo_format_name = self.parse_NoneString(repo_format_name)
373
repo, bzrdir, stacking, repository_policy = \
374
format.initialize_on_transport_ex(target_transport,
375
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
376
force_new_repo=force_new_repo, stacked_on=stacked_on,
377
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
378
make_working_trees=make_working_trees, shared_repo=shared_repo)
382
rich_root = tree_ref = external_lookup = ''
383
repo_bzrdir_name = ''
385
final_stack_pwd = None
387
repo_path = self._repo_relpath(bzrdir.root_transport, repo)
390
rich_root, tree_ref, external_lookup = self._format_to_capabilities(
392
repo_name = repo._format.network_name()
393
repo_bzrdir_name = repo.bzrdir._format.network_name()
394
final_stack = repository_policy._stack_on
395
final_stack_pwd = repository_policy._stack_on_pwd
396
final_stack = final_stack or ''
397
final_stack_pwd = final_stack_pwd or ''
398
return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
399
external_lookup, repo_name, repo_bzrdir_name,
400
bzrdir._format.network_name(),
401
self._serialize_NoneTrueFalse(stacking), final_stack,
405
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
407
def do_bzrdir_request(self):
408
"""open a branch at path and return the branch reference or branch."""
410
reference_url = self._bzrdir.get_branch_reference()
411
if reference_url is None:
412
return SuccessfulSmartServerResponse(('ok', ''))
414
return SuccessfulSmartServerResponse(('ok', reference_url))
415
except errors.NotBranchError:
416
return FailedSmartServerResponse(('nobranch', ))
419
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
421
def do_bzrdir_request(self):
422
"""open a branch at path and return the reference or format."""
424
reference_url = self._bzrdir.get_branch_reference()
425
if reference_url is None:
426
br = self._bzrdir.open_branch(ignore_fallbacks=True)
427
format = br._format.network_name()
428
return SuccessfulSmartServerResponse(('branch', format))
430
return SuccessfulSmartServerResponse(('ref', reference_url))
431
except errors.NotBranchError:
432
return FailedSmartServerResponse(('nobranch', ))