19
22
"""A GIT branch and repository format implementation for bzr."""
25
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dulwich"))
27
26
from bzrlib import bzrdir
28
from bzrlib.foreign import ForeignVcs, VcsMappingRegistry, foreign_vcs_registry
29
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
27
from bzrlib.foreign import foreign_vcs_registry
28
from bzrlib.lockable_files import TransportLock
29
from bzrlib.revisionspec import revspec_registry
30
30
from bzrlib.transport import register_lazy_transport
31
31
from bzrlib.commands import Command, register_command
32
32
from bzrlib.option import Option
34
bzrdir.format_registry.register(
35
'git', LocalGitBzrDirFormat,
36
help='GIT repository.',
37
native=False, experimental=True,
33
from bzrlib.trace import warning
35
MINIMUM_DULWICH_VERSION = (0, 1, 0)
36
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
38
_versions_checked = False
39
def lazy_check_versions():
40
global _versions_checked
43
_versions_checked = True
45
from dulwich import __version__ as dulwich_version
47
warning("Please install dulwich, https://launchpad.net/dulwich")
50
if dulwich_version < MINIMUM_DULWICH_VERSION:
51
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
54
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
56
bzrdir.format_registry.register_lazy('git',
57
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
58
help='GIT repository.', native=False, experimental=True,
60
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
63
class GitBzrDirFormat(bzrdir.BzrDirFormat):
64
_lock_class = TransportLock
66
def is_supported(self):
70
class LocalGitBzrDirFormat(GitBzrDirFormat):
71
"""The .git directory control format."""
74
def _known_formats(self):
75
return set([LocalGitBzrDirFormat()])
77
def open(self, transport, _found=None):
78
"""Open this directory.
82
# we dont grok readonly - git isn't integrated with transport.
84
if url.startswith('readonly+'):
85
url = url[len('readonly+'):]
88
gitrepo = git.repo.Repo(transport.local_abspath("."))
89
except errors.bzr_errors.NotLocalUrl:
90
raise errors.bzr_errors.NotBranchError(path=transport.base)
91
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
92
lockfiles = GitLockableFiles(transport, GitLock())
93
return LocalGitDir(transport, lockfiles, gitrepo, self)
96
def probe_transport(klass, transport):
97
"""Our format is present if the transport ends in '.not/'."""
98
from bzrlib.transport.local import LocalTransport
100
if not isinstance(transport, LocalTransport):
101
raise errors.bzr_errors.NotBranchError(path=transport.base)
103
# This should quickly filter out most things that are not
104
# git repositories, saving us the trouble from loading dulwich.
105
if not transport.has(".git") and not transport.has("objects"):
106
raise errors.bzr_errors.NotBranchError(path=transport.base)
108
import dulwich as git
111
format.open(transport)
113
except git.errors.NotGitRepository, e:
114
raise errors.bzr_errors.NotBranchError(path=transport.base)
115
raise errors.bzr_errors.NotBranchError(path=transport.base)
117
def get_format_description(self):
118
return "Local Git Repository"
120
def get_format_string(self):
121
return "Local Git Repository"
123
def initialize_on_transport(self, transport):
124
from bzrlib.transport.local import LocalTransport
126
if not isinstance(transport, LocalTransport):
127
raise NotImplementedError(self.initialize,
128
"Can't create Git Repositories/branches on "
129
"non-local transports")
131
from dulwich.repo import Repo
132
Repo.create(transport.local_abspath("."))
133
return self.open(transport)
135
def is_supported(self):
139
class RemoteGitBzrDirFormat(GitBzrDirFormat):
140
"""The .git directory control format."""
143
def _known_formats(self):
144
return set([RemoteGitBzrDirFormat()])
146
def open(self, transport, _found=None):
147
"""Open this directory.
150
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
151
if not isinstance(transport, GitSmartTransport):
152
raise errors.bzr_errors.NotBranchError(transport.base)
153
# we dont grok readonly - git isn't integrated with transport.
155
if url.startswith('readonly+'):
156
url = url[len('readonly+'):]
158
from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
159
lockfiles = GitLockableFiles(transport, GitLock())
160
return RemoteGitDir(transport, lockfiles, self)
163
def probe_transport(klass, transport):
164
"""Our format is present if the transport ends in '.not/'."""
165
# little ugly, but works
167
from bzrlib.plugins.git.remote import GitSmartTransport
168
if not isinstance(transport, GitSmartTransport):
169
raise errors.bzr_errors.NotBranchError(transport.base)
170
# The only way to know a path exists and contains a valid repository
171
# is to do a request against it:
173
transport.fetch_pack(lambda x: [], None, lambda x: None,
174
lambda x: mutter("git: %s" % x))
175
except errors.git_errors.GitProtocolError:
176
raise errors.bzr_errors.NotBranchError(path=transport.base)
179
raise errors.bzr_errors.NotBranchError(path=transport.base)
181
def get_format_description(self):
182
return "Remote Git Repository"
184
def get_format_string(self):
185
return "Remote Git Repository"
187
def initialize_on_transport(self, transport):
188
raise errors.bzr_errors.UninitializableFormat(self)
40
191
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
41
192
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
82
226
backend = BzrBackend(directory)
86
# sys.stdout.write(data)
88
#server = GitServer(sys.stdin.read, send_fn)
89
raise NotImplementedError
91
server = TCPGitServer(backend, 'localhost')
92
server.serve_forever()
228
server = TCPGitServer(backend, 'localhost')
229
server.serve_forever()
94
231
register_command(cmd_git_serve)
234
class cmd_git_import(Command):
235
"""Import all branches from a git repository.
239
takes_args = ["src_location", "dest_location"]
241
def run(self, src_location, dest_location):
242
from bzrlib.bzrdir import BzrDir, format_registry
243
from bzrlib.errors import NoRepositoryPresent, NotBranchError
244
from bzrlib.repository import Repository
245
source_repo = Repository.open(src_location)
246
format = format_registry.make_bzrdir('rich-root-pack')
248
target_bzrdir = BzrDir.open(dest_location)
249
except NotBranchError:
250
target_bzrdir = BzrDir.create(dest_location, format=format)
252
target_repo = target_bzrdir.open_repository()
253
except NoRepositoryPresent:
254
target_repo = target_bzrdir.create_repository(shared=True)
256
target_repo.fetch(source_repo)
257
for name, ref in source_repo._git.heads().iteritems():
258
head_loc = os.path.join(dest_location, name)
260
head_bzrdir = BzrDir.open(head_loc)
261
except NotBranchError:
262
head_bzrdir = BzrDir.create(head_loc, format=format)
264
head_branch = head_bzrdir.open_branch()
265
except NotBranchError:
266
head_branch = head_bzrdir.create_branch()
267
head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
270
register_command(cmd_git_import)
98
274
from bzrlib.plugins.git import tests
99
275
return tests.test_suite()