~bzr-pqm/bzr/bzr.dev

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