/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: 2018-11-22 02:23:26 UTC
  • mto: This revision was merged to the branch mainline in revision 7209.
  • Revision ID: jelmer@jelmer.uk-20181122022326-hu77dzuqgc8ruzf4
Review comments.

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