/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
dulwich_minimum_version = (0, 19, 7)
31
31
 
32
 
from .. import (
 
32
from .. import (  # noqa: F401
33
33
    __version__ as breezy_version,
34
34
    errors as brz_errors,
35
35
    trace,
36
 
    version_info,  # noqa: F401
 
36
    version_info,
37
37
    )
38
38
 
39
39
from ..controldir import (
63
63
    try:
64
64
        from dulwich import __version__ as dulwich_version
65
65
    except ImportError:
66
 
        raise brz_errors.DependencyNotPresent("dulwich",
 
66
        raise brz_errors.DependencyNotPresent(
 
67
            "dulwich",
67
68
            "bzr-git: Please install dulwich, https://www.dulwich.io/")
68
69
    else:
69
70
        if dulwich_version < dulwich_minimum_version:
70
 
            raise brz_errors.DependencyNotPresent("dulwich",
 
71
            raise brz_errors.DependencyNotPresent(
 
72
                "dulwich",
71
73
                "bzr-git: Dulwich is too old; at least %d.%d.%d is required" %
72
 
                    dulwich_minimum_version)
 
74
                dulwich_minimum_version)
73
75
 
74
76
 
75
77
_versions_checked = False
 
78
 
 
79
 
76
80
def lazy_check_versions():
77
81
    global _versions_checked
78
82
    if _versions_checked:
80
84
    import_dulwich()
81
85
    _versions_checked = True
82
86
 
83
 
format_registry.register_lazy('git',
84
 
    __name__ + ".dir", "LocalGitControlDirFormat",
85
 
    help='GIT repository.', native=False, experimental=False,
86
 
    )
87
 
 
88
 
format_registry.register_lazy('git-bare',
89
 
    __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",
90
94
    help='Bare GIT repository (no working tree).', native=False,
91
 
    experimental=False,
92
 
    )
 
95
    experimental=False)
93
96
 
94
97
from ..revisionspec import (RevisionSpec_dwim, revspec_registry)
95
98
revspec_registry.register_lazy("git:", __name__ + ".revspec",
96
 
    "RevisionSpec_git")
 
99
                               "RevisionSpec_git")
97
100
RevisionSpec_dwim.append_possible_lazy_revspec(
98
101
    __name__ + ".revspec", "RevisionSpec_git")
99
102
 
106
109
        except brz_errors.InProcessTransport:
107
110
            raise brz_errors.NotBranchError(path=transport.base)
108
111
        if (external_url.startswith("http:") or
109
 
            external_url.startswith("https:")):
 
112
                external_url.startswith("https:")):
110
113
            # Already handled by RemoteGitProber
111
114
            raise brz_errors.NotBranchError(path=transport.base)
112
115
        from .. import urlutils
143
146
 
144
147
    def probe_http_transport(self, transport):
145
148
        from .. import urlutils
146
 
        base_url, _ = urlutils.split_segment_parameters(transport.external_url())
 
149
        base_url, _ = urlutils.split_segment_parameters(
 
150
            transport.external_url())
147
151
        url = urlutils.join(base_url, "info/refs") + "?service=git-upload-pack"
148
152
        from ..transport.http import Request
149
153
        headers = {"Content-Type": "application/x-git-upload-pack-request",
151
155
                   }
152
156
        req = Request('GET', url, accepted_errors=[200, 403, 404, 405],
153
157
                      headers=headers)
154
 
        (scheme, user, password, host, port, path) = urlutils.parse_url(req.get_full_url())
 
158
        (scheme, user, password, host, port,
 
159
         path) = urlutils.parse_url(req.get_full_url())
155
160
        if host == "github.com":
156
 
            # GitHub requires we lie. https://github.com/dulwich/dulwich/issues/562
 
161
            # GitHub requires we lie.
 
162
            # https://github.com/dulwich/dulwich/issues/562
157
163
            req.add_header("User-Agent", user_agent_for_github())
158
164
        elif host == "bazaar.launchpad.net":
159
165
            # Don't attempt Git probes against bazaar.launchpad.net; pad.lv/1744830
