/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006-2010 Canonical Ltd
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
16
17
"""Server-side bzrdir related request implmentations."""
18
19
4416.3.8 by Jonathan Lange
This makes the unit test & one of the acceptance tests pass.
20
from bzrlib import branch, errors, repository, urlutils
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
21
from bzrlib.bzrdir import (
22
    BzrDir,
23
    BzrDirFormat,
5363.2.5 by Jelmer Vernooij
Add dummy foreign prober.
24
    BzrProber,
5363.2.25 by Jelmer Vernooij
Fix some test failures now that bzrlib.controldir.network_format_registry has moved.
25
    )
26
from bzrlib.controldir import (
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
27
    network_format_registry,
28
    )
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
29
from bzrlib.smart.request import (
30
    FailedSmartServerResponse,
31
    SmartServerRequest,
32
    SuccessfulSmartServerResponse,
33
    )
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
34
35
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
36
class SmartServerRequestOpenBzrDir(SmartServerRequest):
37
38
    def do(self, path):
39
        try:
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
40
            t = self.transport_from_client_path(path)
41
        except errors.PathNotChild:
42
            # The client is trying to ask about a path that they have no access
43
            # to.
44
            # Ideally we'd return a FailedSmartServerResponse here rather than
45
            # a "successful" negative, but we want to be compatibile with
46
            # clients that don't anticipate errors from this method.
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
47
            answer = 'no'
48
        else:
5363.2.5 by Jelmer Vernooij
Add dummy foreign prober.
49
            bzr_prober = BzrProber()
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
50
            try:
5363.2.5 by Jelmer Vernooij
Add dummy foreign prober.
51
                bzr_prober.probe_transport(t)
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
52
            except (errors.NotBranchError, errors.UnknownFormatError):
53
                answer = 'no'
54
            else:
55
                answer = 'yes'
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
56
        return SuccessfulSmartServerResponse((answer,))
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
57
58
4634.47.3 by Andrew Bennetts
Add a BzrDir.open_2.1 verb that indicates if there is a workingtree present. Removes the last 2 VFS calls from incremental pushes.
59
class SmartServerRequestOpenBzrDir_2_1(SmartServerRequest):
60
61
    def do(self, path):
62
        """Is there a BzrDir present, and if so does it have a working tree?
63
64
        New in 2.1.
65
        """
4634.47.6 by Andrew Bennetts
Give 'no' response for paths outside the root_client_path.
66
        try:
67
            t = self.transport_from_client_path(path)
68
        except errors.PathNotChild:
69
            # The client is trying to ask about a path that they have no access
70
            # to.
71
            return SuccessfulSmartServerResponse(('no',))
4634.47.3 by Andrew Bennetts
Add a BzrDir.open_2.1 verb that indicates if there is a workingtree present. Removes the last 2 VFS calls from incremental pushes.
72
        try:
73
            bd = BzrDir.open_from_transport(t)
74
        except errors.NotBranchError:
75
            answer = ('no',)
76
        else:
77
            answer = ('yes',)
78
            if bd.has_workingtree():
79
                answer += ('yes',)
80
            else:
81
                answer += ('no',)
82
        return SuccessfulSmartServerResponse(answer)
83
84
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
85
class SmartServerRequestBzrDir(SmartServerRequest):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
86
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
87
    def do(self, path, *args):
5891.1.3 by Andrew Bennetts
Move docstring formatting fixes.
88
        """Open a BzrDir at path, and return `self.do_bzrdir_request(*args)`."""
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
89
        try:
90
            self._bzrdir = BzrDir.open_from_transport(
91
                self.transport_from_client_path(path))
4734.4.3 by Brian de Alwis
Add support for the HPSS to do further probing when a the provided
92
        except errors.NotBranchError, e:
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
93
            return FailedSmartServerResponse(('nobranch',))
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
94
        return self.do_bzrdir_request(*args)
95
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
96
    def _boolean_to_yes_no(self, a_boolean):
97
        if a_boolean:
