/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,
0.200.333 by Jelmer Vernooij
Support progress reporting when creating index.
21
    ui,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
22
    urlutils,
23
    )
24
from bzrlib.errors import (
25
    BzrError,
26
    NoSuchFile,
27
    NotLocalUrl,
28
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
29
from bzrlib.trace import (
30
    info,
31
    )
32
from bzrlib.transport import (
33
    Transport,
34
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
35
0.200.292 by Jelmer Vernooij
Fix formatting.
36
from bzrlib.plugins.git import (
37
    lazy_check_versions,
38
    )
0.200.200 by Jelmer Vernooij
Register lazily where possible.
39
lazy_check_versions()
40
0.200.292 by Jelmer Vernooij
Fix formatting.
41
from bzrlib.plugins.git.branch import (
42
    GitBranch,
43
    )
44
from bzrlib.plugins.git.errors import (
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
45
    GitSmartRemoteNotSupported,
0.200.292 by Jelmer Vernooij
Fix formatting.
46
    NoSuchRef,
47
    )
48
from bzrlib.plugins.git.dir import (
49
    GitDir,
50
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
51
from bzrlib.plugins.git.repository import (
52
    GitRepositoryFormat,
53
    GitRepository,
54
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
55
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
56
import dulwich as git
0.200.292 by Jelmer Vernooij
Fix formatting.
57
from dulwich.errors import (
58
    GitProtocolError,
59
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
60
from dulwich.pack import (
61
    Pack,
62
    PackData,
63
    )
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
64
import os
65
import tempfile
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
66
import urllib
67
import urlparse
68
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
69
try:
70
    from dulwich.pack import load_pack_index
71
except ImportError:
72
    from dulwich.pack import PackIndex as load_pack_index
73
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
74
0.200.181 by Jelmer Vernooij
Support setting tags.
75
# Don't run any tests on GitSmartTransport as it is not intended to be 
76
# a full implementation of Transport
77
def get_test_permutations():
78
    return []
79
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
80
81
class GitSmartTransport(Transport):
82
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
83
    def __init__(self, url, _client=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
84
        Transport.__init__(self, url)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
85
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
86
        hostport, self._path = urllib.splithost(loc)
0.200.340 by Jelmer Vernooij
Fix default port for git+ssh://
87
        (self._host, self._port) = urllib.splitnport(hostport, None)
0.200.166 by Jelmer Vernooij
don't reuse client objects.
88
        self._client = _client
89
0.200.238 by Jelmer Vernooij
Import Transport.has().
90
    def has(self, relpath):
91
        return False
92
0.200.166 by Jelmer Vernooij
don't reuse client objects.
93
    def _get_client(self):
0.200.307 by Jelmer Vernooij
Support git+ssh.
94
        raise NotImplementedError(self._get_client)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
95
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
96
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
97
        if progress is None:
98
            def progress(text):
99
                info("git: %s" % text)
0.200.240 by Jelmer Vernooij
Wrap socket errors.
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)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
106
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
107
    def get(self, path):
108
        raise NoSuchFile(path)
109
0.200.160 by Jelmer Vernooij
Implement abspath.
110
    def abspath(self, relpath):
111
        return urlutils.join(self.base, relpath)
112
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
113
    def clone(self, offset=None):
114
        """See Transport.clone()."""
115
        if offset is None:
116
            newurl = self.base
117
        else:
118
            newurl = urlutils.join(self.base, offset)
119
0.200.307 by Jelmer Vernooij
Support git+ssh.
120
        return self.__class__(newurl, self._client)
121
122
123
class TCPGitSmartTransport(GitSmartTransport):
124
0.200.332 by Jelmer Vernooij
Support activity reporting.
125
    _scheme = 'git'
126
0.200.307 by Jelmer Vernooij
Support git+ssh.
127
    def _get_client(self):
128
        if self._client is not None:
129
            ret = self._client
130
            self._client = None
131
            return ret
0.200.332 by Jelmer Vernooij
Support activity reporting.
132
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
133
            report_activity=self._report_activity)
0.200.307 by Jelmer Vernooij
Support git+ssh.
134
135
136
class SSHGitSmartTransport(GitSmartTransport):
137
0.200.332 by Jelmer Vernooij
Support activity reporting.
138
    _scheme = 'git+ssh'
139
0.200.307 by Jelmer Vernooij
Support git+ssh.
140
    def _get_client(self):
141
        if self._client is not None:
142
            ret = self._client
143
            self._client = None
144
            return ret
0.200.332 by Jelmer Vernooij
Support activity reporting.
145
        return git.client.SSHGitClient(self._host, self._port, thin_packs=False,
146
            report_activity=self._report_activity)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
147
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
148
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
149
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
150
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
151
    def __init__(self, transport, lockfiles, format):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
152
        self._format = format
153
        self.root_transport = transport
154
        self.transport = transport
155
        self._lockfiles = lockfiles
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
156
        self._mode_check_done = None
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
157
158
    def open_repository(self):
159
        return RemoteGitRepository(self, self._lockfiles)
160
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
161
    def open_branch(self, ignore_fallbacks=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
162
        repo = self.open_repository()
163
        # 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.
164
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
165
166
    def open_workingtree(self):
167
        raise NotLocalUrl(self.transport.base)
168
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
169
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
170
class EmptyObjectStoreIterator(dict):
171
172
    def iterobjects(self):
173
        return []
174
175
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
176
class TemporaryPackIterator(Pack):
177
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
178
    def __init__(self, path, resolve_ext_ref):
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
179
        super(TemporaryPackIterator, self).__init__(path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
180
        self.resolve_ext_ref = resolve_ext_ref
181
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
182
    @property
183
    def idx(self):
184
        if self._idx is None:
0.200.302 by Jelmer Vernooij
Fix index creation when fetching from remote host.
185
            if self._data is None:
186
                self._data = PackData(self._data_path)
0.200.333 by Jelmer Vernooij
Support progress reporting when creating index.
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()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
195
            self._idx = load_pack_index(self._idx_path)
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
196
        return self._idx
0.200.205 by Jelmer Vernooij
Fix remote fetching.
197
198
    def __del__(self):
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
199
        os.remove(self._data_path)
200
        os.remove(self._idx_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
201
202
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
203
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
204
205
    def __init__(self, gitdir, lockfiles):
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
206
        GitRepository.__init__(self, gitdir, lockfiles)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
207
        self._refs = None
208
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
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
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
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
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
230
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
231
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
232
                   progress=None):
233
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
234
            progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
235
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
236
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
237
        fd, path = tempfile.mkstemp(suffix=".pack")
238
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
239
        os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
240
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
241
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
242
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
243
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
244
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
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)
0.228.4 by Jelmer Vernooij
Strip ref directory name from tag names.
257
                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.
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)
263
264
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
265
class RemoteGitBranch(GitBranch):
266
267
    def __init__(self, bzrdir, repository, name, lockfiles):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
268
        heads = repository.get_refs()
269
        if not name in heads:
270
            raise NoSuchRef(name)
271
        self._ref = heads[name]
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
272
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
273
274
    def last_revision(self):
0.200.140 by Jelmer Vernooij
Support negotiating with remote git repository and receiving pack.
275
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
276
0.200.169 by Jelmer Vernooij
Fix branch cloning.
277
    def _synchronize_history(self, destination, revision_id):
278
        """See Branch._synchronize_history()."""
279
        destination.generate_revision_history(self.last_revision())
280