/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
1
# Copyright (C) 2006 Canonical Ltd
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
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
20
from bzrlib import branch, errors, repository
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
21
from bzrlib.bzrdir import BzrDir, BzrDirFormat, BzrDirMetaFormat1
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
22
from bzrlib.smart.request import (
23
    FailedSmartServerResponse,
24
    SmartServerRequest,
25
    SuccessfulSmartServerResponse,
26
    )
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
27
28
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
29
class SmartServerRequestOpenBzrDir(SmartServerRequest):
30
31
    def do(self, path):
32
        from bzrlib.bzrdir import BzrDirFormat
33
        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.
34
            t = self.transport_from_client_path(path)
35
        except errors.PathNotChild:
36
            # The client is trying to ask about a path that they have no access
37
            # to.
38
            # Ideally we'd return a FailedSmartServerResponse here rather than
39
            # a "successful" negative, but we want to be compatibile with
40
            # clients that don't anticipate errors from this method.
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
41
            answer = 'no'
42
        else:
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.
43
            default_format = BzrDirFormat.get_default_format()
44
            real_bzrdir = default_format.open(t, _found=True)
45
            try:
46
                real_bzrdir._format.probe_transport(t)
47
            except (errors.NotBranchError, errors.UnknownFormatError):
48
                answer = 'no'
49
            else:
50
                answer = 'yes'
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
51
        return SuccessfulSmartServerResponse((answer,))
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
52
53
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
54
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.
55
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
56
    def do(self, path, *args):
57
        """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.
58
        try:
59
            self._bzrdir = BzrDir.open_from_transport(
60
                self.transport_from_client_path(path))
61
        except errors.NotBranchError:
62
            return FailedSmartServerResponse(('nobranch', ))
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
63
        return self.do_bzrdir_request(*args)
64
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
65
    def _boolean_to_yes_no(self, a_boolean):
66
        if a_boolean:
67
            return 'yes'
68
        else:
69
            return 'no'
70
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
71
    def _format_to_capabilities(self, repo_format):
72
        rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
73
        tree_ref = self._boolean_to_yes_no(
74
            repo_format.supports_tree_reference)
75
        external_lookup = self._boolean_to_yes_no(
76
            repo_format.supports_external_lookups)
77
        return rich_root, tree_ref, external_lookup
78
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
79
    def _repo_relpath(self, current_transport, repository):
80
        """Get the relative path for repository from current_transport."""
81
        # the relpath of the bzrdir in the found repository gives us the
82
        # path segments to pop-out.
83
        relpath = repository.bzrdir.root_transport.relpath(
84
            current_transport.base)
85
        if len(relpath):
86
            segments = ['..'] * len(relpath.split('/'))
87
        else:
88
            segments = []
89
        return '/'.join(segments)
90
91
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
92
class SmartServerBzrDirRequestCloningMetaDir(SmartServerRequestBzrDir):
93
94
    def do_bzrdir_request(self, require_stacking):
4160.2.10 by Andrew Bennetts
Improve documentation of BzrDir.cloning_metadir RPC
95
        """Get the format that should be used when cloning from this dir.
96
97
        New in 1.13.
98
        
99
        :return: on success, a 3-tuple of network names for (control,
100
            repository, branch) directories, where '' signifies "not present".
101
            If this BzrDir contains a branch reference then this will fail with
102
            BranchReference; clients should resolve branch references before
103
            calling this RPC.
