95
92
"property. ", mode, path, commit)
98
def squash_revision(target_repo, rev):
99
"""Remove characters that can't be stored from a revision, if necessary.
101
:param target_repo: Repository in which the revision will be stored
102
:param rev: Revision object, will be modified in-place
104
if not getattr(target_repo._serializer, "squashes_xml_invalid_characters", True):
106
from bzrlib.xml_serializer import escape_invalid_chars
107
rev.message, num_escaped = escape_invalid_chars(rev.message)
109
warn_escaped(rev.foreign_revid, num_escaped)
110
if 'author' in rev.properties:
111
rev.properties['author'], num_escaped = escape_invalid_chars(
112
rev.properties['author'])
114
warn_escaped(rev.foreign_revid, num_escaped)
115
rev.committer, num_escaped = escape_invalid_chars(rev.committer)
117
warn_escaped(rev.foreign_revid, num_escaped)
120
95
class BzrGitMapping(foreign.VcsMapping):
121
96
"""Class that maps between Git and Bazaar semantics."""
122
97
experimental = False
124
BZR_FILE_IDS_FILE = '.bzrfileids'
99
BZR_FILE_IDS_FILE = None
126
BZR_DUMMY_FILE = '.bzrdummy'
101
BZR_DUMMY_FILE = None
128
103
def __init__(self):
129
104
super(BzrGitMapping, self).__init__(foreign_git)
131
106
def __eq__(self, other):
132
return (type(self) == type(other) and
107
return (type(self) == type(other) and
133
108
self.revid_prefix == other.revid_prefix)
152
127
# We must just hope they are valid UTF-8..
130
if type(path) is unicode:
131
path = path.encode("utf-8")
155
132
return escape_file_id(path)
157
134
def is_control_file(self, path):
209
186
def _extract_git_svn_metadata(self, rev, message):
210
187
lines = message.split("\n")
211
if not (lines[-1] == "" and lines[-2].startswith("git-svn-id:")):
188
if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
213
190
git_svn_id = lines[-2].split(": ", 1)[1]
214
191
rev.properties['git-svn-id'] = git_svn_id
234
211
return message, metadata
236
213
def _decode_commit_message(self, rev, message, encoding):
237
message, metadata = self._extract_bzr_metadata(rev, message)
238
return message.decode(encoding), metadata
214
return message.decode(encoding), BzrGitRevisionMetadata()
240
216
def _encode_commit_message(self, rev, message, encoding):
241
217
return message.encode(encoding)
251
227
b.set_raw_chunks(serialize_fileid_map(fileid_map))
254
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip):
230
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip,
255
232
"""Turn a Bazaar revision in to a Git commit
257
234
:param tree_sha: Tree sha for the commit
258
235
:param parent_lookup: Function for looking up the GIT sha equiv of a
237
:param roundtrip: Whether to store roundtripping information.
238
:param verifiers: Verifiers info
260
239
:return dulwich.objects.Commit represent the revision:
262
241
from dulwich.objects import Commit
275
256
metadata.explicit_parent_ids = rev.parent_ids
276
257
if git_p is not None:
277
258
assert len(git_p) == 40, "unexpected length for %r" % git_p
278
commit.parents.append(git_p)
259
parents.append(git_p)
260
commit.parents = parents
280
262
encoding = rev.properties['git-explicit-encoding']
312
294
for k, v in rev.properties.iteritems():
313
295
if not k in mapping_properties:
314
296
metadata.properties[k] = v
315
commit.message = inject_bzr_metadata(commit.message, metadata,
297
if self.roundtripping:
298
commit.message = inject_bzr_metadata(commit.message, metadata,
317
300
assert type(commit.message) == str
326
309
return deserialize_fileid_map(blob.data)
328
def import_commit(self, commit):
311
def import_commit(self, commit, lookup_parent_revid):
329
312
"""Convert a git commit to a bzr revision.
331
:return: a `bzrlib.revision.Revision` object and a
332
dictionary of path -> file ids
314
:return: a `bzrlib.revision.Revision` object, foreign revid and a
334
317
if commit is None:
335
318
raise AssertionError("Commit object can't be None")
336
319
rev = ForeignRevision(commit.id, self,
337
320
self.revision_id_foreign_to_bzr(commit.id))
338
rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
321
rev.parent_ids = tuple([lookup_parent_revid(p) for p in commit.parents])
339
322
rev.git_metadata = None
340
323
def decode_using_encoding(rev, commit, encoding):
341
324
rev.committer = str(commit.committer).decode(encoding)
368
351
rev.timezone = commit.commit_timezone
369
352
if rev.git_metadata is not None:
370
353
md = rev.git_metadata
372
rev.revision_id = md.revision_id
354
roundtrip_revid = md.revision_id
373
355
if md.explicit_parent_ids:
374
356
rev.parent_ids = md.explicit_parent_ids
375
357
rev.properties.update(md.properties)
358
verifiers = md.verifiers
360
roundtrip_revid = None
362
return rev, roundtrip_revid, verifiers
364
def get_fileid_map(self, lookup_object, tree_sha):
365
"""Obtain a fileid map for a particular tree.
367
:param lookup_object: Function for looking up an object
368
:param tree_sha: SHA of the root tree
369
:return: GitFileIdMap instance
372
file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
376
file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
377
return GitFileIdMap(file_ids, self)
379
380
class BzrGitMappingv1(BzrGitMapping):
387
388
class BzrGitMappingExperimental(BzrGitMappingv1):
388
389
revid_prefix = 'git-experimental'
389
390
experimental = True
393
BZR_FILE_IDS_FILE = '.bzrfileids'
395
BZR_DUMMY_FILE = '.bzrdummy'
391
397
def _decode_commit_message(self, rev, message, encoding):
392
398
message = self._extract_hg_metadata(rev, message)
400
406
ret += self._generate_git_svn_metadata(rev, encoding)
403
def import_commit(self, commit):
404
rev, file_ids = super(BzrGitMappingExperimental, self).import_commit(commit)
409
def import_commit(self, commit, lookup_parent_revid):
410
rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
405
411
rev.properties['converted_revision'] = "git %s\n" % commit.id
412
return rev, roundtrip_revid, verifiers
409
415
class GitMappingRegistry(VcsMappingRegistry):
575
581
self.mapping = mapping
577
583
def lookup_file_id(self, path):
584
assert type(path) is str
579
return self.file_ids[path]
586
file_id = self.file_ids[path]
581
return self.mapping.generate_file_id(path)
588
file_id = self.mapping.generate_file_id(path)
589
assert type(file_id) is str
583
592
def lookup_path(self, file_id):
584
593
if self.paths is None:
586
595
for k, v in self.file_ids.iteritems():
587
596
self.paths[v] = k
589
return self.paths[file_id]
598
path = self.paths[file_id]
591
600
return self.mapping.parse_file_id(file_id)
602
assert type(path) is str