37
41
takes_args = ["src_location", "dest_location?"]
39
def run(self, src_location, dest_location=None):
44
Option('colocated', help='Create colocated branches.'),
45
RegistryOption('dest-format',
46
help='Specify a format for this branch. '
47
'See "help formats" for a full list.',
48
lazy_registry=('breezy.controldir', 'format_registry'),
49
converter=lambda name: controldir.format_registry.make_controldir(
52
title="Branch format",
56
def _get_colocated_branch(self, target_controldir, name):
57
from ..errors import NotBranchError
59
return target_controldir.open_branch(name=name)
60
except NotBranchError:
61
return target_controldir.create_branch(name=name)
63
def _get_nested_branch(self, dest_transport, dest_format, name):
64
from ..controldir import ControlDir
65
from ..errors import NotBranchError
66
head_transport = dest_transport.clone(name)
68
head_controldir = ControlDir.open_from_transport(head_transport)
69
except NotBranchError:
70
head_controldir = dest_format.initialize_on_transport_ex(
71
head_transport, create_prefix=True)[1]
73
return head_controldir.open_branch()
74
except NotBranchError:
75
return head_controldir.create_branch()
77
def run(self, src_location, dest_location=None, colocated=False, dest_format=None):
45
from bzrlib.bzrdir import (
85
from ..controldir import (
48
from bzrlib.errors import (
88
from ..errors import (
50
91
NoRepositoryPresent,
53
from bzrlib.repository import (
94
from ..i18n import gettext
95
from ..repository import (
57
from bzrlib.plugins.git.branch import (
61
from bzrlib.plugins.git.repository import GitRepository
99
from ..transport import get_transport
100
from .branch import (
106
from .repository import GitRepository
108
if dest_format is None:
109
dest_format = controldir.format_registry.make_controldir('default')
63
111
if dest_location is None:
64
112
dest_location = os.path.basename(src_location.rstrip("/\\"))
114
dest_transport = get_transport(dest_location)
66
116
source_repo = Repository.open(src_location)
67
117
if not isinstance(source_repo, GitRepository):
68
raise BzrCommandError("%r is not a git repository" % src_location)
119
gettext("%r is not a git repository") % src_location)
70
target_bzrdir = BzrDir.open(dest_location)
121
target_controldir = ControlDir.open_from_transport(dest_transport)
71
122
except NotBranchError:
72
target_bzrdir = BzrDir.create(dest_location)
123
target_controldir = dest_format.initialize_on_transport_ex(
124
dest_transport, shared_repo=True)[1]
74
target_repo = target_bzrdir.find_repository()
126
target_repo = target_controldir.find_repository()
75
127
except NoRepositoryPresent:
76
target_repo = target_bzrdir.create_repository(shared=True)
128
target_repo = target_controldir.create_repository(shared=True)
78
130
if not target_repo.supports_rich_root():
79
raise BzrCommandError("Target repository doesn't support rich roots")
132
gettext("Target repository doesn't support rich roots"))
81
134
interrepo = InterRepository.get(source_repo, target_repo)
82
135
mapping = source_repo.get_mapping()
83
refs = interrepo.fetch()
85
for k, v in extract_tags(refs).iteritems():
86
tags[k] = mapping.revision_id_foreign_to_bzr(v)
87
pb = ui.ui_factory.nested_progress_bar()
89
for i, (name, ref) in enumerate(refs.iteritems()):
90
if name.startswith("refs/tags/"):
136
result = interrepo.fetch()
137
with ui.ui_factory.nested_progress_bar() as pb:
138
for i, (name, sha) in enumerate(result.refs.items()):
140
branch_name = ref_to_branch_name(name)
142
# 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)
106
source_branch = GitBranch(source_repo.bzrdir, source_repo,
108
source_branch.head = ref
144
pb.update(gettext("creating branches"), i, len(result.refs))
145
if (getattr(target_controldir._format, "colocated_branches",
146
False) and colocated):
149
head_branch = self._get_colocated_branch(
150
target_controldir, branch_name)
152
head_branch = self._get_nested_branch(
153
dest_transport, dest_format, branch_name)
154
revid = mapping.revision_id_foreign_to_bzr(sha)
155
source_branch = LocalGitBranch(
156
source_repo.controldir, source_repo, sha)
109
157
if head_branch.last_revision() != revid:
110
158
head_branch.generate_revision_history(revid)
111
159
source_branch.tags.merge_to(head_branch.tags)
160
if not head_branch.get_parent():
161
url = urlutils.join_segment_parameters(
163
{"branch": urlutils.escape(branch_name)})
164
head_branch.set_parent(url)
166
"Use 'bzr checkout' to create a working tree in "
167
"the newly created branches."))
116
170
class cmd_git_object(Command):
125
179
aliases = ["git-objects", "git-cat"]
126
180
takes_args = ["sha1?"]
127
181
takes_options = [Option('directory',
129
help='Location of repository.', type=unicode),
130
Option('pretty', help='Pretty-print objects.')]
183
help='Location of repository.', type=str),
184
Option('pretty', help='Pretty-print objects.')]
131
185
encoding_type = 'exact'
134
188
def run(self, sha1=None, directory=".", pretty=False):
135
from bzrlib.errors import (
138
from bzrlib.bzrdir import (
141
bzrdir, _ = BzrDir.open_containing(directory)
142
repo = bzrdir.find_repository()
143
from bzrlib.plugins.git.object_store import (
189
from ..errors import (
192
from ..controldir import (
195
from .object_store import (
144
196
get_object_store,
198
from ..i18n import gettext
199
controldir, _ = ControlDir.open_containing(directory)
200
repo = controldir.find_repository()
146
201
object_store = get_object_store(repo)
202
with object_store.lock_read():
149
203
if sha1 is not None:
151
obj = object_store[str(sha1)]
205
obj = object_store[sha1.encode('ascii')]
153
raise BzrCommandError("Object not found: %s" % sha1)
208
gettext("Object not found: %s") % sha1)
155
210
text = obj.as_pretty_string()
173
takes_options = [Option('directory',
175
help='Location of repository.', type=unicode)]
226
takes_args = ["location?"]
178
def run(self, directory="."):
179
from bzrlib.bzrdir import (
182
from bzrlib.plugins.git.refs import (
185
from bzrlib.plugins.git.object_store import (
229
def run(self, location="."):
230
from ..controldir import (
236
from .object_store import (
186
237
get_object_store,
188
bzrdir, _ = BzrDir.open_containing(directory)
189
repo = bzrdir.find_repository()
192
object_store = get_object_store(repo)
193
refs = BazaarRefsContainer(bzrdir, object_store)
194
for k, v in refs.as_dict().iteritems():
195
self.outf.write("%s -> %s\n" % (k, v))
239
controldir, _ = ControlDir.open_containing(location)
240
repo = controldir.find_repository()
241
object_store = get_object_store(repo)
242
with object_store.lock_read():
243
refs = get_refs_container(controldir, object_store)
244
for k, v in sorted(refs.as_dict().items()):
245
self.outf.write("%s -> %s\n" %
246
(k.decode('utf-8'), v.decode('utf-8')))
200
249
class cmd_git_apply(Command):
201
250
"""Apply a series of git-am style patches.
203
This command will in the future probably be integrated into
252
This command will in the future probably be integrated into "bzr pull".
256
Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
258
help='Apply patches even if tree has uncommitted changes.')
207
260
takes_args = ["patches*"]
209
def _apply_patch(self, wt, f):
262
def _apply_patch(self, wt, f, signoff):
265
:param wt: A Bazaar working tree object.
266
:param f: Patch file to read.
267
:param signoff: Add Signed-Off-By flag.
210
269
from dulwich.patch import git_am_patch_split
270
from breezy.patch import patch_tree
211
271
(c, diff, version) = git_am_patch_split(f)
212
# FIXME: Process diff
213
wt.commit(committer=c.committer,
272
# FIXME: Cope with git-specific bits in patch
273
# FIXME: Add new files to working tree
274
patch_tree(wt, [diff], strip=1, out=self.outf)
275
message = c.message.decode('utf-8')
277
signed_off_by = wt.branch.get_config().username()
278
message += "Signed-off-by: %s\n" % (signed_off_by, )
279
wt.commit(authors=[c.author.decode('utf-8')], message=message)
216
def run(self, patches_list=None):
217
from bzrlib.workingtree import WorkingTree
281
def run(self, patches_list=None, signoff=False, force=False):
282
from ..errors import UncommittedChanges
283
from ..workingtree import WorkingTree
218
284
if patches_list is None:
219
285
patches_list = []
221
287
tree, _ = WorkingTree.open_containing(".")
288
if tree.basis_tree().changes_from(tree).has_changed() and not force:
289
raise UncommittedChanges(tree)
290
with tree.lock_write():
224
291
for patch in patches_list:
227
self._apply_patch(tree, f)
292
with open(patch, 'r') as f:
293
self._apply_patch(tree, f, signoff=signoff)
296
class cmd_git_push_pristine_tar_deltas(Command):
297
"""Push pristine tar deltas to a git repository."""
299
takes_options = [Option('directory',
301
help='Location of repository.', type=str)]
302
takes_args = ['target', 'package']
304
def run(self, target, package, directory='.'):
305
from ..branch import Branch
306
from ..errors import (
310
from ..trace import warning
311
from ..repository import Repository
312
from .object_store import get_object_store
313
from .pristine_tar import (
314
revision_pristine_tar_data,
315
store_git_pristine_tar_data,
317
source = Branch.open_containing(directory)[0]
318
target_bzr = Repository.open(target)
319
target = getattr(target_bzr, '_git', None)
321
raise CommandError("Target not a git repository")
322
git_store = get_object_store(source.repository)
323
with git_store.lock_read():
324
tag_dict = source.tags.get_tag_dict()
325
for name, revid in tag_dict.iteritems():
327
rev = source.repository.get_revision(revid)
328
except NoSuchRevision:
331
delta, kind = revision_pristine_tar_data(rev)
334
gitid = git_store._lookup_revision_sha1(revid)
335
if (not (name.startswith('upstream/') or
336
name.startswith('upstream-'))):
338
"Unexpected pristine tar revision tagged %s. "
341
upstream_version = name[len("upstream/"):]
342
filename = '%s_%s.orig.tar.%s' % (
343
package, upstream_version, kind)
344
if gitid not in target:
346
"base git id %s for %s missing in target repository",
348
store_git_pristine_tar_data(target, filename.encode('utf-8'),