104
        """
4070.7.4 by Andrew Bennetts
Deal with branch references better in BzrDir.cloning_metadir RPC (changes protocol).
105
        try:
106
            branch_ref = self._bzrdir.get_branch_reference()
107
        except errors.NotBranchError:
108
            branch_ref = None
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
109
        if branch_ref is not None:
110
            # The server shouldn't try to resolve references, and it quite
111
            # possibly can't reach them anyway.  The client needs to resolve
112
            # the branch reference to determine the cloning_metadir.
113
            return FailedSmartServerResponse(('BranchReference',))
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
114
        if require_stacking == "True":
115
            require_stacking = True
116
        else:
117
            require_stacking = False
118
        control_format = self._bzrdir.cloning_metadir(
119
            require_stacking=require_stacking)
120
        control_name = control_format.network_name()
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
121
        # XXX: There should be a method that tells us that the format does/does
122
        # not have subformats.
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
123
        if isinstance(control_format, BzrDirMetaFormat1):
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
124
            branch_name = ('branch',
125
                control_format.get_branch_format().network_name())
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
126
            repository_name = control_format.repository_format.network_name()
127
        else:
128
            # Only MetaDir has delegated formats today.
4084.2.2 by Robert Collins
Review feedback.
129
            branch_name = ('branch', '')
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
130
            repository_name = ''
131
        return SuccessfulSmartServerResponse((control_name, repository_name,
132
            branch_name))
133
134
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
135
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
136
137
    def do(self, path, network_name):
138
        """Create a branch in the bzr dir at path.
139
140
        This operates precisely like 'bzrdir.create_branch'.
141
142
        If a bzrdir is not present, an exception is propogated
143
        rather than 'no branch' because these are different conditions (and
144
        this method should only be called after establishing that a bzr dir
145
        exists anyway).
146
147
        This is the initial version of this method introduced to the smart
148
        server for 1.13.
149
150
        :param path: The path to the bzrdir.
151
        :param network_name: The network name of the branch type to create.
152
        :return: (ok, network_name)
153
        """
154
        bzrdir = BzrDir.open_from_transport(
155
            self.transport_from_client_path(path))
156
        format = branch.network_format_registry.get(network_name)
157
        bzrdir.branch_format = format
158
        result = format.initialize(bzrdir)
159
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
160
            result.repository._format)
161
        branch_format = result._format.network_name()
162
        repo_format = result.repository._format.network_name()
163
        repo_path = self._repo_relpath(bzrdir.root_transport,
164
            result.repository)
165
        # branch format, repo relpath, rich_root, tree_ref, external_lookup,
166
        # repo_network_name
167
        return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
168
            rich_root, tree_ref, external_lookup, repo_format))
169
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
170
171
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
172
173
    def do(self, path, network_name, shared):
174
        """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.
175
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
176
        This operates precisely like 'bzrdir.create_repository'.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
177
4031.3.1 by Frank Aspell
Fixing various typos
178
        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.
179
        rather than 'no branch' because these are different conditions (and
180
        this method should only be called after establishing that a bzr dir
181
        exists anyway).
182
183
        This is the initial version of this method introduced to the smart
184
        server for 1.13.
185
186
        :param path: The path to the bzrdir.
187
        :param network_name: The network name of the repository type to create.
188
        :param shared: The value to pass create_repository for the shared
189
            parameter.
190
        :return: (ok, rich_root, tree_ref, external_lookup, network_name)
191
        """
192
        bzrdir = BzrDir.open_from_transport(
193
            self.transport_from_client_path(path))
194
        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.
195
        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.
196
        bzrdir.repository_format = format
197
        result = format.initialize(bzrdir, shared=shared)
198
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
199
            result._format)
200
        return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
201
            external_lookup, result._format.network_name()))
202
203
204
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
205
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
206
    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.
207
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
208
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
209
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
210
4053.1.2 by Robert Collins
Actually make this branch work.
211
        :return: (relpath, rich_root, tree_ref, external_lookup, network_name).
212
            All are strings, relpath is a / prefixed path, the next three are
213
            either 'yes' or 'no', and the last is a repository format network
214
            name.
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
215
        :raises errors.NoRepositoryPresent: When there is no repository
216
            present.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
217
        """
2692.1.6 by Andrew Bennetts
Fix some >80 columns violations.
218
        bzrdir = BzrDir.open_from_transport(
219
            self.transport_from_client_path(path))
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
220
        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.
