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

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
import base64
18
 
import contextlib
19
 
from io import BytesIO
 
17
from __future__ import absolute_import
 
18
 
 
19
from StringIO import StringIO
20
20
import re
21
21
 
22
 
from . import lazy_import
 
22
from brzlib import lazy_import
23
23
lazy_import.lazy_import(globals(), """
24
 
from breezy import (
 
24
from brzlib import (
25
25
    branch as _mod_branch,
26
26
    diff,
27
27
    email_message,
 
28
    errors,
28
29
    gpg,
29
30
    hooks,
30
31
    registry,
31
32
    revision as _mod_revision,
32
33
    rio,
 
34
    testament,
33
35
    timestamp,
34
36
    trace,
35
37
    )
36
 
from breezy.bzr import (
37
 
    testament,
38
 
    )
39
 
from breezy.bzr.bundle import (
 
38
from brzlib.bundle import (
40
39
    serializer as bundle_serializer,
41
40
    )
42
41
""")
43
 
from . import (
44
 
    errors,
45
 
    )
46
42
 
47
43
 
48
44
class MergeRequestBodyParams(object):
64
60
    """Hooks for MergeDirective classes."""
65
61
 
66
62
    def __init__(self):
67
 
        hooks.Hooks.__init__(self, "breezy.merge_directive",
68
 
                             "BaseMergeDirective.hooks")
69
 
        self.add_hook(
70
 
            'merge_request_body',
 
63
        hooks.Hooks.__init__(self, "brzlib.merge_directive", "BaseMergeDirective.hooks")
 
64
        self.add_hook('merge_request_body',
71
65
            "Called with a MergeRequestBodyParams when a body is needed for"
72
66
            " a merge request.  Callbacks must return a body.  If more"
73
67
            " than one callback is registered, the output of one callback is"
77
71
class BaseMergeDirective(object):
78
72
    """A request to perform a merge into a branch.
79
73
 
80
 
    This is the base class that all merge directive implementations
 
74
    This is the base class that all merge directive implementations 
81
75
    should derive from.
82
76
 
83
 
    :cvar multiple_output_files: Whether or not this merge directive
 
77
    :cvar multiple_output_files: Whether or not this merge directive 
84
78
        stores a set of revisions in more than one file
85
79
    """
86
80
 
147
141
                stanza.add(key, self.__dict__[key])
148
142
        if base_revision:
149
143
            stanza.add('base_revision_id', self.base_revision_id)
150
 
        lines = [b'# ' + self._format_string + b'\n']
 
144
        lines = ['# ' + self._format_string + '\n']
151
145
        lines.extend(rio.to_patch_lines(stanza))
152
 
        lines.append(b'# \n')
 
146
        lines.append('# \n')
153
147
        return lines
154
148
 
155
149
    def write_to_directory(self, path):
161
155
 
162
156
    @classmethod
163
157
    def from_objects(klass, repository, revision_id, time, timezone,
164
 
                     target_branch, patch_type='bundle',
165
 
                     local_target_branch=None, public_branch=None, message=None):
 
158
                 target_branch, patch_type='bundle',
 
159
                 local_target_branch=None, public_branch=None, message=None):
166
160
        """Generate a merge directive from various objects
167
161
 
168
162
        :param repository: The repository containing the revision
205
199
                                                submit_revision_id)
206
200
            type_handler = {'bundle': klass._generate_bundle,
207
201
                            'diff': klass._generate_diff,
208
 
                            None: lambda x, y, z: None}
 
202
                            None: lambda x, y, z: None }
209
203
            patch = type_handler[patch_type](repository, revision_id,
210
204
                                             ancestor_id)
211
205
 
216
210
                                                   revision_id)
217
211
 
218
212
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
219
 
                     patch, patch_type, public_branch, message)
 
213
            patch, patch_type, public_branch, message)
220
214
 
221
215
    def get_disk_name(self, branch):
222
216
        """Generate a suitable basename for storing this directive on disk
228
222
        if self.revision_id == revision_id:
229
223
            revno = [revno]
230
224
        else:
231
 
            try:
232
 
                revno = branch.revision_id_to_dotted_revno(self.revision_id)
233
 
            except errors.NoSuchRevision:
234
 
                revno = ['merge']
235
 
        nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
 
225
            revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
 
226
                ['merge'])
 