98
            return 'yes'
99
        else:
100
            return 'no'
101
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
102
    def _format_to_capabilities(self, repo_format):
103
        rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
104
        tree_ref = self._boolean_to_yes_no(
105
            repo_format.supports_tree_reference)
106
        external_lookup = self._boolean_to_yes_no(
107
            repo_format.supports_external_lookups)
108
        return rich_root, tree_ref, external_lookup
109
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
110
    def _repo_relpath(self, current_transport, repository):
111
        """Get the relative path for repository from current_transport."""
112
        # the relpath of the bzrdir in the found repository gives us the
113
        # path segments to pop-out.
5158.6.10 by Martin Pool
Update more code to use user_transport when it should
114
        relpath = repository.user_transport.relpath(
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
115
            current_transport.base)
116
        if len(relpath):
117
            segments = ['..'] * len(relpath.split('/'))
118
        else:
119
            segments = []
120
        return '/'.join(segments)
121
122
6266.4.1 by Jelmer Vernooij
HPSS call 'BzrDir.destroy_branch'.
123
class SmartServerBzrDirRequestDestroyBranch(SmartServerRequestBzrDir):
124
125
    def do_bzrdir_request(self, name=None):
126
        """Destroy the branch with the specified name.
127
128
        New in 2.5.0.
129
        :return: On success, 'ok'.
130
        """
131
        try:
132
            self._bzrdir.destroy_branch(name)
133
        except errors.NotBranchError, e:
134
            return FailedSmartServerResponse(('nobranch',))
135
        return SuccessfulSmartServerResponse(('ok',))
136
137
6266.3.1 by Jelmer Vernooij
Add HPSS call for BzrDir.has_workingtree.
138
class SmartServerBzrDirRequestHasWorkingTree(SmartServerRequestBzrDir):
139
140
    def do_bzrdir_request(self, name=None):
141
        """Check whether there is a working tree present.
142
143
        New in 2.5.0.
144
145
        :return: If there is a working tree present, 'yes'.
146
            Otherwise 'no'.
147
        """
148
        if self._bzrdir.has_workingtree():
149
            return SuccessfulSmartServerResponse(('yes', ))
150
        else:
151
            return SuccessfulSmartServerResponse(('no', ))
152
153
6266.2.1 by Jelmer Vernooij
New HPSS call BzrDir.destroy_repository.
154
class SmartServerBzrDirRequestDestroyRepository(SmartServerRequestBzrDir):
155
156
    def do_bzrdir_request(self, name=None):
157
        """Destroy the repository.
158
159
        New in 2.5.0.
160
161
        :return: On success, 'ok'.
162
        """
163
        try:
6266.2.2 by Jelmer Vernooij
Fix tests.
164
            self._bzrdir.destroy_repository()
6266.2.1 by Jelmer Vernooij
New HPSS call BzrDir.destroy_repository.
165
        except errors.NoRepositoryPresent, e:
166
            return FailedSmartServerResponse(('norepository',))
167
        return SuccessfulSmartServerResponse(('ok',))
168
169
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
170
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
171
172
    def do_bzrdir_request(self, require_stacking):
4160.2.10 by Andrew Bennetts
Improve documentation of BzrDir.cloning_metadir RPC
173
        """Get the format that should be used when cloning from this dir.
174
175
        New in 1.13.
176
        
177
        :return: on success, a 3-tuple of network names for (control,
178
            repository, branch) directories, where '' signifies "not present".
179
            If this BzrDir contains a branch reference then this will fail with
180
            BranchReference; clients should resolve branch references before
181
            calling this RPC.
182
        """
4070.7.4 by Andrew Bennetts
Deal with branch references better in BzrDir.cloning_metadir RPC (changes protocol).
183
        try:
184
            branch_ref = self._bzrdir.get_branch_reference()
185
        except errors.NotBranchError:
186
            branch_ref = None
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
187
        if branch_ref is not None:
188
            # The server shouldn't try to resolve references, and it quite
