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 (
27
from bzrlib.option import (
32
class cmd_git_import(Command):
33
"""Import all branches from a git repository.
37
takes_args = ["src_location", "dest_location?"]
39
def run(self, src_location, dest_location=None):
40
from collections import defaultdict
46
from bzrlib.bzrdir import (
49
from bzrlib.errors import (
54
from bzrlib.repository import (
58
from bzrlib.plugins.git.branch import (
62
from bzrlib.plugins.git.refs import ref_to_branch_name
63
from bzrlib.plugins.git.repository import GitRepository
65
if dest_location is None:
66
dest_location = os.path.basename(src_location.rstrip("/\\"))
68
source_repo = Repository.open(src_location)
69
if not isinstance(source_repo, GitRepository):
70
raise BzrCommandError("%r is not a git repository" % src_location)
72
target_bzrdir = BzrDir.open(dest_location)
73
except NotBranchError:
74
target_bzrdir = BzrDir.create(dest_location)
76
target_repo = target_bzrdir.find_repository()
77
except NoRepositoryPresent:
78
target_repo = target_bzrdir.create_repository(shared=True)
80
if not target_repo.supports_rich_root():
81
raise BzrCommandError("Target repository doesn't support rich roots")
83
interrepo = InterRepository.get(source_repo, target_repo)
84
mapping = source_repo.get_mapping()
85
refs = interrepo.fetch()
86
unpeeled_tags = defaultdict(set)
88
for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
89
tags[k] = mapping.revision_id_foreign_to_bzr(peeled)
90
if unpeeled is not None:
91
unpeeled_tags[peeled].add(unpeeled)
92
# FIXME: Store unpeeled tag map
93
pb = ui.ui_factory.nested_progress_bar()
95
for i, (name, ref) in enumerate(refs.iteritems()):
97
ref_to_branch_name(name)
99
# Not a branch, ignore
101
pb.update("creating branches", i, len(refs))
102
head_loc = os.path.join(dest_location, name)
104
head_bzrdir = BzrDir.open(head_loc)
105
except NotBranchError:
106
parent_path = urlutils.dirname(head_loc)
107
if not os.path.isdir(parent_path):
108
os.makedirs(parent_path)
109
head_bzrdir = BzrDir.create(head_loc)
111
head_branch = head_bzrdir.open_branch()
112
except NotBranchError:
113
head_branch = head_bzrdir.create_branch()
114
revid = mapping.revision_id_foreign_to_bzr(ref)
115
source_branch = GitBranch(source_repo.bzrdir, source_repo,
117
source_branch.head = ref
118
if head_branch.last_revision() != revid:
119
head_branch.generate_revision_history(revid)
120
source_branch.tags.merge_to(head_branch.tags)
125
class cmd_git_object(Command):
126
"""List or display Git objects by SHA.
128
Cat a particular object's Git representation if a SHA is specified.
129
List all available SHAs otherwise.
134
aliases = ["git-objects", "git-cat"]
135
takes_args = ["sha1?"]
136
takes_options = [Option('directory',
138
help='Location of repository.', type=unicode),
139
Option('pretty', help='Pretty-print objects.')]
140
encoding_type = 'exact'
143
def run(self, sha1=None, directory=".", pretty=False):
144
from bzrlib.errors import (
147
from bzrlib.bzrdir import (
150
bzrdir, _ = BzrDir.open_containing(directory)
151
repo = bzrdir.find_repository()
152
from bzrlib.plugins.git.object_store import (
155
object_store = get_object_store(repo)
160
obj = object_store[str(sha1)]
162
raise BzrCommandError("Object not found: %s" % sha1)
164
text = obj.as_pretty_string()
166
text = obj.as_raw_string()
167
self.outf.write(text)
169
for sha1 in object_store:
170
self.outf.write("%s\n" % sha1)
175
class cmd_git_refs(Command):
176
"""Output all of the virtual refs for a repository.
182
takes_options = [Option('directory',
184
help='Location of repository.', type=unicode)]
187
def run(self, directory="."):
188
from bzrlib.bzrdir import (
191
from bzrlib.plugins.git.refs import (
194
from bzrlib.plugins.git.object_store import (
197
bzrdir, _ = BzrDir.open_containing(directory)
198
repo = bzrdir.find_repository()
201
object_store = get_object_store(repo)
202
refs = BazaarRefsContainer(bzrdir, object_store)
203
for k, v in refs.as_dict().iteritems():
204
self.outf.write("%s -> %s\n" % (k, v))
209
class cmd_git_apply(Command):
210
"""Apply a series of git-am style patches.
212
This command will in the future probably be integrated into
217
Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
219
takes_args = ["patches*"]
221
def _apply_patch(self, wt, f, signoff):
224
:param wt: A Bazaar working tree object.
225
:param f: Patch file to read.
226
:param signoff: Add Signed-Off-By flag.
228
from bzrlib.errors import BzrCommandError
229
from dulwich.patch import git_am_patch_split
231
(c, diff, version) = git_am_patch_split(f)
232
# FIXME: Cope with git-specific bits in patch
233
p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE, cwd=wt.basedir)
237
raise BzrCommandError("error running patch")
240
signed_off_by = wt.branch.get_config().username()
241
message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
242
wt.commit(authors=[c.author], message=message)
244
def run(self, patches_list=None, signoff=False, force=False):
245
from bzrlib.errors import UncommittedChanges
246
from bzrlib.workingtree import WorkingTree
247
if patches_list is None:
250
tree, _ = WorkingTree.open_containing(".")
251
if tree.basis_tree().changes_from(tree).has_changed() and not force:
252
raise UncommittedChanges(tree)
255
for patch in patches_list:
258
self._apply_patch(tree, f, signoff=signoff)