/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/plugins/git/__init__.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-05-10 01:03:05 UTC
  • mfrom: (6883.23.28 bundle-git)
  • Revision ID: breezy.the.bot@gmail.com-20180510010305-aclsb7jlq8pmmawt
Bundle the git plugin with Breezy.

Merged from https://code.launchpad.net/~jelmer/brz/bundle-git/+merge/345138

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, 0)
 
31
 
 
32
from breezy.i18n import gettext
 
33
 
 
34
from ... import (
 
35
    __version__ as breezy_version,
 
36
    errors as bzr_errors,
 
37
    trace,
 
38
    version_info,
 
39
    )
 
40
 
 
41
from ...controldir import (
 
42
    ControlDirFormat,
 
43
    Prober,
 
44
    format_registry,
 
45
    network_format_registry as controldir_network_format_registry,
 
46
    )
 
47
 
 
48
from ...transport import (
 
49
    register_lazy_transport,
 
50
    register_transport_proto,
 
51
    transport_server_registry,
 
52
    )
 
53
from ...commands import (
 
54
    plugin_cmds,
 
55
    )
 
56
 
 
57
 
 
58
if getattr(sys, "frozen", None):
 
59
    # allow import additional libs from ./_lib for bzr.exe only
 
60
    sys.path.append(os.path.normpath(
 
61
        os.path.join(os.path.dirname(__file__), '_lib')))
 
62
 
 
63
 
 
64
def import_dulwich():
 
65
    try:
 
66
        from dulwich import __version__ as dulwich_version
 
67
    except ImportError:
 
68
        raise bzr_errors.DependencyNotPresent("dulwich",
 
69
            "bzr-git: Please install dulwich, https://www.dulwich.io/")
 
70
    else:
 
71
        if dulwich_version < dulwich_minimum_version:
 
72
            raise bzr_errors.DependencyNotPresent("dulwich",
 
73
                "bzr-git: Dulwich is too old; at least %d.%d.%d is required" %
 
74
                    dulwich_minimum_version)
 
75
 
 
76
 
 
77
_versions_checked = False
 
78
def lazy_check_versions():
 
79
    global _versions_checked
 
80
    if _versions_checked:
 
81
        return
 
82
    import_dulwich()
 
83
    _versions_checked = True
 
84
 
 
85
format_registry.register_lazy('git',
 
86
    __name__ + ".dir", "LocalGitControlDirFormat",
 
87
    help='GIT repository.', native=False, experimental=False,
 
88
    )
 
89
 
 
90
format_registry.register_lazy('git-bare',
 
91
    __name__ + ".dir", "BareLocalGitControlDirFormat",
 
92
    help='Bare GIT repository (no working tree).', native=False,
 
93
    experimental=False,
 
94
    )
 
95
 
 
96
from ...revisionspec import (RevisionSpec_dwim, revspec_registry)
 
97
revspec_registry.register_lazy("git:", __name__ + ".revspec",
 
98
    "RevisionSpec_git")
 
99
RevisionSpec_dwim.append_possible_lazy_revspec(
 
100
    __name__ + ".revspec", "RevisionSpec_git")
 
101
 
 
102
 
 
103
class LocalGitProber(Prober):
 
104
 
 
105
    def probe_transport(self, transport):
 
106
        try:
 
107
            external_url = transport.external_url()
 
108
        except bzr_errors.InProcessTransport:
 
109
            raise bzr_errors.NotBranchError(path=transport.base)
 
110
        if (external_url.startswith("http:") or
 
111
            external_url.startswith("https:")):
 
112
            # Already handled by RemoteGitProber
 
113
            raise bzr_errors.NotBranchError(path=transport.base)
 
114
        from ... import urlutils
 
115
        if urlutils.split(transport.base)[1] == ".git":
 
116
            raise bzr_errors.NotBranchError(path=transport.base)
 
117
        if not transport.has_any(['objects', '.git/objects', '.git']):
 
118
            raise bzr_errors.NotBranchError(path=transport.base)
 
119
        lazy_check_versions()
 
120
        from .dir import (
 
121
            BareLocalGitControlDirFormat,
 
122
            LocalGitControlDirFormat,
 
123
            )
 
124
        if transport.has_any(['.git/objects', '.git']):
 
125
            return LocalGitControlDirFormat()
 
126
        if transport.has('info') and transport.has('objects'):
 
127
            return BareLocalGitControlDirFormat()
 
128
        raise bzr_errors.NotBranchError(path=transport.base)
 
129
 
 
130
    @classmethod
 
131
    def known_formats(cls):
 