183
189
            raise brz_errors.NotBranchError(path=transport.base)
184
190
 
185
191
        if (external_url.startswith("http:") or
186
 
            external_url.startswith("https:")):
 
192
                external_url.startswith("https:")):
187
193
            return self.probe_http_transport(transport)
188
194
 
189
195
        if (not external_url.startswith("git://") and
190
 
            not external_url.startswith("git+")):
 
196
                not external_url.startswith("git+")):
191
197
            raise brz_errors.NotBranchError(transport.base)
192
198
 
193
199
        # little ugly, but works
208
214
ControlDirFormat.register_prober(LocalGitProber)
209
215
ControlDirFormat._server_probers.append(RemoteGitProber)
210
216
 
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.")
 
217
register_transport_proto(
 
218
    'git://', help="Access using the Git smart server protocol.")
 
219
register_transport_proto(
 
220
    'git+ssh://',
 
221
    help="Access using the Git smart server protocol over SSH.")
215
222
 
216
223
register_lazy_transport("git://", __name__ + '.remote',
217
224
                        'TCPGitSmartTransport')
221
228
 
222
229
plugin_cmds.register_lazy("cmd_git_import", [], __name__ + ".commands")
223
230
plugin_cmds.register_lazy("cmd_git_object", ["git-objects", "git-cat"],
224
 
    __name__ + ".commands")
 
231
                          __name__ + ".commands")
225
232
plugin_cmds.register_lazy("cmd_git_refs", [], __name__ + ".commands")
226
233
plugin_cmds.register_lazy("cmd_git_apply", [], __name__ + ".commands")
227
234
plugin_cmds.register_lazy("cmd_git_push_pristine_tar_deltas",
228
 
        ['git-push-pristine-tar', 'git-push-pristine'],
229
 
    __name__ + ".commands")
 
235
                          ['git-push-pristine-tar', 'git-push-pristine'],
 
236
                          __name__ + ".commands")
 
237
 
230
238
 
231
239
def extract_git_foreign_revid(rev):
232
240
    try:
245
253
 
246
254
 
247
255
def update_stanza(rev, stanza):
248
 
    mapping = getattr(rev, "mapping", None)
249
256
    try:
250
257
        git_commit = extract_git_foreign_revid(rev)
251
258
    except brz_errors.InvalidRevisionId:
253
260
    else:
254
261
        stanza.add("git-commit", git_commit)
255
262
 
 
263
 
256
264
from ..hooks import install_lazy_named_hook
257
 
