/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1613 by Jelmer Vernooij
Handle encoding better in working tree iter changes.
1
# Copyright (C) 2007-2012 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.1594 by Jelmer Vernooij
Use absolute_import everywhere.
17
from __future__ import absolute_import
18
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
19
from ... import (
0.200.596 by Jelmer Vernooij
Import RemoteGitBranch._get_config().
20
    config,
0.200.707 by Jelmer Vernooij
Add debug routines.
21
    debug,
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
22
    trace,
0.200.333 by Jelmer Vernooij
Support progress reporting when creating index.
23
    ui,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
24
    urlutils,
25
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
26
from ...errors import (
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
27
    BzrError,
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
28
    InProcessTransport,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
29
    InvalidRevisionId,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
30
    NoSuchFile,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
31
    NoSuchRevision,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
32
    NotBranchError,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
33
    NotLocalUrl,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
34
    UninitializableFormat,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
35
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
36
from ...transport import (
0.200.292 by Jelmer Vernooij
Fix formatting.
37
    Transport,
38
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
39
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
40
from . import (
0.200.292 by Jelmer Vernooij
Fix formatting.
41
    lazy_check_versions,
42
    )
0.200.200 by Jelmer Vernooij
Register lazily where possible.
43
lazy_check_versions()
44
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
45
from .branch import (
0.200.292 by Jelmer Vernooij
Fix formatting.
46
    GitBranch,
0.295.1 by Jelmer Vernooij
Split up branch formats.
47
    GitBranchFormat,
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
48
    GitTags,
0.200.292 by Jelmer Vernooij
Fix formatting.
49
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
50
from .dir import (
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
51
    GitControlDirFormat,
52
    GitDir,
53
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
54
from .errors import (
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
55
    GitSmartRemoteNotSupported,
0.200.292 by Jelmer Vernooij
Fix formatting.
56
    NoSuchRef,
57
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
58
from .mapping import (
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
59
    mapping_registry,
60
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
61
from .repository import (
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
62
    GitRepository,
63
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
64
from .refs import (
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
65
    branch_name_to_ref,
0.200.1487 by Jelmer Vernooij
Use peeling.
66
    is_peeled,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
67
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
68
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
69
import dulwich
70
import dulwich.client
0.200.292 by Jelmer Vernooij
Fix formatting.
71
from dulwich.errors import (
72
    GitProtocolError,
73
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
74
from dulwich.pack import (
75
    Pack,
76
    )
0.200.1487 by Jelmer Vernooij
Use peeling.
77
from dulwich.repo import DictRefsContainer
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
78
import os
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
79
import select
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
80
import tempfile
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
81
import urllib
82
import urlparse
0.200.1555 by Jelmer Vernooij
Remove segment parameters for http smart transports.
83
84
# urlparse only supports a limited number of schemes by default
85
0.200.741 by Jelmer Vernooij
Cope with older versions of Python by adding git and git+ssh to the list of known schemes.
86
urlparse.uses_netloc.extend(['git', 'git+ssh'])
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
87
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
88
from dulwich.pack import load_pack_index
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
89
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
90
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
91
# Don't run any tests on GitSmartTransport as it is not intended to be
0.200.181 by Jelmer Vernooij
Support setting tags.
92
# a full implementation of Transport
93
def get_test_permutations():
94
    return []
95
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
96
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
97
def split_git_url(url):
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
98
    """Split a Git URL.
99
100
    :param url: Git URL
101
    :return: Tuple with host, port, username, path.
102
    """
0.200.743 by Jelmer Vernooij
Fix URL parsing.
103
    (scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
104
    path = urllib.unquote(loc)
0.246.2 by Jelmer Vernooij
Improve the fix dealing with git repo's in home directories.
105
    if path.startswith("/~"):
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
106
        path = path[1:]
0.200.743 by Jelmer Vernooij
Fix URL parsing.
107
    (username, hostport) = urllib.splituser(netloc)
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
108
    (host, port) = urllib.splitnport(hostport, None)
109
    return (host, port, username, path)
110
111
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
112
class RemoteGitError(BzrError):
113
0.290.1 by Jelmer Vernooij
Avoid 'message' argument in RemoteGitError; apparently it breaks some versions of Python.
114
    _fmt = "Remote server error: %(msg)s"
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
115
116
0.200.1275 by Jelmer Vernooij
recognize missing repositories
117
def parse_git_error(url, message):
118
    """Parse a remote git server error and return a bzr exception.
119
120
    :param url: URL of the remote repository
121
    :param message: Message sent by the remote git server
122
    """
123
    message = str(message).strip()
124
    if message.startswith("Could not find Repository "):
125
        return NotBranchError(url, message)
0.200.1563 by Jelmer Vernooij
Improve error message.
126
    if message == "HEAD failed to update":
127
        base_url, _ = urlutils.split_segment_parameters(url)
128
        raise BzrError(
129
            ("Unable to update remote HEAD branch. To update the master "
130
             "branch, specify the URL %s,branch=master.") % base_url)
0.200.1275 by Jelmer Vernooij
recognize missing repositories
131
    # Don't know, just return it to the user as-is
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
132
    return RemoteGitError(message)
0.200.1275 by Jelmer Vernooij
recognize missing repositories
133
134
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
135
class GitSmartTransport(Transport):
136
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
137
    def __init__(self, url, _client=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
138
        Transport.__init__(self, url)
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
139
        (self._host, self._port, self._username, self._path) = \
140
            split_git_url(url)
0.200.707 by Jelmer Vernooij
Add debug routines.
141
        if 'transport' in debug.debug_flags:
142
            trace.mutter('host: %r, user: %r, port: %r, path: %r',
143
                         self._host, self._username, self._port, self._path)
0.200.166 by Jelmer Vernooij
don't reuse client objects.
144
        self._client = _client
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
145
        self._stripped_path = self._path.rsplit(",", 1)[0]
0.200.166 by Jelmer Vernooij
don't reuse client objects.
146
0.200.543 by Jelmer Vernooij
Implement GitSmartTransport.external_url().
147
    def external_url(self):
148
        return self.base
149
0.200.238 by Jelmer Vernooij
Import Transport.has().
150
    def has(self, relpath):
151
        return False
152
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
153
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
154
        raise NotImplementedError(self._get_client)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
155
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
156
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
157
        return self._stripped_path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
158
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
159
    def get(self, path):
160
        raise NoSuchFile(path)
161
0.200.160 by Jelmer Vernooij
Implement abspath.
162
    def abspath(self, relpath):
163
        return urlutils.join(self.base, relpath)
164
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
165
    def clone(self, offset=None):
166
        """See Transport.clone()."""
167
        if offset is None:
168
            newurl = self.base
169
        else:
170
            newurl = urlutils.join(self.base, offset)
171
0.200.307 by Jelmer Vernooij
Support git+ssh.
172
        return self.__class__(newurl, self._client)
173
174
175
class TCPGitSmartTransport(GitSmartTransport):
176
0.200.332 by Jelmer Vernooij
Support activity reporting.
177
    _scheme = 'git'
178
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
179
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
180
        if self._client is not None:
181
            ret = self._client
182
            self._client = None
183
            return ret
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
184
        return dulwich.client.TCPGitClient(self._host, self._port,
0.200.859 by Jelmer Vernooij
Trivial cleanups.
185
            thin_packs=thin_packs, report_activity=self._report_activity)
0.200.307 by Jelmer Vernooij
Support git+ssh.
186
187
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
188
class SSHSocketWrapper(object):
189
190
    def __init__(self, sock):
191
        self.sock = sock
192
193
    def read(self, len=None):
194
        return self.sock.recv(len)
195
196
    def write(self, data):
197
        return self.sock.write(data)
198
199
    def can_read(self):
200
        return len(select.select([self.sock.fileno()], [], [], 0)[0]) > 0
201
202
203
class DulwichSSHVendor(dulwich.client.SSHVendor):
204
205
    def __init__(self):
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
206
        from ...transport import ssh
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
207
        self.bzr_ssh_vendor = ssh._get_ssh_vendor()
208
209
    def run_command(self, host, command, username=None, port=None):
210
        connection = self.bzr_ssh_vendor.connect_ssh(username=username,
211
            password=None, port=port, host=host, command=command)
212
        (kind, io_object) = connection.get_sock_or_pipes()
213
        if kind == 'socket':
214
            return SSHSocketWrapper(io_object)
215
        else:
216
            raise AssertionError("Unknown io object kind %r'" % kind)
217
218
219
#dulwich.client.get_ssh_vendor = DulwichSSHVendor
220
221
0.200.307 by Jelmer Vernooij
Support git+ssh.
222
class SSHGitSmartTransport(GitSmartTransport):
223
0.200.332 by Jelmer Vernooij
Support activity reporting.
224
    _scheme = 'git+ssh'
225
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
226
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
227
        path = self._stripped_path
0.200.1318 by Jelmer Vernooij
Strip segment parameters where necessary.
228
        if path.startswith("/~/"):
229
            return path[3:]
230
        return path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
231
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
232
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
233
        if self._client is not None:
234
            ret = self._client
235
            self._client = None
236
            return ret
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
237
        location_config = config.LocationConfig(self.base)
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
238
        client = dulwich.client.SSHGitClient(self._host, self._port, self._username,
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
239
            thin_packs=thin_packs, report_activity=self._report_activity)
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
240
        # Set up alternate pack program paths
241
        upload_pack = location_config.get_user_option('git_upload_pack')
242
        if upload_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
243
            client.alternative_paths["upload-pack"] = upload_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
244
        receive_pack = location_config.get_user_option('git_receive_pack')
245
        if receive_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
246
            client.alternative_paths["receive-pack"] = receive_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
247
        return client
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
248
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
249
0.295.1 by Jelmer Vernooij
Split up branch formats.
250
class RemoteGitBranchFormat(GitBranchFormat):
251
252
    def get_format_description(self):
253
        return 'Remote Git Branch'
254
255
    @property
256
    def _matchingcontroldir(self):
257
        return RemoteGitControlDirFormat()
258
0.295.2 by Jelmer Vernooij
Make RemoteGitBranchFormat uninitializeable.
259
    def initialize(self, a_controldir, name=None, repository=None,
260
                   append_revisions_only=None):
261
        raise UninitializableFormat(self)
262
0.295.1 by Jelmer Vernooij
Split up branch formats.
263
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
264
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
265
0.200.1411 by Jelmer Vernooij
Fix control files.
266
    def __init__(self, transport, format, get_client, client_path):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
267
        self._format = format
268
        self.root_transport = transport
269
        self.transport = transport
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
270
        self._mode_check_done = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
271
        self._get_client = get_client
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
272
        self._client_path = client_path
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
273
        self.base = self.root_transport.base
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
274
        self._refs = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
275
276
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
277
        if progress is None:
278
            def progress(text):
279
                trace.info("git: %s" % text)
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
280
        def wrap_determine_wants(refs_dict):
281
            return determine_wants(remote_refs_dict_to_container(refs_dict))
0.279.1 by William Grant
Support thin packs in fetch_pack and send_pack, since dulwich now handles them properly.
282
        client = self._get_client(thin_packs=True)
0.200.1335 by Jelmer Vernooij
Move _get_client.
283
        try:
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
284
            refs_dict = client.fetch_pack(self._client_path, wrap_determine_wants,
0.200.1335 by Jelmer Vernooij
Move _get_client.
285
                graph_walker, pack_data, progress)
0.200.1628 by Jelmer Vernooij
Handle empty repositories during clone.
286
            if refs_dict is None:
287
                refs_dict = {}
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
288
            self._refs = remote_refs_dict_to_container(refs_dict)
0.200.1487 by Jelmer Vernooij
Use peeling.
289
            return refs_dict
0.200.1335 by Jelmer Vernooij
Move _get_client.
290
        except GitProtocolError, e:
291
            raise parse_git_error(self.transport.external_url(), e)
292
293
    def send_pack(self, get_changed_refs, generate_pack_contents):
0.279.1 by William Grant
Support thin packs in fetch_pack and send_pack, since dulwich now handles them properly.
294
        client = self._get_client(thin_packs=True)
0.200.1335 by Jelmer Vernooij
Move _get_client.
295
        try:
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
296
            return client.send_pack(self._client_path, get_changed_refs,
0.200.1335 by Jelmer Vernooij
Move _get_client.
297
                generate_pack_contents)
298
        except GitProtocolError, e:
299
            raise parse_git_error(self.transport.external_url(), e)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
300
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
301
    def _get_default_ref(self):
302
        return "refs/heads/master"
303
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
304
    def destroy_branch(self, name=None):
305
        refname = self._get_selected_ref(name)
306
        def get_changed_refs(old_refs):
307
            ret = dict(old_refs)
308
            if not refname in ret:
0.200.1395 by Jelmer Vernooij
Fix error reporting.
309
                raise NotBranchError(self.user_url)
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
310
            ret[refname] = "00" * 20
311
            return ret
312
        self.send_pack(get_changed_refs, lambda have, want: [])
313
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
314
    @property
315
    def user_url(self):
316
        return self.control_url
317
0.200.1314 by Jelmer Vernooij
Provide RemoteGitDir.user_transport.
318
    @property
319
    def user_transport(self):
320
        return self.root_transport
321
0.200.1395 by Jelmer Vernooij
Fix error reporting.
322
    @property
323
    def control_url(self):
324
        return self.control_transport.base
325
326
    @property
327
    def control_transport(self):
328
        return self.root_transport
329
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
330
    def open_repository(self):
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
331
        return RemoteGitRepository(self)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
332
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
333
    def open_branch(self, name=None, unsupported=False,
0.200.1473 by Jelmer Vernooij
Support possible_transports argument to .open_branch.
334
            ignore_fallbacks=False, ref=None, possible_transports=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
335
        repo = self.open_repository()
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
336
        refname = self._get_selected_ref(name, ref)
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
337
        return RemoteGitBranch(self, repo, refname)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
338
0.200.662 by Jelmer Vernooij
Deal with recommend_upgrade argument to open_workingtree.
339
    def open_workingtree(self, recommend_upgrade=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
340
        raise NotLocalUrl(self.transport.base)
341
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
342
    def get_peeled(self, name):
343
        return self.get_refs_container().get_peeled(name)
344
0.200.1487 by Jelmer Vernooij
Use peeling.
345
    def get_refs_container(self):
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
346
        if self._refs is not None:
347
            return self._refs
0.200.1487 by Jelmer Vernooij
Use peeling.
348
        refs_dict = self.fetch_pack(lambda x: [], None,
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
349
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
350
        self._refs = remote_refs_dict_to_container(refs_dict)
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
351
        return self._refs
352
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
353
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
354
class EmptyObjectStoreIterator(dict):
355
356
    def iterobjects(self):
357
        return []
358
359
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
360
class TemporaryPackIterator(Pack):
361
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
362
    def __init__(self, path, resolve_ext_ref):
0.279.1 by William Grant
Support thin packs in fetch_pack and send_pack, since dulwich now handles them properly.
363
        super(TemporaryPackIterator, self).__init__(
364
            path, resolve_ext_ref=resolve_ext_ref)
0.278.2 by William Grant
Also override _idx_load rather than index, to be a bit cleaner.
365
        self._idx_load = lambda: self._idx_load_or_generate(self._idx_path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
366
0.278.2 by William Grant
Also override _idx_load rather than index, to be a bit cleaner.
367
    def _idx_load_or_generate(self, path):
368
        if not os.path.exists(path):
369
            pb = ui.ui_factory.nested_progress_bar()
370
            try:
371
                def report_progress(cur, total):
372
                    pb.update("generating index", cur, total)
373
                self.data.create_index(path,
374
                    progress=report_progress)
375
            finally:
376
                pb.finished()
377
        return load_pack_index(path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
378
379
    def __del__(self):
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
380
        if self._idx is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
381
            self._idx.close()
382
            os.remove(self._idx_path)
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
383
        if self._data is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
384
            self._data.close()
385
            os.remove(self._data_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
386
387
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
388
class BzrGitHttpClient(dulwich.client.HttpGitClient):
389
390
    def __init__(self, transport, *args, **kwargs):
391
        self.transport = transport
392
        super(BzrGitHttpClient, self).__init__(transport.external_url(), *args, **kwargs)
393
        import urllib2
394
        self._http_perform = getattr(self.transport, "_perform", urllib2.urlopen)
395
396
    def _perform(self, req):
397
        req.accepted_errors = (200, 404)
398
        req.follow_redirections = True
399
        req.redirected_to = None
400
        return self._http_perform(req)
401
402
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
403
class RemoteGitControlDirFormat(GitControlDirFormat):
404
    """The .git directory control format."""
405
406
    supports_workingtrees = False
407
408
    @classmethod
409
    def _known_formats(self):
410
        return set([RemoteGitControlDirFormat()])
411
0.295.1 by Jelmer Vernooij
Split up branch formats.
412
    def get_branch_format(self):
413
        return RemoteGitBranchFormat()
414
0.200.1413 by Jelmer Vernooij
Fix is_initializable()
415
    def is_initializable(self):
416
        return False
417
418
    def is_supported(self):
419
        return True
420
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
421
    def open(self, transport, _found=None):
422
        """Open this directory.
423
424
        """
425
        # we dont grok readonly - git isn't integrated with transport.
426
        url = transport.base
427
        if url.startswith('readonly+'):
428
            url = url[len('readonly+'):]
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
429
        if isinstance(transport, GitSmartTransport):
430
            get_client = transport._get_client
431
            client_path = transport._get_path()
432
        elif urlparse.urlsplit(transport.external_url())[0] in ("http", "https"):
0.279.1 by William Grant
Support thin packs in fetch_pack and send_pack, since dulwich now handles them properly.
433
            def get_client(thin_packs):
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
434
                return BzrGitHttpClient(transport, thin_packs=thin_packs)
0.200.1555 by Jelmer Vernooij
Remove segment parameters for http smart transports.
435
            client_path, _ = urlutils.split_segment_parameters(transport._path)
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
436
        else:
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
437
            raise NotBranchError(transport.base)
0.200.1411 by Jelmer Vernooij
Fix control files.
438
        return RemoteGitDir(transport, self, get_client, client_path)
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
439
440
    def get_format_description(self):
441
        return "Remote Git Repository"
442
443
    def initialize_on_transport(self, transport):
444
        raise UninitializableFormat(self)
445
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
446
    def supports_transport(self, transport):
447
        try:
448
            external_url = transport.external_url()
449
        except InProcessTransport:
450
            raise NotBranchError(path=transport.base)
451
        return (external_url.startswith("http:") or
452
                external_url.startswith("https:") or
453
                external_url.startswith("git+") or
454
                external_url.startswith("git:"))
455
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
456
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
457
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
458
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
459
    @property
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
460
    def user_url(self):
461
        return self.control_url
462
0.200.1288 by Jelmer Vernooij
Properly raise GitRemoteNotSupported from RemoteGitRepository.
463
    def get_parent_map(self, revids):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
464
        raise GitSmartRemoteNotSupported(self.get_parent_map, self)
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
465
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
466
    def fetch_pack(self, determine_wants, graph_walker, pack_data,
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
467
                   progress=None):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
468
        return self.controldir.fetch_pack(determine_wants, graph_walker,
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
469
                                          pack_data, progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
470
0.200.427 by Jelmer Vernooij
make send_pack accessible.
471
    def send_pack(self, get_changed_refs, generate_pack_contents):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
472
        return self.controldir.send_pack(get_changed_refs, generate_pack_contents)
0.200.427 by Jelmer Vernooij
make send_pack accessible.
473
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
474
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
475
                      progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
476
        fd, path = tempfile.mkstemp(suffix=".pack")
0.200.1299 by Jelmer Vernooij
Make sure file gets closed.
477
        try:
478
            self.fetch_pack(determine_wants, graph_walker,
479
                lambda x: os.write(fd, x), progress)
480
        finally:
481
            os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
482
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
483
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
484
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
485
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
486
    def lookup_bzr_revision_id(self, bzr_revid):
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
487
        # This won't work for any round-tripped bzr revisions, but it's a start..
488
        try:
489
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
490
        except InvalidRevisionId:
491
            raise NoSuchRevision(self, bzr_revid)
492
0.252.48 by Jelmer Vernooij
Implement lookup_foreign_revision for remote branches.
493
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
494
        """Lookup a revision id.
495
496
        """
497
        if mapping is None:
498
            mapping = self.get_mapping()
499
        # Not really an easy way to parse foreign revids here..
500
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
501
0.200.1446 by Jelmer Vernooij
Add stub for RemoteGitRepository.revision_tree.
502
    def revision_tree(self, revid):
503
        raise GitSmartRemoteNotSupported(self.revision_tree, self)
504
0.200.1481 by Jelmer Vernooij
'Implement' RemoteGitRepository.get_revisions.
505
    def get_revisions(self, revids):
506
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
507
0.200.1557 by Jelmer Vernooij
Implement RemoteGitRepository.has_revisions.
508
    def has_revisions(self, revids):
509
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
510
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
511
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
512
class RemoteGitTagDict(GitTags):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
513
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
514
    def get_refs_container(self):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
515
        return self.repository.controldir.get_refs_container()
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
516
517
    def set_tag(self, name, revid):
518
        # FIXME: Not supported yet, should do a push of a new ref
519
        raise NotImplementedError(self.set_tag)
520
521
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
522
class RemoteGitBranch(GitBranch):
523
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
524
    def __init__(self, controldir, repository, name):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
525
        self._sha = None
0.295.1 by Jelmer Vernooij
Split up branch formats.
526
        super(RemoteGitBranch, self).__init__(controldir, repository, name,
527
                RemoteGitBranchFormat())
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
528
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
529
    def last_revision_info(self):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
530
        raise GitSmartRemoteNotSupported(self.last_revision_info, self)
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
531
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
532
    @property
533
    def user_url(self):
534
        return self.control_url
535
536
    @property
537
    def control_url(self):
538
        return self.base
539
0.200.1436 by Jelmer Vernooij
Raise UnsupportedOperation for `Branch.revision_id_to_dotted_revno`,
540
    def revision_id_to_revno(self, revision_id):
541
        raise GitSmartRemoteNotSupported(self.revision_id_to_revno, self)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
542
543
    def last_revision(self):
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
544
        return self.lookup_foreign_revision_id(self.head)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
545
546
    @property
547
    def head(self):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
548
        if self._sha is not None:
549
            return self._sha
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
550
        refs = self.controldir.get_refs_container()
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
551
        name = branch_name_to_ref(self.name)
0.200.1386 by Jelmer Vernooij
Friendlier message if HEAD is not found.
552
        try:
553
            self._sha = refs[name]
554
        except KeyError:
555
            raise NoSuchRef(name, self.repository.user_url, refs)
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
556
        return self._sha
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
557
0.200.169 by Jelmer Vernooij
Fix branch cloning.
558
    def _synchronize_history(self, destination, revision_id):
559
        """See Branch._synchronize_history()."""
560
        destination.generate_revision_history(self.last_revision())
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
561
0.289.1 by Jelmer Vernooij
No parent location for remote repos.
562
    def _get_parent_location(self):
563
        return None
564
0.200.499 by Jelmer Vernooij
Implement RemoteBranch.{get,set}_push_location.
565
    def get_push_location(self):
566
        return None
567
568
    def set_push_location(self, url):
569
        pass
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
570
571
572
def remote_refs_dict_to_container(refs_dict):
573
    base = {}
574
    peeled = {}
575
    for k, v in refs_dict.iteritems():
576
        if is_peeled(k):
577
            peeled[k[:-3]] = v
578
        else:
579
            base[k] = v
580
            peeled[k] = v
581
    ret = DictRefsContainer(base)
582
    ret._peeled = peeled
583
    return ret