/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/mapping.py

  • Committer: Jelmer Vernooij
  • Date: 2019-09-21 17:08:09 UTC
  • mfrom: (7389 work)
  • mto: This revision was merged to the branch mainline in revision 7390.
  • Revision ID: jelmer@jelmer.uk-20190921170809-ejewbeue585deajo
merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
    extract_bzr_metadata,
58
58
    inject_bzr_metadata,
59
59
    CommitSupplement,
60
 
    deserialize_fileid_map,
61
 
    serialize_fileid_map,
62
60
    )
63
61
 
64
62
 
131
129
    """Class that maps between Git and Bazaar semantics."""
132
130
    experimental = False
133
131
 
134
 
    BZR_FILE_IDS_FILE = None
135
 
 
136
132
    BZR_DUMMY_FILE = None
137
133
 
138
134
    def is_special_file(self, filename):
139
 
        return (filename in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE))
 
135
        return (filename in (self.BZR_DUMMY_FILE, ))
140
136
 
141
137
    def __init__(self):
142
138
        super(BzrGitMapping, self).__init__(foreign_vcs_git)
257
253
    def _encode_commit_message(self, rev, message, encoding):
258
254
        return message.encode(encoding)
259
255
 
260
 
    def export_fileid_map(self, fileid_map):
261
 
        """Export a file id map to a fileid map.
262
 
 
263
 
        :param fileid_map: File id map, mapping paths to file ids
264
 
        :return: A Git blob object (or None if there are no entries)
265
 
        """
266
 
        from dulwich.objects import Blob
267
 
        b = Blob()
268
 
        b.set_raw_chunks(serialize_fileid_map(fileid_map))
269
 
        return b
270
 
 
271
256
    def export_commit(self, rev, tree_sha, parent_lookup, lossy,
272
257
                      verifiers):
273
258
        """Turn a Bazaar revision in to a Git commit
370
355
                 for l in rev.properties[u'git-extra'].splitlines()])
371
356
        return commit
372
357
 
373
 
    def import_fileid_map(self, blob):
374
 
        """Convert a git file id map blob.
375
 
 
376
 
        :param blob: Git blob object with fileid map
377
 
        :return: Dictionary mapping paths to file ids
378
 
        """
379
 
        return deserialize_fileid_map(blob.data)
380
 
 
381
358
    def get_revision_id(self, commit):
382
359
        if commit.encoding:
383
360
            encoding = commit.encoding.decode('ascii')
480
457
            rev.properties[u'git-extra'] = b''.join(extra_lines)
481
458
        return rev, roundtrip_revid, verifiers
482
459
 
483
 
    def get_fileid_map(self, lookup_object, tree_sha):
484
 
        """Obtain a fileid map for a particular tree.
485
 
 
486
 
        :param lookup_object: Function for looking up an object
487
 
        :param tree_sha: SHA of the root tree
488
 
        :return: GitFileIdMap instance
489
 
        """
490
 
        try:
491
 
            file_id_map_sha = lookup_object(
492
 
                tree_sha)[self.BZR_FILE_IDS_FILE][1]
493
 
        except KeyError:
494
 
            file_ids = {}
495
 
        else:
496
 
            file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
497
 
        return GitFileIdMap(file_ids, self)
498
 
 
499
460
 
500
461
class BzrGitMappingv1(BzrGitMapping):
501
462
    revid_prefix = b'git-v1'
508
469
class BzrGitMappingExperimental(BzrGitMappingv1):
509
470
    revid_prefix = b'git-experimental'
510
471
    experimental = True
511
 
    roundtripping = True
512
 
 
513
 
    BZR_FILE_IDS_FILE = '.bzrfileids'
 
472
    roundtripping = False
514
473
 
515
474
    BZR_DUMMY_FILE = '.bzrdummy'
516
475
 
674
633
    return (full_url, int(rev), uuid)
675
634
 
676
635
 
677
 
class GitFileIdMap(object):
678
 
 
679
 
    def __init__(self, file_ids, mapping):
680
 
        self.file_ids = file_ids
681
 
        self.paths = None
682
 
        self.mapping = mapping
683
 
 
684
 
    def set_file_id(self, path, file_id):
685
 
        if type(path) is not str:
686
 
            raise TypeError(path)
687
 
        if not isinstance(file_id, bytes):
688
 
            raise TypeError(file_id)
689
 
        self.file_ids[path] = file_id
690
 
 
691
 
    def lookup_file_id(self, path):
692
 
        if not isinstance(path, text_type):
693
 
            raise TypeError(path)
694
 
        try:
695
 
            file_id = self.file_ids[path]
696
 
        except KeyError:
697
 
            file_id = self.mapping.generate_file_id(path)
698
 
        if not isinstance(file_id, bytes):
699
 
            raise TypeError(file_id)
700
 
        return file_id
701
 
 
702
 
    def lookup_path(self, file_id):
703
 
        if self.paths is None:
704
 
            self.paths = {}
705
 
            for k, v in viewitems(self.file_ids):
706
 
                self.paths[v] = k
707
 
        try:
708
 
            path = self.paths[file_id]
709
 
        except KeyError:
710
 
            return self.mapping.parse_file_id(file_id)
711
 
        else:
712
 
            if not isinstance(path, text_type):
713
 
                raise TypeError(path)
714
 
            return path
715
 
 
716
 
    def copy(self):
717
 
        return self.__class__(dict(self.file_ids), self.mapping)
718
 
 
719
 
 
720
636
def needs_roundtripping(repo, revid):
721
637
    try:
722
638
        mapping_registry.parse_revision_id(revid)