/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-07-28 02:47:10 UTC
  • mfrom: (7519.1.1 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200728024710-a2ylds219f1lsl62
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/388173

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
 
 
23
21
import base64
24
22
import stat
25
23
 
39
37
    NULL_REVISION,
40
38
    Revision,
41
39
    )
42
 
from ..sixish import (
43
 
    PY3,
44
 
    text_type,
45
 
    viewitems,
46
 
    )
47
40
from .errors import (
48
41
    NoPushSupport,
49
42
    )
121
114
    if b"<" not in text and b">" not in text:
122
115
        username = text
123
116
        email = text
 
117
    elif b">" not in text:
 
118
        return text + b">"
124
119
    else:
125
120
        if text.rindex(b">") < text.rindex(b"<"):
126
121
            raise ValueError(text)
131
126
    return b"%s <%s>" % (username, email)
132
127
 
133
128
 
 
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
 
134
149
def warn_escaped(commit, num_escaped):
135
150
    trace.warning("Escaped %d XML-invalid characters in %s. Will be unable "
136
151
                  "to regenerate the SHA map.", num_escaped, commit)
175
190
    def generate_file_id(self, path):
176
191
        # Git paths are just bytestrings
177
192
        # We must just hope they are valid UTF-8..
178
 
        if isinstance(path, text_type):
 
193
        if isinstance(path, str):
179
194
            path = path.encode("utf-8")
180
195
        if path == b"":
181
196
            return ROOT_ID
186
201
            return u""
187
202
        if not file_id.startswith(FILE_ID_PREFIX):
188
203
            raise ValueError
189
 
        return unescape_file_id(file_id[len(FILE_ID_PREFIX):]).decode('utf-8')
190
 
 
191
 
    def revid_as_refname(self, revid):
192
 
        if not isinstance(revid, bytes):
193
 
            raise TypeError(revid)
194
 
        if PY3:
195
 
            revid = revid.decode('utf-8')
196
 
        quoted_revid = urlutils.quote(revid)
197
 
        return b"refs/bzr/" + quoted_revid.encode('utf-8')
 
204
        return decode_git_path(unescape_file_id(file_id[len(FILE_ID_PREFIX):]))
198
205
 
199
206
    def import_unusual_file_modes(self, rev, unusual_file_modes):
200
207
        if unusual_file_modes:
252
259
        (message, renames, branch, extra) = extract_hg_metadata(message)
253
260
        if branch is not None:
254
261
            rev.properties[u'hg:extra:branch'] = branch
255
 
        for name, value in viewitems(extra):
 
262
        for name, value in extra.items():
256
263
            rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
257
264
        if renames:
258
265
            rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
259
 
                [(new, old) for (old, new) in viewitems(renames)]))
 
266
                [(new, old) for (old, new) in renames.items()]))
260
267
        return message
261
268
 
262
269
    def _extract_bzr_metadata(self, rev, message):
332
339
            commit.author_timezone = commit.commit_timezone
333
340
        if u'git-gpg-signature' in rev.properties:
334
341
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
335
 
                'utf-8')
336
 
        if u'git-gpg-signature-b64' in rev.properties:
337
 
            commit.gpgsig = base64.b64decode(rev.properties[u'git-gpg-signature-b64'])
 
342
                'utf-8', 'surrogateescape')
338
343
        commit.message = self._encode_commit_message(rev, rev.message,
339
344
                                                     encoding)
340
345
        if not isinstance(commit.message, bytes):
347
352
            mapping_properties = set(
348
353
                [u'author', u'author-timezone', u'author-timezone-neg-utc',
349
354
                 u'commit-timezone-neg-utc', u'git-implicit-encoding',
350
 
                 u'git-gpg-signature', u'git-gpg-signature-b64',
351
 
                 u'git-explicit-encoding',
 
355
                 u'git-gpg-signature', u'git-explicit-encoding',
352
356
                 u'author-timestamp', u'file-modes'])
353
 
            for k, v in viewitems(rev.properties):
 
357
            for k, v in rev.properties.items():
354
358
                if k not in mapping_properties:
355
359
                    metadata.properties[k] = v
356
360
        if not lossy and metadata:
430
434
        if commit._commit_timezone_neg_utc:
431
435
            rev.properties[u'commit-timezone-neg-utc'] = ""
432
436
        if commit.gpgsig:
433
 
            try:
434
 
                rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
435
 
                    'utf-8')
436
 
            except UnicodeDecodeError:
437
 
                rev.properties[u'git-gpg-signature-b64'] = base64.b64encode(
438
 
                    commit.gpgsig)
 
437
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
 
438
                'utf-8', 'surrogateescape')
439
439
        if commit.mergetag:
440
440
            for i, tag in enumerate(commit.mergetag):
441
441
                rev.properties[u'git-mergetag-%d' % i] = tag.as_raw_string()
581
581
def symlink_to_blob(symlink_target):
582
582
    from dulwich.objects import Blob
583
583
    blob = Blob()
584
 
    if isinstance(symlink_target, text_type):
585
 
        symlink_target = symlink_target.encode('utf-8')
 
584
    if isinstance(symlink_target, str):
 
585
        symlink_target = encode_git_path(symlink_target)
586
586
    blob.data = symlink_target
587
587
    return blob
588
588