221
        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.
222
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
223
            repository._format)
4053.1.2 by Robert Collins
Actually make this branch work.
224
        network_name = repository._format.network_name()
225
        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
226
227
228
class SmartServerRequestFindRepositoryV1(SmartServerRequestFindRepository):
229
230
    def do(self, path):
231
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
232
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
233
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
234
4031.3.1 by Frank Aspell
Fixing various typos
235
        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
236
        rather than 'no branch' because these are different conditions.
237
238
        This is the initial version of this method introduced with the smart
239
        server. Modern clients will try the V2 method that adds support for the
240
        supports_external_lookups attribute.
241
242
        :return: norepository or ok, relpath.
243
        """
244
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
245
            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
246
            return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
247
        except errors.NoRepositoryPresent:
248
            return FailedSmartServerResponse(('norepository', ))
249
250
251
class SmartServerRequestFindRepositoryV2(SmartServerRequestFindRepository):
252
253
    def do(self, path):
254
        """try to find a repository from path upwards
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
255
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
256
        This operates precisely like 'bzrdir.find_repository'.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
257
4031.3.1 by Frank Aspell
Fixing various typos
258
        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
259
        rather than 'no branch' because these are different conditions.
260
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
261
        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
262
        returns information about the supports_external_lookups format
263
        attribute too.
264
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
265
        :return: norepository or ok, relpath, rich_root, tree_ref,
266
            external_lookup.
267
        """
268
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
269
            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.
270
            return SuccessfulSmartServerResponse(
271
                ('ok', path, rich_root, tree_ref, external_lookup))
272
        except errors.NoRepositoryPresent:
273
            return FailedSmartServerResponse(('norepository', ))
274
275
276
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
277
278
    def do(self, path):
279
        """try to find a repository from path upwards
280
281
        This operates precisely like 'bzrdir.find_repository'.
282
283
        If a bzrdir is not present, an exception is propogated
284
        rather than 'no branch' because these are different conditions.
285
286
        This is the third edition of this method introduced in bzr 1.13, which
287
        returns information about the network name of the repository format.
288
289
        :return: norepository or ok, relpath, rich_root, tree_ref,
290
            external_lookup, network_name.
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
291
        """
292
        try:
4053.1.2 by Robert Collins
Actually make this branch work.
293
            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
294
            return SuccessfulSmartServerResponse(
4053.1.2 by Robert Collins
Actually make this branch work.
295
                ('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.
296
        except errors.NoRepositoryPresent:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
297
            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.
298
299
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
300
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
301
302
    def do(self, path):
303
        """Initialize a bzrdir at path.
304
305
        The default format of the server is used.
306
        :return: SmartServerResponse(('ok', ))
307
        """
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
308
        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 :).
309
        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.
310
        return SuccessfulSmartServerResponse(('ok', ))
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
311
312
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.
313
class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):
314
315
    def do_bzrdir_request(self):
316
        """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.
317
        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.
318
            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.
319
            if reference_url is None:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
320
                return SuccessfulSmartServerResponse(('ok', ''))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
321
            else:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
322
                return SuccessfulSmartServerResponse(('ok', reference_url))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
323
        except errors.NotBranchError:
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
324
            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.
325
326
327
class SmartServerRequestOpenBranchV2(SmartServerRequestBzrDir):
328
329
    def do_bzrdir_request(self):
330
        """open a branch at path and return the reference or format."""
331
        try:
332
            reference_url = self._bzrdir.get_branch_reference()
333
            if reference_url is None:
4160.2.6 by Andrew Bennetts
Add ignore_fallbacks flag.
334
                br = self._bzrdir.open_branch(ignore_fallbacks=True)
335
                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.
336
                return SuccessfulSmartServerResponse(('branch', format))
337
            else:
338
                return SuccessfulSmartServerResponse(('ref', reference_url))
339
        except errors.NotBranchError:
340
            return FailedSmartServerResponse(('nobranch', ))