/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: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
 
76
76
 
77
77
def escape_file_id(file_id):
78
 
    return file_id.replace(b'_', b'__').replace(b' ', b'_s').replace(b'\x0c', b'_c')
 
78
    file_id = file_id.replace(b'_', b'__')
 
79
    file_id = file_id.replace(b' ', b'_s')
 
80
    file_id = file_id.replace(b'\x0c', b'_c')
 
81
    return file_id
79
82
 
80
83
 
81
84
def unescape_file_id(file_id):
82
85
    ret = bytearray()
83
86
    i = 0
84
87
    while i < len(file_id):
85
 
        if file_id[i:i+1] != b'_':
 
88
        if file_id[i:i + 1] != b'_':
86
89
            ret.append(file_id[i])
87
90
        else:
88
 
            if file_id[i+1:i+2] == b'_':
 
91
            if file_id[i + 1:i + 2] == b'_':
89
92
                ret.append(b"_"[0])
90
 
            elif file_id[i+1:i+2] == b's':
 
93
            elif file_id[i + 1:i + 2] == b's':
91
94
                ret.append(b" "[0])
92
 
            elif file_id[i+1:i+2] == b'c':
 
95
            elif file_id[i + 1:i + 2] == b'c':
93
96
                ret.append(b"\x0c"[0])
94
97
            else:
95
98
                raise ValueError("unknown escape character %s" %
96
 
                        file_id[i+1:i+2])
 
99
                                 file_id[i + 1:i + 2])
97
100
            i += 1
98
101
        i += 1
99
102
    return bytes(ret)
100
103
 
101
104
 
102
105
def fix_person_identifier(text):
103
 
    if not b"<" in text and not b">" in text:
 
106
    if b"<" not in text and b">" not in text:
104
107
        username = text
105
108
        email = text
106
109
    else:
138
141
        super(BzrGitMapping, self).__init__(foreign_vcs_git)
139
142
 
140
143
    def __eq__(self, other):
141
 
        return (type(self) == type(other) and
142
 
                self.revid_prefix == other.revid_prefix)
 
144
        return (type(self) == type(other)
 
145
                and self.revid_prefix == other.revid_prefix)
143
146
 
144
147
    @classmethod
145
148
    def revision_id_foreign_to_bzr(cls, git_rev_id):
154
157
        """Convert a Bazaar revision id to a git revision id handle."""
155
158
        if not bzr_rev_id.startswith(b"%s:" % cls.revid_prefix):
156
159
            raise errors.InvalidRevisionId(bzr_rev_id, cls)
157
 
        return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
 
160
        return bzr_rev_id[len(cls.revid_prefix) + 1:], cls()
158
161
 
159
162
    def generate_file_id(self, path):
160
163
        # Git paths are just bytestrings
223
226
 
224
227
    def _extract_git_svn_metadata(self, rev, message):
225
228
        lines = message.split("\n")
226
 
        if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
 
229
        if not (lines[-1] == "" and len(lines) >= 2 and
 
230
                lines[-2].startswith("git-svn-id:")):
227
231
            return message
228
232
        git_svn_id = lines[-2].split(": ", 1)[1]
229
233
        rev.properties[u'git-svn-id'] = git_svn_id
300
304
        except KeyError:
301
305
            encoding = rev.properties.get(u'git-implicit-encoding', 'utf-8')
302
306
        try:
303
 
            commit.encoding = rev.properties[u'git-explicit-encoding'].encode('ascii')
 
307
            commit.encoding = rev.properties[u'git-explicit-encoding'].encode(
 
308
                'ascii')
304
309
        except KeyError:
305
310
            pass
306
311
        commit.committer = fix_person_identifier(rev.committer.encode(
314
319
            commit.author_time = long(rev.properties[u'author-timestamp'])
315
320
        else:
316
321
            commit.author_time = commit.commit_time
317
 
        commit._commit_timezone_neg_utc = u"commit-timezone-neg-utc" in rev.properties
 
322
        commit._commit_timezone_neg_utc = (
 
323
            u"commit-timezone-neg-utc" in rev.properties)
318
324
        commit.commit_timezone = rev.timezone
319
 
        commit._author_timezone_neg_utc = u"author-timezone-neg-utc" in rev.properties
 
325
        commit._author_timezone_neg_utc = (
 
326
            u"author-timezone-neg-utc" in rev.properties)
320
327
        if u'author-timezone' in rev.properties:
321
328
            commit.author_timezone = int(rev.properties[u'author-timezone'])
322
329
        else:
323
330
            commit.author_timezone = commit.commit_timezone
324
331
        if u'git-gpg-signature' in rev.properties:
325
 
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode('ascii')
 
332
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
 
333
                'ascii')
326
334
        commit.message = self._encode_commit_message(rev, rev.message,
327
 
            encoding)
 
335
                                                     encoding)
328
336
        if not isinstance(commit.message, bytes):
329
337
            raise TypeError(commit.message)
330
338
        if metadata is not None:
338
346
                 u'git-gpg-signature', u'git-explicit-encoding',
339
347
                 u'author-timestamp', u'file-modes'])
340
348
            for k, v in viewitems(rev.properties):
341
 
                if not k in mapping_properties:
 
349
                if k not in mapping_properties:
