92
95
"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)
95
120
class BzrGitMapping(foreign.VcsMapping):
96
121
"""Class that maps between Git and Bazaar semantics."""
97
122
experimental = False
99
BZR_FILE_IDS_FILE = None
124
BZR_FILE_IDS_FILE = '.bzrfileids'
101
BZR_DUMMY_FILE = None
126
BZR_DUMMY_FILE = '.bzrdummy'
103
128
def __init__(self):
104
129
super(BzrGitMapping, self).__init__(foreign_git)
106
131
def __eq__(self, other):
107
return (type(self) == type(other) and
132
return (type(self) == type(other) and
108
133
self.revid_prefix == other.revid_prefix)
127
152
# We must just hope they are valid UTF-8..
130
if type(path) is unicode:
131
path = path.encode("utf-8")
132
155
return escape_file_id(path)
134
157
def is_control_file(self, path):
186
209
def _extract_git_svn_metadata(self, rev, message):
187
210
lines = message.split("\n")
188
if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
211
if not (lines[-1] == "" and lines[-2].startswith("git-svn-id:")):
190
213
git_svn_id = lines[-2].split(": ", 1)[1]
191
214
rev.properties['git-svn-id'] = git_svn_id
211
234
return message, metadata
213
236
def _decode_commit_message(self, rev, message, encoding):
214
return message.decode(encoding), BzrGitRevisionMetadata()
237
message, metadata = self._extract_bzr_metadata(rev, message)
238
return message.decode(encoding), metadata
216
240
def _encode_commit_message(self, rev, message, encoding):
217
241
return message.encode(encoding)
227
251
b.set_raw_chunks(serialize_fileid_map(fileid_map))
230
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip,
254
def export_commit(self, rev, tree_sha, parent_lookup, roundtrip):
232
255
"""Turn a Bazaar revision in to a Git commit
234
257
:param tree_sha: Tree sha for the commit
235
258
: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
239
260
:return dulwich.objects.Commit represent the revision:
241
262
from dulwich.objects import Commit
256
275
metadata.explicit_parent_ids = rev.parent_ids
257
276
if git_p is not None:
258
277
assert len(git_p) == 40, "unexpected length for %r" % git_p
259
parents.append(git_p)
260
commit.parents = parents
278
commit.parents.append(git_p)
262
280
encoding = rev.properties['git-explicit-encoding']
281
299
commit.author_timezone = commit.commit_timezone
282
300
commit.message = self._encode_commit_message(rev, rev.message,
284
assert type(commit.message) == str
285
302
if metadata is not None:
287
304
mapping_registry.parse_revision_id(rev.revision_id)
294
311
for k, v in rev.properties.iteritems():
295
312
if not k in mapping_properties:
296
313
metadata.properties[k] = v
297
if self.roundtripping:
298
commit.message = inject_bzr_metadata(commit.message, metadata,
300
assert type(commit.message) == str
314
commit.message = inject_bzr_metadata(commit.message, metadata)
303
317
def import_fileid_map(self, blob):
309
323
return deserialize_fileid_map(blob.data)
311
def import_commit(self, commit, lookup_parent_revid):
325
def import_commit(self, commit):
312
326
"""Convert a git commit to a bzr revision.
314
:return: a `bzrlib.revision.Revision` object, foreign revid and a
328
:return: a `bzrlib.revision.Revision` object and a
329
dictionary of path -> file ids
317
331
if commit is None:
318
332
raise AssertionError("Commit object can't be None")
319
333
rev = ForeignRevision(commit.id, self,
320
334
self.revision_id_foreign_to_bzr(commit.id))
321
rev.parent_ids = tuple([lookup_parent_revid(p) for p in commit.parents])
335
rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
322
336
rev.git_metadata = None
323
337
def decode_using_encoding(rev, commit, encoding):
324
338
rev.committer = str(commit.committer).decode(encoding)
351
365
rev.timezone = commit.commit_timezone
352
366
if rev.git_metadata is not None:
353
367
md = rev.git_metadata
354
roundtrip_revid = md.revision_id
369
rev.revision_id = md.revision_id
355
370
if md.explicit_parent_ids:
356
371
rev.parent_ids = md.explicit_parent_ids
357
372
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)
380
376
class BzrGitMappingv1(BzrGitMapping):
388
384
class BzrGitMappingExperimental(BzrGitMappingv1):
389
385
revid_prefix = 'git-experimental'
390
386
experimental = True
393
BZR_FILE_IDS_FILE = '.bzrfileids'
395
BZR_DUMMY_FILE = '.bzrdummy'
397
388
def _decode_commit_message(self, rev, message, encoding):
398
389
message = self._extract_hg_metadata(rev, message)
406
397
ret += self._generate_git_svn_metadata(rev, encoding)
409
def import_commit(self, commit, lookup_parent_revid):
410
rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
400
def import_commit(self, commit):
401
rev, file_ids = super(BzrGitMappingExperimental, self).import_commit(commit)
411
402
rev.properties['converted_revision'] = "git %s\n" % commit.id
412
return rev, roundtrip_revid, verifiers
415
406
class GitMappingRegistry(VcsMappingRegistry):
581
572
self.mapping = mapping
583
574
def lookup_file_id(self, path):
584
assert type(path) is str
586
file_id = self.file_ids[path]
576
return self.file_ids[path]
588
file_id = self.mapping.generate_file_id(path)
589
assert type(file_id) is str
578
return self.mapping.generate_file_id(path)
592
580
def lookup_path(self, file_id):
593
581
if self.paths is None:
595
583
for k, v in self.file_ids.iteritems():
596
584
self.paths[v] = k
598
path = self.paths[file_id]
586
return self.paths[file_id]
600
588
return self.mapping.parse_file_id(file_id)
602
assert type(path) is str