/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1014 by Jelmer Vernooij
Fix tests.
1
# Copyright (C) 2007-2010 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.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
19
from bzrlib 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,
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
25
    version_info as bzrlib_version,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
26
    )
27
from bzrlib.errors import (
28
    BzrError,
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
29
    InProcessTransport,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
30
    InvalidRevisionId,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
31
    NoSuchFile,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
32
    NoSuchRevision,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
33
    NotBranchError,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
34
    NotLocalUrl,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
35
    UninitializableFormat,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
36
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
37
from bzrlib.transport import (
38
    Transport,
39
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
40
0.200.292 by Jelmer Vernooij
Fix formatting.
41
from bzrlib.plugins.git import (
42
    lazy_check_versions,
43
    )
0.200.200 by Jelmer Vernooij
Register lazily where possible.
44
lazy_check_versions()
45
0.200.292 by Jelmer Vernooij
Fix formatting.
46
from bzrlib.plugins.git.branch import (
47
    GitBranch,
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.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
50
from bzrlib.plugins.git.dir import (
51
    GitControlDirFormat,
52
    GitDir,
53
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
54
from bzrlib.plugins.git.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.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
58
from bzrlib.plugins.git.mapping import (
59
    mapping_registry,
60
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
61
from bzrlib.plugins.git.repository import (
62
    GitRepository,
63
    )
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
64
from bzrlib.plugins.git.refs import (
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,
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
76
    PackData,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
77
    )
0.200.1487 by Jelmer Vernooij
Use peeling.
78
from dulwich.repo import DictRefsContainer
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
79
import os
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
114
    _fmt = "Remote server error: %(message)s"
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
        if "," in self._path and bzrlib_version < (2, 5, 0):
146
            trace.warning(
147
                "ignoring parameters %r, not supported in bzr < 2.5.",
148
                self._path.rsplit(",", 1)[1])
149
        self._stripped_path = self._path.rsplit(",", 1)[0]
0.200.166 by Jelmer Vernooij
don't reuse client objects.
150
0.200.543 by Jelmer Vernooij
Implement GitSmartTransport.external_url().
151
    def external_url(self):
152
        return self.base
153
0.200.238 by Jelmer Vernooij
Import Transport.has().
154
    def has(self, relpath):
155
        return False
156
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
157
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
158
        raise NotImplementedError(self._get_client)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
159
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
160
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
161
        return self._stripped_path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
162
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
163
    def get(self, path):
164
        raise NoSuchFile(path)
165
0.200.160 by Jelmer Vernooij
Implement abspath.
166
    def abspath(self, relpath):
167
        return urlutils.join(self.base, relpath)
168
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
169
    def clone(self, offset=None):
170
        """See Transport.clone()."""
171
        if offset is None:
172
            newurl = self.base
173
        else:
174
            newurl = urlutils.join(self.base, offset)
175
0.200.307 by Jelmer Vernooij
Support git+ssh.
176
        return self.__class__(newurl, self._client)
177
178
179
class TCPGitSmartTransport(GitSmartTransport):
180
0.200.332 by Jelmer Vernooij
Support activity reporting.
181
    _scheme = 'git'
182
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
183
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
184
        if self._client is not None:
185
            ret = self._client
186
            self._client = None
187
            return ret
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
188
        return dulwich.client.TCPGitClient(self._host, self._port,
0.200.859 by Jelmer Vernooij
Trivial cleanups.
189
            thin_packs=thin_packs, report_activity=self._report_activity)
0.200.307 by Jelmer Vernooij
Support git+ssh.
190
191
192
class SSHGitSmartTransport(GitSmartTransport):
193
0.200.332 by Jelmer Vernooij
Support activity reporting.
194
    _scheme = 'git+ssh'
195
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
196
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
197
        path = self._stripped_path
0.200.1318 by Jelmer Vernooij
Strip segment parameters where necessary.
198
        if path.startswith("/~/"):
199
            return path[3:]
200
        return path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
201
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
202
    def _get_client(self, thin_packs):
0.200.307 by Jelmer Vernooij
Support git+ssh.
203
        if self._client is not None:
204
            ret = self._client
205
            self._client = None
206
            return ret
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
207
        location_config = config.LocationConfig(self.base)
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
208
        client = dulwich.client.SSHGitClient(self._host, self._port, self._username,
0.200.567 by Jelmer Vernooij
Pass along thin pack settings.
209
            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
210
        # Set up alternate pack program paths
211
        upload_pack = location_config.get_user_option('git_upload_pack')
212
        if upload_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
213
            client.alternative_paths["upload-pack"] = upload_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
214
        receive_pack = location_config.get_user_option('git_receive_pack')
215
        if receive_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
216
            client.alternative_paths["receive-pack"] = receive_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
217
        return client
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
218
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
219
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
220
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
221
0.200.1411 by Jelmer Vernooij
Fix control files.
222
    def __init__(self, transport, format, get_client, client_path):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
223
        self._format = format
224
        self.root_transport = transport
225
        self.transport = transport
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
226
        self._mode_check_done = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
227
        self._get_client = get_client
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
228
        self._client_path = client_path
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
229
        self.base = self.root_transport.base
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
230
        self._refs = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
231
232
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
233
        if progress is None:
234
            def progress(text):
235
                trace.info("git: %s" % text)
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
236
        def wrap_determine_wants(refs_dict):
237
            return determine_wants(remote_refs_dict_to_container(refs_dict))
0.200.1335 by Jelmer Vernooij
Move _get_client.
238
        client = self._get_client(thin_packs=False)
239
        try:
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
240
            refs_dict = client.fetch_pack(self._client_path, wrap_determine_wants,
0.200.1335 by Jelmer Vernooij
Move _get_client.
241
                graph_walker, pack_data, progress)
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
242
            self._refs = remote_refs_dict_to_container(refs_dict)
0.200.1487 by Jelmer Vernooij
Use peeling.
243
            return refs_dict
0.200.1335 by Jelmer Vernooij
Move _get_client.
244
        except GitProtocolError, e:
245
            raise parse_git_error(self.transport.external_url(), e)
246
247
    def send_pack(self, get_changed_refs, generate_pack_contents):
248
        client = self._get_client(thin_packs=False)
249
        try:
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
250
            return client.send_pack(self._client_path, get_changed_refs,
0.200.1335 by Jelmer Vernooij
Move _get_client.
251
                generate_pack_contents)
252
        except GitProtocolError, e:
253
            raise parse_git_error(self.transport.external_url(), e)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
254
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
255
    def _get_default_ref(self):
256
        return "refs/heads/master"
257
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
258
    def destroy_branch(self, name=None):
259
        refname = self._get_selected_ref(name)
260
        def get_changed_refs(old_refs):
261
            ret = dict(old_refs)
262
            if not refname in ret:
0.200.1395 by Jelmer Vernooij
Fix error reporting.
263
                raise NotBranchError(self.user_url)
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
264
            ret[refname] = "00" * 20
265
            return ret
266
        self.send_pack(get_changed_refs, lambda have, want: [])
267
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
268
    @property
269
    def user_url(self):
270
        return self.control_url
271
0.200.1314 by Jelmer Vernooij
Provide RemoteGitDir.user_transport.
272
    @property
273
    def user_transport(self):
274
        return self.root_transport
275
0.200.1395 by Jelmer Vernooij
Fix error reporting.
276
    @property
277
    def control_url(self):
278
        return self.control_transport.base
279
280
    @property
281
    def control_transport(self):
282
        return self.root_transport
283
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
284
    def open_repository(self):
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
285
        return RemoteGitRepository(self)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
286
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
287
    def open_branch(self, name=None, unsupported=False,
0.200.1473 by Jelmer Vernooij
Support possible_transports argument to .open_branch.
288
            ignore_fallbacks=False, ref=None, possible_transports=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
289
        repo = self.open_repository()
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
290
        refname = self._get_selected_ref(name, ref)
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
291
        return RemoteGitBranch(self, repo, refname)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
292
0.200.662 by Jelmer Vernooij
Deal with recommend_upgrade argument to open_workingtree.
293
    def open_workingtree(self, recommend_upgrade=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
294
        raise NotLocalUrl(self.transport.base)
295
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
296
    def get_peeled(self, name):
297
        return self.get_refs_container().get_peeled(name)
298
0.200.1487 by Jelmer Vernooij
Use peeling.
299
    def get_refs_container(self):
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
300
        if self._refs is not None:
301
            return self._refs
0.200.1487 by Jelmer Vernooij
Use peeling.
302
        refs_dict = self.fetch_pack(lambda x: [], None,
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
303
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
304
        self._refs = remote_refs_dict_to_container(refs_dict)
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
305
        return self._refs
306
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
307
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
308
class EmptyObjectStoreIterator(dict):
309
310
    def iterobjects(self):
311
        return []
312
313
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
314
class TemporaryPackIterator(Pack):
315
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
316
    def __init__(self, path, resolve_ext_ref):
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
317
        super(TemporaryPackIterator, self).__init__(path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
318
        self.resolve_ext_ref = resolve_ext_ref
319
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
320
    @property
0.200.604 by Jelmer Vernooij
Fix fetching of remote repositories on Windows.
321
    def data(self):
322
        if self._data is None:
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
323
            self._data = PackData(self._data_path)
0.200.604 by Jelmer Vernooij
Fix fetching of remote repositories on Windows.
324
        return self._data
325
326
    @property
0.200.458 by Jelmer Vernooij
Fix index.
327
    def index(self):
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
328
        if self._idx is None:
0.200.562 by Jelmer Vernooij
Only create pack index if it doesn't exist yet.
329
            if not os.path.exists(self._idx_path):
330
                pb = ui.ui_factory.nested_progress_bar()
331
                try:
332
                    def report_progress(cur, total):
333
                        pb.update("generating index", cur, total)
0.200.900 by Jelmer Vernooij
Fix compatibility with newer versions of Dulwich.
334
                    self.data.create_index(self._idx_path, 
0.200.562 by Jelmer Vernooij
Only create pack index if it doesn't exist yet.
335
                        progress=report_progress)
336
                finally:
337
                    pb.finished()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
338
            self._idx = load_pack_index(self._idx_path)
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
339
        return self._idx
0.200.205 by Jelmer Vernooij
Fix remote fetching.
340
341
    def __del__(self):
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
342
        if self._idx is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
343
            self._idx.close()
344
            os.remove(self._idx_path)
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
345
        if self._data is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
346
            self._data.close()
347
            os.remove(self._data_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
348
349
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
350
class BzrGitHttpClient(dulwich.client.HttpGitClient):
351
352
    def __init__(self, transport, *args, **kwargs):
353
        self.transport = transport
354
        super(BzrGitHttpClient, self).__init__(transport.external_url(), *args, **kwargs)
355
        import urllib2
356
        self._http_perform = getattr(self.transport, "_perform", urllib2.urlopen)
357
358
    def _perform(self, req):
359
        req.accepted_errors = (200, 404)
360
        req.follow_redirections = True
361
        req.redirected_to = None
362
        return self._http_perform(req)
363
364
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
365
class RemoteGitControlDirFormat(GitControlDirFormat):
366
    """The .git directory control format."""
367
368
    supports_workingtrees = False
369
370
    @classmethod
371
    def _known_formats(self):
372
        return set([RemoteGitControlDirFormat()])
373
0.200.1413 by Jelmer Vernooij
Fix is_initializable()
374
    def is_initializable(self):
375
        return False
376
377
    def is_supported(self):
378
        return True
379
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
380
    def open(self, transport, _found=None):
381
        """Open this directory.
382
383
        """
384
        # we dont grok readonly - git isn't integrated with transport.
385
        url = transport.base
386
        if url.startswith('readonly+'):
387
            url = url[len('readonly+'):]
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
388
        if isinstance(transport, GitSmartTransport):
389
            get_client = transport._get_client
390
            client_path = transport._get_path()
391
        elif urlparse.urlsplit(transport.external_url())[0] in ("http", "https"):
392
            def get_client(thin_packs=False):
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
393
                return BzrGitHttpClient(transport, thin_packs=thin_packs)
0.200.1555 by Jelmer Vernooij
Remove segment parameters for http smart transports.
394
            client_path, _ = urlutils.split_segment_parameters(transport._path)
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
395
        else:
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
396
            raise NotBranchError(transport.base)
0.200.1411 by Jelmer Vernooij
Fix control files.
397
        return RemoteGitDir(transport, self, get_client, client_path)
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
398
399
    def get_format_description(self):
400
        return "Remote Git Repository"
401
402
    def initialize_on_transport(self, transport):
403
        raise UninitializableFormat(self)
404
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
405
    def supports_transport(self, transport):
406
        try:
407
            external_url = transport.external_url()
408
        except InProcessTransport:
409
            raise NotBranchError(path=transport.base)
410
        return (external_url.startswith("http:") or
411
                external_url.startswith("https:") or
412
                external_url.startswith("git+") or
413
                external_url.startswith("git:"))
414
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
415
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
416
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
417
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
418
    @property
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
419
    def user_url(self):
420
        return self.control_url
421
0.200.1288 by Jelmer Vernooij
Properly raise GitRemoteNotSupported from RemoteGitRepository.
422
    def get_parent_map(self, revids):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
423
        raise GitSmartRemoteNotSupported(self.get_parent_map, self)
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
424
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
425
    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.
426
                   progress=None):
0.200.1335 by Jelmer Vernooij
Move _get_client.
427
        return self.bzrdir.fetch_pack(determine_wants, graph_walker,
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
428
                                          pack_data, progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
429
0.200.427 by Jelmer Vernooij
make send_pack accessible.
430
    def send_pack(self, get_changed_refs, generate_pack_contents):
0.200.1335 by Jelmer Vernooij
Move _get_client.
431
        return self.bzrdir.send_pack(get_changed_refs, generate_pack_contents)
0.200.427 by Jelmer Vernooij
make send_pack accessible.
432
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
433
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
434
                      progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
435
        fd, path = tempfile.mkstemp(suffix=".pack")
0.200.1299 by Jelmer Vernooij
Make sure file gets closed.
436
        try:
437
            self.fetch_pack(determine_wants, graph_walker,
438
                lambda x: os.write(fd, x), progress)
439
        finally:
440
            os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
441
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
442
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
443
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
444
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
445
    def lookup_bzr_revision_id(self, bzr_revid):
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
446
        # This won't work for any round-tripped bzr revisions, but it's a start..
447
        try:
448
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
449
        except InvalidRevisionId:
450
            raise NoSuchRevision(self, bzr_revid)
451
0.252.48 by Jelmer Vernooij
Implement lookup_foreign_revision for remote branches.
452
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
453
        """Lookup a revision id.
454
455
        """
456
        if mapping is None:
457
            mapping = self.get_mapping()
458
        # Not really an easy way to parse foreign revids here..
459
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
460
0.200.1446 by Jelmer Vernooij
Add stub for RemoteGitRepository.revision_tree.
461
    def revision_tree(self, revid):
462
        raise GitSmartRemoteNotSupported(self.revision_tree, self)
463
0.200.1481 by Jelmer Vernooij
'Implement' RemoteGitRepository.get_revisions.
464
    def get_revisions(self, revids):
465
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
466
0.200.1557 by Jelmer Vernooij
Implement RemoteGitRepository.has_revisions.
467
    def has_revisions(self, revids):
468
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
469
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
470
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
471
class RemoteGitTagDict(GitTags):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
472
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
473
    def get_refs_container(self):
0.200.1487 by Jelmer Vernooij
Use peeling.
474
        return self.repository.bzrdir.get_refs_container()
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
475
476
    def set_tag(self, name, revid):
477
        # FIXME: Not supported yet, should do a push of a new ref
478
        raise NotImplementedError(self.set_tag)
479
480
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
481
class RemoteGitBranch(GitBranch):
482
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
483
    def __init__(self, bzrdir, repository, name):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
484
        self._sha = None
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
485
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
486
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
487
    def last_revision_info(self):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
488
        raise GitSmartRemoteNotSupported(self.last_revision_info, self)
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
489
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
490
    @property
491
    def user_url(self):
492
        return self.control_url
493
494
    @property
495
    def control_url(self):
496
        return self.base
497
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
498
    def revision_history(self):
0.200.1436 by Jelmer Vernooij
Raise UnsupportedOperation for `Branch.revision_id_to_dotted_revno`,
499
        raise GitSmartRemoteNotSupported(self.revision_history, self)
500
501
    def revision_id_to_revno(self, revision_id):
502
        raise GitSmartRemoteNotSupported(self.revision_id_to_revno, self)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
503
504
    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.
505
        return self.lookup_foreign_revision_id(self.head)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
506
507
    @property
508
    def head(self):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
509
        if self._sha is not None:
510
            return self._sha
0.200.1499 by Jelmer Vernooij
Fix call to get_refs_container.
511
        refs = self.bzrdir.get_refs_container()
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
512
        name = branch_name_to_ref(self.name)
0.200.1386 by Jelmer Vernooij
Friendlier message if HEAD is not found.
513
        try:
514
            self._sha = refs[name]
515
        except KeyError:
516
            raise NoSuchRef(name, self.repository.user_url, refs)
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
517
        return self._sha
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
518
0.200.169 by Jelmer Vernooij
Fix branch cloning.
519
    def _synchronize_history(self, destination, revision_id):
520
        """See Branch._synchronize_history()."""
521
        destination.generate_revision_history(self.last_revision())
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
522
0.200.499 by Jelmer Vernooij
Implement RemoteBranch.{get,set}_push_location.
523
    def get_push_location(self):
524
        return None
525
526
    def set_push_location(self, url):
527
        pass
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
528
529
530
def remote_refs_dict_to_container(refs_dict):
531
    base = {}
532
    peeled = {}
533
    for k, v in refs_dict.iteritems():
534
        if is_peeled(k):
535
            peeled[k[:-3]] = v
536
        else:
537
            base[k] = v
538
            peeled[k] = v
539
    ret = DictRefsContainer(base)
540
    ret._peeled = peeled
541
    return ret