1
# Copyright (C) 2006-2009 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
21
"""Git-specific subcommands for Bazaar."""
23
from bzrlib.commands import Command
24
from bzrlib.option import Option
26
class cmd_git_serve(Command):
27
"""Provide access to a Bazaar branch using the git protocol.
29
This command is experimental and doesn't do much yet.
33
help='serve contents of directory',
37
def run(self, directory=None):
39
from dulwich.server import TCPGitServer
40
from bzrlib.plugins.git.server import BzrBackend
41
from bzrlib.trace import warning
44
warning("server support in bzr-git is experimental.")
47
directory = os.getcwd()
49
backend = BzrBackend(directory)
51
server = TCPGitServer(backend, 'localhost')
52
server.serve_forever()
55
class cmd_git_import(Command):
56
"""Import all branches from a git repository.
60
takes_args = ["src_location", "dest_location?"]
62
def run(self, src_location, dest_location=None):
68
from bzrlib.bzrdir import (
72
from bzrlib.errors import (
77
from bzrlib.repository import Repository
78
from bzrlib.plugins.git.fetch import InterGitNonGitRepository
79
from bzrlib.plugins.git.repository import GitRepository
81
if dest_location is None:
82
dest_location = os.path.basename(src_location.rstrip("/\\"))
84
source_repo = Repository.open(src_location)
85
if not isinstance(source_repo, GitRepository):
86
raise BzrCommandError("%r is not a git repository" % src_location)
87
format = format_registry.make_bzrdir("1.9-rich-root")
89
target_bzrdir = BzrDir.open(dest_location)
90
except NotBranchError:
91
target_bzrdir = BzrDir.create(dest_location, format=format)
93
target_repo = target_bzrdir.open_repository()
94
except NoRepositoryPresent:
95
target_repo = target_bzrdir.create_repository(shared=True)
97
interrepo = InterGitNonGitRepository(source_repo, target_repo)
98
mapping = source_repo.get_mapping()
99
refs = interrepo.fetch_refs()
100
pb = ui.ui_factory.nested_progress_bar()
102
for i, (name, ref) in enumerate(refs.iteritems()):
103
pb.update("creating branches", i, len(refs))
104
if name.endswith("^{}"):
106
head_loc = os.path.join(dest_location, name)
108
head_bzrdir = BzrDir.open(head_loc)
109
except NotBranchError:
110
parent_path = urlutils.dirname(head_loc)
111
if not os.path.isdir(parent_path):
112
os.makedirs(parent_path)
113
head_bzrdir = BzrDir.create(head_loc, format=format)
115
head_branch = head_bzrdir.open_branch()
116
except NotBranchError:
117
head_branch = head_bzrdir.create_branch()
118
if ("%s^{}" % name) in refs:
119
revid = mapping.revision_id_foreign_to_bzr(refs["%s^{}" % name])
121
revid = mapping.revision_id_foreign_to_bzr(ref)
122
head_branch.generate_revision_history(revid)