/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: 2019-06-02 02:35:46 UTC
  • mfrom: (7309 work)
  • mto: This revision was merged to the branch mainline in revision 7319.
  • Revision ID: jelmer@jelmer.uk-20190602023546-lqco868tnv26d8ow
merge trunk.

Show diffs side-by-side

added added

removed removed

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