/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/merge_directive.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-07-11 21:04:12 UTC
  • mfrom: (7031.1.4 python3-diff)
  • Revision ID: breezy.the.bot@gmail.com-20180711210412-l80sfib91f3uhwc0
Port diff and merge_directive to Python 3.

Merged from https://code.launchpad.net/~jelmer/brz/python3-diff/+merge/349216

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from __future__ import absolute_import
18
18
 
 
19
import base64
19
20
import re
20
21
 
21
22
from . import lazy_import
143
144
                stanza.add(key, self.__dict__[key])
144
145
        if base_revision:
145
146
            stanza.add('base_revision_id', self.base_revision_id)
146
 
        lines = ['# ' + self._format_string + '\n']
 
147
        lines = [b'# ' + self._format_string + b'\n']
147
148
        lines.extend(rio.to_patch_lines(stanza))
148
 
        lines.append('# \n')
 
149
        lines.append(b'# \n')
149
150
        return lines
150
151
 
151
152
    def write_to_directory(self, path):
369
370
    directly using the standard patch program.
370
371
    """
371
372
 
372
 
    _format_string = 'Bazaar merge directive format 1'
 
373
    _format_string = b'Bazaar merge directive format 1'
373
374
 
374
375
    def __init__(self, revision_id, testament_sha1, time, timezone,
375
376
                 target_branch, patch=None, patch_type=None,
424
425
        firstline = b""
425
426
        for line in line_iter:
426
427
            if line.startswith(b'# Bazaar merge directive format '):
427
 
                return _format_registry.get(line[2:].rstrip().decode('utf-8', 'replace'))._from_lines(
 
428
                return _format_registry.get(line[2:].rstrip())._from_lines(
428
429
                    line_iter)
429
430
            firstline = firstline or line.strip()
430
431
        raise errors.NotAMergeDirective(firstline)
454
455
            except KeyError:
455
456
                pass
456
457
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
 
458
        if 'testament_sha1' in kwargs:
 
459
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
457
460
        return MergeDirective(time=time, timezone=timezone,
458
461
                              patch_type=patch_type, patch=patch, **kwargs)
459
462
 
480
483
 
481
484
class MergeDirective2(BaseMergeDirective):
482
485
 
483
 
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
 
486
    _format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
484
487
 
485
488
    def __init__(self, revision_id, testament_sha1, time, timezone,
486
489
                 target_branch, patch=None, source_branch=None, message=None,
510
513
        if self.bundle is None:
511
514
            return None
512
515
        else:
513
 
            return self.bundle.decode('base-64')
 
516
            return base64.b64decode(self.bundle)
514
517
 
515
518
    @classmethod
516
519
    def _from_lines(klass, line_iter):
548
551
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
549
552
        kwargs['base_revision_id'] =\
550
553
            kwargs['base_revision_id'].encode('utf-8')
 
554
        if 'testament_sha1' in kwargs:
 
555
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
551
556
        return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
552
557
                     **kwargs)
553
558
 
554
559
    def to_lines(self):
555
560
        lines = self._to_lines(base_revision=True)
556
561
        if self.patch is not None:
557
 
            lines.append('# Begin patch\n')
 
562
            lines.append(b'# Begin patch\n')
558
563
            lines.extend(self.patch.splitlines(True))
559
564
        if self.bundle is not None:
560
 
            lines.append('# Begin bundle\n')
 
565
            lines.append(b'# Begin bundle\n')
561
566
            lines.extend(self.bundle.splitlines(True))
562
567
        return lines
563
568
 
620
625
                patch = None
621
626
 
622
627
            if include_bundle:
623
 
                bundle = klass._generate_bundle(repository, revision_id,
624
 
                    ancestor_id).encode('base-64')
 
628
                bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
 
629
                    ancestor_id))
625
630
            else:
626
631
                bundle = None
627
632
 
645
650
        calculated_patch = self._generate_diff(repository, self.revision_id,
646
651
                                               self.base_revision_id)
647
652
        # Convert line-endings to UNIX
648
 
        stored_patch = re.sub('\r\n?', '\n', self.patch)
649
 
        calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
 
653
        stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
 
654
        calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
650
655
        # Strip trailing whitespace
651
 
        calculated_patch = re.sub(' *\n', '\n', calculated_patch)
652
 
        stored_patch = re.sub(' *\n', '\n', stored_patch)
 
656
        calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
 
657
        stored_patch = re.sub(b' *\n', b'\n', stored_patch)
653
658
        return (calculated_patch == stored_patch)
654
659
 
655
660
    def get_merge_request(self, repository):
685
690
# already merge directives in the wild that used 0.19. Registering with the old
686
691
# format string to retain compatibility with those merge directives.
687
692
_format_registry.register(MergeDirective2,
688
 
                          'Bazaar merge directive format 2 (Bazaar 0.19)')
 
693
                          b'Bazaar merge directive format 2 (Bazaar 0.19)')