/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/plugins/git/mapping.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-01 21:13:21 UTC
  • mto: This revision was merged to the branch mainline in revision 7027.
  • Revision ID: jelmer@jelmer.uk-20180701211321-dazu15ry3tcvny2l
Fix some git tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
    errors,
29
29
    foreign,
30
30
    trace,
 
31
    urlutils,
31
32
    )
32
33
from ...bzr.inventory import (
33
34
    ROOT_ID,
40
41
from ...revision import (
41
42
    NULL_REVISION,
42
43
    )
43
 
from ...sixish import text_type
 
44
from ...sixish import (
 
45
    text_type,
 
46
    viewitems,
 
47
    )
44
48
from .errors import (
45
49
    NoPushSupport,
46
50
    UnknownCommitExtra,
58
62
    serialize_fileid_map,
59
63
    )
60
64
 
61
 
try:
62
 
    from urllib.parse import quote
63
 
except ImportError:
64
 
    from urllib import quote
65
65
 
66
66
DEFAULT_FILE_MODE = stat.S_IFREG | 0o644
67
67
HG_RENAME_SOURCE = "HG:rename-source"
74
74
 
75
75
 
76
76
def escape_file_id(file_id):
77
 
    return file_id.replace('_', '__').replace(' ', '_s').replace('\x0c', '_c')
 
77
    return file_id.replace(b'_', b'__').replace(b' ', b'_s').replace(b'\x0c', b'_c')
78
78
 
79
79
 
80
80
def unescape_file_id(file_id):
99
99
 
100
100
 
101
101
def fix_person_identifier(text):
102
 
    if not "<" in text and not ">" in text:
 
102
    if not b"<" in text and not b">" in text:
103
103
        username = text
104
104
        email = text
105
105
    else:
106
 
        if text.rindex(">") < text.rindex("<"):
 
106
        if text.rindex(b">") < text.rindex(b"<"):
107
107
            raise ValueError(text)
108
 
        username, email = text.split("<", 2)[-2:]
109
 
        email = email.split(">", 1)[0]
110
 
        if username.endswith(" "):
 
108
        username, email = text.split(b"<", 2)[-2:]
 
109
        email = email.split(b">", 1)[0]
 
110
        if username.endswith(b" "):
111
111
            username = username[:-1]
112
 
    return "%s <%s>" % (username, email)
 
112
    return b"%s <%s>" % (username, email)
113
113
 
114
114
 
115
115
def warn_escaped(commit, num_escaped):
146
146
        from dulwich.protocol import ZERO_SHA
147
147
        if git_rev_id == ZERO_SHA:
148
148
            return NULL_REVISION
149
 
        return "%s:%s" % (cls.revid_prefix, git_rev_id)
 
149
        return b"%s:%s" % (cls.revid_prefix, git_rev_id)
150
150
 
151
151
    @classmethod
152
152
    def revision_id_bzr_to_foreign(cls, bzr_rev_id):
153
153
        """Convert a Bazaar revision id to a git revision id handle."""
154
 
        if not bzr_rev_id.startswith("%s:" % cls.revid_prefix):
 
154
        if not bzr_rev_id.startswith(b"%s:" % cls.revid_prefix):
155
155
            raise errors.InvalidRevisionId(bzr_rev_id, cls)
156
156
        return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
157
157
 
172
172
        return unescape_file_id(file_id[len(FILE_ID_PREFIX):])
173
173
 
174
174
    def revid_as_refname(self, revid):
175
 
        return "refs/bzr/%s" % quote(revid)
 
175
        if not isinstance(revid, bytes):
 
176
            raise TypeError(revid)
 
177
        return b"refs/bzr/" + urlutils.quote(revid)
176
178
 
177
179
    def import_unusual_file_modes(self, rev, unusual_file_modes):
178
180
        if unusual_file_modes:
229
231
        (message, renames, branch, extra) = extract_hg_metadata(message)
230
232
        if branch is not None:
231
233
            rev.properties[u'hg:extra:branch'] = branch
232
 
        for name, value in extra.iteritems():
 
234
        for name, value in viewitems(extra):
233
235
            rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
234
236
        if renames:
235
237
            rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
236
 
                [(new, old) for (old, new) in renames.iteritems()]))
 
238
                [(new, old) for (old, new) in viewitems(renames)]))
237
239
        return message
238
240
 
239
241
    def _extract_bzr_metadata(self, rev, message):
301
303
            encoding))
302
304
        commit.author = fix_person_identifier(
303
305
            rev.get_apparent_authors()[0].encode(encoding))
 
306
        # TODO(jelmer): Don't use this hack.
 
307
        long = getattr(__builtins__, 'long', int)
304
308
        commit.commit_time = long(rev.timestamp)
305
309
        if u'author-timestamp' in rev.properties:
306
310
            commit.author_time = long(rev.properties[u'author-timestamp'])
317
321
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode('ascii')
318
322
        commit.message = self._encode_commit_message(rev, rev.message,
319
323
            encoding)
320
 
        if type(commit.message) is not str:
 
324
        if not isinstance(commit.message, bytes):
321
325
            raise TypeError(commit.message)
322
326
        if metadata is not None:
323
327
            try:
329
333
                 u'commit-timezone-neg-utc', u'git-implicit-encoding',
330
334
                 u'git-gpg-signature', u'git-explicit-encoding',
331
335
                 u'author-timestamp', u'file-modes'])
