/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 test for nick.

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
25
 
 
26
 
from bzrlib.plugins.git import git
27
 
from bzrlib.plugins.git.branch import GitBranch
28
 
from bzrlib.plugins.git.dir import GitDir
29
 
from bzrlib.plugins.git.foreign import ForeignBranch
30
 
from bzrlib.plugins.git.repository import GitFormat, GitRepository
31
 
 
 
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
    )
 
76
import os
 
77
import tempfile
32
78
import urllib
33
79
import urlparse
34
80
 
35
 
from dulwich.pack import PackData
 
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 []
36
86
 
37
87
 
38
88
class GitSmartTransport(Transport):
43
93
        assert scheme == "git"
44
94
        hostport, self._path = urllib.splithost(loc)
45
95
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
46
 
        if _client is not None:
47
 
            self._client = _client
48
 
        else:
49
 
            self._client = git.client.TCPGitClient(self._host, self._port)
 
96
        self._client = _client
 
97
 
 
98
    def has(self, relpath):
 
99
        return False
 
100
 
 
101
    def _get_client(self):
 
102
        if self._client is not None:
 
103
            ret = self._client
 
104
            self._client = None
 
105
            return ret
 
106
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
50
107
 
51
108
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
52
109
        if progress is None:
53
110
            def progress(text):
54
111
                info("git: %s" % text)
55
 
        self._client.fetch_pack(self._path, determine_wants, graph_walker, 
56
 
                pack_data, progress)
57
 
 
58
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
59
 
        fd, path = tempfile.mkstemp(dir=self.pack_dir(), suffix=".pack")
60
 
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
61
 
        os.close(fd)
 
112
        client = self._get_client()
62
113
        try:
63
 
            p = PackData(path)
64
 
            for o in p.iterobjects():
65
 
                yield o
66
 
        finally:
67
 
            os.remove(path)
 
114
            client.fetch_pack(self._path, determine_wants, 
 
115
                graph_walker, pack_data, progress)
 
116
        except GitProtocolError, e:
 
117
            raise BzrError(e)
68
118
 
69
119
    def get(self, path):
70
120
        raise NoSuchFile(path)
71
121
 
 
122
    def abspath(self, relpath):
 
123
        return urlutils.join(self.base, relpath)
 
124
 
72
125
    def clone(self, offset=None):
73
126
        """See Transport.clone()."""
74
127
        if offset is None:
90
143
    def open_repository(self):
91
144
        return RemoteGitRepository(self, self._lockfiles)
92
145
 
93
 
    def open_branch(self):
 
146
    def open_branch(self, _unsupported=False):
94
147
        repo = self.open_repository()
95
148
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
96
149
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
99
152
        raise NotLocalUrl(self.transport.base)
100
153
 
101
154
 
 
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
            self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
 
171
            self._idx = PackIndex(self._idx_path)
 
172
        return self._idx
 
173
 
 
174
    def __del__(self):
 
175
        os.remove(self._data_path)
 
176
        os.remove(self._idx_path)
 
177
 
 
178
 
102
179
class RemoteGitRepository(GitRepository):
103
180
 
104
181
    def __init__(self, gitdir, lockfiles):
105
182
        GitRepository.__init__(self, gitdir, lockfiles)
 
183
        self._refs = None
 
184
 
 
185
    def get_refs(self):
 
186
        if self._refs is not None:
 
187
            return self._refs
 
188
        def determine_wants(heads):
 
189
            self._refs = heads
 
190
            return []
 
191
        self.bzrdir.root_transport.fetch_pack(determine_wants, None, 
 
192
            lambda x: None, lambda x: mutter("git: %s" % x))
 
193
        return self._refs
106
194
 
107
195
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
108
196
                   progress=None):
109
197
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
110
198
            progress)
111
199
 
 
200
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
201
        fd, path = tempfile.mkstemp(suffix=".pack")
 
202
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
 
203
        os.close(fd)
 
204
        if os.path.getsize(path) == 0:
 
205
            return EmptyObjectStoreIterator()
 
206
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
 
207
 
 
208
 
 
209
class RemoteGitTagDict(tag.BasicTags):
 
210
 
 
211
    def __init__(self, branch):
 
212
        self.branch = branch
 
213
        self.repository = branch.repository
 
214
 
 
215
    def get_tag_dict(self):
 
216
        ret = {}
 
217
        refs = self.repository.get_refs()
 
218
        for k,v in refs.iteritems():
 
219
            if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
220
                v = refs.get(k+"^{}", v)
 
221
                ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
222
        return ret
 
223
 
 
224
    def set_tag(self, name, revid):
 
225
        # FIXME: Not supported yet, should do a push of a new ref
 
226
        raise NotImplementedError(self.set_tag)
 
227
 
112
228
 
113
229
class RemoteGitBranch(GitBranch):
114
230
 
115
231
    def __init__(self, bzrdir, repository, name, lockfiles):
116
 
        def determine_wants(heads):
117
 
            self._ref = heads[name]
118
 
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
119
 
                             lambda x: mutter("git: %s" % x))
 
232
        heads = repository.get_refs()
 
233
        if not name in heads:
 
234
            raise NoSuchRef(name)
 
235
        self._ref = heads[name]
120
236
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
121
237
 
122
238
    def last_revision(self):
123
239
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
124
240
 
 
241
    def _synchronize_history(self, destination, revision_id):
 
242
        """See Branch._synchronize_history()."""
 
243
        destination.generate_revision_history(self.last_revision())
 
244