/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-11 17:41:33 UTC
  • mto: This revision was merged to the branch mainline in revision 7440.
  • Revision ID: jelmer@jelmer.uk-20200111174133-ob2p0twwsmvw5ut7
Don't lazy-import errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2006-2009 Canonical Ltd
 
3
 
 
4
# Authors: Robert Collins <robert.collins@canonical.com>
 
5
#          Jelmer Vernooij <jelmer@jelmer.uk>
 
6
#          John Carr <john.carr@unrouted.co.uk>
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
21
 
 
22
 
 
23
"""A GIT branch and repository format implementation for bzr."""
 
24
 
 
25
from __future__ import absolute_import
 
26
 
 
27
import os
 
28
import sys
 
29
 
 
30
dulwich_minimum_version = (0, 19, 11)
 
31
 
 
32
from .. import (  # noqa: F401
 
33
    __version__ as breezy_version,
 
34
    errors as brz_errors,
 
35
    trace,
 
36
    urlutils,
 
37
    version_info,
 
38
    )
 
39
 
 
40
from ..controldir import (
 
41
    ControlDirFormat,
 
42
    Prober,
 
43
    format_registry,
 
44
    network_format_registry as controldir_network_format_registry,
 
45
    )
 
46
 
 
47
from ..transport import (
 
48
    register_lazy_transport,
 
49
    register_transport_proto,
 
50
    transport_server_registry,
 
51
    )
 
52
from ..commands import (
 
53
    plugin_cmds,
 
54
    )
 
55
 
 
56
 
 
57
if getattr(sys, "frozen", None):
 
58
    # allow import additional libs from ./_lib for bzr.exe only
 
59
    sys.path.append(os.path.normpath(
 
60
        os.path.join(os.path.dirname(__file__), '_lib')))
 
61
 
 
62
 
 
63
def import_dulwich():
 
64
    try:
 
65
        from dulwich import __version__ as dulwich_version
 
66
    except ImportError:
 
67
        raise brz_errors.DependencyNotPresent(
 
68
            "dulwich",
 
69
            "bzr-git: Please install dulwich, https://www.dulwich.io/")
 
70
    else:
 
71
        if dulwich_version < dulwich_minimum_version:
 
72
            raise brz_errors.DependencyNotPresent(
 
73
                "dulwich",
 
74
                "bzr-git: Dulwich is too old; at least %d.%d.%d is required" %
 
75
                dulwich_minimum_version)
 
76
 
 
77
 
 
78
_versions_checked = False
 
79
 
 
80
 
 
81
def lazy_check_versions():
 
82
    global _versions_checked
 
83
    if _versions_checked:
 
84
        return
 
85
    import_dulwich()
 
86
    _versions_checked = True
 
87
 
 
88
 
 
89
format_registry.register_lazy(
 
90
    'git', __name__ + ".dir", "LocalGitControlDirFormat",
 
91
    help='GIT repository.', native=False, experimental=False)
 
92
 
 
93
format_registry.register_lazy(
 
94
    'git-bare', __name__ + ".dir", "BareLocalGitControlDirFormat",
 
95
    help='Bare GIT repository (no working tree).', native=False,
 
96
    experimental=False)
 
97
 
 
98
from ..revisionspec import (RevisionSpec_dwim, revspec_registry)
 
99
revspec_registry.register_lazy("git:", __name__ + ".revspec",
 
100
                               "RevisionSpec_git")
 
101
RevisionSpec_dwim.append_possible_lazy_revspec(
 
102
    __name__ + ".revspec", "RevisionSpec_git")
 
103
 
 
104
 
 
105
class LocalGitProber(Prober):
 
106
 
 
107
    @classmethod
 
108
    def priority(klass, transport):
 
109
        return 10
 
110
 
 
111
    def probe_transport(self, transport):
 
112
        try:
 
113
            external_url = transport.external_url()
 
114
        except brz_errors.InProcessTransport:
 
115
            raise brz_errors.NotBranchError(path=transport.base)
 
116
        if (external_url.startswith("http:") or
 
117
                external_url.startswith("https:")):
 
118
            # Already handled by RemoteGitProber
 
119
            raise brz_errors.NotBranchError(path=transport.base)
 
120
        if urlutils.split(transport.base)[1] == ".git":
 
121
            raise brz_errors.NotBranchError(path=transport.base)
 
122
        if not transport.has_any(['objects', '.git/objects', '.git']):
 
123
            raise brz_errors.NotBranchError(path=transport.base)
 
124
        lazy_check_versions()
 
125
        from .dir import (
 
126
            BareLocalGitControlDirFormat,
 
127
            LocalGitControlDirFormat,
 
128
            )
 
129
        if transport.has_any(['.git/objects', '.git']):
 
130
            return LocalGitControlDirFormat()
 
