/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

Add basic infrastructure for dpush.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2007-2008 Canonical Ltd
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 (
19
 
    branch,
20
 
    tag,
21
 
    ui,
22
 
    urlutils,
23
 
    )
24
 
from bzrlib.errors import (
25
 
    BzrError,
26
 
    NoSuchFile,
27
 
    NotLocalUrl,
28
 
    )
29
 
from bzrlib.trace import (
30
 
    info,
31
 
    )
32
 
from bzrlib.transport import (
33
 
    Transport,
34
 
    )
 
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
35
25
 
36
 
from bzrlib.plugins.git import (
37
 
    lazy_check_versions,
38
 
    )
 
26
from bzrlib.plugins.git import lazy_check_versions
39
27
lazy_check_versions()
40
28
 
41
 
from bzrlib.plugins.git.branch import (
42
 
    GitBranch,
43
 
    )
44
 
from bzrlib.plugins.git.errors import (
45
 
    GitSmartRemoteNotSupported,
46
 
    NoSuchRef,
47
 
    )
48
 
from bzrlib.plugins.git.dir import (
49
 
    GitDir,
50
 
    )
51
 
from bzrlib.plugins.git.repository import (
52
 
    GitRepositoryFormat,
53
 
    GitRepository,
54
 
    )
 
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
55
34
 
56
 
import dulwich as git
57
 
from dulwich.errors import (
58
 
    GitProtocolError,
59
 
    )
60
 
from dulwich.pack import (
61
 
    Pack,
62
 
    PackData,
63
 
    )
64
35
import os
65
36
import tempfile
66
37
import urllib
67
38
import urlparse
68
39
 
69
 
try:
70
 
    from dulwich.pack import load_pack_index
71
 
except ImportError:
72
 
    from dulwich.pack import PackIndex as load_pack_index
73
 
 
 
40
import dulwich as git
 
41
from dulwich.pack import PackData, Pack
74
42
 
75
43
# Don't run any tests on GitSmartTransport as it is not intended to be 
76
44
# a full implementation of Transport
83
51
    def __init__(self, url, _client=None):
84
52
        Transport.__init__(self, url)
85
53
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
 
54
        assert scheme == "git"
86
55
        hostport, self._path = urllib.splithost(loc)
87
 
        (self._host, self._port) = urllib.splitnport(hostport, None)
 
56
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
88
57
        self._client = _client
89
58
 
90
 
    def has(self, relpath):
91
 
        return False
92
 
 
93
59
    def _get_client(self):
94
 
        raise NotImplementedError(self._get_client)
 
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)
95
65
 
96
66
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
97
67
        if progress is None:
98
68
            def progress(text):
99
69
                info("git: %s" % text)
100
 
        client = self._get_client()
101
 
        try:
102
 
            client.fetch_pack(self._path, determine_wants, 
103
 
                graph_walker, pack_data, progress)
104
 
        except GitProtocolError, e:
105
 
            raise BzrError(e)
 
70
        self._get_client().fetch_pack(self._path, determine_wants, 
 
71
            graph_walker, pack_data, progress)
106
72
 
107
73
    def get(self, path):
108
74
        raise NoSuchFile(path)
117
83
        else:
118
84
            newurl = urlutils.join(self.base, offset)
119
85
 
120
 
        return self.__class__(newurl, self._client)
121
 
 
122
 
 
123
 
class TCPGitSmartTransport(GitSmartTransport):
124
 
 
125
 
    _scheme = 'git'
126
 
 
127
 
    def _get_client(self):
128
 
        if self._client is not None:
129
 
            ret = self._client
130
 
            self._client = None
131
 
            return ret
132
 
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
133
 
            report_activity=self._report_activity)
134
 
 
135
 
 
136
 
class SSHGitSmartTransport(GitSmartTransport):
137
 
 
138
 
    _scheme = 'git+ssh'
139
 
 
140
 
    def _get_client(self):
141
 
        if self._client is not None:
142
 
            ret = self._client
143
 
            self._client = None
144
 
            return ret
145
 
        return git.client.SSHGitClient(self._host, self._port, thin_packs=False,
146
 
            report_activity=self._report_activity)
 
86
        return GitSmartTransport(newurl, self._client)
147
87
 
148
88
 
149
89
class RemoteGitDir(GitDir):
153
93
        self.root_transport = transport
154
94
        self.transport = transport
155
95
        self._lockfiles = lockfiles
156
 
        self._mode_check_done = None
157
96
 
158
97
    def open_repository(self):
159
98
        return RemoteGitRepository(self, self._lockfiles)
160
99
 
161
 
    def open_branch(self, ignore_fallbacks=False):
 
100
    def open_branch(self, _unsupported=False):
162
101
        repo = self.open_repository()
163
102
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
164
103
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
167
106
        raise NotLocalUrl(self.transport.base)
168
107
 
169
108
 
170
 