189
            # possibly can't reach them anyway.  The client needs to resolve
190
            # the branch reference to determine the cloning_metadir.
191
            return FailedSmartServerResponse(('BranchReference',))
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
192
        if require_stacking == "True":
193
            require_stacking = True
194
        else:
195
            require_stacking = False
196
        control_format = self._bzrdir.cloning_metadir(
197
            require_stacking=require_stacking)
198
        control_name = control_format.network_name()
6155.1.4 by Jelmer Vernooij
Use fixed_components setting.
199
        if not control_format.fixed_components:
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
200
            branch_name = ('branch',
201
                control_format.get_branch_format().network_name())
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
202
            repository_name = control_format.repository_format.network_name()
203
        else:
204
            # Only MetaDir has delegated formats today.
4084.2.2 by Robert Collins
Review feedback.
205
            branch_name = ('branch', '')
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
206
            repository_name = ''
207
        return SuccessfulSmartServerResponse((control_name, repository_name,
208
            branch_name))
209
210
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
211
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
212
213
    def do(self, path, network_name):
214
        """Create a branch in the bzr dir at path.
215
216
        This operates precisely like 'bzrdir.create_branch'.
217
218
        If a bzrdir is not present, an exception is propogated
219
        rather than 'no branch' because these are different conditions (and
220
        this method should only be called after establishing that a bzr dir
221
        exists anyway).
222
223
        This is the initial version of this method introduced to the smart
224
        server for 1.13.
225
226
        :param path: The path to the bzrdir.
227
        :param network_name: The network name of the branch type to create.
5609.21.1 by Andrew Bennetts
Possible fix for #726584, plus drive-by docstring fix.
228
        :return: ('ok', branch_format, repo_path, rich_root, tree_ref,
229
            external_lookup, repo_format)
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
230
        """
231
        bzrdir = BzrDir.open_from_transport(
232
            self.transport_from_client_path(path))
233
        format = branch.network_format_registry.get(network_name)
234
        bzrdir.branch_format = format
235
        result = format.initialize(bzrdir)
236
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
237
            result.repository._format)
238
        branch_format = result._format.network_name()
239
        repo_format = result.repository._format.network_name()
240
        repo_path = self._repo_relpath(bzrdir.root_transport,
241
            result.repository)
242
        # branch format, repo relpath, rich_root, tree_ref, external_lookup,
243
        # repo_network_name
244
        return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
245
            rich_root, tree_ref, external_lookup, repo_format))
246
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
247
248
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
249
250
    def do(self, path, network_name, shared):
251
        """Create a repository in the bzr dir at path.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
252
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
253
        This operates precisely like 'bzrdir.create_repository'.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
254
4031.3.1 by Frank Aspell
Fixing various typos
255
        If a bzrdir is not present, an exception is propagated
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
256
        rather than 'no branch' because these are different conditions (and
257
        this method should only be called after establishing that a bzr dir
258
        exists anyway).
259
260
        This is the initial version of this method introduced to the smart
261
        server for 1.13.
262
263
        :param path: The path to the bzrdir.
264
        :param network_name: The network name of the repository type to create.
265
        :param shared: The value to pass create_repository for the shared
266
            parameter.
267
        :return: (ok, rich_root, tree_ref, external_lookup, network_name)
268
        """
269
        bzrdir = BzrDir.open_from_transport(
270
            self.transport_from_client_path(path))
271
        shared = shared == 'True'
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
272
        format = repository.network_format_registry.get(network_name)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
273
        bzrdir.repository_format = format
274
        result = format.initialize(bzrdir, shared=shared)
275
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
276
            result._format)
277
        return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
278
            external_lookup, result._format.network_name()))
279
280
281
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
282
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
283
    def _find(self, path):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
284
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
285
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
286
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
287
4053.1.2 by Robert Collins
Actually make this branch work.
288
        :return: (relpath, rich_root, tree_ref, external_lookup, network_name).
289
            All are strings, relpath is a / prefixed path, the next three are