131
        if transport.has('info') and transport.has('objects'):
 
132
            return BareLocalGitControlDirFormat()
 
133
        raise brz_errors.NotBranchError(path=transport.base)
 
134
 
 
135
    @classmethod
 
136
    def known_formats(cls):
 
137
        from .dir import (
 
138
            BareLocalGitControlDirFormat,
 
139
            LocalGitControlDirFormat,
 
140
            )
 
141
        return [BareLocalGitControlDirFormat(), LocalGitControlDirFormat()]
 
142
 
 
143
 
 
144
def user_agent_for_github():
 
145
    # GitHub requires we lie. https://github.com/dulwich/dulwich/issues/562
 
146
    return "git/Breezy/%s" % breezy_version
 
147
 
 
148
 
 
149
def is_github_url(url):
 
150
    (scheme, user, password, host, port,
 
151
     path) = urlutils.parse_url(url)
 
152
    return host == "github.com"
 
153
 
 
154
 
 
155
class RemoteGitProber(Prober):
 
156
 
 
157
    @classmethod
 
158
    def priority(klass, transport):
 
159
        # This is a surprisingly good heuristic to determine whether this
 
160
        # prober is more likely to succeed than the Bazaar one.
 
161
        if 'git' in transport.base:
 
162
            return -15
 
163
        return -10
 
164
 
 
165
    def probe_http_transport(self, transport):
 
166
        # This function intentionally doesn't use any of the support code under
 
167
        # breezy.git, since it's called for every repository that's
 
168
        # accessed over HTTP, whether it's Git, Bzr or something else.
 
169
        # Importing Dulwich and the other support code adds unnecessray slowdowns.
 
170
        base_url, _ = urlutils.split_segment_parameters(
 
171
            transport.external_url())
 
172
        url = urlutils.URL.from_string(base_url)
 
173
        url.user = url.quoted_user = None
 
174
        url.password = url.quoted_password = None
 
175
        host = url.host
 
176
        url = urlutils.join(str(url), "info/refs") + "?service=git-upload-pack"
 
177
        headers = {"Content-Type": "application/x-git-upload-pack-request",
 
178
                   "Accept": "application/x-git-upload-pack-result",
 
179
                   }
 
180
        if is_github_url(url):
 
181
            # GitHub requires we lie.
 
182
            # https://github.com/dulwich/dulwich/issues/562
 
183
            headers["User-Agent"] = user_agent_for_github()
 
184
        elif host == "bazaar.launchpad.net":
 
185
            # Don't attempt Git probes against bazaar.launchpad.net; pad.lv/1744830
 
186
            raise brz_errors.NotBranchError(transport.base)
 
187
        resp = transport.request('GET', url, headers=headers)
 
188
        if resp.status in (404, 405):
 
189
            raise brz_errors.NotBranchError(transport.base)
 
190
        elif resp.status != 200:
 
191
            raise brz_errors.InvalidHttpResponse(
 
192
                url, 'Unable to handle http code %d' % resp.status)
 
193
 
 
194
        ct = resp.getheader("Content-Type")
 
195
        if ct and ct.startswith("application/x-git"):
 
196
            from .remote import RemoteGitControlDirFormat
 
197
            return RemoteGitControlDirFormat()
 
198
        elif not ct:
 
199
            from .dir import (
 
200
                BareLocalGitControlDirFormat,
 
201
                )
 
202
            ret = BareLocalGitControlDirFormat()
 
203
            ret._refs_text = resp.read()
 
204
            return ret
 
205
        raise brz_errors.NotBranchError(transport.base)
 
206
 
 
207
    def probe_transport(self, transport):
 
208
        try:
 
209
            external_url = transport.external_url()
 
210
        except brz_errors.InProcessTransport:
 
211
            raise brz_errors.NotBranchError(path=transport.base)
 
212
 
 
213
        if (external_url.startswith("http:") or
 
214
                external_url.startswith("https:")):
 
215
            return self.probe_http_transport(transport)
 
216
 
 
217
        if (not external_url.startswith("git://") and
 
218
                not external_url.startswith("git+")):
 
219
            raise brz_errors.NotBranchError(transport.base)
 
220
 
 
221
        # little ugly, but works
 
222
        from .remote import (
 
223
            GitSmartTransport,
 
224
            RemoteGitControlDirFormat,
 
225
            )
 
226
        if isinstance(transport, GitSmartTransport):
 
227
            return RemoteGitControlDirFormat()
 
228
        raise brz_errors.NotBranchError(path=transport.base)
 
229
 
 
230
    @classmethod
 
231
    def known_formats(cls):
 
232
        from .remote import RemoteGitControlDirFormat
 
233
        return [RemoteGitControlDirFormat()]
 
234
 
 
235
 
 
236
ControlDirFormat.register_prober(LocalGitProber)
 
