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