/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-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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
    )
114
121
    if b"<" not in text and b">" not in text:
115
122
        username = text
116
123
        email = text
117
 
    elif b">" not in text:
118
 
        return text + b">"
119
124
    else:
120
125
        if text.rindex(b">") < text.rindex(b"<"):
121
126
            raise ValueError(text)
126
131
    return b"%s <%s>" % (username, email)
127
132
 
128
133
 
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
134
def warn_escaped(commit, num_escaped):
150
135
    trace.warning("Escaped %d XML-invalid characters in %s. Will be unable "
151
136
                  "to regenerate the SHA map.", num_escaped, commit)
190
175
    def generate_file_id(self, path):
191
176
        # Git paths are just bytestrings
192
177
        # We must just hope they are valid UTF-8..
193
 
        if isinstance(path, str):
 
178
        if isinstance(path, text_type):
194
179
            path = path.encode("utf-8")
195
180
        if path == b"":
196
181
            return ROOT_ID
201
186
            return u""
202
187
        if not file_id.startswith(FILE_ID_PREFIX):
203
188
            raise ValueError
204
 
        return decode_git_path(unescape_file_id(file_id[len(FILE_ID_PREFIX):]))
 
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')
205
198
 
206
199
    def import_unusual_file_modes(self, rev, unusual_file_modes):
207
200
        if unusual_file_modes:
259
252
        (message, renames, branch, extra) = extract_hg_metadata(message)
260
253
        if branch is not None:
261
254
            rev.properties[u'hg:extra:branch'] = branch
262
 
        for name, value in extra.items():
 
255
        for name, value in viewitems(extra):
263
256
            rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
264
257
        if renames:
265
258
            rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
266
 
                [(new, old) for (old, new) in renames.items()]))
 
259
                [(new, old) for (old, new) in viewitems(renames)]))
267
260
        return message
268
261
 
269
262
    def _extract_bzr_metadata(self, rev, message):
339
332
            commit.author_timezone = commit.commit_timezone
340
333
        if u'git-gpg-signature' in rev.properties:
341
334
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
342
 
                'utf-8', 'surrogateescape')
 
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'])
343
338
        commit.message = self._encode_commit_message(rev, rev.message,
344
339
                                                     encoding)
345
340
        if not isinstance(commit.message, bytes):
352
347
            mapping_properties = set(
353
348
                [u'author', u'author-timezone', u'author-timezone-neg-utc',
354
349
                 u'commit-timezone-neg-utc', u'git-implicit-encoding',
355
 
                 u'git-gpg-signature', u'git-explicit-encoding',
 
350
                 u'git-gpg-signature', u'git-gpg-signature-b64',
 
351
                 u'git-explicit-encoding',
356
352
                 u'author-timestamp', u'file-modes'])
357
 
            for k, v in rev.properties.items():
 
353
            for k, v in viewitems(rev.properties):
358
354
                if k not in mapping_properties:
359
355
                    metadata.properties[k] = v
360
356
        if not lossy and metadata:
434
430
        if commit._commit_timezone_neg_utc:
435
431
            rev.properties[u'commit-timezone-neg-utc'] = ""
436
432
        if commit.gpgsig:
437
 
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
438
 
                'utf-8', 'surrogateescape')
 
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)
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, str):
585
 
        symlink_target = encode_git_path(symlink_target)
 
584
    if isinstance(symlink_target, text_type):
 
585
        symlink_target = symlink_target.encode('utf-8')
586
586
    blob.data = symlink_target
587
587
    return blob
588
588