22
21
"""A GIT branch and repository format implementation for bzr."""
26
from bzrlib import bzrdir, errors as bzr_errors
23
from bzrlib import bzrdir
27
24
from bzrlib.foreign import foreign_vcs_registry
28
from bzrlib.lockable_files import TransportLock
29
25
from bzrlib.transport import register_lazy_transport
30
from bzrlib.commands import plugin_cmds
26
from bzrlib.commands import Command, register_command
27
from bzrlib.option import Option
31
28
from bzrlib.trace import warning
33
30
MINIMUM_DULWICH_VERSION = (0, 1, 0)
34
COMPATIBLE_BZR_VERSIONS = [(1, 11, 0), (1, 12, 0)]
36
_versions_checked = False
37
def lazy_check_versions():
38
global _versions_checked
41
_versions_checked = True
43
from dulwich import __version__ as dulwich_version
45
warning("Please install dulwich, https://launchpad.net/dulwich")
48
if dulwich_version < MINIMUM_DULWICH_VERSION:
49
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
52
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
54
bzrdir.format_registry.register_lazy('git',
55
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
56
help='GIT repository.', native=False, experimental=True,
60
from bzrlib.revisionspec import revspec_registry
61
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
33
from dulwich import __version__ as dulwich_version
63
34
except ImportError:
65
from bzrlib.revisionspec import SPEC_TYPES
66
from bzrlib.plugins.git.revspec import RevisionSpec_git
67
SPEC_TYPES.append(RevisionSpec_git)
69
class GitBzrDirFormat(bzrdir.BzrDirFormat):
70
_lock_class = TransportLock
72
def is_supported(self):
76
class LocalGitBzrDirFormat(GitBzrDirFormat):
77
"""The .git directory control format."""
80
def _known_formats(self):
81
return set([LocalGitBzrDirFormat()])
83
def open(self, transport, _found=None):
84
"""Open this directory.
88
# we dont grok readonly - git isn't integrated with transport.
90
if url.startswith('readonly+'):
91
url = url[len('readonly+'):]
94
gitrepo = git.repo.Repo(transport.local_abspath("."))
95
except bzr_errors.NotLocalUrl:
96
raise bzr_errors.NotBranchError(path=transport.base)
97
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
98
lockfiles = GitLockableFiles(transport, GitLock())
99
return LocalGitDir(transport, lockfiles, gitrepo, self)
102
def probe_transport(klass, transport):
103
"""Our format is present if the transport ends in '.not/'."""
104
from bzrlib.transport.local import LocalTransport
106
if not isinstance(transport, LocalTransport):
107
raise bzr_errors.NotBranchError(path=transport.base)
109
# This should quickly filter out most things that are not
110
# git repositories, saving us the trouble from loading dulwich.
111
if not transport.has(".git") and not transport.has("objects"):
112
raise bzr_errors.NotBranchError(path=transport.base)
114
import dulwich as git
117
format.open(transport)
119
except git.errors.NotGitRepository, e:
120
raise bzr_errors.NotBranchError(path=transport.base)
121
raise bzr_errors.NotBranchError(path=transport.base)
123
def get_format_description(self):
124
return "Local Git Repository"
126
def get_format_string(self):
127
return "Local Git Repository"
129
def initialize_on_transport(self, transport):
130
from bzrlib.transport.local import LocalTransport
132
if not isinstance(transport, LocalTransport):
133
raise NotImplementedError(self.initialize,
134
"Can't create Git Repositories/branches on "
135
"non-local transports")
137
from dulwich.repo import Repo
138
Repo.create(transport.local_abspath("."))
139
return self.open(transport)
141
def is_supported(self):
145
class RemoteGitBzrDirFormat(GitBzrDirFormat):
146
"""The .git directory control format."""
149
def _known_formats(self):
150
return set([RemoteGitBzrDirFormat()])
152
def open(self, transport, _found=None):
153
"""Open this directory.
156
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
157
if not isinstance(transport, GitSmartTransport):
158
raise bzr_errors.NotBranchError(transport.base)
159
# we dont grok readonly - git isn't integrated with transport.
161
if url.startswith('readonly+'):
162
url = url[len('readonly+'):]
164
from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
165
lockfiles = GitLockableFiles(transport, GitLock())
166
return RemoteGitDir(transport, lockfiles, self)
169
def probe_transport(klass, transport):
170
"""Our format is present if the transport ends in '.not/'."""
171
# little ugly, but works
173
from bzrlib.plugins.git.remote import GitSmartTransport
174
if not isinstance(transport, GitSmartTransport):
175
raise bzr_errors.NotBranchError(transport.base)
176
# The only way to know a path exists and contains a valid repository
177
# is to do a request against it:
179
transport.fetch_pack(lambda x: [], None, lambda x: None,
180
lambda x: mutter("git: %s" % x))
181
except errors.git_errors.GitProtocolError:
182
raise bzr_errors.NotBranchError(path=transport.base)
185
raise bzr_errors.NotBranchError(path=transport.base)
187
def get_format_description(self):
188
return "Remote Git Repository"
190
def get_format_string(self):
191
return "Remote Git Repository"
193
def initialize_on_transport(self, transport):
194
raise bzr_errors.UninitializableFormat(self)
35
warning("Please install dulwich, https://launchpad.net/dulwich")
38
if dulwich_version < MINIMUM_DULWICH_VERSION:
39
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
42
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
44
bzrdir.format_registry.register(
45
'git', LocalGitBzrDirFormat,
46
help='GIT repository.',
47
native=False, experimental=True,
197
50
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
198
51
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
206
59
"Stupid content tracker")
208
plugin_cmds.register_lazy("cmd_git_serve", [], "bzrlib.plugins.git.commands")
209
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
62
class cmd_git_serve(Command):
63
"""Provide access to a Bazaar branch using the git protocol.
65
This command is experimental and doesn't do much yet.
69
help='serve contents of directory',
73
def run(self, directory=None):
74
from dulwich.server import TCPGitServer
75
from bzrlib.plugins.git.server import BzrBackend
76
from bzrlib.trace import warning
79
warning("server support in bzr-git is experimental.")
82
directory = os.getcwd()
84
backend = BzrBackend(directory)
86
server = TCPGitServer(backend, 'localhost')
87
server.serve_forever()
89
register_command(cmd_git_serve)
92
class cmd_git_import(Command):
93
"""Import all branches from a git repository.
97
takes_args = ["src_location", "dest_location"]
99
def run(self, src_location, dest_location):
100
from bzrlib.bzrdir import BzrDir, format_registry
101
from bzrlib.errors import NoRepositoryPresent, NotBranchError
102
from bzrlib.repository import Repository
103
source_repo = Repository.open(src_location)
104
format = format_registry.make_bzrdir('rich-root-pack')
106
target_bzrdir = BzrDir.open(dest_location)
107
except NotBranchError:
108
target_bzrdir = BzrDir.create(dest_location, format=format)
110
target_repo = target_bzrdir.open_repository()
111
except NoRepositoryPresent:
112
target_repo = target_bzrdir.create_repository(shared=True)
114
target_repo.fetch(source_repo)
115
for name, ref in source_repo._git.heads().iteritems():
116
head_loc = os.path.join(dest_location, name)
118
head_bzrdir = BzrDir.open(head_loc)
119
except NotBranchError:
120
head_bzrdir = BzrDir.create(head_loc, format=format)
122
head_branch = head_bzrdir.open_branch()
123
except NotBranchError:
124
head_branch = head_bzrdir.create_branch()
125
head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
128
register_command(cmd_git_import)
211
131
def test_suite():
212
132
from bzrlib.plugins.git import tests