1
# Copyright (C) 2006 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."""
26
from bzrlib import bzrdir, errors as bzr_errors
27
from bzrlib.foreign import foreign_vcs_registry
28
from bzrlib.lockable_files import TransportLock
29
from bzrlib.transport import register_lazy_transport
30
from bzrlib.commands import Command, register_command
31
from bzrlib.option import Option
32
from bzrlib.trace import warning
34
MINIMUM_DULWICH_VERSION = (0, 1, 0)
35
COMPATIBLE_BZR_VERSIONS = [(1, 11, 0), (1, 12, 0)]
37
_versions_checked = False
38
def lazy_check_versions():
39
global _versions_checked
42
_versions_checked = True
44
from dulwich import __version__ as dulwich_version
46
warning("Please install dulwich, https://launchpad.net/dulwich")
49
if dulwich_version < MINIMUM_DULWICH_VERSION:
50
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
53
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
55
bzrdir.format_registry.register_lazy('git',
56
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
57
help='GIT repository.', native=False, experimental=True,
61
from bzrlib.revisionspec import revspec_registry
62
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
66
from bzrlib.revisionspec import SPEC_TYPES
67
from bzrlib.plugins.git.revspec import RevisionSpec_git
68
SPEC_TYPES.append(RevisionSpec_git)
70
class GitBzrDirFormat(bzrdir.BzrDirFormat):
71
_lock_class = TransportLock
73
def is_supported(self):
77
class LocalGitBzrDirFormat(GitBzrDirFormat):
78
"""The .git directory control format."""
81
def _known_formats(self):
82
return set([LocalGitBzrDirFormat()])
84
def open(self, transport, _found=None):
85
"""Open this directory.
89
# we dont grok readonly - git isn't integrated with transport.
91
if url.startswith('readonly+'):
92
url = url[len('readonly+'):]
95
gitrepo = git.repo.Repo(transport.local_abspath("."))
96
except bzr_errors.NotLocalUrl:
97
raise bzr_errors.NotBranchError(path=transport.base)
98
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
99
lockfiles = GitLockableFiles(transport, GitLock())
100
return LocalGitDir(transport, lockfiles, gitrepo, self)
103
def probe_transport(klass, transport):
104
"""Our format is present if the transport ends in '.not/'."""
105
from bzrlib.transport.local import LocalTransport
107
if not isinstance(transport, LocalTransport):
108
raise bzr_errors.NotBranchError(path=transport.base)
110
# This should quickly filter out most things that are not
111
# git repositories, saving us the trouble from loading dulwich.
112
if not transport.has(".git") and not transport.has("objects"):
113
raise bzr_errors.NotBranchError(path=transport.base)
115
import dulwich as git
118
format.open(transport)
120
except git.errors.NotGitRepository, e:
121
raise bzr_errors.NotBranchError(path=transport.base)
122
raise bzr_errors.NotBranchError(path=transport.base)
124
def get_format_description(self):
125
return "Local Git Repository"
127
def get_format_string(self):
128
return "Local Git Repository"
130
def initialize_on_transport(self, transport):
131
from bzrlib.transport.local import LocalTransport
133
if not isinstance(transport, LocalTransport):
134
raise NotImplementedError(self.initialize,
135
"Can't create Git Repositories/branches on "
136
"non-local transports")
138
from dulwich.repo import Repo
139
Repo.create(transport.local_abspath("."))
140
return self.open(transport)
142
def is_supported(self):
146
class RemoteGitBzrDirFormat(GitBzrDirFormat):
147
"""The .git directory control format."""
150
def _known_formats(self):
151
return set([RemoteGitBzrDirFormat()])
153
def open(self, transport, _found=None):
154
"""Open this directory.
157
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
158
if not isinstance(transport, GitSmartTransport):
159
raise bzr_errors.NotBranchError(transport.base)
160
# we dont grok readonly - git isn't integrated with transport.
162
if url.startswith('readonly+'):
163
url = url[len('readonly+'):]
165
from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
166
lockfiles = GitLockableFiles(transport, GitLock())
167
return RemoteGitDir(transport, lockfiles, self)
170
def probe_transport(klass, transport):
171
"""Our format is present if the transport ends in '.not/'."""
172
# little ugly, but works
174
from bzrlib.plugins.git.remote import GitSmartTransport
175
if not isinstance(transport, GitSmartTransport):
176
raise bzr_errors.NotBranchError(transport.base)
177
# The only way to know a path exists and contains a valid repository
178
# is to do a request against it:
180
transport.fetch_pack(lambda x: [], None, lambda x: None,
181
lambda x: mutter("git: %s" % x))
182
except errors.git_errors.GitProtocolError:
183
raise bzr_errors.NotBranchError(path=transport.base)
186
raise bzr_errors.NotBranchError(path=transport.base)
188
def get_format_description(self):
189
return "Remote Git Repository"
191
def get_format_string(self):
192
return "Remote Git Repository"
194
def initialize_on_transport(self, transport):
195
raise bzr_errors.UninitializableFormat(self)
198
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
199
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
201
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
204
foreign_vcs_registry.register_lazy("git",
205
"bzrlib.plugins.git.mapping",
207
"Stupid content tracker")
210
class cmd_git_serve(Command):
211
"""Provide access to a Bazaar branch using the git protocol.
213
This command is experimental and doesn't do much yet.
217
help='serve contents of directory',
221
def run(self, directory=None):
222
lazy_check_versions()
223
from dulwich.server import TCPGitServer
224
from bzrlib.plugins.git.server import BzrBackend
225
from bzrlib.trace import warning
228
warning("server support in bzr-git is experimental.")
230
if directory is None:
231
directory = os.getcwd()
233
backend = BzrBackend(directory)
235
server = TCPGitServer(backend, 'localhost')
236
server.serve_forever()
238
register_command(cmd_git_serve)
241
class cmd_git_import(Command):
242
"""Import all branches from a git repository.
246
takes_args = ["src_location", "dest_location"]
248
def run(self, src_location, dest_location):
249
from bzrlib.bzrdir import BzrDir, format_registry
250
from bzrlib.errors import NoRepositoryPresent, NotBranchError
251
from bzrlib.repository import Repository
252
source_repo = Repository.open(src_location)
253
format = format_registry.make_bzrdir('rich-root-pack')
255
target_bzrdir = BzrDir.open(dest_location)
256
except NotBranchError:
257
target_bzrdir = BzrDir.create(dest_location, format=format)
259
target_repo = target_bzrdir.open_repository()
260
except NoRepositoryPresent:
261
target_repo = target_bzrdir.create_repository(shared=True)
263
target_repo.fetch(source_repo)
264
for name, ref in source_repo._git.heads().iteritems():
265
head_loc = os.path.join(dest_location, name)
267
head_bzrdir = BzrDir.open(head_loc)
268
except NotBranchError:
269
head_bzrdir = BzrDir.create(head_loc, format=format)
271
head_branch = head_bzrdir.open_branch()
272
except NotBranchError:
273
head_branch = head_bzrdir.create_branch()
274
head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
277
register_command(cmd_git_import)
281
from bzrlib.plugins.git import tests
282
return tests.test_suite()