237
ControlDirFormat.register_prober(RemoteGitProber)
 
238
 
 
239
register_transport_proto(
 
240
    'git://', help="Access using the Git smart server protocol.")
 
241
register_transport_proto(
 
242
    'git+ssh://',
 
243
    help="Access using the Git smart server protocol over SSH.")
 
244
 
 
245
register_lazy_transport("git://", __name__ + '.remote',
 
246
                        'TCPGitSmartTransport')
 
247
register_lazy_transport("git+ssh://", __name__ + '.remote',
 
248
                        'SSHGitSmartTransport')
 
249
 
 
250
 
 
251
plugin_cmds.register_lazy("cmd_git_import", [], __name__ + ".commands")
 
252
plugin_cmds.register_lazy("cmd_git_object", ["git-objects", "git-cat"],
 
253
                          __name__ + ".commands")
 
254
plugin_cmds.register_lazy("cmd_git_refs", [], __name__ + ".commands")
 
255
plugin_cmds.register_lazy("cmd_git_apply", [], __name__ + ".commands")
 
256
plugin_cmds.register_lazy("cmd_git_push_pristine_tar_deltas",
 
257
                          ['git-push-pristine-tar', 'git-push-pristine'],
 
258
                          __name__ + ".commands")
 
259
 
 
260
 
 
261
def extract_git_foreign_revid(rev):
 
262
    try:
 
263
        foreign_revid = rev.foreign_revid
 
264
    except AttributeError:
 
265
        from .mapping import mapping_registry
 
266
        foreign_revid, mapping = \
 
267
            mapping_registry.parse_revision_id(rev.revision_id)
 
268
        return foreign_revid
 
269
    else:
 
270
        from .mapping import foreign_vcs_git
 
271
        if rev.mapping.vcs == foreign_vcs_git:
 
272
            return foreign_revid
 
273
        else:
 
274
            raise brz_errors.InvalidRevisionId(rev.revision_id, None)
 
275
 
 
276
 
 
277
def update_stanza(rev, stanza):
 
278
    try:
 
279
        git_commit = extract_git_foreign_revid(rev)
 
280
    except brz_errors.InvalidRevisionId:
 
281
        pass
 
282
    else:
 
283
        stanza.add("git-commit", git_commit)
 
284
 
 
285
 
 
286
from ..hooks import install_lazy_named_hook
 
287
install_lazy_named_hook(
 
288
    "breezy.version_info_formats.format_rio",
 
289
    "RioVersionInfoBuilder.hooks", "revision", update_stanza,
 
290
    "git commits")
 
291
 
 
292
transport_server_registry.register_lazy(
 
293
    'git', __name__ + '.server', 'serve_git',
 
294
    'Git Smart server protocol over TCP. (default port: 9418)')
 
295
 
 
296
transport_server_registry.register_lazy(
 
297
    'git-receive-pack', __name__ + '.server',
 
298
    'serve_git_receive_pack',
 
299
    help='Git Smart server receive pack command. (inetd mode only)')
 
300
transport_server_registry.register_lazy(
 
301
    'git-upload-pack', __name__ + 'git.server',
 
302
    'serve_git_upload_pack',
 
303
    help='Git Smart server upload pack command. (inetd mode only)')
 
304
 
 
305
from ..repository import (
 
306
    format_registry as repository_format_registry,
 
307
    network_format_registry as repository_network_format_registry,
 
308
    )
 
309
repository_network_format_registry.register_lazy(
 
310
    b'git', __name__ + '.repository', 'GitRepositoryFormat')
 
311
 
 
312
register_extra_lazy_repository_format = getattr(repository_format_registry,
 
313
                                                "register_extra_lazy")
 
314
register_extra_lazy_repository_format(__name__ + '.repository',
 
315
                                      'GitRepositoryFormat')
 
316
 
 
317
from ..branch import (
 
318
    network_format_registry as branch_network_format_registry,
 
319
    )
 
320
branch_network_format_registry.register_lazy(
 
321
    b'git', __name__ + '.branch', 'LocalGitBranchFormat')
 
322
 
 
323
 
 
324
from ..branch import (
 
325
    format_registry as branch_format_registry,
 
326
    )
 
327
branch_format_registry.register_extra_lazy(
 
328
    __name__ + '.branch',
 
329
    'LocalGitBranchFormat',
 
330
    )
 
331
branch_format_registry.register_extra_lazy(
 
332
    __name__ + '.remote',
 
333
    'RemoteGitBranchFormat',
 
334
    )
 
335
 
 
336
 
 
337
from ..workingtree import (
 
338
    format_registry as workingtree_format_registry,
 
339
    )
 
340
workingtree_format_registry.register_extra_lazy(
 
341
    __name__ + '.workingtree',
 
342
    'GitWorkingTreeFormat',
 
343
    )
 