132
        from .dir import (
 
133
            BareLocalGitControlDirFormat,
 
134
            LocalGitControlDirFormat,
 
135
            )
 
136
        return set([BareLocalGitControlDirFormat(), LocalGitControlDirFormat()])
 
137
 
 
138
 
 
139
def user_agent_for_github():
 
140
    # GitHub requires we lie. https://github.com/dulwich/dulwich/issues/562
 
141
    return "git/Breezy/%s" % breezy_version
 
142
 
 
143
 
 
144
class RemoteGitProber(Prober):
 
145
 
 
146
    def probe_http_transport(self, transport):
 
147
        from ... import urlutils
 
148
        base_url, _ = urlutils.split_segment_parameters(transport.external_url())
 
149
        url = urlutils.join(base_url, "info/refs") + "?service=git-upload-pack"
 
150
        from ...transport.http import Request
 
151
        headers = {"Content-Type": "application/x-git-upload-pack-request"}
 
152
        req = Request('GET', url, accepted_errors=[200, 403, 404, 405],
 
153
                      headers=headers)
 
154
        if req.get_host() == "github.com":
 
155
            # GitHub requires we lie. https://github.com/dulwich/dulwich/issues/562
 
156
            req.add_header("User-Agent", user_agent_for_github())
 
157
        elif req.get_host() == "bazaar.launchpad.net":
 
158
            # Don't attempt Git probes against bazaar.launchpad.net; pad.lv/1744830
 
159
            raise bzr_errors.NotBranchError(transport.base)
 
160
        req.follow_redirections = True
 
161
        resp = transport._perform(req)
 
162
        if resp.code in (404, 405):
 
163
            raise bzr_errors.NotBranchError(transport.base)
 
164
        headers = resp.headers
 
165
        ct = headers.getheader("Content-Type")
 
166
        if ct is None:
 
167
            raise bzr_errors.NotBranchError(transport.base)
 
168
        if ct.startswith("application/x-git"):
 
169
            from .remote import RemoteGitControlDirFormat
 
170
            return RemoteGitControlDirFormat()
 
171
        else:
 
172
            from .dir import (
 
173
                BareLocalGitControlDirFormat,
 
174
                )
 
175
            ret = BareLocalGitControlDirFormat()
 
176
            ret._refs_text = resp.read()
 
177
            return ret
 
178
 
 
179
    def probe_transport(self, transport):
 
180
        try:
 
181
            external_url = transport.external_url()
 
182
        except bzr_errors.InProcessTransport:
 
183
            raise bzr_errors.NotBranchError(path=transport.base)
 
184
 
 
185
        if (external_url.startswith("http:") or
 
186
            external_url.startswith("https:")):
 
187
            return self.probe_http_transport(transport)
 
188
 
 
189
        if (not external_url.startswith("git://") and
 
190
            not external_url.startswith("git+")):
 
191
            raise bzr_errors.NotBranchError(transport.base)
 
192
 
 
193
        # little ugly, but works
 
194
        from .remote import (
 
195
            GitSmartTransport,
 
196
            RemoteGitControlDirFormat,
 
197
            )
 
198
        if isinstance(transport, GitSmartTransport):
 
199
            return RemoteGitControlDirFormat()
 
200
        raise bzr_errors.NotBranchError(path=transport.base)
 
201
 
 
202
    @classmethod
 
203
    def known_formats(cls):
 
204
        from .remote import RemoteGitControlDirFormat
 
205
        return set([RemoteGitControlDirFormat()])
 
206
 
 
207
 
 
208
ControlDirFormat.register_prober(LocalGitProber)
 
209
ControlDirFormat._server_probers.append(RemoteGitProber)
 
210
 
 
211
register_transport_proto('git://',
 
212
        help="Access using the Git smart server protocol.")
 
213
register_transport_proto('git+ssh://',
 
214
        help="Access using the Git smart server protocol over SSH.")
 
215
 
 
216
register_lazy_transport("git://", __name__ + '.remote',
 
217
                        'TCPGitSmartTransport')
 
218
register_lazy_transport("git+ssh://", __name__ + '.remote',
 
219
                        'SSHGitSmartTransport')
 
220
 
 
221
 
 
222
plugin_cmds.register_lazy("cmd_git_import", [], __name__ + ".commands")
 
223
plugin_cmds.register_lazy("cmd_git_object", ["git-objects", "git-cat"],
 
224
    __name__ + ".commands")
 
225
plugin_cmds.register_lazy("cmd_git_refs", [], __name__ + ".commands")
 
226
plugin_cmds.register_lazy("cmd_git_apply", [], __name__ + ".commands")
 
