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."""
25
from __future__ import absolute_import
30
dulwich_minimum_version = (0, 19, 0)
32
from breezy.i18n import gettext
35
__version__ as breezy_version,
41
from ...controldir import (
45
network_format_registry as controldir_network_format_registry,
48
from ...transport import (
49
register_lazy_transport,
50
register_transport_proto,
51
transport_server_registry,
53
from ...commands import (
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')))
66
from dulwich import __version__ as dulwich_version
68
raise bzr_errors.DependencyNotPresent("dulwich",
69
"bzr-git: Please install dulwich, https://www.dulwich.io/")
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)
77
_versions_checked = False
78
def lazy_check_versions():
79
global _versions_checked
83
_versions_checked = True
85
format_registry.register_lazy('git',
86
__name__ + ".dir", "LocalGitControlDirFormat",
87
help='GIT repository.', native=False, experimental=False,
90
format_registry.register_lazy('git-bare',
91
__name__ + ".dir", "BareLocalGitControlDirFormat",
92
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):
105
def probe_transport(self, transport):
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()
121
BareLocalGitControlDirFormat,
122
LocalGitControlDirFormat,
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)
131
def known_formats(cls):
133
BareLocalGitControlDirFormat,
134
LocalGitControlDirFormat,
136
return set([BareLocalGitControlDirFormat(), LocalGitControlDirFormat()])
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
144
class RemoteGitProber(Prober):
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],
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")
167
raise bzr_errors.NotBranchError(transport.base)
168
if ct.startswith("application/x-git"):
169
from .remote import RemoteGitControlDirFormat
170
return RemoteGitControlDirFormat()
173
BareLocalGitControlDirFormat,
175
ret = BareLocalGitControlDirFormat()
176
ret._refs_text = resp.read()
179
def probe_transport(self, transport):
181
external_url = transport.external_url()
182
except bzr_errors.InProcessTransport:
183
raise bzr_errors.NotBranchError(path=transport.base)
185
if (external_url.startswith("http:") or
186
external_url.startswith("https:")):
187
return self.probe_http_transport(transport)
189
if (not external_url.startswith("git://") and
190
not external_url.startswith("git+")):
191
raise bzr_errors.NotBranchError(transport.base)
193
# little ugly, but works
194
from .remote import (
196
RemoteGitControlDirFormat,
198
if isinstance(transport, GitSmartTransport):
199
return RemoteGitControlDirFormat()
200
raise bzr_errors.NotBranchError(path=transport.base)
203
def known_formats(cls):
204
from .remote import RemoteGitControlDirFormat
205
return set([RemoteGitControlDirFormat()])
208
ControlDirFormat.register_prober(LocalGitProber)
209
ControlDirFormat._server_probers.append(RemoteGitProber)
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.")
216
register_lazy_transport("git://", __name__ + '.remote',
217
'TCPGitSmartTransport')
218
register_lazy_transport("git+ssh://", __name__ + '.remote',
219
'SSHGitSmartTransport')
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")
231
def extract_git_foreign_revid(rev):
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)
240
from .mapping import foreign_vcs_git
241
if rev.mapping.vcs == foreign_vcs_git:
244
raise bzr_errors.InvalidRevisionId(rev.revision_id, None)
247
def update_stanza(rev, stanza):
248
mapping = getattr(rev, "mapping", None)
250
git_commit = extract_git_foreign_revid(rev)
251
except bzr_errors.InvalidRevisionId:
254
stanza.add("git-commit", git_commit)
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,
262
transport_server_registry.register_lazy('git',
263
__name__ + '.server',
265
'Git Smart server protocol over TCP. (default port: 9418)')
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)')
276
from ...repository import (
277
format_registry as repository_format_registry,
278
network_format_registry as repository_network_format_registry,
280
repository_network_format_registry.register_lazy('git',
281
__name__ + '.repository', 'GitRepositoryFormat')
283
register_extra_lazy_repository_format = getattr(repository_format_registry,
284
"register_extra_lazy")
285
register_extra_lazy_repository_format(__name__ + '.repository',
286
'GitRepositoryFormat')
288
from ...branch import (
289
network_format_registry as branch_network_format_registry,
291
branch_network_format_registry.register_lazy('git',
292
__name__ + '.branch', 'LocalGitBranchFormat')
295
from ...branch import (
296
format_registry as branch_format_registry,
298
branch_format_registry.register_extra_lazy(
299
__name__ + '.branch',
300
'LocalGitBranchFormat',
302
branch_format_registry.register_extra_lazy(
303
__name__ + '.remote',
304
'RemoteGitBranchFormat',
308
from ...workingtree import (
309
format_registry as workingtree_format_registry,
311
workingtree_format_registry.register_extra_lazy(
312
__name__ + '.workingtree',
313
'GitWorkingTreeFormat',
316
controldir_network_format_registry.register_lazy('git',
317
__name__ + ".dir", "GitControlDirFormat")
321
from ...registry import register_lazy
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')
327
from ...send import (
328
format_registry as send_format_registry,
330
send_format_registry.register_lazy('git', __name__ + '.send',
331
'send_git', 'Git am-style diff format')
333
from ...directory_service import directories
334
directories.register_lazy('github:', __name__ + '.directory',
337
directories.register_lazy('git@github.com:', __name__ + '.directory',
341
from ...help_topics import (
344
topic_registry.register_lazy('git', __name__ + '.help', 'help_git',
345
'Using Bazaar with Git')
347
from ...foreign import (
348
foreign_vcs_registry,
350
foreign_vcs_registry.register_lazy("git",
351
__name__ + ".mapping", "foreign_vcs_git", "Stupid content tracker")
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',
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")
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
376
if not repository.control_transport.has("git"):
377
return # No existing cache, don't bother updating
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",
385
from .object_store import BazaarObjectStore
386
store = BazaarObjectStore(repository)
387
with store.lock_write():
389
parent_revisions = set(repository.get_parent_map([revid])[revid])
391
# Isn't this a bit odd - how can a revision that was just committed be missing?
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)
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)
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'):
411
from .server import git_http_hook
412
return git_http_hook(branch, environ['REQUEST_METHOD'],
413
environ['PATH_INFO'])
415
install_lazy_named_hook("breezy.branch",
416
"Branch.hooks", "post_commit", post_commit_update_cache,
418
install_lazy_named_hook("breezy.plugins.loggerhead.apps.branch",
419
"BranchWSGIApp.hooks", "controller",
420
loggerhead_git_hook, "git support")
423
from ...config import (
429
option_registry.register(
431
default=None, from_unicode=bool_from_store, invalid='warning',
433
Allow fetching of Git packs over HTTP.
435
This enables support for fetching Git packs over HTTP in Loggerhead.
440
return tests.test_suite()