332
 
            for k, v in rev.properties.iteritems():
 
336
            for k, v in viewitems(rev.properties):
333
337
                if not k in mapping_properties:
334
338
                    metadata.properties[k] = v
335
339
        if not lossy and metadata:
338
342
                                                     encoding)
339
343
            else:
340
344
                raise NoPushSupport(None, None, self, revision_id=rev.revision_id)
341
 
        if type(commit.message) is not str:
 
345
        if not isinstance(commit.message, bytes):
342
346
            raise TypeError(commit.message)
343
347
        i = 0
344
348
        propname = u'git-mergetag-0'
370
374
                self.revision_id_foreign_to_bzr(commit.id))
371
375
        rev.git_metadata = None
372
376
        def decode_using_encoding(rev, commit, encoding):
373
 
            rev.committer = str(commit.committer).decode(encoding)
 
377
            rev.committer = commit.committer.decode(encoding)
374
378
            if commit.committer != commit.author:
375
379
                rev.properties[u'author'] = str(commit.author).decode(encoding)
376
380
            rev.message, rev.git_metadata = self._decode_commit_message(
377
381
                rev, commit.message, encoding)
378
382
        if commit.encoding is not None:
379
 
            rev.properties[u'git-explicit-encoding'] = commit.encoding
380
 
            decode_using_encoding(rev, commit, commit.encoding)
 
383
            rev.properties[u'git-explicit-encoding'] = commit.encoding.decode('ascii')
 
384
            decode_using_encoding(rev, commit, commit.encoding.decode('ascii'))
381
385
        else:
382
386
            for encoding in ('utf-8', 'latin1'):
383
387
                try:
457
461
 
458
462
 
459
463
class BzrGitMappingv1(BzrGitMapping):
460
 
    revid_prefix = 'git-v1'
 
464
    revid_prefix = b'git-v1'
461
465
    experimental = False
462
466
 
463
467
    def __str__(self):
465
469
 
466
470
 
467
471
class BzrGitMappingExperimental(BzrGitMappingv1):
468
 
    revid_prefix = 'git-experimental'
 
472
    revid_prefix = b'git-experimental'
469
473
    experimental = True
470
474
    roundtripping = True
471
475
 
498
502
        if bzr_revid == NULL_REVISION:
499
503
            from dulwich.protocol import ZERO_SHA
500
504
            return ZERO_SHA, None
501
 
        if not bzr_revid.startswith("git-"):
 
505
        if not bzr_revid.startswith(b"git-"):
502
506
            raise errors.InvalidRevisionId(bzr_revid, None)
503
 
        (mapping_version, git_sha) = bzr_revid.split(":", 1)
 
507
        (mapping_version, git_sha) = bzr_revid.split(b":", 1)
504
508
        mapping = self.get(mapping_version)
505
509
        return mapping.revision_id_bzr_to_foreign(bzr_revid)
506
510
 
508
512
 
509
513
 
510
514
mapping_registry = GitMappingRegistry()
511
 
mapping_registry.register_lazy('git-v1', "breezy.plugins.git.mapping",
 
515
mapping_registry.register_lazy(b'git-v1', "breezy.plugins.git.mapping",
512
516
    "BzrGitMappingv1")
513
 
mapping_registry.register_lazy('git-experimental',
 
517
mapping_registry.register_lazy(b'git-experimental',
514
518
    "breezy.plugins.git.mapping", "BzrGitMappingExperimental")
515
519
# Uncomment the next line to enable the experimental bzr-git mappings.
516
520
# This will make sure all bzr metadata is pushed into git, allowing for
518
522
# NOTE: THIS IS EXPERIMENTAL. IT MAY EAT YOUR DATA OR CORRUPT
519
523
# YOUR BZR OR GIT REPOSITORIES. USE WITH CARE.
520
524
#mapping_registry.set_default('git-experimental')
521
 
mapping_registry.set_default('git-v1')
 
525
mapping_registry.set_default(b'git-v1')
522
526
 
523
527
 
524
528
class ForeignGit(ForeignVcs):
642
646
    def set_file_id(self, path, file_id):
643
647
        if type(path) is not str:
644
648
            raise TypeError(path)
645
 
        if type(file_id) is not str:
 
649
        if not isinstance(file_id, bytes):
646
650
            raise TypeError(file_id)
647
651
        self.file_ids[path] = file_id
648
652
 
649
653
    def lookup_file_id(self, path):
650
 
        if type(path) is not str:
 
654
        if not isinstance(path, text_type):
651
655
            raise TypeError(path)
652
656
        try:
653
657
            file_id = self.file_ids[path]
654
658
        except KeyError:
655
659
            file_id = self.mapping.generate_file_id(path)
656
 
        if type(file_id) is not str:
 
660
        if not isinstance(file_id, bytes):
657
661
            raise TypeError(file_id)
658
662
        return file_id
659
663
 
660
664
    def lookup_path(self, file_id):
661
665
        if self.paths is None:
662
666
            self.paths = {}
663
 
            for k, v in self.file_ids.iteritems():
 
667
            for k, v in viewitems(self.file_ids):
664
668
                self.paths[v] = k
665
669
        try:
666
670
            path = self.paths[file_id]
667
671
        except KeyError:
668
672
            return self.mapping.parse_file_id(file_id)
669
673
        else:
670
 
            if type(path) is not str:
 
674
            if not isinstance(path, text_type):
671
675
                raise TypeError(path)
672
676
            return path
673
677