22
22
"""A GIT branch and repository format implementation for bzr."""
29
from bzrlib import bzrdir, errors as bzr_errors
26
from bzrlib import bzrdir
30
27
from bzrlib.foreign import foreign_vcs_registry
31
28
from bzrlib.lockable_files import TransportLock
29
from bzrlib.revisionspec import revspec_registry
32
30
from bzrlib.transport import register_lazy_transport
33
from bzrlib.commands import plugin_cmds
31
from bzrlib.commands import Command, register_command
32
from bzrlib.option import Option
34
33
from bzrlib.trace import warning
36
35
MINIMUM_DULWICH_VERSION = (0, 1, 0)
37
COMPATIBLE_BZR_VERSIONS = [(1, 11, 0), (1, 12, 0)]
39
if getattr(sys, "frozen", None):
40
# allow import additional libs from ./_lib for bzr.exe only
41
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '_lib')))
36
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
43
38
_versions_checked = False
44
39
def lazy_check_versions():
50
45
from dulwich import __version__ as dulwich_version
51
46
except ImportError:
52
raise ImportError("bzr-git: Please install dulwich, https://launchpad.net/dulwich")
47
warning("Please install dulwich, https://launchpad.net/dulwich")
54
50
if dulwich_version < MINIMUM_DULWICH_VERSION:
55
raise ImportError("bzr-git: Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
51
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
57
54
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
60
57
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
61
58
help='GIT repository.', native=False, experimental=True,
65
from bzrlib.revisionspec import revspec_registry
66
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
70
from bzrlib.revisionspec import SPEC_TYPES
71
from bzrlib.plugins.git.revspec import RevisionSpec_git
72
SPEC_TYPES.append(RevisionSpec_git)
60
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
74
63
class GitBzrDirFormat(bzrdir.BzrDirFormat):
75
64
_lock_class = TransportLock
99
88
gitrepo = git.repo.Repo(transport.local_abspath("."))
100
except bzr_errors.NotLocalUrl:
101
raise bzr_errors.NotBranchError(path=transport.base)
89
except errors.bzr_errors.NotLocalUrl:
90
raise errors.bzr_errors.NotBranchError(path=transport.base)
102
91
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
103
92
lockfiles = GitLockableFiles(transport, GitLock())
104
93
return LocalGitDir(transport, lockfiles, gitrepo, self)
109
98
from bzrlib.transport.local import LocalTransport
111
100
if not isinstance(transport, LocalTransport):
112
raise bzr_errors.NotBranchError(path=transport.base)
101
raise errors.bzr_errors.NotBranchError(path=transport.base)
114
103
# This should quickly filter out most things that are not
115
104
# git repositories, saving us the trouble from loading dulwich.
116
105
if not transport.has(".git") and not transport.has("objects"):
117
raise bzr_errors.NotBranchError(path=transport.base)
106
raise errors.bzr_errors.NotBranchError(path=transport.base)
119
108
import dulwich as git
122
111
format.open(transport)
124
113
except git.errors.NotGitRepository, e:
125
raise bzr_errors.NotBranchError(path=transport.base)
126
raise bzr_errors.NotBranchError(path=transport.base)
114
raise errors.bzr_errors.NotBranchError(path=transport.base)
115
raise errors.bzr_errors.NotBranchError(path=transport.base)
128
117
def get_format_description(self):
129
118
return "Local Git Repository"
161
150
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
162
151
if not isinstance(transport, GitSmartTransport):
163
raise bzr_errors.NotBranchError(transport.base)
152
raise errors.bzr_errors.NotBranchError(transport.base)
164
153
# we dont grok readonly - git isn't integrated with transport.
165
154
url = transport.base
166
155
if url.startswith('readonly+'):
178
167
from bzrlib.plugins.git.remote import GitSmartTransport
179
168
if not isinstance(transport, GitSmartTransport):
180
raise bzr_errors.NotBranchError(transport.base)
169
raise errors.bzr_errors.NotBranchError(transport.base)
181
170
# The only way to know a path exists and contains a valid repository
182
171
# is to do a request against it:
184
173
transport.fetch_pack(lambda x: [], None, lambda x: None,
185
174
lambda x: mutter("git: %s" % x))
186
175
except errors.git_errors.GitProtocolError:
187
raise bzr_errors.NotBranchError(path=transport.base)
176
raise errors.bzr_errors.NotBranchError(path=transport.base)
190
raise bzr_errors.NotBranchError(path=transport.base)
179
raise errors.bzr_errors.NotBranchError(path=transport.base)
192
181
def get_format_description(self):
193
182
return "Remote Git Repository"
211
200
"Stupid content tracker")
213
plugin_cmds.register_lazy("cmd_git_serve", [], "bzrlib.plugins.git.commands")
214
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
203
class cmd_git_serve(Command):
204
"""Provide access to a Bazaar branch using the git protocol.
206
This command is experimental and doesn't do much yet.
210
help='serve contents of directory',
214
def run(self, directory=None):
215
lazy_check_versions()
216
from dulwich.server import TCPGitServer
217
from bzrlib.plugins.git.server import BzrBackend
218
from bzrlib.trace import warning
221
warning("server support in bzr-git is experimental.")
223
if directory is None:
224
directory = os.getcwd()
226
backend = BzrBackend(directory)
228
server = TCPGitServer(backend, 'localhost')
229
server.serve_forever()
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)
216
273
def test_suite():
217
274
from bzrlib.plugins.git import tests