/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
17
import bzrlib
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
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
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
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
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
44
0.200.292 by Jelmer Vernooij
Fix formatting.
45
from bzrlib.plugins.git import (
46
    lazy_check_versions,
47
    )
0.200.200 by Jelmer Vernooij
Register lazily where possible.
48
lazy_check_versions()
49
0.200.292 by Jelmer Vernooij
Fix formatting.
50
from bzrlib.plugins.git.branch import (
51
    GitBranch,
52
    )
53
from bzrlib.plugins.git.errors import (
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
54
    GitSmartRemoteNotSupported,
0.200.292 by Jelmer Vernooij
Fix formatting.
55
    NoSuchRef,
56
    )
57
from bzrlib.plugins.git.dir import (
58
    GitDir,
59
    )
60
from bzrlib.plugins.git.foreign import (
61
    ForeignBranch,
62
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
63
from bzrlib.plugins.git.repository import (
64
    GitRepositoryFormat,
65
    GitRepository,
66
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
67
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
68
import dulwich as git
0.200.292 by Jelmer Vernooij
Fix formatting.
69
from dulwich.errors import (
70
    GitProtocolError,
71
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
72
from dulwich.pack import (
73
    Pack,
74
    PackData,
75
    )
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
76
import os
77
import tempfile
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
78
import urllib
79
import urlparse
80
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
81
try:
82
    from dulwich.pack import load_pack_index
83
except ImportError:
84
    from dulwich.pack import PackIndex as load_pack_index
85
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
86
0.200.181 by Jelmer Vernooij
Support setting tags.
87
# Don't run any tests on GitSmartTransport as it is not intended to be 
88
# a full implementation of Transport
89
def get_test_permutations():
90
    return []
91
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
92
93
class GitSmartTransport(Transport):
94
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
95
    def __init__(self, url, _client=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
96
        Transport.__init__(self, url)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
97
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
98
        hostport, self._path = urllib.splithost(loc)
99
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
0.200.166 by Jelmer Vernooij
don't reuse client objects.
100
        self._client = _client
101
0.200.238 by Jelmer Vernooij
Import Transport.has().
102
    def has(self, relpath):
103
        return False
104
0.200.166 by Jelmer Vernooij
don't reuse client objects.
105
    def _get_client(self):
0.200.307 by Jelmer Vernooij
Support git+ssh.
106
        raise NotImplementedError(self._get_client)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
107
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
108
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
109
        if progress is None:
110
            def progress(text):
111
                info("git: %s" % text)
0.200.240 by Jelmer Vernooij
Wrap socket errors.
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)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
118
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
119
    def get(self, path):
120
        raise NoSuchFile(path)
121
0.200.160 by Jelmer Vernooij
Implement abspath.
122
    def abspath(self, relpath):
123
        return urlutils.join(self.base, relpath)
124
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
125
    def clone(self, offset=None):
126
        """See Transport.clone()."""
127
        if offset is None:
128
            newurl = self.base
129
        else:
130
            newurl = urlutils.join(self.base, offset)
131
0.200.307 by Jelmer Vernooij
Support git+ssh.
132
        return self.__class__(newurl, self._client)
133
134
135
class TCPGitSmartTransport(GitSmartTransport):
136
137
    def _get_client(self):
138
        if self._client is not None:
139
            ret = self._client
140
            self._client = None
141
            return ret
142
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
143
144
145
class SSHGitSmartTransport(GitSmartTransport):
146
147
    def _get_client(self):
148
        if self._client is not None:
149
            ret = self._client
150
            self._client = None
151
            return ret
0.200.308 by Jelmer Vernooij
Register git protocols.
152
        return git.client.SSHGitClient(self._host, self._port, thin_packs=False)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
153
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
154
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
155
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
156
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
157
    def __init__(self, transport, lockfiles, format):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
158
        self._format = format
159
        self.root_transport = transport
160
        self.transport = transport
161
        self._lockfiles = lockfiles
162
163
    def open_repository(self):
164
        return RemoteGitRepository(self, self._lockfiles)
165
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
166
    def open_branch(self, ignore_fallbacks=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
167
        repo = self.open_repository()
168
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
169
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
170
171
    def open_workingtree(self):
172
        raise NotLocalUrl(self.transport.base)
173
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
174
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
175
class EmptyObjectStoreIterator(dict):
176
177
    def iterobjects(self):
178
        return []
179
180
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
181
class TemporaryPackIterator(Pack):
182
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
183
    def __init__(self, path, resolve_ext_ref):
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
184
        super(TemporaryPackIterator, self).__init__(path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
185
        self.resolve_ext_ref = resolve_ext_ref
186
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
187
    @property
188
    def idx(self):
189
        if self._idx is None:
0.200.302 by Jelmer Vernooij
Fix index creation when fetching from remote host.
190
            if self._data is None:
191
                self._data = PackData(self._data_path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
192
            self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
193
            self._idx = load_pack_index(self._idx_path)
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
194
        return self._idx
0.200.205 by Jelmer Vernooij
Fix remote fetching.
195
196
    def __del__(self):
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
197
        os.remove(self._data_path)
198
        os.remove(self._idx_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
199
200
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
201
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
202
203
    def __init__(self, gitdir, lockfiles):
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
204
        GitRepository.__init__(self, gitdir, lockfiles)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
205
        self._refs = None
206
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
207
    @property
208
    def inventories(self):
209
        raise GitSmartRemoteNotSupported()
210
211
    @property
212
    def revisions(self):
213
        raise GitSmartRemoteNotSupported()
214
215
    @property
216
    def texts(self):
217
        raise GitSmartRemoteNotSupported()
218
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
219
    def get_refs(self):
220
        if self._refs is not None:
221
            return self._refs
222
        def determine_wants(heads):
223
            self._refs = heads
224
            return []
225
        self.bzrdir.root_transport.fetch_pack(determine_wants, None, 
226
            lambda x: None, lambda x: mutter("git: %s" % x))
227
        return self._refs
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
228
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
229
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
230
                   progress=None):
231
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
232
            progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
233
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
234
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
235
        fd, path = tempfile.mkstemp(suffix=".pack")
236
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
237
        os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
238
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
239
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
240
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
241
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
242
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
243
class RemoteGitTagDict(tag.BasicTags):
244
245
    def __init__(self, branch):
246
        self.branch = branch
247
        self.repository = branch.repository
248
249
    def get_tag_dict(self):
250
        ret = {}
251
        refs = self.repository.get_refs()
252
        for k,v in refs.iteritems():
253
            if k.startswith("refs/tags/") and not k.endswith("^{}"):
254
                v = refs.get(k+"^{}", v)
0.228.4 by Jelmer Vernooij
Strip ref directory name from tag names.
255
                ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
256
        return ret
257
258
    def set_tag(self, name, revid):
259
        # FIXME: Not supported yet, should do a push of a new ref
260
        raise NotImplementedError(self.set_tag)
261
262
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
263
class RemoteGitBranch(GitBranch):
264
265
    def __init__(self, bzrdir, repository, name, lockfiles):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
266
        heads = repository.get_refs()
267
        if not name in heads:
268
            raise NoSuchRef(name)
269
        self._ref = heads[name]
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
270
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
271
272
    def last_revision(self):
0.200.140 by Jelmer Vernooij
Support negotiating with remote git repository and receiving pack.
273
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
274
0.200.169 by Jelmer Vernooij
Fix branch cloning.
275
    def _synchronize_history(self, destination, revision_id):
276
        """See Branch._synchronize_history()."""
277
        destination.generate_revision_history(self.last_revision())
278