145
150
def export_unusual_file_modes(self, rev):
147
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(rev.properties['file-modes'])])
152
return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(rev.properties['file-modes'].encode("utf-8"))])
156
def _generate_git_svn_metadata(self, rev):
158
return "\ngit-svn-id: %s\n" % rev.properties["git-svn-id"].encode("utf-8")
162
def _generate_hg_message_tail(self, rev):
166
for name in rev.properties:
167
if name == 'hg:extra:branch':
168
branch = rev.properties['hg:extra:branch']
169
elif name.startswith('hg:extra'):
170
extra[name[len('hg:extra:'):]] = base64.b64decode(rev.properties[name])
171
elif name == 'hg:renames':
172
renames = bencode.bdecode(base64.b64decode(rev.properties['hg:renames']))
173
# TODO: Export other properties as 'bzr:' extras?
174
ret = format_hg_metadata(renames, branch, extra)
175
assert isinstance(ret, str)
178
def _extract_git_svn_metadata(self, rev, message):
179
lines = message.split("\n")
180
if not (lines[-1] == "" and lines[-2].startswith("git-svn-id:")):
182
git_svn_id = lines[-2].split(": ", 1)[1]
183
rev.properties['git-svn-id'] = git_svn_id
184
(url, rev, uuid) = parse_git_svn_id(git_svn_id)
185
# FIXME: Convert this to converted-from property somehow..
186
ret = "\n".join(lines[:-2])
187
assert isinstance(ret, str)
190
def _extract_hg_metadata(self, rev, message):
191
(message, renames, branch, extra) = extract_hg_metadata(message)
192
if branch is not None:
193
rev.properties['hg:extra:branch'] = branch
194
for name, value in extra.iteritems():
195
rev.properties['hg:extra:' + name] = base64.b64encode(value)
197
rev.properties['hg:renames'] = base64.b64encode(bencode.bencode([(new, old) for (old, new) in renames.iteritems()]))
200
def _decode_commit_message(self, rev, message):
201
return message.decode("utf-8", "replace")
203
def _encode_commit_message(self, rev, message):
204
return message.encode("utf-8")
206
def export_commit(self, rev, tree_sha, parent_lookup):
207
"""Turn a Bazaar revision in to a Git commit
209
:param tree_sha: Tree sha for the commit
210
:param parent_lookup: Function for looking up the GIT sha equiv of a bzr revision
211
:return dulwich.objects.Commit represent the revision:
213
from dulwich.objects import Commit
215
commit.tree = tree_sha
216
for p in rev.parent_ids:
217
git_p = parent_lookup(p)
218
if git_p is not None:
219
assert len(git_p) == 40, "unexpected length for %r" % git_p
220
commit.parents.append(git_p)
221
commit.committer = fix_person_identifier(rev.committer.encode("utf-8"))
222
commit.author = fix_person_identifier(rev.get_apparent_authors()[0].encode("utf-8"))
223
commit.commit_time = long(rev.timestamp)
224
if 'author-timestamp' in rev.properties:
225
commit.author_time = long(rev.properties['author-timestamp'])
227
commit.author_time = commit.commit_time
228
commit.commit_timezone = rev.timezone
229
if 'author-timezone' in rev.properties:
230
commit.author_timezone = int(rev.properties['author-timezone'])
232
commit.author_timezone = commit.commit_timezone
233
commit.message = self._encode_commit_message(rev, rev.message)
151
236
def import_commit(self, commit):
152
237
"""Convert a git commit to a bzr revision.
183
268
revid_prefix = 'git-experimental'
184
269
experimental = True
271
def _decode_commit_message(self, rev, message):
272
message = self._extract_hg_metadata(rev, message)
273
message = self._extract_git_svn_metadata(rev, message)
274
return message.decode("utf-8", "replace")
276
def _encode_commit_message(self, rev, message):
277
ret = message.encode("utf-8")
278
ret += self._generate_hg_message_tail(rev)
279
ret += self._generate_git_svn_metadata(rev)
282
def import_commit(self, commit):
283
rev = super(BzrGitMappingExperimental, self).import_commit(commit)
284
rev.properties['converted_revision'] = "git %s\n" % commit.id
187
288
class GitMappingRegistry(VcsMappingRegistry):
188
289
"""Registry with available git mappings."""
202
303
"BzrGitMappingv1")
203
304
mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
204
305
"BzrGitMappingExperimental")
306
mapping_registry.set_default('git-v1')
207
309
class ForeignGit(ForeignVcs):
208
310
"""The Git Stupid Content Tracker"""
313
def branch_format(self):
314
from bzrlib.plugins.git.branch import GitBranchFormat
315
return GitBranchFormat()
318
def repository_format(self):
319
from bzrlib.plugins.git.repository import GitRepositoryFormat
320
return GitRepositoryFormat()
210
322
def __init__(self):
211
323
super(ForeignGit, self).__init__(mapping_registry)
324
self.abbreviation = "git"
327
def serialize_foreign_revid(self, foreign_revid):
214
331
def show_foreign_revid(cls, foreign_revid):
260
377
"Unknown kind, perms=%r." % (mode,))
263
def entry_mode(entry):
264
"""Determine the git file mode for an inventory entry."""
265
if entry.kind == 'directory':
380
def object_mode(kind, executable):
381
if kind == 'directory':
266
382
return stat.S_IFDIR
267
elif entry.kind == 'symlink':
383
elif kind == 'symlink':
268
384
return stat.S_IFLNK
269
elif entry.kind == 'file':
270
386
mode = stat.S_IFREG | 0644
390
elif kind == 'tree-reference':
391
from dulwich.objects import S_IFGITLINK
275
394
raise AssertionError
397
def entry_mode(entry):
398
"""Determine the git file mode for an inventory entry."""
399
return object_mode(entry.kind, entry.executable)
278
402
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
279
403
from dulwich.objects import Tree
353
482
yield tree.id, tree, cur.encode("utf-8")
356
def revision_to_commit(rev, tree_sha, parent_lookup):
357
"""Turn a Bazaar revision in to a Git commit
359
:param tree_sha: Tree sha for the commit
360
:param parent_lookup: Function for looking up the GIT sha equiv of a bzr revision
361
:return dulwich.objects.Commit represent the revision:
363
from dulwich.objects import Commit
365
commit.tree = tree_sha
366
for p in rev.parent_ids:
367
git_p = parent_lookup(p)
368
if git_p is not None:
369
assert len(git_p) == 40, "unexpected length for %r" % git_p
370
commit.parents.append(git_p)
371
commit.message = rev.message.encode("utf-8")
372
commit.committer = fix_person_identifier(rev.committer.encode("utf-8"))
373
commit.author = fix_person_identifier(rev.get_apparent_authors()[0].encode("utf-8"))
374
commit.commit_time = long(rev.timestamp)
375
if 'author-timestamp' in rev.properties:
376
commit.author_time = long(rev.properties['author-timestamp'])
378
commit.author_time = commit.commit_time
379
commit.commit_timezone = rev.timezone
380
if 'author-timezone' in rev.properties:
381
commit.author_timezone = int(rev.properties['author-timezone'])
383
commit.author_timezone = commit.commit_timezone
485
def parse_git_svn_id(text):
486
(head, uuid) = text.rsplit(" ", 1)
487
(full_url, rev) = head.rsplit("@", 1)
488
return (full_url, int(rev), uuid)