344
 
 
345
controldir_network_format_registry.register_lazy(
 
346
    b'git', __name__ + ".dir", "GitControlDirFormat")
 
347
 
 
348
 
 
349
from ..diff import format_registry as diff_format_registry
 
350
diff_format_registry.register_lazy(
 
351
    'git', __name__ + '.send',
 
352
    'GitDiffTree', 'Git am-style diff format')
 
353
 
 
354
from ..send import (
 
355
    format_registry as send_format_registry,
 
356
    )
 
357
send_format_registry.register_lazy('git', __name__ + '.send',
 
358
                                   'send_git', 'Git am-style diff format')
 
359
 
 
360
from ..directory_service import directories
 
361
directories.register_lazy('github:', __name__ + '.directory',
 
362
                          'GitHubDirectory',
 
363
                          'GitHub directory.')
 
364
directories.register_lazy('git@github.com:', __name__ + '.directory',
 
365
                          'GitHubDirectory',
 
366
                          'GitHub directory.')
 
367
 
 
368
from ..help_topics import (
 
369
    topic_registry,
 
370
    )
 
371
topic_registry.register_lazy(
 
372
    'git', __name__ + '.help', 'help_git', 'Using Bazaar with Git')
 
373
 
 
374
from ..foreign import (
 
375
    foreign_vcs_registry,
 
376
    )
 
377
foreign_vcs_registry.register_lazy(
 
378
    "git", __name__ + ".mapping", "foreign_vcs_git", "Stupid content tracker")
 
379
 
 
380
 
 
381
def update_git_cache(repository, revid):
 
382
    """Update the git cache after a local commit."""
 
383
    if getattr(repository, "_git", None) is not None:
 
384
        return  # No need to update cache for git repositories
 
385
 
 
386
    if not repository.control_transport.has("git"):
 
387
        return  # No existing cache, don't bother updating
 
388
    try:
 
389
        lazy_check_versions()
 
390
    except brz_errors.DependencyNotPresent as e:
 
391
        # dulwich is probably missing. silently ignore
 
392
        trace.mutter("not updating git map for %r: %s",
 
393
                     repository, e)
 
394
 
 
395
    from .object_store import BazaarObjectStore
 
396
    store = BazaarObjectStore(repository)
 
397
    with store.lock_write():
 
398
        try:
 
399
            parent_revisions = set(repository.get_parent_map([revid])[revid])
 
400
        except KeyError:
 
401
            # Isn't this a bit odd - how can a revision that was just committed
 
402
            # be missing?
 
403
            return
 
404
        missing_revisions = store._missing_revisions(parent_revisions)
 
405
        if not missing_revisions:
 
406
            store._cache.idmap.start_write_group()
 
407
            try:
 
408
                # Only update if the cache was up to date previously
 
409
                store._update_sha_map_revision(revid)
 
410
            except BaseException:
 
411
                store._cache.idmap.abort_write_group()
 
412
                raise
 
413
            else:
 
414
                store._cache.idmap.commit_write_group()
 
415
 
 
416
 
 
417
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
 
418
                             new_revno, new_revid):
 
419
    if local_branch is not None:
 
420
        update_git_cache(local_branch.repository, new_revid)
 
421
    update_git_cache(master_branch.repository, new_revid)
 
422
 
 
423
 
 
424
def loggerhead_git_hook(branch_app, environ):
 
425
    branch = branch_app.branch
 
426
    config_stack = branch.get_config_stack()
 
427
    if config_stack.get('http_git'):
 
428
        return None
 
429
    from .server import git_http_hook
 
430
    return git_http_hook(branch, environ['REQUEST_METHOD'],
 
431
                         environ['PATH_INFO'])
 
432
 
 
433
 
 
434
install_lazy_named_hook("breezy.branch",
 
435
                        "Branch.hooks", "post_commit",
 
436
                        post_commit_update_cache, "git cache")
 
437
install_lazy_named_hook("breezy.plugins.loggerhead.apps.branch",
 
438
                        "BranchWSGIApp.hooks", "controller",
 
439
                        loggerhead_git_hook, "git support")
 
440
 
 
441
 
 
442
from ..config import (
 
443
    option_registry,
 
444
    Option,
 
445
    bool_from_store,
 
446
    )
 
447
 
 
448
option_registry.register(
 
449
    Option('git.http',
 
450
           default=None, from_unicode=bool_from_store, invalid='warning',
 
451
           help='''\
 
452
Allow fetching of Git packs over HTTP.
 
453
 
 
454
This enables support for fetching Git packs over HTTP in Loggerhead.
 
455
'''))
 
456
 
 
457
 
 
458
def test_suite():
 
459
    from . import tests
 
460
    return tests.test_suite()