/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 colocated branch 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,
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):
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)
204
222
        self._lockfiles = lockfiles
205
223
        self._mode_check_done = None
206
224
 
207
 
    def _branch_name_to_ref(self, name, default=None):
208
 
        return branch_name_to_ref(name, default=default)
 
225
    @property
 
226
    def user_url(self):
 
227
        return self.control_url
209
228
 
210
229
    def open_repository(self):
211
230
        return RemoteGitRepository(self, self._lockfiles)
212
231
 
213
 
    def _open_branch(self, name=None, ignore_fallbacks=False, 
214
 
                    unsupported=False):
 
232
    def open_branch(self, name=None, unsupported=False,
 
233
            ignore_fallbacks=False):
215
234
        repo = self.open_repository()
216
 
        refname = self._branch_name_to_ref(name)
 
235
        refname = self._get_selected_ref(name)
217
236
        return RemoteGitBranch(self, repo, refname, self._lockfiles)
218
237
 
219
238
    def open_workingtree(self, recommend_upgrade=False):
235
254
    @property
236
255
    def data(self):
237
256
        if self._data is None:
238
 
            self._data = ThinPackData(self.resolve_ext_ref, self._data_path)
 
257
            self._data = PackData(self._data_path)
239
258
        return self._data
240
259
 
241
260
    @property
262
281
            os.remove(self._data_path)
263
282
 
264
283
 
 
284
class RemoteGitControlDirFormat(GitControlDirFormat):
 
285
    """The .git directory control format."""
 
286
 
 
287
    supports_workingtrees = False
 
288
 
 
289
    @classmethod
 
290
    def _known_formats(self):
 
291
        return set([RemoteGitControlDirFormat()])
 
292
 
 
293
    def open(self, transport, _found=None):
 
294
        """Open this directory.
 
295
 
 
296
        """
 
297
        # we dont grok readonly - git isn't integrated with transport.
 
298
        url = transport.base
 
299
        if url.startswith('readonly+'):
 
300
            url = url[len('readonly+'):]
 
301
        if (not url.startswith("git://") and not url.startswith("git+")):
 
302
            raise NotBranchError(transport.base)
 
303
        if not isinstance(transport, GitSmartTransport):
 
304
            raise NotBranchError(transport.base)
 
305
        lockfiles = GitLockableFiles(transport, GitLock())
 
306
        return RemoteGitDir(transport, lockfiles, self)
 
307
 
 
308
    def get_format_description(self):
 
309
        return "Remote Git Repository"
 
310
 
 
311
    def initialize_on_transport(self, transport):
 
312
        raise UninitializableFormat(self)
 
313
 
 
314
 
265
315
class RemoteGitRepository(GitRepository):
266
316
 
267
317
    def __init__(self, gitdir, lockfiles):
269
319
        self._refs = None
270
320
 
271
321
    @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):
 
322
    def user_url(self):
 
323
        return self.control_url
 
324
 
 
325
    def get_parent_map(self, revids):
281
326
        raise GitSmartRemoteNotSupported()
282
327
 
283
328
    def get_refs(self):
298
343
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
299
344
                      progress=None):
300
345
        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)
 
346
        try:
 
347
            self.fetch_pack(determine_wants, graph_walker,
 
348
                lambda x: os.write(fd, x), progress)
 
349
        finally:
 
350
            os.close(fd)
304
351
        if os.path.getsize(path) == 0:
305
352
            return EmptyObjectStoreIterator()
306
353
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
322
369
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
323
370
 
324
371
 
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
 
372
class RemoteGitTagDict(GitTags):
 
373
 
 
374
    def get_refs(self):
 
375
        return self.repository.get_refs()
 
376
 
 
377
    def _iter_tag_refs(self, refs):
 
378
        for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
 
379
            yield (k, peeled, unpeeled,
 
380
                  self.branch.mapping.revision_id_foreign_to_bzr(peeled))
336
381
 
337
382
    def set_tag(self, name, revid):
338
383
        # FIXME: Not supported yet, should do a push of a new ref
346
391
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
347
392
                lockfiles)
348
393
 
 
394
    @property
 
395
    def user_url(self):
 
396
        return self.control_url
 
397
 
 
398
    @property
 
399
    def control_url(self):
 
400
        return self.base
 
401
 
349
402
    def revision_history(self):
350
403
        raise GitSmartRemoteNotSupported()
351
404
 
365
418
        if self._sha is not None:
366
419
            return self._sha
367
420
        heads = self.repository.get_refs()
368
 
        name = self.bzrdir._branch_name_to_ref(self.name, "HEAD")
 
421
        name = branch_name_to_ref(self.name, "HEAD")
369
422
        if name in heads:
370
423
            self._sha = heads[name]
371
424
        else: