~bzr-pqm/bzr/bzr.dev

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