1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
# Copyright (C) 2006-2009 Canonical Ltd
4
# Authors: Robert Collins <robert.collins@canonical.com>
5
# Jelmer Vernooij <jelmer@jelmer.uk>
6
# John Carr <john.carr@unrouted.co.uk>
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.
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.
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
23
"""A GIT branch and repository format implementation for bzr."""
28
dulwich_minimum_version = (0, 19, 11)
30
from .. import ( # noqa: F401
31
__version__ as breezy_version,
38
from ..controldir import (
42
network_format_registry as controldir_network_format_registry,
45
from ..transport import (
46
register_lazy_transport,
47
register_transport_proto,
48
transport_server_registry,
50
from ..commands import (
55
if getattr(sys, "frozen", None):
56
# allow import additional libs from ./_lib for bzr.exe only
57
sys.path.append(os.path.normpath(
58
os.path.join(os.path.dirname(__file__), '_lib')))
63
from dulwich import __version__ as dulwich_version
65
raise brz_errors.DependencyNotPresent(
67
"bzr-git: Please install dulwich, https://www.dulwich.io/")
69
if dulwich_version < dulwich_minimum_version:
70
raise brz_errors.DependencyNotPresent(
72
"bzr-git: Dulwich is too old; at least %d.%d.%d is required" %
73
dulwich_minimum_version)
76
_versions_checked = False
79
def lazy_check_versions():
80
global _versions_checked
84
_versions_checked = True
87
format_registry.register_lazy(
88
'git', __name__ + ".dir", "LocalGitControlDirFormat",
89
help='GIT repository.', native=False, experimental=False)
91
format_registry.register_lazy(
92
'git-bare', __name__ + ".dir", "BareLocalGitControlDirFormat",
93
help='Bare GIT repository (no working tree).', native=False,
96
from ..revisionspec import (RevisionSpec_dwim, revspec_registry)
97
revspec_registry.register_lazy("git:", __name__ + ".revspec",
99
RevisionSpec_dwim.append_possible_lazy_revspec(
100
__name__ + ".revspec", "RevisionSpec_git")
103
class LocalGitProber(Prober):
106
def priority(klass, transport):
109
def probe_transport(self, transport):
111
external_url = transport.external_url()
112
except brz_errors.InProcessTransport:
113
raise brz_errors.NotBranchError(path=transport.base)
114
if (external_url.startswith("http:") or
115
external_url.startswith("https:")):
116
# Already handled by RemoteGitProber
117
raise brz_errors.NotBranchError(path=transport.base)
118
if urlutils.split(transport.base)[1] == ".git":
119
raise brz_errors.NotBranchError(path=transport.base)
120
if not transport.has_any(['objects', '.git/objects', '.git']):
121
raise brz_errors.NotBranchError(path=transport.base)
122
lazy_check_versions()
124
BareLocalGitControlDirFormat,
125
LocalGitControlDirFormat,
127
if transport.has_any(['.git/objects', '.git']):
128
return LocalGitControlDirFormat()
129
if transport.has('info') and transport.has('objects'):
130
return BareLocalGitControlDirFormat()
131
raise brz_errors.NotBranchError(path=transport.base)
134
def known_formats(cls):
136
BareLocalGitControlDirFormat,
137
LocalGitControlDirFormat,
139
return [BareLocalGitControlDirFormat(), LocalGitControlDirFormat()]
142
def user_agent_for_github():
143
# GitHub requires we lie. https://github.com/dulwich/dulwich/issues/562
144
return "git/Breezy/%s" % breezy_version
147
def is_github_url(url):
148
(scheme, user, password, host, port,
149
path) = urlutils.parse_url(url)
150
return host in ("github.com", "gopkg.in")
153
class RemoteGitProber(Prober):
156
def priority(klass, transport):
157
# This is a surprisingly good heuristic to determine whether this
158
# prober is more likely to succeed than the Bazaar one.
159
if 'git' in transport.base:
163
def probe_http_transport(self, transport):
164
# This function intentionally doesn't use any of the support code under
165
# breezy.git, since it's called for every repository that's
166
# accessed over HTTP, whether it's Git, Bzr or something else.
167
# Importing Dulwich and the other support code adds unnecessray slowdowns.
168
base_url = urlutils.strip_segment_parameters(transport.external_url())
169
url = urlutils.URL.from_string(base_url)
170
url.user = url.quoted_user = None
171
url.password = url.quoted_password = None
173
url = urlutils.join(str(url), "info/refs") + "?service=git-upload-pack"
174
headers = {"Content-Type": "application/x-git-upload-pack-request",
175
"Accept": "application/x-git-upload-pack-result",
177
if is_github_url(url):
178
# GitHub requires we lie.
179
# https://github.com/dulwich/dulwich/issues/562
180
headers["User-Agent"] = user_agent_for_github()
181
elif host == "bazaar.launchpad.net":
182
# Don't attempt Git probes against bazaar.launchpad.net; pad.lv/1744830
183
raise brz_errors.NotBranchError(transport.base)
184
resp = transport.request('GET', url, headers=headers)
185
if resp.status in (404, 405):
186
raise brz_errors.NotBranchError(transport.base)
187
elif resp.status != 200:
188
raise brz_errors.InvalidHttpResponse(
189
url, 'Unable to handle http code %d' % resp.status)
191
ct = resp.getheader("Content-Type")
192
if ct and ct.startswith("application/x-git"):
193
from .remote import RemoteGitControlDirFormat
194
return RemoteGitControlDirFormat()
197
BareLocalGitControlDirFormat,
199
ret = BareLocalGitControlDirFormat()
200
ret._refs_text = resp.read()
202
raise brz_errors.NotBranchError(transport.base)
204
def probe_transport(self, transport):
206
external_url = transport.external_url()
207
except brz_errors.InProcessTransport:
208
raise brz_errors.NotBranchError(path=transport.base)
210
if (external_url.startswith("http:") or
211
external_url.startswith("https:")):
212
return self.probe_http_transport(transport)
214
if (not external_url.startswith("git://") and
215
not external_url.startswith("git+")):
216
raise brz_errors.NotBranchError(transport.base)
218
# little ugly, but works
219
from .remote import (
221
RemoteGitControlDirFormat,
223
if isinstance(transport, GitSmartTransport):
224
return RemoteGitControlDirFormat()
225
raise brz_errors.NotBranchError(path=transport.base)
228
def known_formats(cls):
229
from .remote import RemoteGitControlDirFormat
230
return [RemoteGitControlDirFormat()]
233
ControlDirFormat.register_prober(LocalGitProber)
234
ControlDirFormat.register_prober(RemoteGitProber)
236
register_transport_proto(
237
'git://', help="Access using the Git smart server protocol.")
238
register_transport_proto(
240
help="Access using the Git smart server protocol over SSH.")
242
register_lazy_transport("git://", __name__ + '.remote',
243
'TCPGitSmartTransport')
244
register_lazy_transport("git+ssh://", __name__ + '.remote',
245
'SSHGitSmartTransport')
248
plugin_cmds.register_lazy("cmd_git_import", [], __name__ + ".commands")
249
plugin_cmds.register_lazy("cmd_git_object", ["git-objects", "git-cat"],
250
__name__ + ".commands")
251
plugin_cmds.register_lazy("cmd_git_refs", [], __name__ + ".commands")
252
plugin_cmds.register_lazy("cmd_git_apply", [], __name__ + ".commands")
253
plugin_cmds.register_lazy("cmd_git_push_pristine_tar_deltas",
254
['git-push-pristine-tar', 'git-push-pristine'],
255
__name__ + ".commands")
258
def extract_git_foreign_revid(rev):
260
foreign_revid = rev.foreign_revid
261
except AttributeError:
262
from .mapping import mapping_registry
263
foreign_revid, mapping = \
264
mapping_registry.parse_revision_id(rev.revision_id)
267
from .mapping import foreign_vcs_git
268
if rev.mapping.vcs == foreign_vcs_git:
271
raise brz_errors.InvalidRevisionId(rev.revision_id, None)
274
def update_stanza(rev, stanza):
276
git_commit = extract_git_foreign_revid(rev)
277
except brz_errors.InvalidRevisionId:
280
stanza.add("git-commit", git_commit)
283
from ..hooks import install_lazy_named_hook
284
install_lazy_named_hook(
285
"breezy.version_info_formats.format_rio",
286
"RioVersionInfoBuilder.hooks", "revision", update_stanza,
289
transport_server_registry.register_lazy(
290
'git', __name__ + '.server', 'serve_git',
291
'Git Smart server protocol over TCP. (default port: 9418)')
293
transport_server_registry.register_lazy(
294
'git-receive-pack', __name__ + '.server',
295
'serve_git_receive_pack',
296
help='Git Smart server receive pack command. (inetd mode only)')
297
transport_server_registry.register_lazy(
298
'git-upload-pack', __name__ + 'git.server',
299
'serve_git_upload_pack',
300
help='Git Smart server upload pack command. (inetd mode only)')
302
from ..repository import (
303
format_registry as repository_format_registry,
304
network_format_registry as repository_network_format_registry,
306
repository_network_format_registry.register_lazy(
307
b'git', __name__ + '.repository', 'GitRepositoryFormat')
309
register_extra_lazy_repository_format = getattr(repository_format_registry,
310
"register_extra_lazy")
311
register_extra_lazy_repository_format(__name__ + '.repository',
312
'GitRepositoryFormat')
314
from ..branch import (
315
network_format_registry as branch_network_format_registry,
317
branch_network_format_registry.register_lazy(
318
b'git', __name__ + '.branch', 'LocalGitBranchFormat')
321
from ..branch import (
322
format_registry as branch_format_registry,
324
branch_format_registry.register_extra_lazy(
325
__name__ + '.branch',
326
'LocalGitBranchFormat',
328
branch_format_registry.register_extra_lazy(
329
__name__ + '.remote',
330
'RemoteGitBranchFormat',
334
from ..workingtree import (
335
format_registry as workingtree_format_registry,
337
workingtree_format_registry.register_extra_lazy(
338
__name__ + '.workingtree',
339
'GitWorkingTreeFormat',
342
controldir_network_format_registry.register_lazy(
343
b'git', __name__ + ".dir", "GitControlDirFormat")
346
from ..diff import format_registry as diff_format_registry
347
diff_format_registry.register_lazy(
348
'git', __name__ + '.send',
349
'GitDiffTree', 'Git am-style diff format')
352
format_registry as send_format_registry,
354
send_format_registry.register_lazy('git', __name__ + '.send',
355
'send_git', 'Git am-style diff format')
357
from ..directory_service import directories
358
directories.register_lazy('github:', __name__ + '.directory',
361
directories.register_lazy('git@github.com:', __name__ + '.directory',
365
from ..help_topics import (
368
topic_registry.register_lazy(
369
'git', __name__ + '.help', 'help_git', 'Using Bazaar with Git')
371
from ..foreign import (
372
foreign_vcs_registry,
374
foreign_vcs_registry.register_lazy(
375
"git", __name__ + ".mapping", "foreign_vcs_git", "Stupid content tracker")
378
def update_git_cache(repository, revid):
379
"""Update the git cache after a local commit."""
380
if getattr(repository, "_git", None) is not None:
381
return # No need to update cache for git repositories
383
if not repository.control_transport.has("git"):
384
return # No existing cache, don't bother updating
386
lazy_check_versions()
387
except brz_errors.DependencyNotPresent as e:
388
# dulwich is probably missing. silently ignore
389
trace.mutter("not updating git map for %r: %s",
392
from .object_store import BazaarObjectStore
393
store = BazaarObjectStore(repository)
394
with store.lock_write():
396
parent_revisions = set(repository.get_parent_map([revid])[revid])
398
# Isn't this a bit odd - how can a revision that was just committed
401
missing_revisions = store._missing_revisions(parent_revisions)
402
if not missing_revisions:
403
store._cache.idmap.start_write_group()
405
# Only update if the cache was up to date previously
406
store._update_sha_map_revision(revid)
407
except BaseException:
408
store._cache.idmap.abort_write_group()
411
store._cache.idmap.commit_write_group()
414
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
415
new_revno, new_revid):
416
if local_branch is not None:
417
update_git_cache(local_branch.repository, new_revid)
418
update_git_cache(master_branch.repository, new_revid)
421
def loggerhead_git_hook(branch_app, environ):
422
branch = branch_app.branch
423
config_stack = branch.get_config_stack()
424
if config_stack.get('http_git'):
426
from .server import git_http_hook
427
return git_http_hook(branch, environ['REQUEST_METHOD'],
428
environ['PATH_INFO'])
431
install_lazy_named_hook("breezy.branch",
432
"Branch.hooks", "post_commit",
433
post_commit_update_cache, "git cache")
434
install_lazy_named_hook("breezy.plugins.loggerhead.apps.branch",
435
"BranchWSGIApp.hooks", "controller",
436
loggerhead_git_hook, "git support")
439
from ..config import (
445
option_registry.register(
447
default=None, from_unicode=bool_from_store, invalid='warning',
449
Allow fetching of Git packs over HTTP.
451
This enables support for fetching Git packs over HTTP in Loggerhead.
457
return tests.test_suite()