37
45
takes_args = ["src_location", "dest_location?"]
39
def run(self, src_location, dest_location=None):
48
Option('colocated', help='Create colocated branches.'),
51
def _get_colocated_branch(self, target_controldir, name):
52
from ..errors import NotBranchError
54
return target_controldir.open_branch(name=name)
55
except NotBranchError:
56
return target_controldir.create_branch(name=name)
58
def _get_nested_branch(self, dest_transport, dest_format, name):
59
from ..controldir import ControlDir
60
from ..errors import NotBranchError
61
head_transport = dest_transport.clone(name)
63
head_controldir = ControlDir.open_from_transport(head_transport)
64
except NotBranchError:
65
head_controldir = dest_format.initialize_on_transport_ex(
66
head_transport, create_prefix=True)[1]
68
return head_controldir.open_branch()
69
except NotBranchError:
70
return head_controldir.create_branch()
72
def run(self, src_location, dest_location=None, colocated=False):
45
from bzrlib.bzrdir import (
80
from ..controldir import (
48
from bzrlib.errors import (
83
from ..errors import (
50
86
NoRepositoryPresent,
53
from bzrlib.repository import (
89
from ..i18n import gettext
90
from ..repository import (
57
from bzrlib.plugins.git.branch import (
61
from bzrlib.plugins.git.repository import GitRepository
94
from ..transport import get_transport
101
from .repository import GitRepository
103
dest_format = controldir.ControlDirFormat.get_default_format()
104
if dest_format is None:
105
raise BzrError('no default format')
63
107
if dest_location is None:
64
108
dest_location = os.path.basename(src_location.rstrip("/\\"))
110
dest_transport = get_transport(dest_location)
66
112
source_repo = Repository.open(src_location)
67
113
if not isinstance(source_repo, GitRepository):
68
raise BzrCommandError("%r is not a git repository" % src_location)
114
raise BzrCommandError(
115
gettext("%r is not a git repository") % src_location)
70
target_bzrdir = BzrDir.open(dest_location)
117
target_controldir = ControlDir.open_from_transport(dest_transport)
71
118
except NotBranchError:
72
target_bzrdir = BzrDir.create(dest_location)
119
target_controldir = dest_format.initialize_on_transport_ex(
120
dest_transport, shared_repo=True)[1]
74
target_repo = target_bzrdir.find_repository()
122
target_repo = target_controldir.find_repository()
75
123
except NoRepositoryPresent:
76
target_repo = target_bzrdir.create_repository(shared=True)
124
target_repo = target_controldir.create_repository(shared=True)
78
126
if not target_repo.supports_rich_root():
79
raise BzrCommandError("Target repository doesn't support rich roots")
127
raise BzrCommandError(
128
gettext("Target repository doesn't support rich roots"))
81
130
interrepo = InterRepository.get(source_repo, target_repo)
82
131
mapping = source_repo.get_mapping()
83
132
refs = interrepo.fetch()
85
for k, v in extract_tags(refs).iteritems():
86
tags[k] = mapping.revision_id_foreign_to_bzr(v)
87
133
pb = ui.ui_factory.nested_progress_bar()
89
for i, (name, ref) in enumerate(refs.iteritems()):
90
if name.startswith("refs/tags/"):
135
for i, (name, sha) in enumerate(viewitems(refs)):
137
branch_name = ref_to_branch_name(name)
139
# 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
141
pb.update(gettext("creating branches"), i, len(refs))
142
if (getattr(target_controldir._format, "colocated_branches",
143
False) and colocated):
146
head_branch = self._get_colocated_branch(
147
target_controldir, branch_name)
149
head_branch = self._get_nested_branch(
150
dest_transport, dest_format, branch_name)
151
revid = mapping.revision_id_foreign_to_bzr(sha)
152
source_branch = LocalGitBranch(
153
source_repo.controldir, source_repo, sha)
109
154
if head_branch.last_revision() != revid:
110
155
head_branch.generate_revision_history(revid)
111
156
source_branch.tags.merge_to(head_branch.tags)
157
if not head_branch.get_parent():
158
url = urlutils.join_segment_parameters(
160
{"branch": urlutils.escape(branch_name)})
161
head_branch.set_parent(url)
165
"Use 'bzr checkout' to create a working tree in "
166
"the newly created branches."))
116
169
class cmd_git_object(Command):
125
178
aliases = ["git-objects", "git-cat"]
126
179
takes_args = ["sha1?"]
127
180
takes_options = [Option('directory',
129
help='Location of repository.', type=unicode),
130
Option('pretty', help='Pretty-print objects.')]
182
help='Location of repository.', type=text_type),
183
Option('pretty', help='Pretty-print objects.')]
131
184
encoding_type = 'exact'
134
187
def run(self, sha1=None, directory=".", pretty=False):
135
from bzrlib.errors import (
188
from ..errors import (
138
from bzrlib.bzrdir import (
191
from ..controldir import (
141
bzrdir, _ = BzrDir.open_containing(directory)
142
repo = bzrdir.find_repository()
143
from bzrlib.plugins.git.object_store import (
194
from .object_store import (
144
195
get_object_store,
197
from ..i18n import gettext
198
controldir, _ = ControlDir.open_containing(directory)
199
repo = controldir.find_repository()
146
200
object_store = get_object_store(repo)
201
with object_store.lock_read():
149
202
if sha1 is not None:
151
obj = object_store[str(sha1)]
204
obj = object_store[sha1.encode('ascii')]
153
raise BzrCommandError("Object not found: %s" % sha1)
206
raise BzrCommandError(
207
gettext("Object not found: %s") % sha1)
155
209
text = obj.as_pretty_string()
173
takes_options = [Option('directory',
175
help='Location of repository.', type=unicode)]
225
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 (
228
def run(self, location="."):
229
from ..controldir import (
235
from .object_store import (
186
236
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))
238
controldir, _ = ControlDir.open_containing(location)
239
repo = controldir.find_repository()
240
object_store = get_object_store(repo)
241
with object_store.lock_read():
242
refs = get_refs_container(controldir, object_store)
243
for k, v in sorted(viewitems(refs.as_dict())):
244
self.outf.write("%s -> %s\n" %
245
(k.decode('utf-8'), v.decode('utf-8')))
200
248
class cmd_git_apply(Command):
201
249
"""Apply a series of git-am style patches.
203
This command will in the future probably be integrated into
251
This command will in the future probably be integrated into "bzr pull".
255
Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
257
help='Apply patches even if tree has uncommitted changes.')
207
259
takes_args = ["patches*"]
209
def _apply_patch(self, wt, f):
261
def _apply_patch(self, wt, f, signoff):
264
:param wt: A Bazaar working tree object.
265
:param f: Patch file to read.
266
:param signoff: Add Signed-Off-By flag.
210
268
from dulwich.patch import git_am_patch_split
269
from breezy.patch import patch_tree
211
270
(c, diff, version) = git_am_patch_split(f)
212
# FIXME: Process diff
213
wt.commit(committer=c.committer,
271
# FIXME: Cope with git-specific bits in patch
272
# FIXME: Add new files to working tree
273
patch_tree(wt, [diff], strip=1, out=self.outf)
274
message = c.message.decode('utf-8')
276
signed_off_by = wt.branch.get_config().username()
277
message += "Signed-off-by: %s\n" % (signed_off_by, )
278
wt.commit(authors=[c.author.decode('utf-8')], message=message)
216
def run(self, patches_list=None):
217
from bzrlib.workingtree import WorkingTree
280
def run(self, patches_list=None, signoff=False, force=False):
281
from ..errors import UncommittedChanges
282
from ..workingtree import WorkingTree
218
283
if patches_list is None:
219
284
patches_list = []
221
286
tree, _ = WorkingTree.open_containing(".")
287
if tree.basis_tree().changes_from(tree).has_changed() and not force:
288
raise UncommittedChanges(tree)
289
with tree.lock_write():
224
290
for patch in patches_list:
227
self._apply_patch(tree, f)
291
with open(patch, 'r') as f:
292
self._apply_patch(tree, f, signoff=signoff)
295
class cmd_git_push_pristine_tar_deltas(Command):
296
"""Push pristine tar deltas to a git repository."""
298
takes_options = [Option('directory',
300
help='Location of repository.', type=text_type)]
301
takes_args = ['target', 'package']
303
def run(self, target, package, directory='.'):
304
from ..branch import Branch
305
from ..errors import (
309
from ..trace import warning
310
from ..repository import Repository
311
from .object_store import get_object_store
312
from .pristine_tar import (
313
revision_pristine_tar_data,
314
store_git_pristine_tar_data,
316
source = Branch.open_containing(directory)[0]
317
target_bzr = Repository.open(target)
318
target = getattr(target_bzr, '_git', None)
320
raise BzrCommandError("Target not a git repository")
321
git_store = get_object_store(source.repository)
322
with git_store.lock_read():
323
tag_dict = source.tags.get_tag_dict()
324
for name, revid in tag_dict.iteritems():
326
rev = source.repository.get_revision(revid)
327
except NoSuchRevision:
330
delta, kind = revision_pristine_tar_data(rev)
333
gitid = git_store._lookup_revision_sha1(revid)
334
if (not (name.startswith('upstream/') or
335
name.startswith('upstream-'))):
337
"Unexpected pristine tar revision tagged %s. "
340
upstream_version = name[len("upstream/"):]
341
filename = '%s_%s.orig.tar.%s' % (
342
package, upstream_version, kind)
343
if gitid not in target:
345
"base git id %s for %s missing in target repository",
347
store_git_pristine_tar_data(target, filename.encode('utf-8'),