/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: Jelmer Vernooij
  • Date: 2019-02-04 01:01:24 UTC
  • mto: This revision was merged to the branch mainline in revision 7268.
  • Revision ID: jelmer@jelmer.uk-20190204010124-ni0i4qc6f5tnbvux
Fix source tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
from __future__ import absolute_import
17
18
 
18
 
from StringIO import StringIO
 
19
import base64
19
20
import re
20
21
 
21
 
from bzrlib import (
 
22
from . import lazy_import
 
23
lazy_import.lazy_import(globals(), """
 
24
from breezy import (
22
25
    branch as _mod_branch,
23
26
    diff,
 
27
    email_message,
24
28
    errors,
25
29
    gpg,
26
30
    hooks,
27
31
    registry,
28
32
    revision as _mod_revision,
29
33
    rio,
30
 
    testament,
31
34
    timestamp,
32
35
    trace,
33
36
    )
34
 
from bzrlib.bundle import (
 
37
from breezy.bzr import (
 
38
    testament,
 
39
    )
 
40
from breezy.bundle import (
35
41
    serializer as bundle_serializer,
36
42
    )
37
 
from bzrlib.email_message import EmailMessage
 
43
""")
 
44
from .sixish import (
 
45
    BytesIO,
 
46
    )
38
47
 
39
48
 
40
49
class MergeRequestBodyParams(object):
56
65
    """Hooks for MergeDirective classes."""
57
66
 
58
67
    def __init__(self):
59
 
        hooks.Hooks.__init__(self)
60
 
        self.create_hook(hooks.HookPoint('merge_request_body',
 
68
        hooks.Hooks.__init__(self, "breezy.merge_directive",
 
69
                             "BaseMergeDirective.hooks")
 
70
        self.add_hook(
 
71
            'merge_request_body',
61
72
            "Called with a MergeRequestBodyParams when a body is needed for"
62
73
            " a merge request.  Callbacks must return a body.  If more"
63
74
            " than one callback is registered, the output of one callback is"
64
 
            " provided to the next.", (1, 15, 0), False))
 
75
            " provided to the next.", (1, 15, 0))
65
76
 
66
77
 
67
78
class BaseMergeDirective(object):
68
79
    """A request to perform a merge into a branch.
69
80
 
70
 
    This is the base class that all merge directive implementations 
 
81
    This is the base class that all merge directive implementations
71
82
    should derive from.
72
83
 
73
 
    :cvar multiple_output_files: Whether or not this merge directive 
 
84
    :cvar multiple_output_files: Whether or not this merge directive
74
85
        stores a set of revisions in more than one file
75
86
    """
76
87
 
79
90
    multiple_output_files = False
80
91
 
81
92
    def __init__(self, revision_id, testament_sha1, time, timezone,
82
 
                 target_branch, patch=None, source_branch=None, message=None,
83
 
                 bundle=None):
 
93
                 target_branch, patch=None, source_branch=None,
 
94
                 message=None, bundle=None):
84
95
        """Constructor.
85
96
 
86
97
        :param revision_id: The revision to merge
88
99
            merge.
89
100
        :param time: The current POSIX timestamp time
90
101
        :param timezone: The timezone offset
91
 
        :param target_branch: The branch to apply the merge to
 
102
        :param target_branch: Location of branch to apply the merge to
92
103
        :param patch: The text of a diff or bundle
93
104
        :param source_branch: A public location to merge the revision from
94
105
        :param message: The message to use when committing this merge
137
148
                stanza.add(key, self.__dict__[key])
138
149
        if base_revision:
139
150
            stanza.add('base_revision_id', self.base_revision_id)
140
 
        lines = ['# ' + self._format_string + '\n']
 
151
        lines = [b'# ' + self._format_string + b'\n']
141
152
        lines.extend(rio.to_patch_lines(stanza))
142
 
        lines.append('# \n')
 
153
        lines.append(b'# \n')
143
154
        return lines
144
155
 
145
156
    def write_to_directory(self, path):
151
162
 
152
163
    @classmethod
153
164
    def from_objects(klass, repository, revision_id, time, timezone,
154
 
                 target_branch, patch_type='bundle',
155
 
                 local_target_branch=None, public_branch=None, message=None):
 
165
                     target_branch, patch_type='bundle',
 
166
                     local_target_branch=None, public_branch=None, message=None):
156
167
        """Generate a merge directive from various objects
