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 (
26
from bzrlib.option import (
30
from bzrlib.plugins.git import (
34
class cmd_git_serve(Command):
35
"""Provide access to a Bazaar branch using the git protocol.
37
This command is experimental and doesn't do much yet.
41
help='serve contents of directory',
45
def run(self, directory=None):
47
from dulwich.server import TCPGitServer
48
from bzrlib.plugins.git.server import BzrBackend
49
from bzrlib.trace import warning
52
warning("server support in bzr-git is experimental.")
55
directory = os.getcwd()
57
backend = BzrBackend(directory)
59
server = TCPGitServer(backend, 'localhost')
60
server.serve_forever()
63
class cmd_git_import(Command):
64
"""Import all branches from a git repository.
68
takes_args = ["src_location", "dest_location?"]
70
def run(self, src_location, dest_location=None):
76
from bzrlib.bzrdir import (
80
from bzrlib.errors import (
85
from bzrlib.repository import Repository
86
from bzrlib.plugins.git.branch import GitBranch
87
from bzrlib.plugins.git.fetch import InterGitNonGitRepository
88
from bzrlib.plugins.git.repository import GitRepository
90
if dest_location is None:
91
dest_location = os.path.basename(src_location.rstrip("/\\"))
93
source_repo = Repository.open(src_location)
94
if not isinstance(source_repo, GitRepository):
95
raise BzrCommandError("%r is not a git repository" % src_location)
96
format = get_rich_root_format()
98
target_bzrdir = BzrDir.open(dest_location)
99
except NotBranchError:
100
target_bzrdir = BzrDir.create(dest_location, format=format)
102
target_repo = target_bzrdir.open_repository()
103
except NoRepositoryPresent:
104
target_repo = target_bzrdir.create_repository(shared=True)
106
interrepo = InterGitNonGitRepository(source_repo, target_repo)
107
mapping = source_repo.get_mapping()
108
refs = interrepo.fetch_refs()
109
pb = ui.ui_factory.nested_progress_bar()
111
for i, (name, ref) in enumerate(refs.iteritems()):
112
if name.startswith("refs/tags/"):
114
pb.update("creating branches", i, len(refs))
115
head_loc = os.path.join(dest_location, name)
117
head_bzrdir = BzrDir.open(head_loc)
118
except NotBranchError:
119
parent_path = urlutils.dirname(head_loc)
120
if not os.path.isdir(parent_path):
121
os.makedirs(parent_path)
122
head_bzrdir = BzrDir.create(head_loc, format=format)
124
head_branch = head_bzrdir.open_branch()
125
except NotBranchError:
126
head_branch = head_bzrdir.create_branch()
127
revid = mapping.revision_id_foreign_to_bzr(ref)
128
source_branch = GitBranch(source_repo.bzrdir, source_repo,
130
head_branch.generate_revision_history(revid)
131
source_branch.tags.merge_to(head_branch.tags)