/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 issues pointed out by pyflakes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2008 Canonical Ltd
 
1
# Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import bzrlib
18
 
from bzrlib import urlutils
19
 
from bzrlib.bzrdir import BzrDir, BzrDirFormat
20
 
from bzrlib.errors import NoSuchFile, NotLocalUrl
21
 
from bzrlib.lockable_files import TransportLock
22
 
from bzrlib.repository import Repository
23
 
from bzrlib.trace import info
24
 
from bzrlib.transport import Transport
 
18
from bzrlib import (
 
19
    tag,
 
20
    trace,
 
21
    ui,
 
22
    urlutils,
 
23
    )
 
24
from bzrlib.errors import (
 
25
    BzrError,
 
26
    InvalidRevisionId,
 
27
    NoSuchFile,
 
28
    NoSuchRevision,
 
29
    NotLocalUrl,
 
30
    )
 
31
from bzrlib.trace import (
 
32
    info,
 
33
    )
 
34
from bzrlib.transport import (
 
35
    Transport,
 
36
    )
25
37
 
26
 
from bzrlib.plugins.git import lazy_check_versions
 
38
from bzrlib.plugins.git import (
 
39
    lazy_check_versions,
 
40
    )
27
41
lazy_check_versions()
28
42
 
29
 
from bzrlib.plugins.git.branch import GitBranch
30
 
from bzrlib.plugins.git.errors import NoSuchRef
31
 
from bzrlib.plugins.git.dir import GitDir
32
 
from bzrlib.plugins.git.foreign import ForeignBranch
33
 
from bzrlib.plugins.git.repository import GitFormat, GitRepository
 
43
from bzrlib.plugins.git.branch import (
 
44
    GitBranch,
 
45
    extract_tags,
 
46
    )
 
47
from bzrlib.plugins.git.errors import (
 
48
    GitSmartRemoteNotSupported,
 
49
    NoSuchRef,
 
50
    )
 
51
from bzrlib.plugins.git.dir import (
 
52
    GitDir,
 
53
    )
 
54
from bzrlib.plugins.git.mapping import (
 
55
    mapping_registry,
 
56
    )
 
57
from bzrlib.plugins.git.repository import (
 
58
    GitRepository,
 
59
    )
34
60
 
 
61
import dulwich as git
 
62
from dulwich.errors import (
 
63
    GitProtocolError,
 
64
    )
 
65
from dulwich.pack import (
 
66
    Pack,
 
67
    )
35
68
import os
36
69
import tempfile
37
70
import urllib
38
71
import urlparse
39
72
 
40
 
import dulwich as git
41
 
from dulwich.pack import PackData, Pack, PackIndex
 
73
from dulwich.pack import load_pack_index
 
74
 
42
75
 
43
76
# Don't run any tests on GitSmartTransport as it is not intended to be 
44
77
# a full implementation of Transport
51
84
    def __init__(self, url, _client=None):
52
85
        Transport.__init__(self, url)
53
86
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
54
 
        assert scheme == "git"
55
87
        hostport, self._path = urllib.splithost(loc)
56
 
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
 
88
        (self._username, hostport) = urllib.splituser(hostport)
 
89
        (self._host, self._port) = urllib.splitnport(hostport, None)
57
90
        self._client = _client
58
91
 
59
 
    def _get_client(self):
60
 
        if self._client is not None:
61
 
            ret = self._client
62
 
            self._client = None
63
 
            return ret
64
 
        return git.client.TCPGitClient(self._host, self._port, 
65
 
            capabilities=["multi_ack", "side-band-64k", "ofs-delta", "side-band"])
 
92
    def external_url(self):
 
93
        return self.base
 
94
 
 
95
    def has(self, relpath):
 
96
        return False
 
97
 
 
98
    def _get_client(self, thin_packs):
 
99
        raise NotImplementedError(self._get_client)
 
100
 
 
101
    def _get_path(self):
 
102
        return self._path
66
103
 
67
104
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
68
105
        if progress is None:
69
106
            def progress(text):
70
107
                info("git: %s" % text)
71
 
        self._get_client().fetch_pack(self._path, determine_wants, 
72
 
            graph_walker, pack_data, progress)
 
108
        client = self._get_client(thin_packs=False)
 
109
        try:
 
110
            return client.fetch_pack(self._get_path(), determine_wants, 
 
111
                graph_walker, pack_data, progress)
 
112
        except GitProtocolError, e:
 
113
            raise BzrError(e)
 
114
 
 
115
    def send_pack(self, get_changed_refs, generate_pack_contents):
 
116
        client = self._get_client(thin_packs=False)
 
117
        try:
 
118
            return client.send_pack(self._get_path(), get_changed_refs, 
 
119
                generate_pack_contents)
 
120
        except GitProtocolError, e:
 
121
            raise BzrError(e)
73
122
 
74
123
    def get(self, path):
75
124
        raise NoSuchFile(path)
84
133
        else:
85
134
            newurl = urlutils.join(self.base, offset)
86
135
 
87
 
        return GitSmartTransport(newurl, self._client)
 
136
        return self.__class__(newurl, self._client)
 
137
 
 
138
 
 
139
class TCPGitSmartTransport(GitSmartTransport):
 
140
 
 
141
    _scheme = 'git'
 
142
 
 
143
    def _get_client(self, thin_packs):
 
144
        if self._client is not None:
 
145
            ret = self._client
 
146
            self._client = None
 
147
            return ret
 
148
        return git.client.TCPGitClient(self._host, self._port, thin_packs=thin_packs,
 
149
            report_activity=self._report_activity)
 
150
 
 
151
 
 
152
class SSHGitSmartTransport(GitSmartTransport):
 
153
 
 
154
    _scheme = 'git+ssh'
 
