1
# Copyright (C) 2006-2009 Canonical Ltd
3
# Authors: Robert Collins <robert.collins@canonical.com>
4
# Jelmer Vernooij <jelmer@samba.org>
5
# John Carr <john.carr@unrouted.co.uk>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
"""A GIT branch and repository format implementation for bzr."""
33
from bzrlib.foreign import (
36
from bzrlib.lockable_files import (
39
from bzrlib.transport import (
40
register_lazy_transport,
41
register_transport_proto,
43
from bzrlib.commands import (
46
from bzrlib.trace import (
49
from bzrlib.version_info_formats.format_rio import (
50
RioVersionInfoBuilder,
54
# versions ending in 'exp' mean experimental mappings
55
# versions ending in 'dev' mean development version
56
# versions ending in 'final' mean release (well tested, etc)
57
version_info = (0, 2, 0, 'dev', 0)
59
if version_info[3] == 'final':
60
version_string = '%d.%d.%d' % version_info[:3]
62
version_string = '%d.%d.%d%s%d' % version_info
63
__version__ = version_string
65
MINIMUM_DULWICH_VERSION = (0, 1, 1)
66
COMPATIBLE_BZR_VERSIONS = [(1, 15, 0)]
68
if getattr(sys, "frozen", None):
69
# allow import additional libs from ./_lib for bzr.exe only
70
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '_lib')))
72
_versions_checked = False
73
def lazy_check_versions():
74
global _versions_checked
77
_versions_checked = True
79
from dulwich import __version__ as dulwich_version
81
raise ImportError("bzr-git: Please install dulwich, https://launchpad.net/dulwich")
83
if dulwich_version < MINIMUM_DULWICH_VERSION:
84
raise ImportError("bzr-git: Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
86
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
88
bzrdir.format_registry.register_lazy('git',
89
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
90
help='GIT repository.', native=False, experimental=True,
93
from bzrlib.revisionspec import revspec_registry
94
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
98
class GitBzrDirFormat(bzrdir.BzrDirFormat):
99
_lock_class = TransportLock
101
def is_supported(self):
105
class LocalGitBzrDirFormat(GitBzrDirFormat):
106
"""The .git directory control format."""
109
def _known_formats(self):
110
return set([LocalGitBzrDirFormat()])
112
def open(self, transport, _found=None):
113
"""Open this directory.
116
import dulwich as git
117
# we dont grok readonly - git isn't integrated with transport.
119
if url.startswith('readonly+'):
120
url = url[len('readonly+'):]
123
gitrepo = git.repo.Repo(transport.local_abspath("."))
124
except bzr_errors.NotLocalUrl:
125
raise bzr_errors.NotBranchError(path=transport.base)
126
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
127
lockfiles = GitLockableFiles(transport, GitLock())
128
return LocalGitDir(transport, lockfiles, gitrepo, self)
131
def probe_transport(klass, transport):
132
"""Our format is present if the transport ends in '.not/'."""
133
from bzrlib.transport.local import LocalTransport
135
if not isinstance(transport, LocalTransport):
136
raise bzr_errors.NotBranchError(path=transport.base)
138
# This should quickly filter out most things that are not
139
# git repositories, saving us the trouble from loading dulwich.
140
if not transport.has(".git") and not transport.has("objects"):
141
raise bzr_errors.NotBranchError(path=transport.base)
143
import dulwich as git
146
format.open(transport)
148
except git.errors.NotGitRepository, e:
149
raise bzr_errors.NotBranchError(path=transport.base)
150
raise bzr_errors.NotBranchError(path=transport.base)
152
def get_format_description(self):
153
return "Local Git Repository"
155
def get_format_string(self):
156
return "Local Git Repository"
158
def initialize_on_transport(self, transport):
159
from bzrlib.transport.local import LocalTransport
161
if not isinstance(transport, LocalTransport):
162
raise NotImplementedError(self.initialize,
163
"Can't create Git Repositories/branches on "
164
"non-local transports")
166
from dulwich.repo import Repo
167
Repo.create(transport.local_abspath("."))
168
return self.open(transport)
170
def is_supported(self):
174
class RemoteGitBzrDirFormat(GitBzrDirFormat):
175
"""The .git directory control format."""
178
def _known_formats(self):
179
return set([RemoteGitBzrDirFormat()])
181
def open(self, transport, _found=None):
182
"""Open this directory.
185
# we dont grok readonly - git isn't integrated with transport.
187
if url.startswith('readonly+'):
188
url = url[len('readonly+'):]
189
if (not url.startswith("git://") and
190
not url.startswith("git+")):
191
raise bzr_errors.NotBranchError(transport.base)
192
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
193
if not isinstance(transport, GitSmartTransport):
194
raise bzr_errors.NotBranchError(transport.base)
195
from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
196
lockfiles = GitLockableFiles(transport, GitLock())
197
return RemoteGitDir(transport, lockfiles, self)
200
def probe_transport(klass, transport):
201
"""Our format is present if the transport ends in '.not/'."""
203
if url.startswith('readonly+'):
204
url = url[len('readonly+'):]
205
if (not url.startswith("git://") and
206
not url.startswith("git+")):
207
raise bzr_errors.NotBranchError(transport.base)
208
# little ugly, but works
210
from bzrlib.plugins.git.remote import GitSmartTransport
211
if not isinstance(transport, GitSmartTransport):
212
raise bzr_errors.NotBranchError(transport.base)
213
# The only way to know a path exists and contains a valid repository
214
# is to do a request against it:
216
transport.fetch_pack(lambda x: [], None, lambda x: None,
217
lambda x: mutter("git: %s" % x))
218
except errors.git_errors.GitProtocolError:
219
raise bzr_errors.NotBranchError(path=transport.base)
222
raise bzr_errors.NotBranchError(path=transport.base)
224
def get_format_description(self):
225
return "Remote Git Repository"
227
def get_format_string(self):
228
return "Remote Git Repository"
230
def initialize_on_transport(self, transport):
231
raise bzr_errors.UninitializableFormat(self)
234
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
235
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
237
register_transport_proto('git://',
238
help="Access using the Git smart server protocol.")
239
register_transport_proto('git+ssh://',
240
help="Access using the Git smart server protocol over SSH.")
242
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
243
'TCPGitSmartTransport')
244
register_lazy_transport("git+ssh://", 'bzrlib.plugins.git.remote',
245
'SSHGitSmartTransport')
247
foreign_vcs_registry.register_lazy("git",
248
"bzrlib.plugins.git.mapping", "foreign_git", "Stupid content tracker")
250
plugin_cmds.register_lazy("cmd_git_serve", [], "bzrlib.plugins.git.commands")
251
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
253
def update_stanza(rev, stanza):
254
mapping = getattr(rev, "mapping", None)
255
if mapping is not None and mapping.revid_prefix.startswith("git-"):
256
stanza.add("git-commit", rev.foreign_revid)
259
RioVersionInfoBuilder.hooks.install_named_hook('revision',
262
def get_rich_root_format(stacked=False):
264
return bzrdir.format_registry.make_bzrdir("1.9-rich-root")
266
return bzrdir.format_registry.make_bzrdir("default-rich-root")
269
from bzrlib.plugins.git import tests
270
return tests.test_suite()