290
            either 'yes' or 'no', and the last is a repository format network
291
            name.
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
292
        :raises errors.NoRepositoryPresent: When there is no repository
293
            present.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
294
        """
2692.1.6 by Andrew Bennetts
Fix some >80 columns violations.
295
        bzrdir = BzrDir.open_from_transport(
296
            self.transport_from_client_path(path))
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
297
        repository = bzrdir.find_repository()
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
298
        path = self._repo_relpath(bzrdir.root_transport, repository)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
299
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
300
            repository._format)
4053.1.2 by Robert Collins
Actually make this branch work.
301
        network_name = repository._format.network_name()
302
        return path, rich_root, tree_ref, external_lookup, network_name
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
303
304
305
class SmartServerRequestFindRepositoryV1(SmartServerRequestFindRepository):
306
307
    def do(self, path):
308
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
309
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
310
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
311
4031.3.1 by Frank Aspell
Fixing various typos
312
        If a bzrdir is not present, an exception is propagated
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
313
        rather than 'no branch' because these are different conditions.
314
315
        This is the initial version of this method introduced with the smart
316
        server. Modern clients will try the V2 method that adds support for the
317
        supports_external_lookups attribute.
318
319
        :return: norepository or ok, relpath.
320
        """
321
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
322
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
323
            return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
324
        except errors.NoRepositoryPresent:
325
            return FailedSmartServerResponse(('norepository', ))
326
327
328
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
329
330
    def do(self, path):
331
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
332
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
333
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
334
4031.3.1 by Frank Aspell
Fixing various typos
335
        If a bzrdir is not present, an exception is propagated
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
336
        rather than 'no branch' because these are different conditions.
337
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
338
        This is the second edition of this method introduced in bzr 1.3, which
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
339
        returns information about the supports_external_lookups format
340
        attribute too.
341
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
342
        :return: norepository or ok, relpath, rich_root, tree_ref,
343
            external_lookup.
344
        """
345
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
346
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
347
            return SuccessfulSmartServerResponse(
348
                ('ok', path, rich_root, tree_ref, external_lookup))
349
        except errors.NoRepositoryPresent:
350
            return FailedSmartServerResponse(('norepository', ))
351
352
353
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
354
355
    def do(self, path):
356
        """try to find a repository from path upwards
357
358
        This operates precisely like 'bzrdir.find_repository'.
359
360
        If a bzrdir is not present, an exception is propogated
361
        rather than 'no branch' because these are different conditions.
362
363
        This is the third edition of this method introduced in bzr 1.13, which
364
        returns information about the network name of the repository format.
365
366
        :return: norepository or ok, relpath, rich_root, tree_ref,
367
            external_lookup, network_name.
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
368
        """
369
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
370
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
371
            return SuccessfulSmartServerResponse(
4053.1.2 by Robert Collins
Actually make this branch work.
372
                ('ok', path, rich_root, tree_ref, external_lookup, name))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
373
        except errors.NoRepositoryPresent:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
374
            return FailedSmartServerResponse(('norepository', ))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
375
376
4288.1.2 by Robert Collins
Create a server verb for doing BzrDir.get_config()
377
class SmartServerBzrDirRequestConfigFile(SmartServerRequestBzrDir):
378
379
    def do_bzrdir_request(self):
380
        """Get the configuration bytes for a config file in bzrdir.
381
        
382
        The body is not utf8 decoded - it is the literal bytestream from disk.
383
        """
384
        config = self._bzrdir._get_config()
385
        if config is None:
386
            content = ''
387
        else:
388
            content = config._get_config_file().read()
389
        return SuccessfulSmartServerResponse((), content)
390
391
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
392
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
393
394
    def do(self, path):
395
        """Initialize a bzrdir at path.
396
397
        The default format of the server is used.
398
        :return: SmartServerResponse(('ok', ))