install_lazy_named_hook("breezy.version_info_formats.format_rio",
 
265
install_lazy_named_hook(
 
266
    "breezy.version_info_formats.format_rio",
258
267
    "RioVersionInfoBuilder.hooks", "revision", update_stanza,
259
268
    "git commits")
260
269
 
261
 
 
262
 
transport_server_registry.register_lazy('git',
263
 
    __name__ + '.server',
264
 
    'serve_git',
 
270
transport_server_registry.register_lazy(
 
271
    'git', __name__ + '.server', 'serve_git',
265
272
    'Git Smart server protocol over TCP. (default port: 9418)')
266
273
 
267
 
transport_server_registry.register_lazy('git-receive-pack',
268
 
    __name__ + '.server',
 
274
transport_server_registry.register_lazy(
 
275
    'git-receive-pack', __name__ + '.server',
269
276
    'serve_git_receive_pack',
270
277
    help='Git Smart server receive pack command. (inetd mode only)')
271
 
transport_server_registry.register_lazy('git-upload-pack',
272
 
    __name__ + 'git.server',
 
278
transport_server_registry.register_lazy(
 
279
    'git-upload-pack', __name__ + 'git.server',
273
280
    'serve_git_upload_pack',
274
281
    help='Git Smart server upload pack command. (inetd mode only)')
275
282
 
277
284
    format_registry as repository_format_registry,
278
285
    network_format_registry as repository_network_format_registry,
279
286
    )
280
 
repository_network_format_registry.register_lazy(b'git',
281
 
    __name__ + '.repository', 'GitRepositoryFormat')
 
287
repository_network_format_registry.register_lazy(
 
288
    b'git', __name__ + '.repository', 'GitRepositoryFormat')
282
289
 
283
290
register_extra_lazy_repository_format = getattr(repository_format_registry,
284
 
    "register_extra_lazy")
 
291
                                                "register_extra_lazy")
285
292
register_extra_lazy_repository_format(__name__ + '.repository',
286
 
    'GitRepositoryFormat')
 
293
                                      'GitRepositoryFormat')
287
294
 
288
295
from ..branch import (
289
296
    network_format_registry as branch_network_format_registry,
290
297
    )
291
 
branch_network_format_registry.register_lazy(b'git',
292
 
    __name__ + '.branch', 'LocalGitBranchFormat')
 
298
branch_network_format_registry.register_lazy(
 
299
    b'git', __name__ + '.branch', 'LocalGitBranchFormat')
293
300
 
294
301
 
295
302
from ..branch import (
313
320
    'GitWorkingTreeFormat',
314
321
    )
315
322
 
316
 
controldir_network_format_registry.register_lazy(b'git',
317
 
    __name__ + ".dir", "GitControlDirFormat")
 
323
controldir_network_format_registry.register_lazy(
 
324
    b'git', __name__ + ".dir", "GitControlDirFormat")
318
325
 
319
326
 
320
327
from ..diff import format_registry as diff_format_registry
351
358
def update_git_cache(repository, revid):
352
359
    """Update the git cache after a local commit."""
353
360
    if getattr(repository, "_git", None) is not None:
354
 
        return # No need to update cache for git repositories
 
361
        return  # No need to update cache for git repositories
355
362
 
356
363
    if not repository.control_transport.has("git"):
357
 
        return # No existing cache, don't bother updating
 
364
        return  # No existing cache, don't bother updating
358
365
    try:
359
366
        lazy_check_versions()
360
367
    except brz_errors.DependencyNotPresent as e:
361
368
        # dulwich is probably missing. silently ignore
362
369
        trace.mutter("not updating git map for %r: %s",
363
 
            repository, e)
 
370
                     repository, e)
364
371
 
365
372
    from .object_store import BazaarObjectStore
366
373
    store = BazaarObjectStore(repository)
368
375
        try:
369
376
            parent_revisions = set(repository.get_parent_map([revid])[revid])
370
377
        except KeyError:
371
 
            # Isn't this a bit odd - how can a revision that was just committed be missing?
 
378
            # Isn't this a bit odd - how can a revision that was just committed
 
379
            # be missing?
372
380
            return
373
381
        missing_revisions = store._missing_revisions(parent_revisions)
374
382
        if not missing_revisions:
384
392
 
385
393
 
386
394
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
387
 
        new_revno, new_revid):
 
395
                             new_revno, new_revid):
388
396
    if local_branch is not None:
389
397
        update_git_cache(local_branch.repository, new_revid)
390
398
    update_git_cache(master_branch.repository, new_revid)
397
405
        return None
398
406
    from .server import git_http_hook
399
407
    return git_http_hook(branch, environ['REQUEST_METHOD'],
400
 
        environ['PATH_INFO'])
 
408
                         environ['PATH_INFO'])
 
409
 
401
410
 
402
411
install_lazy_named_hook("breezy.branch",
403
 
    "Branch.hooks", "post_commit", post_commit_update_cache,
404
 
    "git cache")
 
412
                        "Branch.hooks", "post_commit",
 
413
                        post_commit_update_cache, "git cache")
405
414
install_lazy_named_hook("breezy.plugins.loggerhead.apps.branch",
406
 
    "BranchWSGIApp.hooks", "controller",
407
 
    loggerhead_git_hook, "git support")
 
415
                        "BranchWSGIApp.hooks", "controller",
 
416
                        loggerhead_git_hook, "git support")
408
417
 
409
418
 
410
419
from ..config import (
422
431
This enables support for fetching Git packs over HTTP in Loggerhead.
423
432
'''))
424
433
 
 
434
 
425
435
def test_suite():
426
436
    from . import tests
427
437
    return tests.test_suite()