class EmptyObjectStoreIterator(dict):
171
 
 
172
 
    def iterobjects(self):
173
 
        return []
174
 
 
175
 
 
176
 
class TemporaryPackIterator(Pack):
177
 
 
178
 
    def __init__(self, path, resolve_ext_ref):
179
 
        super(TemporaryPackIterator, self).__init__(path)
180
 
        self.resolve_ext_ref = resolve_ext_ref
181
 
 
182
 
    @property
183
 
    def idx(self):
184
 
        if self._idx is None:
185
 
            if self._data is None:
186
 
                self._data = PackData(self._data_path)
187
 
            pb = ui.ui_factory.nested_progress_bar()
188
 
            try:
189
 
                def report_progress(cur, total):
190
 
                    pb.update("generating index", cur, total)
191
 
                self._data.create_index_v2(self._idx_path, self.resolve_ext_ref,
192
 
                    progress=report_progress)
193
 
            finally:
194
 
                pb.finished()
195
 
            self._idx = load_pack_index(self._idx_path)
196
 
        return self._idx
 
109
class TemporaryPackIterator(object):
 
110
 
 
111
    def __init__(self, path):
 
112
        self.path_data = path
 
113
        basename = path[:-len(".pack")]
 
114
        p = PackData(path)
 
115
        self.path_idx = basename+".idx"
 
116
        p.create_index_v2(self.path_idx)
 
117
        self.pack = Pack(basename)
 
118
        self._iter = self.pack.iterobjects()
197
119
 
198
120
    def __del__(self):
199
 
        os.remove(self._data_path)
200
 
        os.remove(self._idx_path)
 
121
        os.remove(self.path_data)
 
122
        os.remove(self.path_idx)
 
123
 
 
124
    def next(self):
 
125
        return (self._iter.next(), None)
 
126
 
 
127
    def __len__(self):
 
128
        return len(self.pack)
201
129
 
202
130
 
203
131
class RemoteGitRepository(GitRepository):
204
132
 
205
133
    def __init__(self, gitdir, lockfiles):
206
134
        GitRepository.__init__(self, gitdir, lockfiles)
207
 
        self._refs = None
208
 
 
209
 
    @property
210
 
    def inventories(self):
211
 
        raise GitSmartRemoteNotSupported()
212
 
 
213
 
    @property
214
 
    def revisions(self):
215
 
        raise GitSmartRemoteNotSupported()
216
 
 
217
 
    @property
218
 
    def texts(self):
219
 
        raise GitSmartRemoteNotSupported()
220
 
 
221
 
    def get_refs(self):
222
 
        if self._refs is not None:
223
 
            return self._refs
224
 
        def determine_wants(heads):
225
 
            self._refs = heads
226
 
            return []
227
 
        self.bzrdir.root_transport.fetch_pack(determine_wants, None, 
228
 
            lambda x: None, lambda x: mutter("git: %s" % x))
229
 
        return self._refs
230
135
 
231
136
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
232
137
                   progress=None):
233
138
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
234
139
            progress)
235
140
 
236
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
141
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
237
142
        fd, path = tempfile.mkstemp(suffix=".pack")
238
143
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
239
144
        os.close(fd)
240
 
        if os.path.getsize(path) == 0:
241
 
            return EmptyObjectStoreIterator()
242
 
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
243
 
 
244
 
 
245
 
class RemoteGitTagDict(tag.BasicTags):
246
 
 
247
 
    def __init__(self, branch):
248
 
        self.branch = branch
249
 
        self.repository = branch.repository
250
 
 
251
 
    def get_tag_dict(self):
252
 
        ret = {}
253
 
        refs = self.repository.get_refs()
254
 
        for k,v in refs.iteritems():
255
 
            if k.startswith("refs/tags/") and not k.endswith("^{}"):
256
 
                v = refs.get(k+"^{}", v)
257
 
                ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
258
 
        return ret
259
 
 
260
 
    def set_tag(self, name, revid):
261
 
        # FIXME: Not supported yet, should do a push of a new ref
262
 
        raise NotImplementedError(self.set_tag)
 
145
        ret = TemporaryPackIterator(path)
 
146
        return (len(ret), iter(ret.next, None))
263
147
 
264
148
 
265
149
class RemoteGitBranch(GitBranch):
266
150
 
267
151
    def __init__(self, bzrdir, repository, name, lockfiles):
268
 
        heads = repository.get_refs()
269
 
        if not name in heads:
270
 
            raise NoSuchRef(name)
271
 
        self._ref = heads[name]
 
152
        def determine_wants(heads):
 
153
            if not name in heads:
 
154
                raise NoSuchRef(name)
 
155
            self._ref = heads[name]
 
156
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
 
157
                             lambda x: mutter("git: %s" % x))
272
158
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
273
159
 
274
 
    def revision_history(self):
275
 
        raise GitSmartRemoteNotSupported()
276
 
 
277
160
    def last_revision(self):
278
161
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
279
162