399
        """
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
400
        target_transport = self.transport_from_client_path(path)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
401
        BzrDirFormat.get_default_format().initialize_on_transport(target_transport)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
402
        return SuccessfulSmartServerResponse(('ok', ))
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
403
404
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
405
class SmartServerRequestBzrDirInitializeEx(SmartServerRequestBzrDir):
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
406
407
    def parse_NoneTrueFalse(self, arg):
408
        if not arg:
409
            return None
410
        if arg == 'False':
411
            return False
412
        if arg == 'True':
413
            return True
414
        raise AssertionError("invalid arg %r" % arg)
415
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
416
    def parse_NoneString(self, arg):
417
        return arg or None
418
419
    def _serialize_NoneTrueFalse(self, arg):
420
        if arg is False:
421
            return 'False'
422
        if not arg:
423
            return ''
424
        return 'True'
425
426
    def do(self, bzrdir_network_name, path, use_existing_dir, create_prefix,
427
        force_new_repo, stacked_on, stack_on_pwd, repo_format_name,
428
        make_working_trees, shared_repo):
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
429
        """Initialize a bzrdir at path as per
430
        BzrDirFormat.initialize_on_transport_ex.
431
432
        New in 1.16.  (Replaces BzrDirFormat.initialize_ex verb from 1.15).
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
433
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
434
        :return: return SuccessfulSmartServerResponse((repo_path, rich_root,
435
            tree_ref, external_lookup, repo_network_name,
436
            repo_bzrdir_network_name, bzrdir_format_network_name,
437
            NoneTrueFalse(stacking), final_stack, final_stack_pwd,
438
            repo_lock_token))
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
439
        """
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
440
        target_transport = self.transport_from_client_path(path)
441
        format = network_format_registry.get(bzrdir_network_name)
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
442
        use_existing_dir = self.parse_NoneTrueFalse(use_existing_dir)
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
443
        create_prefix = self.parse_NoneTrueFalse(create_prefix)
444
        force_new_repo = self.parse_NoneTrueFalse(force_new_repo)
445
        stacked_on = self.parse_NoneString(stacked_on)
446
        stack_on_pwd = self.parse_NoneString(stack_on_pwd)
447
        make_working_trees = self.parse_NoneTrueFalse(make_working_trees)
448
        shared_repo = self.parse_NoneTrueFalse(shared_repo)
449
        if stack_on_pwd == '.':
450
            stack_on_pwd = target_transport.base
451
        repo_format_name = self.parse_NoneString(repo_format_name)
452
        repo, bzrdir, stacking, repository_policy = \
453
            format.initialize_on_transport_ex(target_transport,
454
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
455
            force_new_repo=force_new_repo, stacked_on=stacked_on,
456
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
457
            make_working_trees=make_working_trees, shared_repo=shared_repo)
458
        if repo is None:
459
            repo_path = ''
460
            repo_name = ''
461
            rich_root = tree_ref = external_lookup = ''
462
            repo_bzrdir_name = ''
463
            final_stack = None
464
            final_stack_pwd = None
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
465
            repo_lock_token = ''
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
466
        else:
467
            repo_path = self._repo_relpath(bzrdir.root_transport, repo)
468
            if repo_path == '':
469
                repo_path = '.'
470
            rich_root, tree_ref, external_lookup = self._format_to_capabilities(
471
                repo._format)
472
            repo_name = repo._format.network_name()
473
            repo_bzrdir_name = repo.bzrdir._format.network_name()
474
            final_stack = repository_policy._stack_on
4416.3.11 by Jonathan Lange
Guard in case it's none.
475
            final_stack_pwd = repository_policy._stack_on_pwd
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
476
            # It is returned locked, but we need to do the lock to get the lock
477
            # token.
478
            repo.unlock()
5200.3.3 by Robert Collins
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
479
            repo_lock_token = repo.lock_write().repository_token or ''
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
480
            if repo_lock_token:
481
                repo.leave_lock_in_place()
482
            repo.unlock()
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
483
        final_stack = final_stack or ''
484
        final_stack_pwd = final_stack_pwd or ''
4416.3.11 by Jonathan Lange
Guard in case it's none.
485
486
        # We want this to be relative to the bzrdir.
487
        if final_stack_pwd:
488
            final_stack_pwd = urlutils.relative_url(
4416.3.12 by Jonathan Lange
This makes the test pass, but it's a bit ick.
489
                target_transport.base, final_stack_pwd)
490
491
        # Can't meaningfully return a root path.
492
        if final_stack.startswith('/'):
4416.3.13 by Jonathan Lange
full_path -> client_path, use _root_client_path rather than
493
            client_path = self._root_client_path + final_stack[1:]
4416.3.12 by Jonathan Lange
This makes the test pass, but it's a bit ick.
494
            final_stack = urlutils.relative_url(
4416.3.13 by Jonathan Lange
full_path -> client_path, use _root_client_path rather than
495
                self._root_client_path, client_path)
4416.3.12 by Jonathan Lange
This makes the test pass, but it's a bit ick.
496
            final_stack_pwd = '.'
4416.3.11 by Jonathan Lange
Guard in case it's none.
497
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
498
        return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
499
            external_lookup, repo_name, repo_bzrdir_name,
500
            bzrdir._format.network_name(),
501
            self._serialize_NoneTrueFalse(stacking), final_stack,
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
502
            final_stack_pwd, repo_lock_token))
4294.2.7 by Robert Collins
Start building up a BzrDir.initialize_ex verb for the smart server.
503
504
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
505
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
506
507
    def do_bzrdir_request(self):
508
        """open a branch at path and return the branch reference or branch."""
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
509
        try:
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
510
            reference_url = self._bzrdir.get_branch_reference()
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
511
            if reference_url is None:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
512
                return SuccessfulSmartServerResponse(('ok', ''))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
513
            else:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
514
                return SuccessfulSmartServerResponse(('ok', reference_url))
4734.4.3 by Brian de Alwis
Add support for the HPSS to do further probing when a the provided
515
        except errors.NotBranchError, e:
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
516
            return FailedSmartServerResponse(('nobranch',))
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
517
518
519
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
520
521
    def do_bzrdir_request(self):
522
        """open a branch at path and return the reference or format."""
523
        try:
524
            reference_url = self._bzrdir.get_branch_reference()
525
            if reference_url is None:
4160.2.6 by Andrew Bennetts
Add ignore_fallbacks flag.
526
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
527
                format = br._format.network_name()
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
528
                return SuccessfulSmartServerResponse(('branch', format))
529
            else:
530
                return SuccessfulSmartServerResponse(('ref', reference_url))
4734.4.3 by Brian de Alwis
Add support for the HPSS to do further probing when a the provided
531
        except errors.NotBranchError, e:
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
532
            return FailedSmartServerResponse(('nobranch',))
533
534
535
class SmartServerRequestOpenBranchV3(SmartServerRequestBzrDir):
536
537
    def do_bzrdir_request(self):
538
        """Open a branch at path and return the reference or format.
539
        
540
        This version introduced in 2.1.
541
542
        Differences to SmartServerRequestOpenBranchV2:
543
          * can return 2-element ('nobranch', extra), where 'extra' is a string
544
            with an explanation like 'location is a repository'.  Previously
545
            a 'nobranch' response would never have more than one element.
546
        """
547
        try:
548
            reference_url = self._bzrdir.get_branch_reference()
549
            if reference_url is None:
550
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
551
                format = br._format.network_name()
552
                return SuccessfulSmartServerResponse(('branch', format))
553
            else:
554
                return SuccessfulSmartServerResponse(('ref', reference_url))
555
        except errors.NotBranchError, e:
4734.4.9 by Andrew Bennetts
More tests and comments.
556
            # Stringify the exception so that its .detail attribute will be
557
            # filled out.
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
558
            str(e)
559
            resp = ('nobranch',)
560
            detail = e.detail
561
            if detail:
562
                if detail.startswith(': '):
563
                    detail = detail[2:]
564
                resp += (detail,)
565
            return FailedSmartServerResponse(resp)
566