18
18
# along with this program; if not, write to the Free Software
19
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
21
"""Git-specific subcommands for Bazaar."""
24
23
from bzrlib.commands import Command
25
24
from bzrlib.option import Option
26
from bzrlib.plugins.git import get_rich_root_format
27
28
class cmd_git_serve(Command):
28
29
"""Provide access to a Bazaar branch using the git protocol.
52
53
server = TCPGitServer(backend, 'localhost')
53
54
server.serve_forever()
55
57
class cmd_git_import(Command):
56
58
"""Import all branches from a git repository.
60
takes_args = ["src_location", "dest_location"]
62
takes_args = ["src_location", "dest_location?"]
62
def run(self, src_location, dest_location):
63
from bzrlib.bzrdir import BzrDir, format_registry
64
from bzrlib.errors import NoRepositoryPresent, NotBranchError
64
def run(self, src_location, dest_location=None):
70
from bzrlib.bzrdir import (
74
from bzrlib.errors import (
65
79
from bzrlib.repository import Repository
80
from bzrlib.plugins.git.fetch import InterGitNonGitRepository
81
from bzrlib.plugins.git.repository import GitRepository
83
if dest_location is None:
84
dest_location = os.path.basename(src_location.rstrip("/\\"))
66
86
source_repo = Repository.open(src_location)
67
format = format_registry.make_bzrdir('rich-root-pack')
87
if not isinstance(source_repo, GitRepository):
88
raise BzrCommandError("%r is not a git repository" % src_location)
89
format = get_rich_root_format()
69
91
target_bzrdir = BzrDir.open(dest_location)
70
92
except NotBranchError:
74
96
except NoRepositoryPresent:
75
97
target_repo = target_bzrdir.create_repository(shared=True)
77
target_repo.fetch(source_repo)
78
for name, ref in source_repo._git.heads().iteritems():
79
head_loc = os.path.join(dest_location, name)
81
head_bzrdir = BzrDir.open(head_loc)
82
except NotBranchError:
83
head_bzrdir = BzrDir.create(head_loc, format=format)
85
head_branch = head_bzrdir.open_branch()
86
except NotBranchError:
87
head_branch = head_bzrdir.create_branch()
88
head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
99
interrepo = InterGitNonGitRepository(source_repo, target_repo)
100
mapping = source_repo.get_mapping()
101
refs = interrepo.fetch_refs()
102
pb = ui.ui_factory.nested_progress_bar()
104
for i, (name, ref) in enumerate(refs.iteritems()):
105
pb.update("creating branches", i, len(refs))
106
if name.endswith("^{}"):
108
head_loc = os.path.join(dest_location, name)
110
head_bzrdir = BzrDir.open(head_loc)
111
except NotBranchError:
112
parent_path = urlutils.dirname(head_loc)
113
if not os.path.isdir(parent_path):
114
os.makedirs(parent_path)
115
head_bzrdir = BzrDir.create(head_loc, format=format)
117
head_branch = head_bzrdir.open_branch()
118
except NotBranchError:
119
head_branch = head_bzrdir.create_branch()
120
if ("%s^{}" % name) in refs:
121
revid = mapping.revision_id_foreign_to_bzr(refs["%s^{}" % name])
123
revid = mapping.revision_id_foreign_to_bzr(ref)
124
head_branch.generate_revision_history(revid)