227
        nick = re.sub('(\W+)', '-', branch.nick).strip('-')
236
228
        return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
237
229
 
238
230
    @staticmethod
239
231
    def _generate_diff(repository, revision_id, ancestor_id):
240
232
        tree_1 = repository.revision_tree(ancestor_id)
241
233
        tree_2 = repository.revision_tree(revision_id)
242
 
        s = BytesIO()
 
234
        s = StringIO()
243
235
        diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
244
236
        return s.getvalue()
245
237
 
246
238
    @staticmethod
247
239
    def _generate_bundle(repository, revision_id, ancestor_id):
248
 
        s = BytesIO()
 
240
        s = StringIO()
249
241
        bundle_serializer.write_bundle(repository, revision_id,
250
242
                                       ancestor_id, s)
251
243
        return s.getvalue()
257
249
        :return: a string
258
250
        """
259
251
        my_gpg = gpg.GPGStrategy(branch.get_config_stack())
260
 
        return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
 
252
        return my_gpg.sign(''.join(self.to_lines()))
261
253
 
262
254
    def to_email(self, mail_to, branch, sign=False):
263
255
        """Serialize as an email message.
277
269
        if sign:
278
270
            body = self.to_signed(branch)
279
271
        else:
280
 
            body = b''.join(self.to_lines())
 
272
            body = ''.join(self.to_lines())
281
273
        message = email_message.EmailMessage(mail_from, mail_to, subject,
282
 
                                             body)
 
274
            body)
283
275
        return message
284
276
 
285
277
    def install_revisions(self, target_repo):
287
279
        if not target_repo.has_revision(self.revision_id):
288
280
            if self.patch_type == 'bundle':
289
281
                info = bundle_serializer.read_bundle(
290
 
                    BytesIO(self.get_raw_bundle()))
 
282
                    StringIO(self.get_raw_bundle()))
291
283
                # We don't use the bundle's target revision, because
292
284
                # MergeDirective.revision_id is authoritative.
293
285
                try:
305
297
                                           info.real_revisions)
306
298
                    for revision in info.real_revisions:
307
299
                        for parent_id in revision.parent_ids:
308
 
                            if (parent_id not in bundle_revisions
309
 
                                    and not target_repo.has_revision(parent_id)):
 
300
                            if (parent_id not in bundle_revisions and
 
301
                                not target_repo.has_revision(parent_id)):
310
302
                                missing_revisions.append(parent_id)
311
303
                    # reverse missing revisions to try to get heads first
312
304
                    unique_missing = []
351
343
        elif len(self.hooks['merge_request_body']) > 0:
352
344
            trace.warning('Cannot run merge_request_body hooks because mail'
353
345
                          ' client %s does not support message bodies.',
354
 
                          mail_client.__class__.__name__)
 
346
                        mail_client.__class__.__name__)
355
347
        mail_client.compose_merge_request(to, subject,
356
 
                                          b''.join(self.to_lines()),
 
348
                                          ''.join(self.to_lines()),
357
349
                                          basename, body)
358
350
 
359
351
 
373
365
    directly using the standard patch program.
374
366
    """
375
367
 
376
 
    _format_string = b'Bazaar merge directive format 1'
 
368
    _format_string = 'Bazaar merge directive format 1'
377
369
 
378
370
    def __init__(self, revision_id, testament_sha1, time, timezone,
379
371
                 target_branch, patch=None, patch_type=None,
393
385
        :param message: The message to use when committing this merge
394
386
        """
395
387
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
396
 
                                    timezone, target_branch, patch, source_branch, message)
 
388
            timezone, target_branch, patch, source_branch, message)
397
389
        if patch_type not in (None, 'diff', 'bundle'):
398
390
            raise ValueError(patch_type)
399
391
        if patch_type != 'bundle' and source_branch is None:
425
417
        :return: a MergeRequest
