/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2007-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
16
0.358.3 by Jelmer Vernooij
Enable absolute import.
17
"""Remote dirs, repositories and branches."""
18
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
19
from __future__ import absolute_import
20
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
21
import gzip
7404.5.3 by Jelmer Vernooij
Reuse connections.
22
from io import BytesIO
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
23
import re
24
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
25
from .. import (
0.200.596 by Jelmer Vernooij
Import RemoteGitBranch._get_config().
26
    config,
0.200.707 by Jelmer Vernooij
Add debug routines.
27
    debug,
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
28
    errors,
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
29
    osutils,
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
30
    trace,
0.200.333 by Jelmer Vernooij
Support progress reporting when creating index.
31
    ui,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
32
    urlutils,
33
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
34
from ..push import (
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
35
    PushResult,
36
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
37
from ..errors import (
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
38
    AlreadyBranchError,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
39
    BzrError,
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
40
    DivergedBranches,
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
41
    InProcessTransport,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
42
    InvalidRevisionId,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
43
    NoSuchFile,
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
44
    NoSuchRevision,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
45
    NoSuchTag,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
46
    NotBranchError,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
47
    NotLocalUrl,
7103.1.2 by Jelmer Vernooij
Handle PermissionDenied.
48
    PermissionDenied,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
49
    UninitializableFormat,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
50
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
51
from ..revisiontree import RevisionTree
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
52
from ..sixish import (
53
    text_type,
54
    viewitems,
55
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
56
from ..transport import (
0.200.292 by Jelmer Vernooij
Fix formatting.
57
    Transport,
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
58
    register_urlparse_netloc_protocol,
0.200.292 by Jelmer Vernooij
Fix formatting.
59
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
60
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
61
from . import (
0.200.292 by Jelmer Vernooij
Fix formatting.
62
    lazy_check_versions,
7359.1.1 by Jelmer Vernooij
Only set user agent for GitHub.
63
    is_github_url,
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
64
    user_agent_for_github,
0.200.292 by Jelmer Vernooij
Fix formatting.
65
    )
0.200.200 by Jelmer Vernooij
Register lazily where possible.
66
lazy_check_versions()
67
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
68
from .branch import (
0.200.292 by Jelmer Vernooij
Fix formatting.
69
    GitBranch,
0.295.1 by Jelmer Vernooij
Split up branch formats.
70
    GitBranchFormat,
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
71
    GitBranchPushResult,
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
72
    GitTags,
0.406.2 by Jelmer Vernooij
Add tests.
73
    _quick_lookup_revno,
0.200.292 by Jelmer Vernooij
Fix formatting.
74
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
75
from .dir import (
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
76
    GitControlDirFormat,
77
    GitDir,
78
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
79
from .errors import (
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
80
    GitSmartRemoteNotSupported,
0.200.292 by Jelmer Vernooij
Fix formatting.
81
    NoSuchRef,
82
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
83
from .mapping import (
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
84
    mapping_registry,
85
    )
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
86
from .object_store import (
87
    get_object_store,
88
    )
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
89
from .push import (
90
    remote_divergence,
91
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
92
from .repository import (
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
93
    GitRepository,
7290.20.3 by Jelmer Vernooij
Fix tests for git/full history branches.
94
    GitRepositoryFormat,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
95
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
96
from .refs import (
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
97
    branch_name_to_ref,
0.200.1487 by Jelmer Vernooij
Use peeling.
98
    is_peeled,
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
99
    ref_to_tag_name,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
100
    tag_name_to_ref,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
101
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
102
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
103
import dulwich
104
import dulwich.client
0.200.292 by Jelmer Vernooij
Fix formatting.
105
from dulwich.errors import (
106
    GitProtocolError,
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
107
    HangupException,
0.200.292 by Jelmer Vernooij
Fix formatting.
108
    )
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
109
from dulwich.pack import (
110
    Pack,
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
111
    pack_objects_to_data,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
112
    )
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
113
from dulwich.protocol import ZERO_SHA
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
114
from dulwich.refs import (
115
    DictRefsContainer,
116
    SYMREF,
117
    )
118
from dulwich.repo import (
119
    NotGitRepository,
120
    )
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
121
import os
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
122
import select
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
123
import tempfile
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
124
125
try:
126
    import urllib.parse as urlparse
127
    from urllib.parse import splituser, splitnport
128
except ImportError:
129
    import urlparse
130
    from urllib import splituser, splitnport
0.200.1555 by Jelmer Vernooij
Remove segment parameters for http smart transports.
131
132
# urlparse only supports a limited number of schemes by default
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
133
register_urlparse_netloc_protocol('git')
134
register_urlparse_netloc_protocol('git+ssh')
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
135
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
136
from dulwich.pack import load_pack_index
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
137
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
138
0.406.1 by Jelmer Vernooij
Properly lookup revnos for brz-git push result.
139
class GitPushResult(PushResult):
140
141
    def _lookup_revno(self, revid):
0.406.2 by Jelmer Vernooij
Add tests.
142
        try:
143
            return _quick_lookup_revno(self.source_branch, self.target_branch,
7143.15.2 by Jelmer Vernooij
Run autopep8.
144
                                       revid)
0.406.2 by Jelmer Vernooij
Add tests.
145
        except GitSmartRemoteNotSupported:
146
            return None
0.406.1 by Jelmer Vernooij
Properly lookup revnos for brz-git push result.
147
148
    @property
149
    def old_revno(self):
150
        return self._lookup_revno(self.old_revid)
151
152
    @property
153
    def new_revno(self):
154
        return self._lookup_revno(self.new_revid)
155
156
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
157
# Don't run any tests on GitSmartTransport as it is not intended to be
0.200.181 by Jelmer Vernooij
Support setting tags.
158
# a full implementation of Transport
159
def get_test_permutations():
160
    return []
161
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
162
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
163
def split_git_url(url):
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
164
    """Split a Git URL.
165
166
    :param url: Git URL
167
    :return: Tuple with host, port, username, path.
168
    """
0.200.743 by Jelmer Vernooij
Fix URL parsing.
169
    (scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
170
    path = urlparse.unquote(loc)
0.246.2 by Jelmer Vernooij
Improve the fix dealing with git repo's in home directories.
171
    if path.startswith("/~"):
0.200.709 by Jelmer Vernooij
When unpacking URLs, strip leftmost slash to match gits behaviour.
172
        path = path[1:]
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
173
    (username, hostport) = splituser(netloc)
174
    (host, port) = splitnport(hostport, None)
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
175
    return (host, port, username, path)
176
177
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
178
class RemoteGitError(BzrError):
179
0.290.1 by Jelmer Vernooij
Avoid 'message' argument in RemoteGitError; apparently it breaks some versions of Python.
180
    _fmt = "Remote server error: %(msg)s"
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
181
182
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
183
class HeadUpdateFailed(BzrError):
184
185
    _fmt = ("Unable to update remote HEAD branch. To update the master "
186
            "branch, specify the URL %(base_url)s,branch=master.")
187
188
    def __init__(self, base_url):
189
        super(HeadUpdateFailed, self).__init__()
190
        self.base_url = base_url
191
192
0.200.1275 by Jelmer Vernooij
recognize missing repositories
193
def parse_git_error(url, message):
194
    """Parse a remote git server error and return a bzr exception.
195
196
    :param url: URL of the remote repository
197
    :param message: Message sent by the remote git server
198
    """
199
    message = str(message).strip()
7143.15.2 by Jelmer Vernooij
Run autopep8.
200
    if (message.startswith("Could not find Repository ")
201
        or message == 'Repository not found.'
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
202
            or (message.startswith('Repository ') and
203
                message.endswith(' not found.'))):
0.200.1275 by Jelmer Vernooij
recognize missing repositories
204
        return NotBranchError(url, message)
0.200.1563 by Jelmer Vernooij
Improve error message.
205
    if message == "HEAD failed to update":
206
        base_url, _ = urlutils.split_segment_parameters(url)
7103.1.1 by Jelmer Vernooij
Improved error parsing for Git branches.
207
        return HeadUpdateFailed(base_url)
7103.1.2 by Jelmer Vernooij
Handle PermissionDenied.
208
    if message.startswith('access denied or repository not exported:'):
7103.1.3 by Jelmer Vernooij
Strip space.
209
        extra, path = message.split(': ', 1)
7103.1.2 by Jelmer Vernooij
Handle PermissionDenied.
210
        return PermissionDenied(path, extra)
7131.7.4 by Jelmer Vernooij
Don't use GitLab string.
211
    if message.endswith('You are not allowed to push code to this project.'):
7131.7.2 by Jelmer Vernooij
Handle github PermissionDenied.
212
        return PermissionDenied(url, message)
7131.7.3 by Jelmer Vernooij
Handle one more error.
213
    if message.endswith(' does not appear to be a git repository'):
214
        return NotBranchError(url, message)
7379.1.1 by Jelmer Vernooij
Handle invalid repository name on GitHub.
215
    if re.match('(.+) is not a valid repository name',
216
                message.splitlines()[0]):
217
        return NotBranchError(url, message)
7131.7.2 by Jelmer Vernooij
Handle github PermissionDenied.
218
    m = re.match(r'Permission to ([^ ]+) denied to ([^ ]+)\.', message)
219
    if m:
220
        return PermissionDenied(m.group(1), 'denied to %s' % m.group(2))
0.200.1275 by Jelmer Vernooij
recognize missing repositories
221
    # Don't know, just return it to the user as-is
0.200.1562 by Jelmer Vernooij
Add separate exception for remote errors.
222
    return RemoteGitError(message)
0.200.1275 by Jelmer Vernooij
recognize missing repositories
223
224
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
225
class GitSmartTransport(Transport):
226
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
227
    def __init__(self, url, _client=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
228
        Transport.__init__(self, url)
0.200.708 by Jelmer Vernooij
Factor out URL parsing.
229
        (self._host, self._port, self._username, self._path) = \
230
            split_git_url(url)
0.200.707 by Jelmer Vernooij
Add debug routines.
231
        if 'transport' in debug.debug_flags:
232
            trace.mutter('host: %r, user: %r, port: %r, path: %r',
233
                         self._host, self._username, self._port, self._path)
0.200.166 by Jelmer Vernooij
don't reuse client objects.
234
        self._client = _client
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
235
        self._stripped_path = self._path.rsplit(",", 1)[0]
0.200.166 by Jelmer Vernooij
don't reuse client objects.
236
0.200.543 by Jelmer Vernooij
Implement GitSmartTransport.external_url().
237
    def external_url(self):
238
        return self.base
239
0.200.238 by Jelmer Vernooij
Import Transport.has().
240
    def has(self, relpath):
241
        return False
242
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
243
    def _get_client(self):
0.200.307 by Jelmer Vernooij
Support git+ssh.
244
        raise NotImplementedError(self._get_client)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
245
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
246
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
247
        return self._stripped_path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
248
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
249
    def get(self, path):
250
        raise NoSuchFile(path)
251
0.200.160 by Jelmer Vernooij
Implement abspath.
252
    def abspath(self, relpath):
253
        return urlutils.join(self.base, relpath)
254
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
255
    def clone(self, offset=None):
256
        """See Transport.clone()."""
257
        if offset is None:
258
            newurl = self.base
259
        else:
260
            newurl = urlutils.join(self.base, offset)
261
0.200.307 by Jelmer Vernooij
Support git+ssh.
262
        return self.__class__(newurl, self._client)
263
264
265
class TCPGitSmartTransport(GitSmartTransport):
266
0.200.332 by Jelmer Vernooij
Support activity reporting.
267
    _scheme = 'git'
268
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
269
    def _get_client(self):
0.200.307 by Jelmer Vernooij
Support git+ssh.
270
        if self._client is not None:
271
            ret = self._client
272
            self._client = None
273
            return ret
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
274
        if self._host == '':
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
275
            # return dulwich.client.LocalGitClient()
276
            return dulwich.client.SubprocessGitClient()
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
277
        return dulwich.client.TCPGitClient(
278
            self._host, self._port, report_activity=self._report_activity)
0.200.307 by Jelmer Vernooij
Support git+ssh.
279
280
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
281
class SSHSocketWrapper(object):
282
283
    def __init__(self, sock):
284
        self.sock = sock
285
286
    def read(self, len=None):
287
        return self.sock.recv(len)
288
289
    def write(self, data):
290
        return self.sock.write(data)
291
292
    def can_read(self):
293
        return len(select.select([self.sock.fileno()], [], [], 0)[0]) > 0
294
295
296
class DulwichSSHVendor(dulwich.client.SSHVendor):
297
298
    def __init__(self):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
299
        from ..transport import ssh
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
300
        self.bzr_ssh_vendor = ssh._get_ssh_vendor()
301
302
    def run_command(self, host, command, username=None, port=None):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
303
        connection = self.bzr_ssh_vendor.connect_ssh(
304
            username=username, password=None, port=port, host=host,
305
            command=command)
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
306
        (kind, io_object) = connection.get_sock_or_pipes()
307
        if kind == 'socket':
308
            return SSHSocketWrapper(io_object)
309
        else:
310
            raise AssertionError("Unknown io object kind %r'" % kind)
311
312
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
313
# dulwich.client.get_ssh_vendor = DulwichSSHVendor
0.200.1624 by Jelmer Vernooij
Add ssh vendor for dulwich that uses the bzr ssh vendor.
314
315
0.200.307 by Jelmer Vernooij
Support git+ssh.
316
class SSHGitSmartTransport(GitSmartTransport):
317
0.200.332 by Jelmer Vernooij
Support activity reporting.
318
    _scheme = 'git+ssh'
319
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
320
    def _get_path(self):
0.200.1464 by Jelmer Vernooij
Warn about ignoring path segment parameters when using bzr 2.4.
321
        path = self._stripped_path
0.200.1318 by Jelmer Vernooij
Strip segment parameters where necessary.
322
        if path.startswith("/~/"):
323
            return path[3:]
324
        return path
0.200.470 by Jelmer Vernooij
Properly parse username in URLs.
325
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
326
    def _get_client(self):
0.200.307 by Jelmer Vernooij
Support git+ssh.
327
        if self._client is not None:
328
            ret = self._client
329
            self._client = None
330
            return ret
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
331
        location_config = config.LocationConfig(self.base)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
332
        client = dulwich.client.SSHGitClient(
333
            self._host, self._port, self._username,
334
            report_activity=self._report_activity)
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
335
        # Set up alternate pack program paths
336
        upload_pack = location_config.get_user_option('git_upload_pack')
337
        if upload_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
338
            client.alternative_paths["upload-pack"] = upload_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
339
        receive_pack = location_config.get_user_option('git_receive_pack')
340
        if receive_pack:
0.200.949 by Jelmer Vernooij
merge support for specifying alternative paths for git executables.
341
            client.alternative_paths["receive-pack"] = receive_pack
0.253.1 by Ross Light
Added configuration options for git-upload-pack and git-receive-pack
342
        return client
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
343
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
344
0.295.1 by Jelmer Vernooij
Split up branch formats.
345
class RemoteGitBranchFormat(GitBranchFormat):
346
347
    def get_format_description(self):
348
        return 'Remote Git Branch'
349
350
    @property
351
    def _matchingcontroldir(self):
352
        return RemoteGitControlDirFormat()
353
0.295.2 by Jelmer Vernooij
Make RemoteGitBranchFormat uninitializeable.
354
    def initialize(self, a_controldir, name=None, repository=None,
355
                   append_revisions_only=None):
356
        raise UninitializableFormat(self)
357
0.295.1 by Jelmer Vernooij
Split up branch formats.
358
0.407.1 by Jelmer Vernooij
Improve progress reporting.
359
class DefaultProgressReporter(object):
360
361
    _GIT_PROGRESS_PARTIAL_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
362
    _GIT_PROGRESS_TOTAL_RE = re.compile(r"(.*?): (\d+)")
363
364
    def __init__(self, pb):
365
        self.pb = pb
366
367
    def progress(self, text):
7018.3.2 by Jelmer Vernooij
Fix some git tests.
368
        text = text.rstrip(b"\r\n")
369
        text = text.decode('utf-8')
7131.7.1 by Jelmer Vernooij
Handle permission denied by GitLab.
370
        if text.lower().startswith('error: '):
7018.3.2 by Jelmer Vernooij
Fix some git tests.
371
            trace.show_error('git: %s', text[len(b'error: '):])
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
372
        else:
0.407.1 by Jelmer Vernooij
Improve progress reporting.
373
            trace.mutter("git: %s", text)
374
            g = self._GIT_PROGRESS_PARTIAL_RE.match(text)
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
375
            if g is not None:
0.407.1 by Jelmer Vernooij
Improve progress reporting.
376
                (text, pct, current, total) = g.groups()
377
                self.pb.update(text, int(current), int(total))
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
378
            else:
0.407.1 by Jelmer Vernooij
Improve progress reporting.
379
                g = self._GIT_PROGRESS_TOTAL_RE.match(text)
380
                if g is not None:
381
                    (text, total) = g.groups()
382
                    self.pb.update(text, None, int(total))
383
                else:
384
                    trace.note("%s", text)
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
385
386
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
387
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
388
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
389
    def __init__(self, transport, format, client, client_path):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
390
        self._format = format
391
        self.root_transport = transport
392
        self.transport = transport
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
393
        self._mode_check_done = None
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
394
        self._client = client
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
395
        self._client_path = client_path
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
396
        self.base = self.root_transport.base
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
397
        self._refs = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
398
0.322.1 by Jelmer Vernooij
Fix access of remote git branches.
399
    @property
400
    def _gitrepository_class(self):
401
        return RemoteGitRepository
402
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
403
    def archive(self, format, committish, write_data, progress=None,
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
404
                write_error=None, subdirs=None, prefix=None):
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
405
        if progress is None:
406
            pb = ui.ui_factory.nested_progress_bar()
407
            progress = DefaultProgressReporter(pb).progress
408
        else:
409
            pb = None
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
410
        def progress_wrapper(message):
411
            if message.startswith(b"fatal: Unknown archive format \'"):
412
                format = message.strip()[len(b"fatal: Unknown archive format '"):-1]
413
                raise errors.NoSuchExportFormat(format.decode('ascii'))
414
            return progress(message)
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
415
        try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
416
            self._client.archive(
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
417
                self._client_path, committish, write_data, progress_wrapper,
418
                write_error,
419
                format=(format.encode('ascii') if format else None),
420
                subdirs=subdirs,
421
                prefix=(prefix.encode('utf-8') if prefix else None))
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
422
        except GitProtocolError as e:
423
            raise parse_git_error(self.transport.external_url(), e)
424
        finally:
425
            if pb is not None:
426
                pb.finished()
427
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
428
    def fetch_pack(self, determine_wants, graph_walker, pack_data,
429
                   progress=None):
0.200.1335 by Jelmer Vernooij
Move _get_client.
430
        if progress is None:
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
431
            pb = ui.ui_factory.nested_progress_bar()
0.407.1 by Jelmer Vernooij
Improve progress reporting.
432
            progress = DefaultProgressReporter(pb).progress
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
433
        else:
434
            pb = None
0.200.1335 by Jelmer Vernooij
Move _get_client.
435
        try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
436
            result = self._client.fetch_pack(
437
                self._client_path, determine_wants, graph_walker, pack_data,
438
                progress)
0.376.1 by Jelmer Vernooij
Add tests for remote operations.
439
            if result.refs is None:
440
                result.refs = {}
7143.15.2 by Jelmer Vernooij
Run autopep8.
441
            self._refs = remote_refs_dict_to_container(
442
                result.refs, result.symrefs)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
443
            return result
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
444
        except GitProtocolError as e:
0.200.1335 by Jelmer Vernooij
Move _get_client.
445
            raise parse_git_error(self.transport.external_url(), e)
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
446
        finally:
447
            if pb is not None:
448
                pb.finished()
0.200.1335 by Jelmer Vernooij
Move _get_client.
449
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
450
    def send_pack(self, get_changed_refs, generate_pack_data, progress=None):
451
        if progress is None:
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
452
            pb = ui.ui_factory.nested_progress_bar()
0.407.1 by Jelmer Vernooij
Improve progress reporting.
453
            progress = DefaultProgressReporter(pb).progress
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
454
        else:
455
            pb = None
7143.15.2 by Jelmer Vernooij
Run autopep8.
456
0.419.1 by Jelmer Vernooij
Simplify pushing to Git directories.
457
        def get_changed_refs_wrapper(refs):
458
            # TODO(jelmer): This drops symref information
459
            self._refs = remote_refs_dict_to_container(refs)
460
            return get_changed_refs(refs)
0.200.1335 by Jelmer Vernooij
Move _get_client.
461
        try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
462
            return self._client.send_pack(
463
                self._client_path, get_changed_refs_wrapper,
464
                generate_pack_data, progress)
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
465
        except GitProtocolError as e:
0.200.1335 by Jelmer Vernooij
Move _get_client.
466
            raise parse_git_error(self.transport.external_url(), e)
0.405.1 by Jelmer Vernooij
Use same logic for interpreting progress reports everywhere.
467
        finally:
468
            if pb is not None:
469
                pb.finished()
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
470
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
471
    def create_branch(self, name=None, repository=None,
472
                      append_revisions_only=None, ref=None):
473
        refname = self._get_selected_ref(name, ref)
474
        if refname != b'HEAD' and refname in self.get_refs_container():
475
            raise AlreadyBranchError(self.user_url)
476
        if refname in self.get_refs_container():
7143.15.2 by Jelmer Vernooij
Run autopep8.
477
            ref_chain, unused_sha = self.get_refs_container().follow(
478
                self._get_selected_ref(None))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
479
            if ref_chain[0] == b'HEAD':
480
                refname = ref_chain[1]
481
        repo = self.open_repository()
482
        return RemoteGitBranch(self, repo, refname)
483
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
484
    def destroy_branch(self, name=None):
485
        refname = self._get_selected_ref(name)
7143.15.2 by Jelmer Vernooij
Run autopep8.
486
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
487
        def get_changed_refs(old_refs):
7240.3.1 by Jelmer Vernooij
When sending refs to a remote server, don't send a copy of the old refs.
488
            ret = {}
7240.3.2 by Jelmer Vernooij
Fix tests.
489
            if refname not in old_refs:
0.200.1395 by Jelmer Vernooij
Fix error reporting.
490
                raise NotBranchError(self.user_url)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
491
            ret[refname] = dulwich.client.ZERO_SHA
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
492
            return ret
7143.15.2 by Jelmer Vernooij
Run autopep8.
493
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
494
        def generate_pack_data(have, want, ofs_delta=False):
495
            return pack_objects_to_data([])
496
        self.send_pack(get_changed_refs, generate_pack_data)
0.200.1393 by Jelmer Vernooij
Implement removal of remote branches.
497
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
498
    @property
499
    def user_url(self):
500
        return self.control_url
501
0.200.1314 by Jelmer Vernooij
Provide RemoteGitDir.user_transport.
502
    @property
503
    def user_transport(self):
504
        return self.root_transport
505
0.200.1395 by Jelmer Vernooij
Fix error reporting.
506
    @property
507
    def control_url(self):
508
        return self.control_transport.base
509
510
    @property
511
    def control_transport(self):
512
        return self.root_transport
513
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
514
    def open_repository(self):
0.200.1415 by Jelmer Vernooij
Fix lock files for remote directories.
515
        return RemoteGitRepository(self)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
516
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
517
    def get_branch_reference(self, name=None):
518
        ref = branch_name_to_ref(name)
519
        val = self.get_refs_container().read_ref(ref)
520
        if val.startswith(SYMREF):
521
            return val[len(SYMREF):]
522
        return None
523
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
524
    def open_branch(self, name=None, unsupported=False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
525
                    ignore_fallbacks=False, ref=None, possible_transports=None,
526
                    nascent_ok=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
527
        repo = self.open_repository()
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
528
        ref = self._get_selected_ref(name, ref)
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
529
        try:
530
            if not nascent_ok and ref not in self.get_refs_container():
7143.16.10 by Jelmer Vernooij
Fix E128.
531
                raise NotBranchError(
532
                    self.root_transport.base, controldir=self)
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
533
        except NotGitRepository:
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
534
            raise NotBranchError(self.root_transport.base,
7143.15.2 by Jelmer Vernooij
Run autopep8.
535
                                 controldir=self)
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
536
        ref_chain, unused_sha = self.get_refs_container().follow(ref)
537
        return RemoteGitBranch(self, repo, ref_chain[-1])
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
538
0.200.662 by Jelmer Vernooij
Deal with recommend_upgrade argument to open_workingtree.
539
    def open_workingtree(self, recommend_upgrade=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
540
        raise NotLocalUrl(self.transport.base)
541
0.310.4 by Jelmer Vernooij
Implement RemoteControlDir.has_workingtree.
542
    def has_workingtree(self):
543
        return False
544
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
545
    def get_peeled(self, name):
546
        return self.get_refs_container().get_peeled(name)
547
0.200.1487 by Jelmer Vernooij
Use peeling.
548
    def get_refs_container(self):
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
549
        if self._refs is not None:
550
            return self._refs
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
551
        result = self.fetch_pack(lambda x: None, None,
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
552
                                 lambda x: None,
553
                                 lambda x: trace.mutter("git: %s" % x))
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
554
        self._refs = remote_refs_dict_to_container(
7143.15.2 by Jelmer Vernooij
Run autopep8.
555
            result.refs, result.symrefs)
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
556
        return self._refs
557
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
558
    def push_branch(self, source, revision_id=None, overwrite=False,
559
                    remember=False, create_prefix=False, lossy=False,
560
                    name=None):
561
        """Push the source branch into this ControlDir."""
562
        if revision_id is None:
563
            # No revision supplied by the user, default to the branch
564
            # revision
565
            revision_id = source.last_revision()
566
0.406.1 by Jelmer Vernooij
Properly lookup revnos for brz-git push result.
567
        push_result = GitPushResult()
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
568
        push_result.workingtree_updated = None
569
        push_result.master_branch = None
570
        push_result.source_branch = source
571
        push_result.stacked_on = None
572
        push_result.branch_push_result = None
573
        repo = self.find_repository()
574
        refname = self._get_selected_ref(name)
0.407.1 by Jelmer Vernooij
Improve progress reporting.
575
        if isinstance(source, GitBranch) and lossy:
576
            raise errors.LossyPushToSameVCS(source.controldir, self)
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
577
        source_store = get_object_store(source.repository)
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
578
        fetch_tags = source.get_config_stack().get('branch.fetch_tags')
579
        def get_changed_refs(refs):
580
            self._refs = remote_refs_dict_to_container(refs)
581
            ret = {}
582
            # TODO(jelmer): Unpeel if necessary
583
            push_result.new_original_revid = revision_id
584
            if lossy:
585
                new_sha = source_store._lookup_revision_sha1(revision_id)
586
            else:
587
                try:
588
                    new_sha = repo.lookup_bzr_revision_id(revision_id)[0]
589
                except errors.NoSuchRevision:
590
                    raise errors.NoRoundtrippingSupport(
591
                        source, self.open_branch(name=name, nascent_ok=True))
592
            if not overwrite:
593
                if remote_divergence(ret.get(refname), new_sha,
594
                                     source_store):
595
                    raise DivergedBranches(
596
                        source, self.open_branch(name, nascent_ok=True))
597
            ret[refname] = new_sha
598
            if fetch_tags:
7289.1.5 by Jelmer Vernooij
Fix flake8 warning.
599
                for tagname, revid in viewitems(source.tags.get_tag_dict()):
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
600
                    if lossy:
601
                        new_sha = source_store._lookup_revision_sha1(revid)
602
                    else:
603
                        try:
604
                            new_sha = repo.lookup_bzr_revision_id(revid)[0]
605
                        except errors.NoSuchRevision:
606
                            continue
7289.1.5 by Jelmer Vernooij
Fix flake8 warning.
607
                    ret[tag_name_to_ref(tagname)] = new_sha
7289.1.2 by Jelmer Vernooij
Fix pushing of tags as part of nascent git branches.
608
            return ret
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
609
        with source_store.lock_read():
610
            if lossy:
611
                generate_pack_data = source_store.generate_lossy_pack_data
612
            else:
613
                generate_pack_data = source_store.generate_pack_data
614
            new_refs = self.send_pack(get_changed_refs, generate_pack_data)
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
615
        push_result.new_revid = repo.lookup_foreign_revision_id(
7143.15.2 by Jelmer Vernooij
Run autopep8.
616
            new_refs[refname])
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
617
        try:
618
            old_remote = self._refs[refname]
619
        except KeyError:
620
            old_remote = ZERO_SHA
621
        push_result.old_revid = repo.lookup_foreign_revision_id(old_remote)
622
        self._refs = remote_refs_dict_to_container(new_refs)
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
623
        push_result.target_branch = self.open_branch(name)
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
624
        if old_remote != ZERO_SHA:
625
            push_result.branch_push_result = GitBranchPushResult()
626
            push_result.branch_push_result.source_branch = source
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
627
            push_result.branch_push_result.target_branch = (
628
                push_result.target_branch)
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
629
            push_result.branch_push_result.local_branch = None
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
630
            push_result.branch_push_result.master_branch = (
631
                push_result.target_branch)
0.403.3 by Jelmer Vernooij
Test RemoteGitDir.push_branch.
632
            push_result.branch_push_result.old_revid = push_result.old_revid
633
            push_result.branch_push_result.new_revid = push_result.new_revid
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
634
            push_result.branch_push_result.new_original_revid = (
635
                push_result.new_original_revid)
0.401.4 by Jelmer Vernooij
Implement RemoteGitDir.push_branch.
636
        if source.get_push_location() is None or remember:
637
            source.set_push_location(push_result.target_branch.base)
638
        return push_result
639
0.409.1 by Jelmer Vernooij
Don't probe for commondir over remote transport.
640
    def _find_commondir(self):
641
        # There is no way to find the commondir, if there is any.
642
        return self
643
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
644
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
645
class EmptyObjectStoreIterator(dict):
646
647
    def iterobjects(self):
648
        return []
649
650
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
651
class TemporaryPackIterator(Pack):
652
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
653
    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.
654
        super(TemporaryPackIterator, self).__init__(
655
            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.
656
        self._idx_load = lambda: self._idx_load_or_generate(self._idx_path)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
657
0.278.2 by William Grant
Also override _idx_load rather than index, to be a bit cleaner.
658
    def _idx_load_or_generate(self, path):
659
        if not os.path.exists(path):
7143.22.2 by Jelmer Vernooij
use more context libs for progress bars.
660
            with ui.ui_factory.nested_progress_bar() as pb:
0.278.2 by William Grant
Also override _idx_load rather than index, to be a bit cleaner.
661
                def report_progress(cur, total):
662
                    pb.update("generating index", cur, total)
7143.22.3 by Jelmer Vernooij
merge trunk.
663
                self.data.create_index(path, progress=report_progress)
0.278.2 by William Grant
Also override _idx_load rather than index, to be a bit cleaner.
664
        return load_pack_index(path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
665
666
    def __del__(self):
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
667
        if self._idx is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
668
            self._idx.close()
669
            os.remove(self._idx_path)
0.200.611 by Jelmer Vernooij
Merge warning fix from Naoki.
670
        if self._data is not None:
0.241.1 by Naoki INADA
Fix can't delete tempfile on Windows
671
            self._data.close()
672
            os.remove(self._data_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
673
674
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
675
class BzrGitHttpClient(dulwich.client.HttpGitClient):
676
677
    def __init__(self, transport, *args, **kwargs):
678
        self.transport = transport
7268.10.2 by Jelmer Vernooij
Strip username.
679
        url = urlutils.URL.from_string(transport.external_url())
680
        url.user = url.quoted_user = None
681
        url.password = url.quoted_password = None
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
682
        url = urlutils.split_segment_parameters(str(url))[0]
683
        super(BzrGitHttpClient, self).__init__(url, *args, **kwargs)
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
684
685
    def _http_request(self, url, headers=None, data=None,
686
                      allow_compression=False):
687
        """Perform HTTP request.
688
689
        :param url: Request URL.
690
        :param headers: Optional custom headers to override defaults.
691
        :param data: Request data.
692
        :param allow_compression: Allow GZipped communication.
693
        :return: Tuple (`response`, `read`), where response is an `urllib3`
7140.1.1 by Jelmer Vernooij
Rollback https://code.launchpad.net/~jelmer/brz/python3-git-fix-http/+merge/356238
694
            response object with additional `content_type` and
695
            `redirect_location` properties, and `read` is a consumable read
696
            method for the response data.
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
697
        """
7359.1.1 by Jelmer Vernooij
Only set user agent for GitHub.
698
        if is_github_url(url):
699
            headers['User-agent'] = user_agent_for_github()
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
700
        headers["Pragma"] = "no-cache"
701
        if allow_compression:
702
            headers["Accept-Encoding"] = "gzip"
703
        else:
704
            headers["Accept-Encoding"] = "identity"
705
7296.2.2 by Jelmer Vernooij
Add a urllib3-like interface.
706
        response = self.transport.request(
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
707
            ('GET' if data is None else 'POST'),
7320.1.1 by Jelmer Vernooij
Fix git http support.
708
            url,
7296.2.2 by Jelmer Vernooij
Add a urllib3-like interface.
709
            body=data,
710
            headers=headers, retries=8)
711
712
        if response.status == 404:
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
713
            raise NotGitRepository()
7296.2.2 by Jelmer Vernooij
Add a urllib3-like interface.
714
        elif response.status != 200:
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
715
            raise GitProtocolError("unexpected http resp %d for %s" %
716
                                   (response.code, url))
717
718
        # TODO: Optimization available by adding `preload_content=False` to the
719
        # request and just passing the `read` method on instead of going via
720
        # `BytesIO`, if we can guarantee that the entire response is consumed
721
        # before issuing the next to still allow for connection reuse from the
722
        # pool.
723
        if response.getheader("Content-Encoding") == "gzip":
7404.5.3 by Jelmer Vernooij
Reuse connections.
724
            read = gzip.GzipFile(fileobj=BytesIO(response.read())).read
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
725
        else:
726
            read = response.read
727
728
        class WrapResponse(object):
729
730
            def __init__(self, response):
731
                self._response = response
7320.1.1 by Jelmer Vernooij
Fix git http support.
732
                self.status = response.status
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
733
                self.content_type = response.getheader("Content-Type")
7320.1.1 by Jelmer Vernooij
Fix git http support.
734
                self.redirect_location = response._actual.geturl()
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
735
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
736
            def readlines(self):
737
                return self._response.readlines()
738
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
739
            def close(self):
7320.1.1 by Jelmer Vernooij
Fix git http support.
740
                pass
0.409.2 by Jelmer Vernooij
call out to HTTP transport rather than creating new connection.
741
742
        return WrapResponse(response), read
0.200.1337 by Jelmer Vernooij
Re-use http connection if possible.
743
744
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
745
def _git_url_and_path_from_transport(external_url):
7371.3.3 by Jelmer Vernooij
Fix tests.
746
    url, _ = urlutils.split_segment_parameters(external_url)
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
747
    return urlparse.urlsplit(url)
748
749
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
750
class RemoteGitControlDirFormat(GitControlDirFormat):
751
    """The .git directory control format."""
752
753
    supports_workingtrees = False
754
755
    @classmethod
756
    def _known_formats(self):
757
        return set([RemoteGitControlDirFormat()])
758
0.295.1 by Jelmer Vernooij
Split up branch formats.
759
    def get_branch_format(self):
760
        return RemoteGitBranchFormat()
761
7290.20.3 by Jelmer Vernooij
Fix tests for git/full history branches.
762
    @property
763
    def repository_format(self):
764
        return GitRepositoryFormat()
765
0.200.1413 by Jelmer Vernooij
Fix is_initializable()
766
    def is_initializable(self):
767
        return False
768
769
    def is_supported(self):
770
        return True
771
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
772
    def open(self, transport, _found=None):
773
        """Open this directory.
774
775
        """
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
776
        split_url = _git_url_and_path_from_transport(transport.external_url())
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
777
        if isinstance(transport, GitSmartTransport):
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
778
            client = transport._get_client()
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
779
        elif split_url.scheme in ("http", "https"):
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
780
            client = BzrGitHttpClient(transport)
7380.1.1 by Jelmer Vernooij
Several more fixes for git merge proposals.
781
        elif split_url.scheme in ('file', ):
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
782
            client = dulwich.client.LocalGitClient()
0.200.1336 by Jelmer Vernooij
Support the git smart server http protocol.
783
        else:
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
784
            raise NotBranchError(transport.base)
0.344.1 by Jelmer Vernooij
Allow using local git executable by accessing git:///some/path.
785
        if not _found:
7143.15.2 by Jelmer Vernooij
Run autopep8.
786
            pass  # TODO(jelmer): Actually probe for something
7371.3.2 by Jelmer Vernooij
Fix URL parsing for Git.
787
        return RemoteGitDir(transport, self, client, split_url.path)
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
788
789
    def get_format_description(self):
790
        return "Remote Git Repository"
791
792
    def initialize_on_transport(self, transport):
793
        raise UninitializableFormat(self)
794
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
795
    def supports_transport(self, transport):
796
        try:
797
            external_url = transport.external_url()
798
        except InProcessTransport:
799
            raise NotBranchError(path=transport.base)
7143.15.2 by Jelmer Vernooij
Run autopep8.
800
        return (external_url.startswith("http:")
801
                or external_url.startswith("https:")
802
                or external_url.startswith("git+")
803
                or external_url.startswith("git:"))
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
804
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
805
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
806
class GitRemoteRevisionTree(RevisionTree):
807
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
808
    def archive(self, format, name, root=None, subdir=None, force_mtime=None):
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
809
        """Create an archive of this tree.
810
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
811
        :param format: Format name (e.g. 'tar')
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
812
        :param name: target file name
813
        :param root: Root directory name (or None)
814
        :param subdir: Subdirectory to export (or None)
815
        :return: Iterator over archive chunks
816
        """
817
        commit = self._repository.lookup_bzr_revision_id(
818
            self.get_revision_id())[0]
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
819
        f = tempfile.SpooledTemporaryFile()
820
        # git-upload-archive(1) generaly only supports refs. So let's see if we
821
        # can find one.
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
822
        reverse_refs = {
7143.15.2 by Jelmer Vernooij
Run autopep8.
823
            v: k for (k, v) in
824
            self._repository.controldir.get_refs_container().as_dict().items()}
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
825
        try:
826
            committish = reverse_refs[commit]
827
        except KeyError:
828
            # No? Maybe the user has uploadArchive.allowUnreachable enabled.
829
            # Let's hope for the best.
830
            committish = commit
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
831
        self._repository.archive(
7143.15.2 by Jelmer Vernooij
Run autopep8.
832
            format, committish, f.write,
833
            subdirs=([subdir] if subdir else None),
834
            prefix=(root + '/') if root else '')
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
835
        f.seek(0)
6968.4.4 by Jelmer Vernooij
Update for API changes from archive branch.
836
        return osutils.file_iterator(f)
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
837
7192.5.1 by Jelmer Vernooij
Remove more file ids.
838
    def is_versioned(self, path):
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
839
        raise GitSmartRemoteNotSupported(self.is_versioned, self)
840
841
    def has_filename(self, path):
842
        raise GitSmartRemoteNotSupported(self.has_filename, self)
843
7192.5.1 by Jelmer Vernooij
Remove more file ids.
844
    def get_file_text(self, path):
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
845
        raise GitSmartRemoteNotSupported(self.get_file_text, self)
846
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
847
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
848
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
849
7254.5.1 by Jelmer Vernooij
Add supports_random_access attribute to repositories.
850
    supports_random_access = False
851
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
852
    @property
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
853
    def user_url(self):
854
        return self.control_url
855
0.200.1288 by Jelmer Vernooij
Properly raise GitRemoteNotSupported from RemoteGitRepository.
856
    def get_parent_map(self, revids):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
857
        raise GitSmartRemoteNotSupported(self.get_parent_map, self)
0.200.319 by Jelmer Vernooij
Print proper error when trying unsupported operations against a git server.
858
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
859
    def archive(self, *args, **kwargs):
860
        return self.controldir.archive(*args, **kwargs)
861
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
862
    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.
863
                   progress=None):
7143.2.1 by Jelmer Vernooij
Don't hardcode the list of supported archive formats.
864
        return self.controldir.fetch_pack(
7143.16.8 by Jelmer Vernooij
Fix E126
865
            determine_wants, graph_walker, pack_data, progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
866
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
867
    def send_pack(self, get_changed_refs, generate_pack_data):
868
        return self.controldir.send_pack(get_changed_refs, generate_pack_data)
0.200.427 by Jelmer Vernooij
make send_pack accessible.
869
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
870
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
871
                      progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
872
        fd, path = tempfile.mkstemp(suffix=".pack")
0.200.1299 by Jelmer Vernooij
Make sure file gets closed.
873
        try:
874
            self.fetch_pack(determine_wants, graph_walker,
7143.15.2 by Jelmer Vernooij
Run autopep8.
875
                            lambda x: os.write(fd, x), progress)
0.200.1299 by Jelmer Vernooij
Make sure file gets closed.
876
        finally:
877
            os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
878
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
879
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
880
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
881
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
882
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
883
        # This won't work for any round-tripped bzr revisions, but it's a
884
        # start..
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
885
        try:
886
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
887
        except InvalidRevisionId:
888
            raise NoSuchRevision(self, bzr_revid)
889
0.252.48 by Jelmer Vernooij
Implement lookup_foreign_revision for remote branches.
890
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
891
        """Lookup a revision id.
892
893
        """
894
        if mapping is None:
895
            mapping = self.get_mapping()
896
        # Not really an easy way to parse foreign revids here..
897
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
898
0.200.1446 by Jelmer Vernooij
Add stub for RemoteGitRepository.revision_tree.
899
    def revision_tree(self, revid):
6968.4.1 by Jelmer Vernooij
Add support for exporting archives in Git.
900
        return GitRemoteRevisionTree(self, revid)
0.200.1446 by Jelmer Vernooij
Add stub for RemoteGitRepository.revision_tree.
901
0.200.1481 by Jelmer Vernooij
'Implement' RemoteGitRepository.get_revisions.
902
    def get_revisions(self, revids):
903
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
904
0.200.1557 by Jelmer Vernooij
Implement RemoteGitRepository.has_revisions.
905
    def has_revisions(self, revids):
906
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
907
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
908
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
909
class RemoteGitTagDict(GitTags):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
910
911
    def set_tag(self, name, revid):
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
912
        sha = self.branch.lookup_bzr_revision_id(revid)[0]
913
        self._set_ref(name, sha)
914
915
    def delete_tag(self, name):
916
        self._set_ref(name, dulwich.client.ZERO_SHA)
917
918
    def _set_ref(self, name, sha):
919
        ref = tag_name_to_ref(name)
7143.15.2 by Jelmer Vernooij
Run autopep8.
920
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
921
        def get_changed_refs(old_refs):
7240.3.1 by Jelmer Vernooij
When sending refs to a remote server, don't send a copy of the old refs.
922
            ret = {}
7240.3.2 by Jelmer Vernooij
Fix tests.
923
            if sha == dulwich.client.ZERO_SHA and ref not in old_refs:
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
924
                raise NoSuchTag(name)
925
            ret[ref] = sha
926
            return ret
7143.15.2 by Jelmer Vernooij
Run autopep8.
927
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
928
        def generate_pack_data(have, want, ofs_delta=False):
929
            return pack_objects_to_data([])
930
        self.repository.send_pack(get_changed_refs, generate_pack_data)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
931
932
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
933
class RemoteGitBranch(GitBranch):
934
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
935
    def __init__(self, controldir, repository, name):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
936
        self._sha = None
0.295.1 by Jelmer Vernooij
Split up branch formats.
937
        super(RemoteGitBranch, self).__init__(controldir, repository, name,
7143.15.2 by Jelmer Vernooij
Run autopep8.
938
                                              RemoteGitBranchFormat())
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
939
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
940
    def last_revision_info(self):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
941
        raise GitSmartRemoteNotSupported(self.last_revision_info, self)
0.200.1317 by Jelmer Vernooij
Avoid NotImplementedError.
942
0.200.1068 by Jelmer Vernooij
Implement user_url/control_url.
943
    @property
944
    def user_url(self):
945
        return self.control_url
946
947
    @property
948
    def control_url(self):
949
        return self.base
950
0.200.1436 by Jelmer Vernooij
Raise UnsupportedOperation for `Branch.revision_id_to_dotted_revno`,
951
    def revision_id_to_revno(self, revision_id):
952
        raise GitSmartRemoteNotSupported(self.revision_id_to_revno, self)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
953
954
    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.
955
        return self.lookup_foreign_revision_id(self.head)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
956
957
    @property
958
    def head(self):
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
959
        if self._sha is not None:
960
            return self._sha
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
961
        refs = self.controldir.get_refs_container()
0.200.1561 by Jelmer Vernooij
Some fixes for colocated branch handling.
962
        name = branch_name_to_ref(self.name)
0.200.1386 by Jelmer Vernooij
Friendlier message if HEAD is not found.
963
        try:
964
            self._sha = refs[name]
965
        except KeyError:
966
            raise NoSuchRef(name, self.repository.user_url, refs)
0.200.919 by Jelmer Vernooij
Simplify ref handling in remote.py.
967
        return self._sha
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
968
0.200.169 by Jelmer Vernooij
Fix branch cloning.
969
    def _synchronize_history(self, destination, revision_id):
970
        """See Branch._synchronize_history()."""
7143.12.1 by Jelmer Vernooij
Support cloning revisions referenced only by an annotated tag.
971
        if revision_id is None:
972
            revision_id = self.last_revision()
973
        destination.generate_revision_history(revision_id)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
974
0.289.1 by Jelmer Vernooij
No parent location for remote repos.
975
    def _get_parent_location(self):
976
        return None
977
0.200.499 by Jelmer Vernooij
Implement RemoteBranch.{get,set}_push_location.
978
    def get_push_location(self):
979
        return None
980
981
    def set_push_location(self, url):
982
        pass
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
983
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
984
    def _iter_tag_refs(self):
985
        """Iterate over the tag refs.
986
987
        :param refs: Refs dictionary (name -> git sha1)
988
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
989
        """
990
        refs = self.controldir.get_refs_container()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
991
        for ref_name, unpeeled in refs.as_dict().items():
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
992
            try:
993
                tag_name = ref_to_tag_name(ref_name)
994
            except (ValueError, UnicodeDecodeError):
995
                continue
996
            peeled = refs.get_peeled(ref_name)
997
            if peeled is None:
7058.4.36 by Jelmer Vernooij
Fix peeled error.
998
                # Let's just hope it's a commit
999
                peeled = unpeeled
6973.6.2 by Jelmer Vernooij
Fix more tests.
1000
            if not isinstance(tag_name, text_type):
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
1001
                raise TypeError(tag_name)
1002
            yield (ref_name, tag_name, peeled, unpeeled)
1003
7131.12.1 by Jelmer Vernooij
Support uncommit on remote git branches.
1004
    def set_last_revision_info(self, revno, revid):
1005
        self.generate_revision_history(revid)
1006
1007
    def generate_revision_history(self, revision_id, last_rev=None,
1008
                                  other_branch=None):
1009
        sha = self.lookup_bzr_revision_id(revision_id)[0]
1010
        def get_changed_refs(old_refs):
1011
            return {self.ref: sha}
1012
        def generate_pack_data(have, want, ofs_delta=False):
1013
            return pack_objects_to_data([])
1014
        self.repository.send_pack(get_changed_refs, generate_pack_data)
1015
        self._sha = sha
1016
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
1017
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
1018
def remote_refs_dict_to_container(refs_dict, symrefs_dict={}):
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
1019
    base = {}
1020
    peeled = {}
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
1021
    for k, v in refs_dict.items():
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
1022
        if is_peeled(k):
1023
            peeled[k[:-3]] = v
1024
        else:
1025
            base[k] = v
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
1026
    for name, target in symrefs_dict.items():
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
1027
        base[name] = SYMREF + target
0.200.1488 by Jelmer Vernooij
Factor out remote_refs_dict_to_container.
1028
    ret = DictRefsContainer(base)
1029
    ret._peeled = peeled
1030
    return ret