37
37
takes_args = ["src_location", "dest_location?"]
39
def _get_colocated_branch(self, target_bzrdir, name):
40
from bzrlib.errors import NotBranchError
42
return target_bzrdir.open_branch(name=name)
43
except NotBranchError:
44
return target_bzrdir.create_branch(name=name)
46
def _get_nested_branch(self, dest_transport, dest_format, name):
47
from bzrlib.bzrdir import BzrDir
48
from bzrlib.errors import NotBranchError
49
head_transport = dest_transport.clone(name)
51
head_bzrdir = BzrDir.open_from_transport(head_transport)
52
except NotBranchError:
53
head_bzrdir = dest_format.initialize_on_transport_ex(
54
head_transport, create_prefix=True)[1]
56
return head_bzrdir.open_branch()
57
except NotBranchError:
58
return head_bzrdir.create_branch()
39
60
def run(self, src_location, dest_location=None):
41
63
from bzrlib import (
50
74
NoRepositoryPresent,
77
from bzrlib.plugins.git import gettext
53
78
from bzrlib.repository import (
82
from bzrlib.transport import get_transport
57
83
from bzrlib.plugins.git.branch import (
86
from bzrlib.plugins.git.refs import (
61
89
from bzrlib.plugins.git.repository import GitRepository
91
dest_format = controldir.ControlDirFormat.get_default_format()
63
93
if dest_location is None:
64
94
dest_location = os.path.basename(src_location.rstrip("/\\"))
96
dest_transport = get_transport(dest_location)
66
98
source_repo = Repository.open(src_location)
67
99
if not isinstance(source_repo, GitRepository):
68
raise BzrCommandError("%r is not a git repository" % src_location)
100
raise BzrCommandError(gettext("%r is not a git repository") % src_location)
70
target_bzrdir = BzrDir.open(dest_location)
102
target_bzrdir = BzrDir.open_from_transport(dest_transport)
71
103
except NotBranchError:
72
target_bzrdir = BzrDir.create(dest_location)
104
target_bzrdir = dest_format.initialize_on_transport_ex(
105
dest_transport, shared_repo=True)[1]
74
107
target_repo = target_bzrdir.find_repository()
75
108
except NoRepositoryPresent:
76
109
target_repo = target_bzrdir.create_repository(shared=True)
78
111
if not target_repo.supports_rich_root():
79
raise BzrCommandError("Target repository doesn't support rich roots")
112
raise BzrCommandError(gettext("Target repository doesn't support rich roots"))
81
114
interrepo = InterRepository.get(source_repo, target_repo)
82
115
mapping = source_repo.get_mapping()
83
116
refs = interrepo.fetch()
85
for k, v in extract_tags(refs).iteritems():
86
tags[k] = mapping.revision_id_foreign_to_bzr(v)
117
refs_dict = refs.as_dict()
87
118
pb = ui.ui_factory.nested_progress_bar()
89
for i, (name, ref) in enumerate(refs.iteritems()):
90
if name.startswith("refs/tags/"):
120
for i, (name, sha) in enumerate(refs_dict.iteritems()):
122
branch_name = ref_to_branch_name(name)
124
# Not a branch, ignore
92
pb.update("creating branches", i, len(refs))
93
head_loc = os.path.join(dest_location, name)
95
head_bzrdir = BzrDir.open(head_loc)
96
except NotBranchError:
97
parent_path = urlutils.dirname(head_loc)
98
if not os.path.isdir(parent_path):
99
os.makedirs(parent_path)
100
head_bzrdir = BzrDir.create(head_loc)
102
head_branch = head_bzrdir.open_branch()
103
except NotBranchError:
104
head_branch = head_bzrdir.create_branch()
105
revid = mapping.revision_id_foreign_to_bzr(ref)
126
pb.update(gettext("creating branches"), i, len(refs_dict))
127
if getattr(target_bzrdir._format, "colocated_branches", False):
130
head_branch = self._get_colocated_branch(target_bzrdir, branch_name)
132
head_branch = self._get_nested_branch(dest_transport, dest_format, branch_name)
133
revid = mapping.revision_id_foreign_to_bzr(sha)
106
134
source_branch = GitBranch(source_repo.bzrdir, source_repo,
108
source_branch.head = ref
136
source_branch.head = sha
109
137
if head_branch.last_revision() != revid:
110
138
head_branch.generate_revision_history(revid)
111
139
source_branch.tags.merge_to(head_branch.tags)
140
if not head_branch.get_parent():
141
url = urlutils.join_segment_parameters(
142
source_branch.base, {"ref": urllib.quote(name, '')})
143
head_branch.set_parent(url)
147
"Use 'bzr checkout' to create a working tree in "
148
"the newly created branches."))
116
151
class cmd_git_object(Command):
138
173
from bzrlib.bzrdir import (
176
from bzrlib.plugins.git.object_store import (
179
from bzrlib.plugins.git import gettext
141
180
bzrdir, _ = BzrDir.open_containing(directory)
142
181
repo = bzrdir.find_repository()
143
from bzrlib.plugins.git.object_store import (
146
182
object_store = get_object_store(repo)
183
object_store.lock_read()
149
185
if sha1 is not None:
151
187
obj = object_store[str(sha1)]
153
raise BzrCommandError("Object not found: %s" % sha1)
189
raise BzrCommandError(gettext("Object not found: %s") % sha1)
155
191
text = obj.as_pretty_string()
182
218
from bzrlib.plugins.git.refs import (
185
221
from bzrlib.plugins.git.object_store import (
186
222
get_object_store,
188
224
bzrdir, _ = BzrDir.open_containing(directory)
189
225
repo = bzrdir.find_repository()
226
object_store = get_object_store(repo)
227
object_store.lock_read()
192
object_store = get_object_store(repo)
193
refs = BazaarRefsContainer(bzrdir, object_store)
229
refs = get_refs_container(bzrdir)
194
230
for k, v in refs.as_dict().iteritems():
195
231
self.outf.write("%s -> %s\n" % (k, v))
233
object_store.unlock()
200
236
class cmd_git_apply(Command):
244
Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
246
help='Apply patches even if tree has uncommitted changes.')
207
248
takes_args = ["patches*"]
209
def _apply_patch(self, wt, f):
250
def _apply_patch(self, wt, f, signoff):
253
:param wt: A Bazaar working tree object.
254
:param f: Patch file to read.
255
:param signoff: Add Signed-Off-By flag.
257
from bzrlib.errors import BzrCommandError
210
258
from dulwich.patch import git_am_patch_split
211
260
(c, diff, version) = git_am_patch_split(f)
212
# FIXME: Process diff
213
wt.commit(committer=c.committer,
261
# FIXME: Cope with git-specific bits in patch
262
# FIXME: Add new files to working tree
263
p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE,
268
raise BzrCommandError(gettext("error running patch"))
271
signed_off_by = wt.branch.get_config().username()
272
message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
273
wt.commit(authors=[c.author], message=message)
216
def run(self, patches_list=None):
275
def run(self, patches_list=None, signoff=False, force=False):
276
from bzrlib.errors import UncommittedChanges
217
277
from bzrlib.workingtree import WorkingTree
218
278
if patches_list is None:
219
279
patches_list = []
221
281
tree, _ = WorkingTree.open_containing(".")
282
if tree.basis_tree().changes_from(tree).has_changed() and not force:
283
raise UncommittedChanges(tree)
222
284
tree.lock_write()
224
286
for patch in patches_list:
225
287
f = open(patch, 'r')
227
self._apply_patch(tree, f)
289
self._apply_patch(tree, f, signoff=signoff)