426
418
        """
427
419
        line_iter = iter(lines)
428
 
        firstline = b""
 
420
        firstline = ""
429
421
        for line in line_iter:
430
 
            if line.startswith(b'# Bazaar merge directive format '):
 
422
            if line.startswith('# Bazaar merge directive format '):
431
423
                return _format_registry.get(line[2:].rstrip())._from_lines(
432
424
                    line_iter)
433
425
            firstline = firstline or line.strip()
441
433
            patch = None
442
434
            patch_type = None
443
435
        else:
444
 
            patch = b''.join(patch_lines)
 
436
            patch = ''.join(patch_lines)
445
437
            try:
446
 
                bundle_serializer.read_bundle(BytesIO(patch))
 
438
                bundle_serializer.read_bundle(StringIO(patch))
447
439
            except (errors.NotABundle, errors.BundleNotSupported,
448
440
                    errors.BadBundle):
449
441
                patch_type = 'diff'
458
450
            except KeyError:
459
451
                pass
460
452
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
461
 
        if 'testament_sha1' in kwargs:
462
 
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
463
453
        return MergeDirective(time=time, timezone=timezone,
464
454
                              patch_type=patch_type, patch=patch, **kwargs)
465
455
 
471
461
 
472
462
    @staticmethod
473
463
    def _generate_bundle(repository, revision_id, ancestor_id):
474
 
        s = BytesIO()
 
464
        s = StringIO()
475
465
        bundle_serializer.write_bundle(repository, revision_id,
476
466
                                       ancestor_id, s, '0.9')
477
467
        return s.getvalue()
486
476
 
487
477
class MergeDirective2(BaseMergeDirective):
488
478
 
489
 
    _format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
 
479
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
490
480
 
491
481
    def __init__(self, revision_id, testament_sha1, time, timezone,
492
482
                 target_branch, patch=None, source_branch=None, message=None,
494
484
        if source_branch is None and bundle is None:
495
485
            raise errors.NoMergeSource()
496
486
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
497
 
                                    timezone, target_branch, patch, source_branch, message)
 
487
            timezone, target_branch, patch, source_branch, message)
498
488
        self.bundle = bundle
499
489
        self.base_revision_id = base_revision_id
500
490
 
516
506
        if self.bundle is None:
517
507
            return None
518
508
        else:
519
 
            return base64.b64decode(self.bundle)
 
509
            return self.bundle.decode('base-64')
520
510
 
521
511
    @classmethod
522
512
    def _from_lines(klass, line_iter):
524
514
        patch = None
525
515
        bundle = None
526
516
        try:
527
 
            start = next(line_iter)
 
517
            start = line_iter.next()
528
518
        except StopIteration:
529
519
            pass
530
520
        else:
531
 
            if start.startswith(b'# Begin patch'):
 
521
            if start.startswith('# Begin patch'):
532
522
                patch_lines = []
533
523
                for line in line_iter:
534
 
                    if line.startswith(b'# Begin bundle'):
 
524
                    if line.startswith('# Begin bundle'):
535
525
                        start = line
536
526
                        break
537
527
                    patch_lines.append(line)
538
528
                else:
539
529
                    start = None
540
 
                patch = b''.join(patch_lines)
 
530
                patch = ''.join(patch_lines)
541
531
            if start is not None:
542
 
                if start.startswith(b'# Begin bundle'):
543
 
                    bundle = b''.join(line_iter)
 
532
                if start.startswith('# Begin bundle'):
 
533
                    bundle = ''.join(line_iter)
544
534
                else:
545
535
                    raise errors.IllegalMergeDirectivePayload(start)
546
536
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
554
544
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
555
545
        kwargs['base_revision_id'] =\
556
546
            kwargs['base_revision_id'].encode('utf-8')
557
 
        if 'testament_sha1' in kwargs:
558
 
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
559
547
        return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
560
548
                     **kwargs)
561
549
 
562
550
    def to_lines(self):
563
551
        lines = self._to_lines(base_revision=True)
564
552
        if self.patch is not None:
565
 
            lines.append(b'# Begin patch\n')
 
553
            lines.append('# Begin patch\n')
566
554
            lines.extend(self.patch.splitlines(True))
567
555
        if self.bundle is not None:
568
 
            lines.append(b'# Begin bundle\n')
 
556
            lines.append('# Begin bundle\n')
569
557
            lines.extend(self.bundle.splitlines(True))
570
558
        return lines
571
559
 
572
560
    @classmethod
573
561
    def from_objects(klass, repository, revision_id, time, timezone,
574
 
                     target_branch, include_patch=True, include_bundle=True,
575
 
                     local_target_branch=None, public_branch=None, message=None,
576
 
                     base_revision_id=None):
 
562
                 target_branch, include_patch=True, include_bundle=True,
 
563
                 local_target_branch=None, public_branch=None, message=None,
 
564
                 base_revision_id=None):
577
565
        """Generate a merge directive from various objects