157
168
 
158
169
        :param repository: The repository containing the revision
162
173
        :param target_branch: The url of the branch to merge into
163
174
        :param patch_type: 'bundle', 'diff' or None, depending on the type of
164
175
            patch desired.
165
 
        :param local_target_branch: a local copy of the target branch
166
 
        :param public_branch: location of a public branch containing the target
167
 
            revision.
 
176
        :param local_target_branch: the submit branch, either itself or a local copy
 
177
        :param public_branch: location of a public branch containing
 
178
            the target revision.
168
179
        :param message: Message to use when committing the merge
169
180
        :return: The merge directive
170
181
 
178
189
        if revision_id == _mod_revision.NULL_REVISION:
179
190
            t_revision_id = None
180
191
        t = testament.StrictTestament3.from_revision(repository, t_revision_id)
181
 
        submit_branch = _mod_branch.Branch.open(target_branch)
 
192
        if local_target_branch is None:
 
193
            submit_branch = _mod_branch.Branch.open(target_branch)
 
194
        else:
 
195
            submit_branch = local_target_branch
182
196
        if submit_branch.get_public_branch() is not None:
183
197
            target_branch = submit_branch.get_public_branch()
184
198
        if patch_type is None:
192
206
                                                submit_revision_id)
193
207
            type_handler = {'bundle': klass._generate_bundle,
194
208
                            'diff': klass._generate_diff,
195
 
                            None: lambda x, y, z: None }
 
209
                            None: lambda x, y, z: None}
196
210
            patch = type_handler[patch_type](repository, revision_id,
197
211
                                             ancestor_id)
198
212
 
203
217
                                                   revision_id)
204
218
 
205
219
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
206
 
            patch, patch_type, public_branch, message)
 
220
                     patch, patch_type, public_branch, message)
207
221
 
208
222
    def get_disk_name(self, branch):
209
223
        """Generate a suitable basename for storing this directive on disk
215
229
        if self.revision_id == revision_id:
216
230
            revno = [revno]
217
231
        else:
218
 
            revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
219
 
                ['merge'])
220
 
        nick = re.sub('(\W+)', '-', branch.nick).strip('-')
 
232
            try:
 
233
                revno = branch.revision_id_to_dotted_revno(self.revision_id)
 
234
            except errors.NoSuchRevision:
 
235
                revno = ['merge']
 
236
        nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
221
237
        return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
222
238
 
223
239
    @staticmethod
224
240
    def _generate_diff(repository, revision_id, ancestor_id):
225
241
        tree_1 = repository.revision_tree(ancestor_id)
226
242
        tree_2 = repository.revision_tree(revision_id)
227
 
        s = StringIO()
 
243
        s = BytesIO()
228
244
        diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
229
245
        return s.getvalue()
230
246
 
231
247
    @staticmethod
232
248
    def _generate_bundle(repository, revision_id, ancestor_id):
233
 
        s = StringIO()
 
249
        s = BytesIO()
234
250
        bundle_serializer.write_bundle(repository, revision_id,
235
251
                                       ancestor_id, s)
236
252
        return s.getvalue()
241
257
        :param branch: The source branch, to get the signing strategy
242
258
        :return: a string
243
259
        """
244
 
        my_gpg = gpg.GPGStrategy(branch.get_config())
245
 
        return my_gpg.sign(''.join(self.to_lines()))
 
260
        my_gpg = gpg.GPGStrategy(branch.get_config_stack())
 
261
        return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
246
262
 
247
263
    def to_email(self, mail_to, branch, sign=False):