155
 
 
156
    def _get_path(self):
 
157
        if self._path.startswith("/~/"):
 
158
            return self._path[3:]
 
159
        return self._path
 
160
 
 
161
    def _get_client(self, thin_packs):
 
162
        if self._client is not None:
 
163
            ret = self._client
 
164
            self._client = None
 
165
            return ret
 
166
        return git.client.SSHGitClient(self._host, self._port, self._username,
 
167
            thin_packs=thin_packs, report_activity=self._report_activity)
88
168
 
89
169
 
90
170
class RemoteGitDir(GitDir):
94
174
        self.root_transport = transport
95
175
        self.transport = transport
96
176
        self._lockfiles = lockfiles
 
177
        self._mode_check_done = None
97
178
 
98
179
    def open_repository(self):
99
180
        return RemoteGitRepository(self, self._lockfiles)
100
181
 
101
 
    def open_branch(self, _unsupported=False):
 
182
    def open_branch(self, ignore_fallbacks=False):
102
183
        repo = self.open_repository()
103
184
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
104
185
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
116
197
class TemporaryPackIterator(Pack):
117
198
 
118
199
    def __init__(self, path, resolve_ext_ref):
 
200
        super(TemporaryPackIterator, self).__init__(path)
119
201
        self.resolve_ext_ref = resolve_ext_ref
120
 
        super(TemporaryPackIterator, self).__init__(path)
121
202
 
122
203
    @property
123
 
    def idx(self):
 
204
    def index(self):
124
205
        if self._idx is None:
125
 
            self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
126
 
            self._idx = PackIndex(self._idx_path)
 
206
            if not os.path.exists(self._idx_path):
 
207
                pb = ui.ui_factory.nested_progress_bar()
 
208
                try:
 
209
                    def report_progress(cur, total):
 
210
                        pb.update("generating index", cur, total)
 
211
                    self.data.create_index(self._idx_path, self.resolve_ext_ref,
 
212
                        progress=report_progress)
 
213
                finally:
 
214
                    pb.finished()
 
215
            self._idx = load_pack_index(self._idx_path)
127
216
        return self._idx
128
217
 
129
218
    def __del__(self):
135
224
 
136
225
    def __init__(self, gitdir, lockfiles):
137
226
        GitRepository.__init__(self, gitdir, lockfiles)
 
227
        self._refs = None
 
228
 
 
229
    @property
 
230
    def inventories(self):
 
231
        raise GitSmartRemoteNotSupported()
 
232
 
 
233
    @property
 
234
    def revisions(self):
 
235
        raise GitSmartRemoteNotSupported()
 
236
 
 
237
    @property
 
238
    def texts(self):
 
239
        raise GitSmartRemoteNotSupported()
 
240
 
 
241
    def get_refs(self):
 
242
        if self._refs is not None:
 
243
            return self._refs
 
244
        self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None, 
 
245
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
 
246
        return self._refs
138
247
 
139
248
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
140
249
                   progress=None):
141
 
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
142
 
            progress)
 
250
        return self._transport.fetch_pack(determine_wants, graph_walker,
 
251
                                          pack_data, progress)
 
252
 
 
253
    def send_pack(self, get_changed_refs, generate_pack_contents):
 
254
        return self._transport.send_pack(get_changed_refs, generate_pack_contents)
143
255
 
144
256
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
145
257
        fd, path = tempfile.mkstemp(suffix=".pack")
149
261
            return EmptyObjectStoreIterator()
150
262
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
151
263
 
 
264
    def lookup_git_revid(self, bzr_revid):
 
265
        # This won't work for any round-tripped bzr revisions, but it's a start..
 
266
        try:
 
267
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
268
        except InvalidRevisionId:
 
269
            raise NoSuchRevision(self, bzr_revid)
 
270
 
 
271
 
 
272
class RemoteGitTagDict(tag.BasicTags):
 
273
 
 
274
    def __init__(self, branch):
 
275
        self.branch = branch
 
276
        self.repository = branch.repository
 
277
 
 
278
    def get_tag_dict(self):
 
279
        return extract_tags(self.repository.get_refs(), self.branch.mapping)
 
280
 
 
281
    def set_tag(self, name, revid):
 
282
        # FIXME: Not supported yet, should do a push of a new ref
 
283
        raise NotImplementedError(self.set_tag)
 
284
 
152
285
 
153
286
class RemoteGitBranch(GitBranch):
154
287
 
155
288
    def __init__(self, bzrdir, repository, name, lockfiles):
156
 
        def determine_wants(heads):
157
 
            if not name in heads:
158
 
                raise NoSuchRef(name)
159
 
            self._ref = heads[name]
160
 
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
161
 
                             lambda x: mutter("git: %s" % x))
162
 
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
 
289
        self._ref = None
 
290
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, 
 
291
                lockfiles)
 
292
 
 
293
    def revision_history(self):
 
294
        raise GitSmartRemoteNotSupported()
163
295
 
164
296
    def last_revision(self):
165
 
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
 
297
        return self.mapping.revision_id_foreign_to_bzr(self.head)
 
298
 
 
299
    @property
 
300
    def head(self):
 
301
        if self._ref is not None:
 
302
            return self._ref
 
303
        heads = self.repository.get_refs()
 
304
        if not self.name in heads:
 
305
            raise NoSuchRef(self.name)
 
306
        self._ref = heads[self.name]
 
307
        return self._ref
166
308
 
167
309
    def _synchronize_history(self, destination, revision_id):
168
310
        """See Branch._synchronize_history()."""
169
311
        destination.generate_revision_history(self.last_revision())
170
312
 
 
313
    def get_push_location(self):
 
314
        return None
 
315
 
 
316
    def set_push_location(self, url):
 
317
        pass