/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to remote.py

Fix more tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from bzrlib import (
18
18
    config,
19
19
    debug,
20
 
    tag,
21
20
    trace,
22
21
    ui,
23
22
    urlutils,
27
26
    InvalidRevisionId,
28
27
    NoSuchFile,
29
28
    NoSuchRevision,
 
29
    NotBranchError,
30
30
    NotLocalUrl,
 
31
    UninitializableFormat,
31
32
    )
32
33
from bzrlib.transport import (
33
34
    Transport,
40
41
 
41
42
from bzrlib.plugins.git.branch import (
42
43
    GitBranch,
 
44
    GitTags,
 
45
    )
 
46
from bzrlib.plugins.git.dir import (
 
47
    GitControlDirFormat,
 
48
    GitDir,
 
49
    GitLockableFiles,
 
50
    GitLock,
43
51
    )
44
52
from bzrlib.plugins.git.errors import (
45
53
    GitSmartRemoteNotSupported,
46
54
    NoSuchRef,
47
55
    )
48
 
from bzrlib.plugins.git.dir import (
49
 
    GitDir,
50
 
    )
51
56
from bzrlib.plugins.git.mapping import (
52
57
    mapping_registry,
53
58
    )
65
70
    )
66
71
from dulwich.pack import (
67
72
    Pack,
68
 
    ThinPackData,
 
73
    PackData,
69
74
    )
70
75
import os
71
76
import tempfile
97
102
    return (host, port, username, path)
98
103
 
99
104
 
 
105
def parse_git_error(url, message):
 
106
    """Parse a remote git server error and return a bzr exception.
 
107
 
 
108
    :param url: URL of the remote repository
 
109
    :param message: Message sent by the remote git server
 
110
    """
 
111
    message = str(message).strip()
 
112
    if message.startswith("Could not find Repository "):
 
113
        return NotBranchError(url, message)
 
114
    # Don't know, just return it to the user as-is
 
115
    return BzrError(message)
 
116
 
 
117
 
100
118
class GitSmartTransport(Transport):
101
119
 
102
120
    def __init__(self, url, _client=None):
118
136
        raise NotImplementedError(self._get_client)
119
137
 
120
138
    def _get_path(self):
121
 
        return self._path
 
139
        return urlutils.split_segment_parameters_raw(self._path)[0]
122
140
 
123
141
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
124
142
        if progress is None:
129
147
            return client.fetch_pack(self._get_path(), determine_wants,
130
148
                graph_walker, pack_data, progress)
131
149
        except GitProtocolError, e:
132
 
            raise BzrError(e)
 
150
            raise parse_git_error(self.external_url(), e)
133
151
 
134
152
    def send_pack(self, get_changed_refs, generate_pack_contents):
135
153
        client = self._get_client(thin_packs=False)
137
155
            return client.send_pack(self._get_path(), get_changed_refs,
138
156
                generate_pack_contents)
139
157
        except GitProtocolError, e:
140
 
            raise BzrError(e)
 
158
            raise parse_git_error(self.external_url(), e)
141
159
 
142
160
    def get(self, path):
143
161
        raise NoSuchFile(path)
173
191
    _scheme = 'git+ssh'
174
192
 
175
193
    def _get_path(self):
176
 
        if self._path.startswith("/~/"):
177
 
            return self._path[3:]
178
 
        return self._path
 
194
        path = urlutils.split_segment_parameters_raw(self._path)[0]
 
195
        if path.startswith("/~/"):
 
196
            return path[3:]
 
197
        return path
179
198
 
180
199
    def _get_client(self, thin_packs):
181
200
        if self._client is not None:
204
223
        self._lockfiles = lockfiles
205
224
        self._mode_check_done = None
206
225
 
207
 
    def _branch_name_to_ref(self, name, default=None):
208
 
        return branch_name_to_ref(name, default=default)
 
226
    @property
 
227
    def user_url(self):
 
228
        return self.control_url
 
229
 
 
230
    @property
 
231
    def user_transport(self):
 
232
        return self.root_transport
209
233
 
210
234
    def open_repository(self):
211
235
        return RemoteGitRepository(self, self._lockfiles)
212
236
 
213
 
    def _open_branch(self, name=None, ignore_fallbacks=False, 
214
 
                    unsupported=False):
 
237
    def open_branch(self, name=None, unsupported=False,
 
238
            ignore_fallbacks=False):
215
239
        repo = self.open_repository()
216
 
        refname = self._branch_name_to_ref(name)
 
240
        refname = self._get_selected_ref(name)
217
241
        return RemoteGitBranch(self, repo, refname, self._lockfiles)
218
242
 
219
243
    def open_workingtree(self, recommend_upgrade=False):
235
259
    @property