248
264
        """Serialize as an email message.
253
269
        :param sign: If True, gpg-sign the email
254
270
        :return: an email message
255
271
        """
256
 
        mail_from = branch.get_config().username()
 
272
        mail_from = branch.get_config_stack().get('email')
257
273
        if self.message is not None:
258
274
            subject = self.message
259
275
        else:
262
278
        if sign:
263
279
            body = self.to_signed(branch)
264
280
        else:
265
 
            body = ''.join(self.to_lines())
266
 
        message = EmailMessage(mail_from, mail_to, subject, body)
 
281
            body = b''.join(self.to_lines())
 
282
        message = email_message.EmailMessage(mail_from, mail_to, subject,
 
283
                                             body)
267
284
        return message
268
285
 
269
286
    def install_revisions(self, target_repo):
271
288
        if not target_repo.has_revision(self.revision_id):
272
289
            if self.patch_type == 'bundle':
273
290
                info = bundle_serializer.read_bundle(
274
 
                    StringIO(self.get_raw_bundle()))
 
291
                    BytesIO(self.get_raw_bundle()))
275
292
                # We don't use the bundle's target revision, because
276
293
                # MergeDirective.revision_id is authoritative.
277
294
                try:
289
306
                                           info.real_revisions)
290
307
                    for revision in info.real_revisions:
291
308
                        for parent_id in revision.parent_ids:
292
 
                            if (parent_id not in bundle_revisions and
293
 
                                not target_repo.has_revision(parent_id)):
 
309
                            if (parent_id not in bundle_revisions
 
310
                                    and not target_repo.has_revision(parent_id)):
294
311
                                missing_revisions.append(parent_id)
295
312
                    # reverse missing revisions to try to get heads first
296
313
                    unique_missing = []
335
352
        elif len(self.hooks['merge_request_body']) > 0:
336
353
            trace.warning('Cannot run merge_request_body hooks because mail'
337
354
                          ' client %s does not support message bodies.',
338
 
                        mail_client.__class__.__name__)
 
355
                          mail_client.__class__.__name__)
339
356
        mail_client.compose_merge_request(to, subject,
340
 
                                          ''.join(self.to_lines()),
 
357
                                          b''.join(self.to_lines()),
341
358
                                          basename, body)
342
359
 
343
360
 
357
374
    directly using the standard patch program.
358
375
    """
359
376
 
360
 
    _format_string = 'Bazaar merge directive format 1'
 
377
    _format_string = b'Bazaar merge directive format 1'
361
378
 
362
379
    def __init__(self, revision_id, testament_sha1, time, timezone,
363
380
                 target_branch, patch=None, patch_type=None,
369
386
            merge.
370
387
        :param time: The current POSIX timestamp time
371
388
        :param timezone: The timezone offset
372
 
        :param target_branch: The branch to apply the merge to
 
389
        :param target_branch: Location of the branch to apply the merge to
373
390
        :param patch: The text of a diff or bundle
374
391
        :param patch_type: None, "diff" or "bundle", depending on the contents
375
392
            of patch
377
394
        :param message: The message to use when committing this merge
378
395
        """
379
396
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
380
 
            timezone, target_branch, patch, source_branch, message)
 
397
                                    timezone, target_branch, patch, source_branch, message)
381
398
        if patch_type not in (None, 'diff', 'bundle'):
382
399
            raise ValueError(patch_type)
383
400
        if patch_type != 'bundle' and source_branch is None:
409
426
        :return: a MergeRequest