227
plugin_cmds.register_lazy("cmd_git_push_pristine_tar_deltas",
 
228
        ['git-push-pristine-tar', 'git-push-pristine'],
 
229
    __name__ + ".commands")
 
230
 
 
231
def extract_git_foreign_revid(rev):
 
232
    try:
 
233
        foreign_revid = rev.foreign_revid
 
234
    except AttributeError:
 
235
        from .mapping import mapping_registry
 
236
        foreign_revid, mapping = \
 
237
            mapping_registry.parse_revision_id(rev.revision_id)
 
238
        return foreign_revid
 
239
    else:
 
240
        from .mapping import foreign_vcs_git
 
241
        if rev.mapping.vcs == foreign_vcs_git:
 
242
            return foreign_revid
 
243
        else:
 
244
            raise bzr_errors.InvalidRevisionId(rev.revision_id, None)
 
245
 
 
246
 
 
247
def update_stanza(rev, stanza):
 
248
    mapping = getattr(rev, "mapping", None)
 
249
    try:
 
250
        git_commit = extract_git_foreign_revid(rev)
 
251
    except bzr_errors.InvalidRevisionId:
 
252
        pass
 
253
    else:
 
254
        stanza.add("git-commit", git_commit)
 
255
 
 
256
from ...hooks import install_lazy_named_hook
 
257
install_lazy_named_hook("breezy.version_info_formats.format_rio",
 
258
    "RioVersionInfoBuilder.hooks", "revision", update_stanza,
 
259
    "git commits")
 
260
 
 
261
 
 
262
transport_server_registry.register_lazy('git',
 
263
    __name__ + '.server',
 
264
    'serve_git',
 
265
    'Git Smart server protocol over TCP. (default port: 9418)')
 
266
 
 
267
transport_server_registry.register_lazy('git-receive-pack',
 
268
    __name__ + '.server',
 
269
    'serve_git_receive_pack',
 
270
    help='Git Smart server receive pack command. (inetd mode only)')
 
271
transport_server_registry.register_lazy('git-upload-pack',
 
272
    __name__ + 'git.server',
 
273
    'serve_git_upload_pack',
 
274
    help='Git Smart server upload pack command. (inetd mode only)')
 
275
 
 
276
from ...repository import (
 
277
    format_registry as repository_format_registry,
 
278
    network_format_registry as repository_network_format_registry,
 
279
    )
 
280
repository_network_format_registry.register_lazy('git',
 
281
    __name__ + '.repository', 'GitRepositoryFormat')
 
282
 
 
283
register_extra_lazy_repository_format = getattr(repository_format_registry,
 
284
    "register_extra_lazy")
 
285
register_extra_lazy_repository_format(__name__ + '.repository',
 
286
    'GitRepositoryFormat')
 
287
 
 
288
from ...branch import (
 
289
    network_format_registry as branch_network_format_registry,
 
290
    )
 
291
branch_network_format_registry.register_lazy('git',
 
292
    __name__ + '.branch', 'LocalGitBranchFormat')
 
293
 
 
294
 
 
295
from ...branch import (
 
296
    format_registry as branch_format_registry,
 
297
    )
 
298
branch_format_registry.register_extra_lazy(
 
299
    __name__ + '.branch',
 
300
    'LocalGitBranchFormat',
 
301
    )
 
302
branch_format_registry.register_extra_lazy(
 
303
    __name__ + '.remote',
 
304
    'RemoteGitBranchFormat',
 
305
    )
 
306
 
 
307
 
 
308
from ...workingtree import (
 
309
    format_registry as workingtree_format_registry,
 
310
    )
 
311
workingtree_format_registry.register_extra_lazy(
 
312
    __name__ + '.workingtree',
 
313
    'GitWorkingTreeFormat',
 
314
    )
 
315
 
 
316
controldir_network_format_registry.register_lazy('git',
 
317
    __name__ + ".dir", "GitControlDirFormat")
 
318
 
 
319
 
 
320
try:
 
321
    from ...registry import register_lazy
 
322
except ImportError:
 
323
    from ...diff import format_registry as diff_format_registry
 
324
    diff_format_registry.register_lazy('git', __name__ + '.send',
 
325
        'GitDiffTree', 'Git am-style diff format')
 
326
 
 
327
    from ...send import (
 
328
        format_registry as send_format_registry,
 
329
        )
 
330
    send_format_registry.register_lazy('git', __name__ + '.send',
 
331
                                       'send_git', 'Git am-style diff format')
 
332
 
 
333
    from ...directory_service import directories
 
334
    directories.register_lazy('github:', __name__ + '.directory',
 
335
                              'GitHubDirectory',
 
336
                              'GitHub directory.')
 
337
    directories.register_lazy('git@github.com:', __name__ + '.directory',
 
338
                              'GitHubDirectory',
 
339
                              'GitHub directory.')
 
340
 
 
341
    from ...help_topics import (
 
342
        topic_registry,
 
343
        )
 
344
    topic_registry.register_lazy('git', __name__ + '.help', 'help_git',
 
345
        'Using Bazaar with Git')
 
346
 
 
347
    from ...foreign import (
 
348
        foreign_vcs_registry,
 
349
        )
 
350
    foreign_vcs_registry.register_lazy("git",
 
351
        __name__ + ".mapping", "foreign_vcs_git", "Stupid content tracker")
 
352
else:
 
353
    register_lazy("breezy.diff", "format_registry",
 
354
        'git', __name__ + '.send', 'GitDiffTree',
 
355
        'Git am-style diff format')
 
356
    register_lazy("breezy.send", "format_registry",
 
357
        'git', __name__ + '.send', 'send_git',
 
358
        'Git am-style diff format')
 
359
    register_lazy('breezy.directory_service', 'directories', 'github:',
 
360
            __name__ + '.directory', 'GitHubDirectory',
 
361
            'GitHub directory.')
 
362
    register_lazy('breezy.directory_service', 'directories',
 
363
            'git@github.com:', __name__ + '.directory',
 
364
            'GitHubDirectory', 'GitHub directory.')
 
365
    register_lazy('breezy.help_topics', 'topic_registry',
 
366
            'git', __name__ + '.help', 'help_git',
 
367
            'Using Bazaar with Git')
 
368
    register_lazy('breezy.foreign', 'foreign_vcs_registry', "git",
 
369
        __name__ + ".mapping", "foreign_vcs_git", "Stupid content tracker")
 
370
 
 
371
def update_git_cache(repository, revid):
 
372
    """Update the git cache after a local commit."""
 
373
    if getattr(repository, "_git", None) is not None:
 
374
        return # No need to update cache for git repositories
 
375
 
 
376
    if not repository.control_transport.has("git"):
 
377
        return # No existing cache, don't bother updating
 
378
    try:
 
379
        lazy_check_versions()
 
380
    except bzr_errors.DependencyNotPresent, e:
 
381
        # dulwich is probably missing. silently ignore
 
382
        trace.mutter("not updating git map for %r: %s",
 
383
            repository, e)
 
384
 
 
385
    from .object_store import BazaarObjectStore
 
386
    store = BazaarObjectStore(repository)
 
387
    with store.lock_write():
 
388
        try:
 
389
            parent_revisions = set(repository.get_parent_map([revid])[revid])
 
390
        except KeyError:
 
391
            # Isn't this a bit odd - how can a revision that was just committed be missing?
 
392
            return
 
393
        missing_revisions = store._missing_revisions(parent_revisions)
 
394
        if not missing_revisions:
 
395
            # Only update if the cache was up to date previously
 
396
            store._update_sha_map_revision(revid)
 
397
 
 
398
 
 
399
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
 
400
        new_revno, new_revid):
 
401
    if local_branch is not None:
 
402
        update_git_cache(local_branch.repository, new_revid)
 
403
    update_git_cache(master_branch.repository, new_revid)
 
404
 
 
405
 
 
406
def loggerhead_git_hook(branch_app, environ):
 
407
    branch = branch_app.branch
 
408
    config_stack = branch.get_config_stack()
 
409
    if config_stack.get('http_git'):
 
410
        return None
 
411
    from .server import git_http_hook
 
412
    return git_http_hook(branch, environ['REQUEST_METHOD'],
 
413
        environ['PATH_INFO'])
 
414
 
 
415
install_lazy_named_hook("breezy.branch",
 
416
    "Branch.hooks", "post_commit", post_commit_update_cache,
 
417
    "git cache")
 
418
install_lazy_named_hook("breezy.plugins.loggerhead.apps.branch",
 
419
    "BranchWSGIApp.hooks", "controller",
 
420
    loggerhead_git_hook, "git support")
 
421
 
 
422
 
 
423
from ...config import (
 
424
    option_registry,
 
425
    Option,
 
426
    bool_from_store,
 
427
    )
 
428
 
 
429
option_registry.register(
 
430
    Option('git.http',
 
431
           default=None, from_unicode=bool_from_store, invalid='warning',
 
432
           help='''\
 
433
Allow fetching of Git packs over HTTP.
 
434
 
 
435
This enables support for fetching Git packs over HTTP in Loggerhead.
 
436
'''))
 
437
 
 
438
def test_suite():
 
439
    from . import tests
 
440
    return tests.test_suite()