236
260
    def data(self):
237
261
        if self._data is None:
238
 
            self._data = ThinPackData(self.resolve_ext_ref, self._data_path)
 
262
            self._data = PackData(self._data_path)
239
263
        return self._data
240
264
 
241
265
    @property
262
286
            os.remove(self._data_path)
263
287
 
264
288
 
 
289
class RemoteGitControlDirFormat(GitControlDirFormat):
 
290
    """The .git directory control format."""
 
291
 
 
292
    supports_workingtrees = False
 
293
 
 
294
    @classmethod
 
295
    def _known_formats(self):
 
296
        return set([RemoteGitControlDirFormat()])
 
297
 
 
298
    def open(self, transport, _found=None):
 
299
        """Open this directory.
 
300
 
 
301
        """
 
302
        # we dont grok readonly - git isn't integrated with transport.
 
303
        url = transport.base
 
304
        if url.startswith('readonly+'):
 
305
            url = url[len('readonly+'):]
 
306
        if (not url.startswith("git://") and not url.startswith("git+")):
 
307
            raise NotBranchError(transport.base)
 
308
        if not isinstance(transport, GitSmartTransport):
 
309
            raise NotBranchError(transport.base)
 
310
        lockfiles = GitLockableFiles(transport, GitLock())
 
311
        return RemoteGitDir(transport, lockfiles, self)
 
312
 
 
313
    def get_format_description(self):
 
314
        return "Remote Git Repository"
 
315
 
 
316
    def initialize_on_transport(self, transport):
 
317
        raise UninitializableFormat(self)
 
318
 
 
319
 
265
320
class RemoteGitRepository(GitRepository):
266
321
 
267
322
    def __init__(self, gitdir, lockfiles):
269
324
        self._refs = None
270
325
 
271
326
    @property
272
 
    def inventories(self):
273
 
        raise GitSmartRemoteNotSupported()
274
 
 
275
 
    @property
276
 
    def revisions(self):
277
 
        raise GitSmartRemoteNotSupported()
278
 
 
279
 
    @property
280
 
    def texts(self):
 
327
    def user_url(self):
 
328
        return self.control_url
 
329
 
 
330
    def get_parent_map(self, revids):
281
331
        raise GitSmartRemoteNotSupported()
282
332
 
283
333
    def get_refs(self):
298
348
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
299
349
                      progress=None):
300
350
        fd, path = tempfile.mkstemp(suffix=".pack")
301
 
        self.fetch_pack(determine_wants, graph_walker,
302
 
            lambda x: os.write(fd, x), progress)
303
 
        os.close(fd)
 
351
        try:
 
352
            self.fetch_pack(determine_wants, graph_walker,
 
353
                lambda x: os.write(fd, x), progress)
 
354
        finally:
 
355
            os.close(fd)
304
356
        if os.path.getsize(path) == 0:
305
357
            return EmptyObjectStoreIterator()
306
358
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
322
374
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
323
375
 
324
376
 
325
 
class RemoteGitTagDict(tag.BasicTags):
326
 
 
327
 
    def __init__(self, branch):
328
 
        self.branch = branch
329
 
        self.repository = branch.repository
330
 
 
331
 
    def get_tag_dict(self):
332
 
        tags = {}
333
 
        for k, v in extract_tags(self.repository.get_refs()).iteritems():
334
 
            tags[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
335
 
        return tags
 
377
class RemoteGitTagDict(GitTags):
 
378
 
 
379
    def get_refs(self):
 
380
        return self.repository.get_refs()
 
381
 
 
382
    def _iter_tag_refs(self, refs):
 
383
        for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
 
384
            yield (k, peeled, unpeeled,
 
385
                  self.branch.mapping.revision_id_foreign_to_bzr(peeled))
336
386
 
337
387
    def set_tag(self, name, revid):
338
388
        # FIXME: Not supported yet, should do a push of a new ref
346
396
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
347
397
                lockfiles)
348
398
 
 
399
    def last_revision_info(self):
 
400
        raise GitSmartRemoteNotSupported()
 
401
 
 
402
    @property
 
403
    def user_url(self):
 
404
        return self.control_url
 
405
 
 
406
    @property
 
407
    def control_url(self):
 
408
        return self.base
 
409
 
349
410
    def revision_history(self):
350
411
        raise GitSmartRemoteNotSupported()
351
412
 
365
426
        if self._sha is not None:
366
427
            return self._sha
367
428
        heads = self.repository.get_refs()
368
 
        name = self.bzrdir._branch_name_to_ref(self.name, "HEAD")
 
429
        name = branch_name_to_ref(self.name, "HEAD")
369
430
        if name in heads:
370
431
            self._sha = heads[name]
371
432
        else: