/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: 2020-06-08 23:03:56 UTC
  • mto: (7490.40.2 work)
  • mto: This revision was merged to the branch mainline in revision 7511.
  • Revision ID: jelmer@jelmer.uk-20200608230356-a4clct7auuvhbfep
Fix handling of a particular kind of broken committer id.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""Converters, etc for going between Bazaar and Git ids."""
20
20
 
 
21
from __future__ import absolute_import
 
22
 
21
23
import base64
22
24
import stat
23
25
 
37
39
    NULL_REVISION,
38
40
    Revision,
39
41
    )
 
42
from ..sixish import (
 
43
    PY3,
 
44
    text_type,
 
45
    viewitems,
 
46
    )
40
47
from .errors import (
41
48
    NoPushSupport,
42
49
    )
126
133
    return b"%s <%s>" % (username, email)
127
134
 
128
135
 
129
 
def decode_git_path(path):
130
 
    """Take a git path and decode it."""
131
 
    try:
132
 
        return path.decode('utf-8')
133
 
    except UnicodeDecodeError:
134
 
        if PY3:
135
 
            return path.decode('utf-8', 'surrogateescape')
136
 
        raise
137
 
 
138
 
 
139
 
def encode_git_path(path):
140
 
    """Take a regular path and encode it for git."""
141
 
    try:
142
 
        return path.encode('utf-8')
143
 
    except UnicodeEncodeError:
144
 
        if PY3:
145
 
            return path.encode('utf-8', 'surrogateescape')
146
 
        raise
147
 
 
148
 
 
149
136
def warn_escaped(commit, num_escaped):
150
137
    trace.warning("Escaped %d XML-invalid characters in %s. Will be unable "
151
138
                  "to regenerate the SHA map.", num_escaped, commit)
190
177
    def generate_file_id(self, path):
191
178
        # Git paths are just bytestrings
192
179
        # We must just hope they are valid UTF-8..
193
 
        if isinstance(path, str):
 
180
        if isinstance(path, text_type):
194
181
            path = path.encode("utf-8")
195
182
        if path == b"":
196
183
            return ROOT_ID
201
188
            return u""
202
189
        if not file_id.startswith(FILE_ID_PREFIX):
203
190
            raise ValueError
204
 
        return decode_git_path(unescape_file_id(file_id[len(FILE_ID_PREFIX):]))
 
191
        return unescape_file_id(file_id[len(FILE_ID_PREFIX):]).decode('utf-8')
 
192
 
 
193
    def revid_as_refname(self, revid):
 
194
        if not isinstance(revid, bytes):
 
195
            raise TypeError(revid)
 
196
        if PY3:
 
197
            revid = revid.decode('utf-8')
 
198
        quoted_revid = urlutils.quote(revid)
 
199
        return b"refs/bzr/" + quoted_revid.encode('utf-8')
205
200
 
206
201
    def import_unusual_file_modes(self, rev, unusual_file_modes):
207
202
        if unusual_file_modes:
259
254
        (message, renames, branch, extra) = extract_hg_metadata(message)
260
255
        if branch is not None:
261
256
            rev.properties[u'hg:extra:branch'] = branch
262
 
        for name, value in extra.items():
 
257
        for name, value in viewitems(extra):
263
258
            rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
264
259
        if renames:
265
260
            rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
266
 
                [(new, old) for (old, new) in renames.items()]))
 
261
                [(new, old) for (old, new) in viewitems(renames)]))
267
262
        return message
268
263
 
269
264
    def _extract_bzr_metadata(self, rev, message):
339
334
            commit.author_timezone = commit.commit_timezone
340
335
        if u'git-gpg-signature' in rev.properties:
341
336
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
342
 
                'utf-8', 'surrogateescape')
 
337
                'utf-8')
 
338
        if u'git-gpg-signature-b64' in rev.properties:
 
339
            commit.gpgsig = base64.b64decode(rev.properties[u'git-gpg-signature-b64'])
343
340
        commit.message = self._encode_commit_message(rev, rev.message,
344
341
                                                     encoding)
345
342
        if not isinstance(commit.message, bytes):
352
349
            mapping_properties = set(
353
350
                [u'author', u'author-timezone', u'author-timezone-neg-utc',
354
351
                 u'commit-timezone-neg-utc', u'git-implicit-encoding',
355
 
                 u'git-gpg-signature', u'git-explicit-encoding',
 
352
                 u'git-gpg-signature', u'git-gpg-signature-b64',
 
353
                 u'git-explicit-encoding',
356
354
                 u'author-timestamp', u'file-modes'])
357
 
            for k, v in rev.properties.items():
 
355
            for k, v in viewitems(rev.properties):
358
356
                if k not in mapping_properties:
359
357
                    metadata.properties[k] = v
360
358
        if not lossy and metadata:
434
432
        if commit._commit_timezone_neg_utc:
435
433
            rev.properties[u'commit-timezone-neg-utc'] = ""
436
434
        if commit.gpgsig:
437
 
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
438
 
                'utf-8', 'surrogateescape')
 
435
            try:
 
436
                rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
 
437
                    'utf-8')
 
438
            except UnicodeDecodeError:
 
439
                rev.properties[u'git-gpg-signature-b64'] = base64.b64encode(
 
440
                    commit.gpgsig)
439
441
        if commit.mergetag:
440
442
            for i, tag in enumerate(commit.mergetag):
441
443
                rev.properties[u'git-mergetag-%d' % i] = tag.as_raw_string()
581
583
def symlink_to_blob(symlink_target):
582
584
    from dulwich.objects import Blob
583
585
    blob = Blob()
584
 
    if isinstance(symlink_target, str):
585
 
        symlink_target = encode_git_path(symlink_target)
 
586
    if isinstance(symlink_target, text_type):
 
587
        symlink_target = symlink_target.encode('utf-8')
586
588
    blob.data = symlink_target
587
589
    return blob
588
590