342
350
                    metadata.properties[k] = v
343
351
        if not lossy and metadata:
344
352
            if self.roundtripping:
345
353
                commit.message = inject_bzr_metadata(commit.message, metadata,
346
354
                                                     encoding)
347
355
            else:
348
 
                raise NoPushSupport(None, None, self, revision_id=rev.revision_id)
 
356
                raise NoPushSupport(
 
357
                    None, None, self, revision_id=rev.revision_id)
349
358
        if not isinstance(commit.message, bytes):
350
359
            raise TypeError(commit.message)
351
360
        i = 0
355
364
            i += 1
356
365
            propname = u'git-mergetag-%d' % i
357
366
        if u'git-extra' in rev.properties:
358
 
            commit.extra.extend([l.split(b' ', 1) for l in rev.properties[u'git-extra'].splitlines()])
 
367
            commit.extra.extend(
 
368
                [l.split(b' ', 1)
 
369
                 for l in rev.properties[u'git-extra'].splitlines()])
359
370
        return commit
360
371
 
361
372
    def import_fileid_map(self, blob):
375
386
        if commit is None:
376
387
            raise AssertionError("Commit object can't be None")
377
388
        rev = ForeignRevision(commit.id, self,
378
 
                self.revision_id_foreign_to_bzr(commit.id))
 
389
                              self.revision_id_foreign_to_bzr(commit.id))
379
390
        rev.git_metadata = None
 
391
 
380
392
        def decode_using_encoding(rev, commit, encoding):
381
393
            rev.committer = commit.committer.decode(encoding)
382
394
            if commit.committer != commit.author:
384
396
            rev.message, rev.git_metadata = self._decode_commit_message(
385
397
                rev, commit.message, encoding)
386
398
        if commit.encoding is not None:
387
 
            rev.properties[u'git-explicit-encoding'] = commit.encoding.decode('ascii')
 
399
            rev.properties[u'git-explicit-encoding'] = commit.encoding.decode(
 
400
                'ascii')
388
401
            decode_using_encoding(rev, commit, commit.encoding.decode('ascii'))
389
402
        else:
390
403
            for encoding in ('utf-8', 'latin1'):
405
418
        if commit._commit_timezone_neg_utc:
406
419
            rev.properties[u'commit-timezone-neg-utc'] = ""
407
420
        if commit.gpgsig:
408
 
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode('ascii')
 
421
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
 
422
                'ascii')
409
423
        if commit.mergetag:
410
424
            for i, tag in enumerate(commit.mergetag):
411
425
                rev.properties[u'git-mergetag-%d' % i] = tag.as_raw_string()
444
458
                unknown_extra_fields.append(k)
445
459
        if unknown_extra_fields:
446
460
            raise UnknownCommitExtra(
447
 
                commit, [f.decode('ascii', 'replace') for f in unknown_extra_fields])
 
461
                commit,
 
462
                [f.decode('ascii', 'replace') for f in unknown_extra_fields])
448
463
        if extra_lines:
449
464
            rev.properties[u'git-extra'] = b''.join(extra_lines)
450
465
        return rev, roundtrip_revid, verifiers
457
472
        :return: GitFileIdMap instance
458
473
        """
459
474
        try:
460
 
            file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
 
475
            file_id_map_sha = lookup_object(
 
476
                tree_sha)[self.BZR_FILE_IDS_FILE][1]
461
477
        except KeyError:
462
478
            file_ids = {}
463
479
        else:
495
511
        return ret
496
512
 
497
513
    def import_commit(self, commit, lookup_parent_revid):
498
 
        rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
 
514
        rev, roundtrip_revid, verifiers = super(
 
515
            BzrGitMappingExperimental, self).import_commit(
 
516
                commit, lookup_parent_revid)
499
517
        rev.properties[u'converted_revision'] = "git %s\n" % commit.id
500
518
        return rev, roundtrip_revid, verifiers
501
519
 
518
536
 
519
537
mapping_registry = GitMappingRegistry()
520
538
mapping_registry.register_lazy(b'git-v1', __name__,
521
 
    "BzrGitMappingv1")
 
539
                               "BzrGitMappingv1")
522
540
mapping_registry.register_lazy(b'git-experimental',
523
 
    __name__, "BzrGitMappingExperimental")
 
541
                               __name__, "BzrGitMappingExperimental")
524
542
# Uncomment the next line to enable the experimental bzr-git mappings.
525
543
# This will make sure all bzr metadata is pushed into git, allowing for
526
544
# full roundtripping later.
527
545
# NOTE: THIS IS EXPERIMENTAL. IT MAY EAT YOUR DATA OR CORRUPT
528
546
# YOUR BZR OR GIT REPOSITORIES. USE WITH CARE.
529
 
#mapping_registry.set_default('git-experimental')
 
547
# mapping_registry.set_default('git-experimental')
530
548
mapping_registry.set_default(b'git-v1')
531
549
 
532
550
 
553
571
 
554
572
    @classmethod
555
573
    def show_foreign_revid(cls, foreign_revid):
556
 
        return { "git commit": foreign_revid.decode('utf-8') }
 
574
        return {"git commit": foreign_revid.decode('utf-8')}
557
575
 
558
576
 
559
577
foreign_vcs_git = ForeignGit()