410
427
        """
411
428
        line_iter = iter(lines)
412
 
        firstline = ""
 
429
        firstline = b""
413
430
        for line in line_iter:
414
 
            if line.startswith('# Bazaar merge directive format '):
 
431
            if line.startswith(b'# Bazaar merge directive format '):
415
432
                return _format_registry.get(line[2:].rstrip())._from_lines(
416
433
                    line_iter)
417
434
            firstline = firstline or line.strip()
425
442
            patch = None
426
443
            patch_type = None
427
444
        else:
428
 
            patch = ''.join(patch_lines)
 
445
            patch = b''.join(patch_lines)
429
446
            try:
430
 
                bundle_serializer.read_bundle(StringIO(patch))
 
447
                bundle_serializer.read_bundle(BytesIO(patch))
431
448
            except (errors.NotABundle, errors.BundleNotSupported,
432
449
                    errors.BadBundle):
433
450
                patch_type = 'diff'
442
459
            except KeyError:
443
460
                pass
444
461
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
 
462
        if 'testament_sha1' in kwargs:
 
463
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
445
464
        return MergeDirective(time=time, timezone=timezone,
446
465
                              patch_type=patch_type, patch=patch, **kwargs)
447
466
 
453
472
 
454
473
    @staticmethod
455
474
    def _generate_bundle(repository, revision_id, ancestor_id):
456
 
        s = StringIO()
 
475
        s = BytesIO()
457
476
        bundle_serializer.write_bundle(repository, revision_id,
458
477
                                       ancestor_id, s, '0.9')
459
478
        return s.getvalue()
468
487
 
469
488
class MergeDirective2(BaseMergeDirective):
470
489
 
471
 
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
 
490
    _format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
472
491
 
473
492
    def __init__(self, revision_id, testament_sha1, time, timezone,
474
493
                 target_branch, patch=None, source_branch=None, message=None,
476
495
        if source_branch is None and bundle is None:
477
496
            raise errors.NoMergeSource()
478
497
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
479
 
            timezone, target_branch, patch, source_branch, message)
 
498
                                    timezone, target_branch, patch, source_branch, message)
480
499
        self.bundle = bundle
481
500
        self.base_revision_id = base_revision_id
482
501
 
498
517
        if self.bundle is None:
499
518
            return None
500
519
        else:
501
 
            return self.bundle.decode('base-64')
 
520
            return base64.b64decode(self.bundle)
502
521
 
503
522
    @classmethod
504
523
    def _from_lines(klass, line_iter):
506
525
        patch = None
507
526
        bundle = None
508
527
        try:
509
 
            start = line_iter.next()
 
528
            start = next(line_iter)
510
529
        except StopIteration:
511
530
            pass
512
531
        else:
513
 
            if start.startswith('# Begin patch'):
 
532
            if start.startswith(b'# Begin patch'):
514
533
                patch_lines = []
515
534
                for line in line_iter:
516
 
                    if line.startswith('# Begin bundle'):
 
535
                    if line.startswith(b'# Begin bundle'):
517
536
                        start = line
518
537
                        break
519
538
                    patch_lines.append(line)
520
539
                else:
521
540
                    start = None
522
 
                patch = ''.join(patch_lines)
 
541
                patch = b''.join(patch_lines)
523
542
            if start is not None:
524
 
                if start.startswith('# Begin bundle'):
525
 
                    bundle = ''.join(line_iter)
 
543
                if start.startswith(b'# Begin bundle'):
 
544
                    bundle = b''.join(line_iter)
526
545
                else:
527
546
                    raise errors.IllegalMergeDirectivePayload(start)
528
547
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
536
555
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
537
556
        kwargs['base_revision_id'] =\
538
557
            kwargs['base_revision_id'].encode('utf-8')
 
558
        if 'testament_sha1' in kwargs:
 
559
            kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
539
560
        return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
540
561
                     **kwargs)
541
562
 
542
563
    def to_lines(self):
543
564
        lines = self._to_lines(base_revision=True)
544
565
        if self.patch is not None:
545
 
            lines.append('# Begin patch\n')
 
566
            lines.append(b'# Begin patch\n')
546
567
            lines.extend(self.patch.splitlines(True))
547
568
        if self.bundle is not None:
548
 
            lines.append('# Begin bundle\n')
 
569
            lines.append(b'# Begin bundle\n')
549
570
            lines.extend(self.bundle.splitlines(True))
550
571
        return lines
551
572
 
552
573
    @classmethod
553
574
    def from_objects(klass, repository, revision_id, time, timezone,
554
 
                 target_branch, include_patch=True, include_bundle=True,
555
 
                 local_target_branch=None, public_branch=None, message=None,
556
 
                 base_revision_id=None):
 
575
                     target_branch, include_patch=True, include_bundle=True,
 
576
                     local_target_branch=None, public_branch=None, message=None,
 
577
                     base_revision_id=None):
557
578
        """Generate a merge directive from various objects