578
566
 
579
567
        :param repository: The repository containing the revision
595
583
        If the message is not supplied, the message from revision_id will be
596
584
        used for the commit.
597
585
        """
598
 
        with contextlib.ExitStack() as exit_stack:
599
 
            exit_stack.enter_context(repository.lock_write())
 
586
        locked = []
 
587
        try:
 
588
            repository.lock_write()
 
589
            locked.append(repository)
600
590
            t_revision_id = revision_id
601
 
            if revision_id == b'null:':
 
591
            if revision_id == 'null:':
602
592
                t_revision_id = None
603
593
            t = testament.StrictTestament3.from_revision(repository,
604
 
                                                         t_revision_id)
 
594
                t_revision_id)
605
595
            if local_target_branch is None:
606
596
                submit_branch = _mod_branch.Branch.open(target_branch)
607
597
            else:
608
598
                submit_branch = local_target_branch
609
 
            exit_stack.enter_context(submit_branch.lock_read())
 
599
            submit_branch.lock_read()
 
600
            locked.append(submit_branch)
610
601
            if submit_branch.get_public_branch() is not None:
611
602
                target_branch = submit_branch.get_public_branch()
612
603
            submit_revision_id = submit_branch.last_revision()
625
616
                patch = None
626
617
 
627
618
            if include_bundle:
628
 
                bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
629
 
                                                                 ancestor_id))
 
619
                bundle = klass._generate_bundle(repository, revision_id,
 
620
                    ancestor_id).encode('base-64')
630
621
            else:
631
622
                bundle = None
632
623
 
633
624
            if public_branch is not None and not include_bundle:
634
625
                public_branch_obj = _mod_branch.Branch.open(public_branch)
635
 
                exit_stack.enter_context(public_branch_obj.lock_read())
 
626
                public_branch_obj.lock_read()
 
627
                locked.append(public_branch_obj)
636
628
                if not public_branch_obj.repository.has_revision(
637
 
                        revision_id):
 
629
                    revision_id):
638
630
                    raise errors.PublicBranchOutOfDate(public_branch,
639
631
                                                       revision_id)
640
632
            testament_sha1 = t.as_sha1()
 
633
        finally:
 
634
            for entry in reversed(locked):
 
635
                entry.unlock()
641
636
        return klass(revision_id, testament_sha1, time, timezone,
642
 
                     target_branch, patch, public_branch, message, bundle,
643
 
                     base_revision_id)
 
637
            target_branch, patch, public_branch, message, bundle,
 
638
            base_revision_id)
644
639
 
645
640
    def _verify_patch(self, repository):
646
641
        calculated_patch = self._generate_diff(repository, self.revision_id,
647
642
                                               self.base_revision_id)
648
643
        # Convert line-endings to UNIX
649
 
        stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
650
 
        calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
 
644
        stored_patch = re.sub('\r\n?', '\n', self.patch)
 
645
        calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
651
646
        # Strip trailing whitespace
652
 
        calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
653
 
        stored_patch = re.sub(b' *\n', b'\n', stored_patch)
 
647
        calculated_patch = re.sub(' *\n', '\n', calculated_patch)
 
648
        stored_patch = re.sub(' *\n', '\n', stored_patch)
654
649
        return (calculated_patch == stored_patch)
655
650
 
656
651
    def get_merge_request(self, repository):
686
681
# already merge directives in the wild that used 0.19. Registering with the old
687
682
# format string to retain compatibility with those merge directives.
688
683
_format_registry.register(MergeDirective2,
689
 
                          b'Bazaar merge directive format 2 (Bazaar 0.19)')
 
684
                          'Bazaar merge directive format 2 (Bazaar 0.19)')