/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 branch cloning.

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
 
    urlutils,
22
 
    )
23
 
from bzrlib.bzrdir import (
24
 
    BzrDir,
25
 
    BzrDirFormat,
26
 
    )
27
 
from bzrlib.errors import (
28
 
    BzrError,
29
 
    NoSuchFile,
30
 
    NotLocalUrl,
31
 
    )
32
 
from bzrlib.lockable_files import (
33
 
    TransportLock,
34
 
    )
35
 
from bzrlib.repository import (
36
 
    Repository,
37
 
    )
38
 
from bzrlib.trace import (
39
 
    info,
40
 
    )
41
 
from bzrlib.transport import (
42
 
    Transport,
43
 
    )
44
 
 
45
 
from bzrlib.plugins.git import (
46
 
    lazy_check_versions,
47
 
    )
48
 
lazy_check_versions()
49
 
 
50
 
from bzrlib.plugins.git.branch import (
51
 
    GitBranch,
52
 
    )
53
 
from bzrlib.plugins.git.errors import (
54
 
    NoSuchRef,
55
 
    )
56
 
from bzrlib.plugins.git.dir import (
57
 
    GitDir,
58
 
    )
59
 
from bzrlib.plugins.git.foreign import (
60
 
    ForeignBranch,
61
 
    )
62
 
from bzrlib.plugins.git.repository import (
63
 
    GitRepositoryFormat,
64
 
    GitRepository,
65
 
    )
66
 
 
67
 
import dulwich as git
68
 
from dulwich.errors import (
69
 
    GitProtocolError,
70
 
    )
71
 
from dulwich.pack import (
72
 
    Pack,
73
 
    PackData,
74
 
    PackIndex,
75
 
    )
 
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
 
25
 
 
26
from bzrlib.plugins.git import git
 
27
from bzrlib.plugins.git.branch import GitBranch
 
28
from bzrlib.plugins.git.errors import NoSuchRef
 
29
from bzrlib.plugins.git.dir import GitDir
 
30
from bzrlib.plugins.git.foreign import ForeignBranch
 
31
from bzrlib.plugins.git.repository import GitFormat, GitRepository
 
32
 
76
33
import os
77
34
import tempfile
78
35
import urllib
79
36
import urlparse
80
37
 
81
 
 
82
 
# Don't run any tests on GitSmartTransport as it is not intended to be 
83
 
# a full implementation of Transport
84
 
def get_test_permutations():
85
 
    return []
 
38
from dulwich.pack import PackData, Pack
86
39
 
87
40
 
88
41
class GitSmartTransport(Transport):
95
48
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
96
49
        self._client = _client
97
50
 
98
 
    def has(self, relpath):
99
 
        return False
100
 
 
101
51
    def _get_client(self):
102
52
        if self._client is not None:
103
53
            ret = self._client
104
54
            self._client = None
105
55
            return ret
106
 
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
 
56
        return git.client.TCPGitClient(self._host, self._port)
107
57
 
108
58
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
109
59
        if progress is None:
110
60
            def progress(text):
111
61
                info("git: %s" % text)
112
 
        client = self._get_client()
113
 
        try:
114
 
            client.fetch_pack(self._path, determine_wants, 
115
 
                graph_walker, pack_data, progress)
116
 
        except GitProtocolError, e:
117
 
            raise BzrError(e)
 
62
        self._get_client().fetch_pack(self._path, determine_wants, 
 
63
            graph_walker, pack_data, progress)
118
64
 
119
65
    def get(self, path):
120
66
        raise NoSuchFile(path)
143
89
    def open_repository(self):
144
90
        return RemoteGitRepository(self, self._lockfiles)
145
91
 
146
 
    def open_branch(self, _unsupported=False):
 
92
    def open_branch(self):
147
93
        repo = self.open_repository()
148
94
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
149
95
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
152
98
        raise NotLocalUrl(self.transport.base)
153
99
 
154
100
 
155
 
class EmptyObjectStoreIterator(dict):
156
 
 
157
 
    def iterobjects(self):
158
 
        return []
159
 
 
160
 
 
161
 
class TemporaryPackIterator(Pack):
162
 
 
163
 
    def __init__(self, path, resolve_ext_ref):
164
 
        self.resolve_ext_ref = resolve_ext_ref
165
 
        super(TemporaryPackIterator, self).__init__(path)
166
 
 
167
 
    @property
168
 
    def idx(self):
169
 
        if self._idx is None:
170
 
            if self._data is None:
171
 
                self._data = PackData(self._data_path)
172
 
            self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
173
 
            self._idx = PackIndex(self._idx_path)
174
 
        return self._idx
175
 
 
176
 
    def __del__(self):
177
 
        os.remove(self._data_path)
178
 
        os.remove(self._idx_path)
179
 
 
180
 
 
181
101
class RemoteGitRepository(GitRepository):
182
102
 
183
103
    def __init__(self, gitdir, lockfiles):
184
104
        GitRepository.__init__(self, gitdir, lockfiles)
185
 
        self._refs = None
186
 
 
187
 
    def get_refs(self):
188
 
        if self._refs is not None:
189
 
            return self._refs
190
 
        def determine_wants(heads):
191
 
            self._refs = heads
192
 
            return []
193
 
        self.bzrdir.root_transport.fetch_pack(determine_wants, None, 
194
 
            lambda x: None, lambda x: mutter("git: %s" % x))
195
 
        return self._refs
196
105
 
197
106
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
198
107
                   progress=None):
199
108
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
200
109
            progress)
201
110
 
202
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
111
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
203
112
        fd, path = tempfile.mkstemp(suffix=".pack")
204
113
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
205
114
        os.close(fd)
206
 
        if os.path.getsize(path) == 0:
207
 
            return EmptyObjectStoreIterator()
208
 
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
209
 
 
210
 
 
211
 
class RemoteGitTagDict(tag.BasicTags):
212
 
 
213
 
    def __init__(self, branch):
214
 
        self.branch = branch
215
 
        self.repository = branch.repository
216
 
 
217
 
    def get_tag_dict(self):
218
 
        ret = {}
219
 
        refs = self.repository.get_refs()
220
 
        for k,v in refs.iteritems():
221
 
            if k.startswith("refs/tags/") and not k.endswith("^{}"):
222
 
                v = refs.get(k+"^{}", v)
223
 
                ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
224
 
        return ret
225
 
 
226
 
    def set_tag(self, name, revid):
227
 
        # FIXME: Not supported yet, should do a push of a new ref
228
 
        raise NotImplementedError(self.set_tag)
 
115
        try:
 
116
            basename = path[:-len(".pack")]
 
117
            p = PackData(path)
 
118
            p.create_index_v2(basename+".idx")
 
119
            for o in Pack(basename).iterobjects():
 
120
                yield o
 
121
        finally:
 
122
            os.remove(path)
229
123
 
230
124
 
231
125
class RemoteGitBranch(GitBranch):
232
126
 
233
127
    def __init__(self, bzrdir, repository, name, lockfiles):
234
 
        heads = repository.get_refs()
235
 
        if not name in heads:
236
 
            raise NoSuchRef(name)
237
 
        self._ref = heads[name]
 
128
        def determine_wants(heads):
 
129
            if not name in heads:
 
130
                raise NoSuchRef(name)
 
131
            self._ref = heads[name]
 
132
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
 
133
                             lambda x: mutter("git: %s" % x))
238
134
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
239
135
 
240
136
    def last_revision(self):