558
579
 
559
580
        :param repository: The repository containing the revision
563
584
        :param target_branch: The url of the branch to merge into
564
585
        :param include_patch: If true, include a preview patch
565
586
        :param include_bundle: If true, include a bundle
566
 
        :param local_target_branch: a local copy of the target branch
567
 
        :param public_branch: location of a public branch containing the target
568
 
            revision.
 
587
        :param local_target_branch: the target branch, either itself or a local copy
 
588
        :param public_branch: location of a public branch containing
 
589
            the target revision.
569
590
        :param message: Message to use when committing the merge
570
591
        :return: The merge directive
571
592
 
580
601
            repository.lock_write()
581
602
            locked.append(repository)
582
603
            t_revision_id = revision_id
583
 
            if revision_id == 'null:':
 
604
            if revision_id == b'null:':
584
605
                t_revision_id = None
585
606
            t = testament.StrictTestament3.from_revision(repository,
586
 
                t_revision_id)
587
 
            submit_branch = _mod_branch.Branch.open(target_branch)
 
607
                                                         t_revision_id)
 
608
            if local_target_branch is None:
 
609
                submit_branch = _mod_branch.Branch.open(target_branch)
 
610
            else:
 
611
                submit_branch = local_target_branch
588
612
            submit_branch.lock_read()
589
613
            locked.append(submit_branch)
590
614
            if submit_branch.get_public_branch() is not None:
605
629
                patch = None
606
630
 
607
631
            if include_bundle:
608
 
                bundle = klass._generate_bundle(repository, revision_id,
609
 
                    ancestor_id).encode('base-64')
 
632
                bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
 
633
                                                                 ancestor_id))
610
634
            else:
611
635
                bundle = None
612
636
 
615
639
                public_branch_obj.lock_read()
616
640
                locked.append(public_branch_obj)
617
641
                if not public_branch_obj.repository.has_revision(
618
 
                    revision_id):
 
642
                        revision_id):
619
643
                    raise errors.PublicBranchOutOfDate(public_branch,
620
644
                                                       revision_id)
621
645
            testament_sha1 = t.as_sha1()
623
647
            for entry in reversed(locked):
624
648
                entry.unlock()
625
649
        return klass(revision_id, testament_sha1, time, timezone,
626
 
            target_branch, patch, public_branch, message, bundle,
627
 
            base_revision_id)
 
650
                     target_branch, patch, public_branch, message, bundle,
 
651
                     base_revision_id)
628
652
 
629
653
    def _verify_patch(self, repository):
630
654
        calculated_patch = self._generate_diff(repository, self.revision_id,
631
655
                                               self.base_revision_id)
632
656
        # Convert line-endings to UNIX
633
 
        stored_patch = re.sub('\r\n?', '\n', self.patch)
634
 
        calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
 
657
        stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
 
658
        calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
635
659
        # Strip trailing whitespace
636
 
        calculated_patch = re.sub(' *\n', '\n', calculated_patch)
637
 
        stored_patch = re.sub(' *\n', '\n', stored_patch)
 
660
        calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
 
661
        stored_patch = re.sub(b' *\n', b'\n', stored_patch)
638
662
        return (calculated_patch == stored_patch)
639
663
 
640
664
    def get_merge_request(self, repository):
670
694
# already merge directives in the wild that used 0.19. Registering with the old
671
695
# format string to retain compatibility with those merge directives.
672
696
_format_registry.register(MergeDirective2,
673
 
                          'Bazaar merge directive format 2 (Bazaar 0.19)')
 
697
                          b'Bazaar merge directive format 2 (Bazaar 0.19)')