~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/bzrdir.py

  • Committer: Martin Pool
  • Date: 2009-03-03 03:01:49 UTC
  • mfrom: (4070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4073.
  • Revision ID: mbp@sourcefrog.net-20090303030149-8p8o8hszdtqa7w8f
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Server-side bzrdir related request implmentations."""
18
18
 
19
19
 
20
 
from bzrlib import errors
 
20
from bzrlib import branch, errors, repository
21
21
from bzrlib.bzrdir import BzrDir, BzrDirFormat
22
22
from bzrlib.smart.request import (
23
23
    FailedSmartServerResponse,
51
51
        return SuccessfulSmartServerResponse((answer,))
52
52
 
53
53
 
54
 
class SmartServerRequestFindRepository(SmartServerRequest):
 
54
class SmartServerRequestBzrDir(SmartServerRequest):
55
55
 
56
56
    def _boolean_to_yes_no(self, a_boolean):
57
57
        if a_boolean:
59
59
        else:
60
60
            return 'no'
61
61
 
62
 
    def _find(self, path):
63
 
        """try to find a repository from path upwards
64
 
        
65
 
        This operates precisely like 'bzrdir.find_repository'.
66
 
        
67
 
        :return: (relpath, rich_root, tree_ref, external_lookup) flags. All are
68
 
            strings, relpath is a / prefixed path, and the other three are
69
 
            either 'yes' or 'no'.
70
 
        :raises errors.NoRepositoryPresent: When there is no repository
71
 
            present.
72
 
        """
73
 
        bzrdir = BzrDir.open_from_transport(
74
 
            self.transport_from_client_path(path))
75
 
        repository = bzrdir.find_repository()
76
 
        # the relpath of the bzrdir in the found repository gives us the 
 
62
    def _format_to_capabilities(self, repo_format):
 
63
        rich_root = self._boolean_to_yes_no(repo_format.rich_root_data)
 
64
        tree_ref = self._boolean_to_yes_no(
 
65
            repo_format.supports_tree_reference)
 
66
        external_lookup = self._boolean_to_yes_no(
 
67
            repo_format.supports_external_lookups)
 
68
        return rich_root, tree_ref, external_lookup
 
69
 
 
70
    def _repo_relpath(self, current_transport, repository):
 
71
        """Get the relative path for repository from current_transport."""
 
72
        # the relpath of the bzrdir in the found repository gives us the
77
73
        # path segments to pop-out.
78
74
        relpath = repository.bzrdir.root_transport.relpath(
79
 
            bzrdir.root_transport.base)
 
75
            current_transport.base)
80
76
        if len(relpath):
81
77
            segments = ['..'] * len(relpath.split('/'))
82
78
        else:
83
79
            segments = []
84
 
        rich_root = self._boolean_to_yes_no(repository.supports_rich_root())
85
 
        tree_ref = self._boolean_to_yes_no(
86
 
            repository._format.supports_tree_reference)
87
 
        external_lookup = self._boolean_to_yes_no(
88
 
            repository._format.supports_external_lookups)
89
 
        return '/'.join(segments), rich_root, tree_ref, external_lookup
 
80
        return '/'.join(segments)
 
81
 
 
82
 
 
83
class SmartServerRequestCreateBranch(SmartServerRequestBzrDir):
 
84
 
 
85
    def do(self, path, network_name):
 
86
        """Create a branch in the bzr dir at path.
 
87
 
 
88
        This operates precisely like 'bzrdir.create_branch'.
 
89
 
 
90
        If a bzrdir is not present, an exception is propogated
 
91
        rather than 'no branch' because these are different conditions (and
 
92
        this method should only be called after establishing that a bzr dir
 
93
        exists anyway).
 
94
 
 
95
        This is the initial version of this method introduced to the smart
 
96
        server for 1.13.
 
97
 
 
98
        :param path: The path to the bzrdir.
 
99
        :param network_name: The network name of the branch type to create.
 
100
        :return: (ok, network_name)
 
101
        """
 
102
        bzrdir = BzrDir.open_from_transport(
 
103
            self.transport_from_client_path(path))
 
104
        format = branch.network_format_registry.get(network_name)
 
105
        bzrdir.branch_format = format
 
106
        result = format.initialize(bzrdir)
 
107
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
 
108
            result.repository._format)
 
109
        branch_format = result._format.network_name()
 
110
        repo_format = result.repository._format.network_name()
 
111
        repo_path = self._repo_relpath(bzrdir.root_transport,
 
112
            result.repository)
 
113
        # branch format, repo relpath, rich_root, tree_ref, external_lookup,
 
114
        # repo_network_name
 
115
        return SuccessfulSmartServerResponse(('ok', branch_format, repo_path,
 
116
            rich_root, tree_ref, external_lookup, repo_format))
 
117
 
 
118
 
 
119
class SmartServerRequestCreateRepository(SmartServerRequestBzrDir):
 
120
 
 
121
    def do(self, path, network_name, shared):
 
122
        """Create a repository in the bzr dir at path.
 
123
 
 
124
        This operates precisely like 'bzrdir.create_repository'.
 
125
 
 
126
        If a bzrdir is not present, an exception is propogated
 
127
        rather than 'no branch' because these are different conditions (and
 
128
        this method should only be called after establishing that a bzr dir
 
129
        exists anyway).
 
130
 
 
131
        This is the initial version of this method introduced to the smart
 
132
        server for 1.13.
 
133
 
 
134
        :param path: The path to the bzrdir.
 
135
        :param network_name: The network name of the repository type to create.
 
136
        :param shared: The value to pass create_repository for the shared
 
137
            parameter.
 
138
        :return: (ok, rich_root, tree_ref, external_lookup, network_name)
 
139
        """
 
140
        bzrdir = BzrDir.open_from_transport(
 
141
            self.transport_from_client_path(path))
 
142
        shared = shared == 'True'
 
143
        format = repository.network_format_registry.get(network_name)
 
144
        bzrdir.repository_format = format
 
145
        result = format.initialize(bzrdir, shared=shared)
 
146
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
 
147
            result._format)
 
148
        return SuccessfulSmartServerResponse(('ok', rich_root, tree_ref,
 
149
            external_lookup, result._format.network_name()))
 
150
 
 
151
 
 
152
class SmartServerRequestFindRepository(SmartServerRequestBzrDir):
 
153
 
 
154
    def _find(self, path):
 
155
        """try to find a repository from path upwards
 
156
 
 
157
        This operates precisely like 'bzrdir.find_repository'.
 
158
 
 
159
        :return: (relpath, rich_root, tree_ref, external_lookup, network_name).
 
160
            All are strings, relpath is a / prefixed path, the next three are
 
161
            either 'yes' or 'no', and the last is a repository format network
 
162
            name.
 
163
        :raises errors.NoRepositoryPresent: When there is no repository
 
164
            present.
 
165
        """
 
166
        bzrdir = BzrDir.open_from_transport(
 
167
            self.transport_from_client_path(path))
 
168
        repository = bzrdir.find_repository()
 
169
        path = self._repo_relpath(bzrdir.root_transport, repository)
 
170
        rich_root, tree_ref, external_lookup = self._format_to_capabilities(
 
171
            repository._format)
 
172
        network_name = repository._format.network_name()
 
173
        return path, rich_root, tree_ref, external_lookup, network_name
90
174
 
91
175
 
92
176
class SmartServerRequestFindRepositoryV1(SmartServerRequestFindRepository):
93
177
 
94
178
    def do(self, path):
95
179
        """try to find a repository from path upwards
96
 
        
 
180
 
97
181
        This operates precisely like 'bzrdir.find_repository'.
98
 
        
 
182
 
99
183
        If a bzrdir is not present, an exception is propogated
100
184
        rather than 'no branch' because these are different conditions.
101
185
 
106
190
        :return: norepository or ok, relpath.
107
191
        """
108
192
        try:
109
 
            path, rich_root, tree_ref, external_lookup = self._find(path)
 
193
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
110
194
            return SuccessfulSmartServerResponse(('ok', path, rich_root, tree_ref))
111
195
        except errors.NoRepositoryPresent:
112
196
            return FailedSmartServerResponse(('norepository', ))
116
200
 
117
201
    def do(self, path):
118
202
        """try to find a repository from path upwards
119
 
        
 
203
 
120
204
        This operates precisely like 'bzrdir.find_repository'.
121
 
        
 
205
 
122
206
        If a bzrdir is not present, an exception is propogated
123
207
        rather than 'no branch' because these are different conditions.
124
208
 
126
210
        returns information about the supports_external_lookups format
127
211
        attribute too.
128
212
 
129
 
        :return: norepository or ok, relpath.
 
213
        :return: norepository or ok, relpath, rich_root, tree_ref,
 
214
            external_lookup.
130
215
        """
131
216
        try:
132
 
            path, rich_root, tree_ref, external_lookup = self._find(path)
 
217
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
133
218
            return SuccessfulSmartServerResponse(
134
219
                ('ok', path, rich_root, tree_ref, external_lookup))
135
220
        except errors.NoRepositoryPresent:
136
221
            return FailedSmartServerResponse(('norepository', ))
137
222
 
138
223
 
 
224
class SmartServerRequestFindRepositoryV3(SmartServerRequestFindRepository):
 
225
 
 
226
    def do(self, path):
 
227
        """try to find a repository from path upwards
 
228
 
 
229
        This operates precisely like 'bzrdir.find_repository'.
 
230
 
 
231
        If a bzrdir is not present, an exception is propogated
 
232
        rather than 'no branch' because these are different conditions.
 
233
 
 
234
        This is the third edition of this method introduced in bzr 1.13, which
 
235
        returns information about the network name of the repository format.
 
236
 
 
237
        :return: norepository or ok, relpath, rich_root, tree_ref,
 
238
            external_lookup, network_name.
 
239
        """
 
240
        try:
 
241
            path, rich_root, tree_ref, external_lookup, name = self._find(path)
 
242
            return SuccessfulSmartServerResponse(
 
243
                ('ok', path, rich_root, tree_ref, external_lookup, name))
 
244
        except errors.NoRepositoryPresent:
 
245
            return FailedSmartServerResponse(('norepository', ))
 
246
 
 
247
 
139
248
class SmartServerRequestInitializeBzrDir(SmartServerRequest):
140
249
 
141
250
    def do(self, path):
153
262
 
154
263
    def do(self, path):
155
264
        """try to open a branch at path and return ok/nobranch.
156
 
        
 
265
 
157
266
        If a bzrdir is not present, an exception is propogated
158
267
        rather than 'no branch' because these are different conditions.
159
268
        """