19
21
"""A GIT branch and repository format implementation for bzr."""
25
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dulwich"))
27
25
from bzrlib import bzrdir
28
from bzrlib.foreign import ForeignVcs, VcsMappingRegistry, foreign_vcs_registry
29
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
26
from bzrlib.foreign import foreign_vcs_registry
30
27
from bzrlib.transport import register_lazy_transport
32
bzrdir.format_registry.register(
33
'git', LocalGitBzrDirFormat,
34
help='GIT repository.',
35
native=False, experimental=True,
28
from bzrlib.commands import Command, register_command
29
from bzrlib.option import Option
30
from bzrlib.trace import warning
32
MINIMUM_DULWICH_VERSION = (0, 1, 0)
33
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
35
_versions_checked = False
36
def lazy_check_versions():
37
global _versions_checked
40
_versions_checked = True
42
from dulwich import __version__ as dulwich_version
44
warning("Please install dulwich, https://launchpad.net/dulwich")
47
if dulwich_version < MINIMUM_DULWICH_VERSION:
48
warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
51
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
53
bzrdir.format_registry.register_lazy('git',
54
"bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
55
help='GIT repository.', native=False, experimental=True,
38
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
39
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
59
# TODO: This should be lazier
60
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
61
bzrdir.BzrDirFormat.register_control_format_lazy("bzrlib.plugins.git.dir", "LocalGitBzrDirFormat")
62
bzrdir.BzrDirFormat.register_control_format_lazy("bzrlib.plugins.git.dir", "RemoteGitBzrDirFormat")
41
64
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
42
65
'GitSmartTransport')
45
class ForeignGit(ForeignVcs):
49
git_mapping_registry = VcsMappingRegistry()
50
git_mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
51
"BzrGitMappingExperimental")
52
foreign_vcs_registry.register("git", ForeignGit(git_mapping_registry),
53
"Stupid content tracker")
67
foreign_vcs_registry.register_lazy("git",
68
"bzrlib.plugins.git.mapping",
70
"Stupid content tracker")
73
class cmd_git_serve(Command):
74
"""Provide access to a Bazaar branch using the git protocol.
76
This command is experimental and doesn't do much yet.
80
help='serve contents of directory',
84
def run(self, directory=None):
86
from dulwich.server import TCPGitServer
87
from bzrlib.plugins.git.server import BzrBackend
88
from bzrlib.trace import warning
91
warning("server support in bzr-git is experimental.")
94
directory = os.getcwd()
96
backend = BzrBackend(directory)
98
server = TCPGitServer(backend, 'localhost')
99
server.serve_forever()
101
register_command(cmd_git_serve)
104
class cmd_git_import(Command):
105
"""Import all branches from a git repository.
109
takes_args = ["src_location", "dest_location"]
111
def run(self, src_location, dest_location):
112
from bzrlib.bzrdir import BzrDir, format_registry
113
from bzrlib.errors import NoRepositoryPresent, NotBranchError
114
from bzrlib.repository import Repository
115
source_repo = Repository.open(src_location)
116
format = format_registry.make_bzrdir('rich-root-pack')
118
target_bzrdir = BzrDir.open(dest_location)
119
except NotBranchError:
120
target_bzrdir = BzrDir.create(dest_location, format=format)
122
target_repo = target_bzrdir.open_repository()
123
except NoRepositoryPresent:
124
target_repo = target_bzrdir.create_repository(shared=True)
126
target_repo.fetch(source_repo)
127
for name, ref in source_repo._git.heads().iteritems():
128
head_loc = os.path.join(dest_location, name)
130
head_bzrdir = BzrDir.open(head_loc)
131
except NotBranchError:
132
head_bzrdir = BzrDir.create(head_loc, format=format)
134
head_branch = head_bzrdir.open_branch()
135
except NotBranchError:
136
head_branch = head_bzrdir.create_branch()
137
head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
140
register_command(cmd_git_import)