/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

More work on roundtrip push support.

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,
20
21
    trace,
21
22
    ui,
22
23
    urlutils,
26
27
    InvalidRevisionId,
27
28
    NoSuchFile,
28
29
    NoSuchRevision,
29
 
    NotBranchError,
30
30
    NotLocalUrl,
31
 
    UninitializableFormat,
32
31
    )
33
32
from bzrlib.transport import (
34
33
    Transport,
41
40
 
42
41
from bzrlib.plugins.git.branch import (
43
42
    GitBranch,
44
 
    GitTags,
45
 
    )
46
 
from bzrlib.plugins.git.dir import (
47
 
    GitControlDirFormat,
48
 
    GitDir,
49
 
    GitLockableFiles,
50
 
    GitLock,
51
43
    )
52
44
from bzrlib.plugins.git.errors import (
53
45
    GitSmartRemoteNotSupported,
54
46
    NoSuchRef,
55
47
    )
 
48
from bzrlib.plugins.git.dir import (
 
49
    GitDir,
 
50
    )
56
51
from bzrlib.plugins.git.mapping import (
57
52
    mapping_registry,
58
53
    )
70
65
    )
71
66
from dulwich.pack import (
72
67
    Pack,
73
 
    PackData,
 
68
    ThinPackData,
74
69
    )
75
70
import os
76
71
import tempfile
102
97
    return (host, port, username, path)
103
98
 
104
99
 
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
 
 
118
100
class GitSmartTransport(Transport):
119
101
 
120
102
    def __init__(self, url, _client=None):
136
118
        raise NotImplementedError(self._get_client)
137
119
 
138
120
    def _get_path(self):
139
 
        return urlutils.split_segment_parameters_raw(self._path)[0]
 
121
        return self._path
140
122
 
141
123
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
142
124
        if progress is None:
147
129
            return client.fetch_pack(self._get_path(), determine_wants,
148
130
                graph_walker, pack_data, progress)
149
131
        except GitProtocolError, e:
150
 
            raise parse_git_error(self.external_url(), e)
 
132
            raise BzrError(e)
151
133
 
152
134
    def send_pack(self, get_changed_refs, generate_pack_contents):
153
135
        client = self._get_client(thin_packs=False)
155
137
            return client.send_pack(self._get_path(), get_changed_refs,
156
138
                generate_pack_contents)
157
139
        except GitProtocolError, e:
158
 
            raise parse_git_error(self.external_url(), e)
 
140
            raise BzrError(e)
159
141
 
160
142
    def get(self, path):
161
143
        raise NoSuchFile(path)
191
173
    _scheme = 'git+ssh'
192
174
 
193
175
    def _get_path(self):
194
 
        path = urlutils.split_segment_parameters_raw(self._path)[0]
195
 
        if path.startswith("/~/"):
196
 
            return path[3:]
197
 
        return path
 
176
        if self._path.startswith("/~/"):
 
177
            return self._path[3:]
 
178
        return self._path
198
179
 
199
180
    def _get_client(self, thin_packs):
200
181
        if self._client is not None:
223
204
        self._lockfiles = lockfiles
224
205
        self._mode_check_done = None
225
206
 
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
 
207
    def _branch_name_to_ref(self, name, default=None):
 
208
        return branch_name_to_ref(name, default=default)
233
209
 
234
210
    def open_repository(self):
235
211
        return RemoteGitRepository(self, self._lockfiles)
236
212
 
237
 
    def open_branch(self, name=None, unsupported=False,
238
 
            ignore_fallbacks=False):
 
213
    def _open_branch(self, name=None, ignore_fallbacks=False, 
 
214
                    unsupported=False):
239
215
        repo = self.open_repository()
240
 
        refname = self._get_selected_ref(name)
 
216
        refname = self._branch_name_to_ref(name)
241
217
        return RemoteGitBranch(self, repo, refname, self._lockfiles)
242
218
 
243
219
    def open_workingtree(self, recommend_upgrade=False):
259
235
    @property
260
236
    def data(self):
261
237
        if self._data is None:
262
 
            self._data = PackData(self._data_path)
 
238
            self._data = ThinPackData(self.resolve_ext_ref, self._data_path)
263
239
        return self._data
264
240
 
265
241
    @property
286
262
            os.remove(self._data_path)
287
263
 
288
264
 
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
 
 
320
265
class RemoteGitRepository(GitRepository):
321
266
 
322
267
    def __init__(self, gitdir, lockfiles):
324
269
        self._refs = None
325
270
 
326
271
    @property
327
 
    def user_url(self):
328
 
        return self.control_url
329
 
 
330
 
    def get_parent_map(self, revids):
 
272
    def inventories(self):
 
273
        raise GitSmartRemoteNotSupported()
 
274
 
 
275
    @property
 
276
    def revisions(self):
 
277
        raise GitSmartRemoteNotSupported()
 
278
 
 
279
    @property
 
280
    def texts(self):
331
281
        raise GitSmartRemoteNotSupported()
332
282
 
333
283
    def get_refs(self):
348
298
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
349
299
                      progress=None):
350
300
        fd, path = tempfile.mkstemp(suffix=".pack")
351
 
        try:
352
 
            self.fetch_pack(determine_wants, graph_walker,
353
 
                lambda x: os.write(fd, x), progress)
354
 
        finally:
355
 
            os.close(fd)
 
301
        self.fetch_pack(determine_wants, graph_walker,
 
302
            lambda x: os.write(fd, x), progress)
 
303
        os.close(fd)
356
304
        if os.path.getsize(path) == 0:
357
305
            return EmptyObjectStoreIterator()
358
306
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
374
322
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
375
323
 
376
324
 
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))
 
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
386
336
 
387
337
    def set_tag(self, name, revid):
388
338
        # FIXME: Not supported yet, should do a push of a new ref
396
346
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
397
347
                lockfiles)
398
348
 
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
 
 
410
349
    def revision_history(self):
411
350
        raise GitSmartRemoteNotSupported()
412
351
 
426
365
        if self._sha is not None:
427
366
            return self._sha
428
367
        heads = self.repository.get_refs()
429
 
        name = branch_name_to_ref(self.name, "HEAD")
 
368
        name = self.bzrdir._branch_name_to_ref(self.name, "HEAD")
430
369
        if name in heads:
431
370
            self._sha = heads[name]
432
371
        else: