/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-07-08 10:56:06 UTC
  • mto: This revision was merged to the branch mainline in revision 7030.
  • Revision ID: jelmer@jelmer.uk-20180708105606-d53hkks89qq88twu
Use separate .as_bytes method rather than __bytes__.

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
19
import re
20
20
 
21
 
from bzrlib import (
 
21
from . import lazy_import
 
22
lazy_import.lazy_import(globals(), """
 
23
from breezy import (
22
24
    branch as _mod_branch,
23
25
    diff,
 
26
    email_message,
24
27
    errors,
25
28
    gpg,
26
29
    hooks,
31
34
    timestamp,
32
35
    trace,
33
36
    )
34
 
from bzrlib.bundle import (
 
37
from breezy.bundle import (
35
38
    serializer as bundle_serializer,
36
39
    )
37
 
from bzrlib.email_message import EmailMessage
 
40
""")
 
41
from .sixish import (
 
42
    BytesIO,
 
43
    )
38
44
 
39
45
 
40
46
class MergeRequestBodyParams(object):
56
62
    """Hooks for MergeDirective classes."""
57
63
 
58
64
    def __init__(self):
59
 
        hooks.Hooks.__init__(self)
60
 
        self.create_hook(hooks.HookPoint('merge_request_body',
 
65
        hooks.Hooks.__init__(self, "breezy.merge_directive", "BaseMergeDirective.hooks")
 
66
        self.add_hook('merge_request_body',
61
67
            "Called with a MergeRequestBodyParams when a body is needed for"
62
68
            " a merge request.  Callbacks must return a body.  If more"
63
69
            " than one callback is registered, the output of one callback is"
64
 
            " provided to the next.", (1, 15, 0), False))
 
70
            " provided to the next.", (1, 15, 0))
65
71
 
66
72
 
67
73
class BaseMergeDirective(object):
79
85
    multiple_output_files = False
80
86
 
81
87
    def __init__(self, revision_id, testament_sha1, time, timezone,
82
 
                 target_branch, patch=None, source_branch=None, message=None,
83
 
                 bundle=None):
 
88
                 target_branch, patch=None, source_branch=None,
 
89
                 message=None, bundle=None):
84
90
        """Constructor.
85
91
 
86
92
        :param revision_id: The revision to merge
88
94
            merge.
89
95
        :param time: The current POSIX timestamp time
90
96
        :param timezone: The timezone offset
91
 
        :param target_branch: The branch to apply the merge to
 
97
        :param target_branch: Location of branch to apply the merge to
92
98
        :param patch: The text of a diff or bundle
93
99
        :param source_branch: A public location to merge the revision from
94
100
        :param message: The message to use when committing this merge
162
168
        :param target_branch: The url of the branch to merge into
163
169
        :param patch_type: 'bundle', 'diff' or None, depending on the type of
164
170
            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.
 
171
        :param local_target_branch: the submit branch, either itself or a local copy
 
172
        :param public_branch: location of a public branch containing
 
173
            the target revision.
168
174
        :param message: Message to use when committing the merge
169
175
        :return: The merge directive
170
176
 
178
184
        if revision_id == _mod_revision.NULL_REVISION:
179
185
            t_revision_id = None
180
186
        t = testament.StrictTestament3.from_revision(repository, t_revision_id)
181
 
        submit_branch = _mod_branch.Branch.open(target_branch)
 
187
        if local_target_branch is None:
 
188
            submit_branch = _mod_branch.Branch.open(target_branch)
 
189
        else:
 
190
            submit_branch = local_target_branch
182
191
        if submit_branch.get_public_branch() is not None:
183
192
            target_branch = submit_branch.get_public_branch()
184
193
        if patch_type is None:
215
224
        if self.revision_id == revision_id:
216
225
            revno = [revno]
217
226
        else:
218
 
            revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
219
 
                ['merge'])
220
 
        nick = re.sub('(\W+)', '-', branch.nick).strip('-')
 
227
            try:
 
228
                revno = branch.revision_id_to_dotted_revno(self.revision_id)
 
229
            except errors.NoSuchRevision:
 
230
                revno = ['merge']
 
231
        nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
221
232
        return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
222
233
 
223
234
    @staticmethod
224
235
    def _generate_diff(repository, revision_id, ancestor_id):
225
236
        tree_1 = repository.revision_tree(ancestor_id)
226
237
        tree_2 = repository.revision_tree(revision_id)
227
 
        s = StringIO()
 
238
        s = BytesIO()
228
239
        diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
229
240
        return s.getvalue()
230
241
 
231
242
    @staticmethod
232
243
    def _generate_bundle(repository, revision_id, ancestor_id):
233
 
        s = StringIO()
 
244
        s = BytesIO()
234
245
        bundle_serializer.write_bundle(repository, revision_id,
235
246
                                       ancestor_id, s)
236
247
        return s.getvalue()
241
252
        :param branch: The source branch, to get the signing strategy
242
253
        :return: a string
243
254
        """
244
 
        my_gpg = gpg.GPGStrategy(branch.get_config())
245
 
        return my_gpg.sign(''.join(self.to_lines()))
 
255
        my_gpg = gpg.GPGStrategy(branch.get_config_stack())
 
256
        return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
246
257
 
247
258
    def to_email(self, mail_to, branch, sign=False):
248
259
        """Serialize as an email message.
253
264
        :param sign: If True, gpg-sign the email
254
265
        :return: an email message
255
266
        """
256
 
        mail_from = branch.get_config().username()
 
267
        mail_from = branch.get_config_stack().get('email')
257
268
        if self.message is not None:
258
269
            subject = self.message
259
270
        else:
262
273
        if sign:
263
274
            body = self.to_signed(branch)
264
275
        else:
265
 
            body = ''.join(self.to_lines())
266
 
        message = EmailMessage(mail_from, mail_to, subject, body)
 
276
            body = b''.join(self.to_lines())
 
277
        message = email_message.EmailMessage(mail_from, mail_to, subject,
 
278
            body)
267
279
        return message
268
280
 
269
281
    def install_revisions(self, target_repo):
271
283
        if not target_repo.has_revision(self.revision_id):
272
284
            if self.patch_type == 'bundle':
273
285
                info = bundle_serializer.read_bundle(
274
 
                    StringIO(self.get_raw_bundle()))
 
286
                    BytesIO(self.get_raw_bundle()))
275
287
                # We don't use the bundle's target revision, because
276
288
                # MergeDirective.revision_id is authoritative.
277
289
                try:
337
349
                          ' client %s does not support message bodies.',
338
350
                        mail_client.__class__.__name__)
339
351
        mail_client.compose_merge_request(to, subject,
340
 
                                          ''.join(self.to_lines()),
 
352
                                          b''.join(self.to_lines()),
341
353
                                          basename, body)
342
354
 
343
355
 
369
381
            merge.
370
382
        :param time: The current POSIX timestamp time
371
383
        :param timezone: The timezone offset
372
 
        :param target_branch: The branch to apply the merge to
 
384
        :param target_branch: Location of the branch to apply the merge to
373
385
        :param patch: The text of a diff or bundle
374
386
        :param patch_type: None, "diff" or "bundle", depending on the contents
375
387
            of patch
409
421
        :return: a MergeRequest
410
422
        """
411
423
        line_iter = iter(lines)
412
 
        firstline = ""
 
424
        firstline = b""
413
425
        for line in line_iter:
414
 
            if line.startswith('# Bazaar merge directive format '):
415
 
                return _format_registry.get(line[2:].rstrip())._from_lines(
 
426
            if line.startswith(b'# Bazaar merge directive format '):
 
427
                return _format_registry.get(line[2:].rstrip().decode('utf-8', 'replace'))._from_lines(
416
428
                    line_iter)
417
429
            firstline = firstline or line.strip()
418
430
        raise errors.NotAMergeDirective(firstline)
425
437
            patch = None
426
438
            patch_type = None
427
439
        else:
428
 
            patch = ''.join(patch_lines)
 
440
            patch = b''.join(patch_lines)
429
441
            try:
430
 
                bundle_serializer.read_bundle(StringIO(patch))
 
442
                bundle_serializer.read_bundle(BytesIO(patch))
431
443
            except (errors.NotABundle, errors.BundleNotSupported,
432
444
                    errors.BadBundle):
433
445
                patch_type = 'diff'
453
465
 
454
466
    @staticmethod
455
467
    def _generate_bundle(repository, revision_id, ancestor_id):
456
 
        s = StringIO()
 
468
        s = BytesIO()
457
469
        bundle_serializer.write_bundle(repository, revision_id,
458
470
                                       ancestor_id, s, '0.9')
459
471
        return s.getvalue()
506
518
        patch = None
507
519
        bundle = None
508
520
        try:
509
 
            start = line_iter.next()
 
521
            start = next(line_iter)
510
522
        except StopIteration:
511
523
            pass
512
524
        else:
513
 
            if start.startswith('# Begin patch'):
 
525
            if start.startswith(b'# Begin patch'):
514
526
                patch_lines = []
515
527
                for line in line_iter:
516
 
                    if line.startswith('# Begin bundle'):
 
528
                    if line.startswith(b'# Begin bundle'):
517
529
                        start = line
518
530
                        break
519
531
                    patch_lines.append(line)
520
532
                else:
521
533
                    start = None
522
 
                patch = ''.join(patch_lines)
 
534
                patch = b''.join(patch_lines)
523
535
            if start is not None:
524
 
                if start.startswith('# Begin bundle'):
525
 
                    bundle = ''.join(line_iter)
 
536
                if start.startswith(b'# Begin bundle'):
 
537
                    bundle = b''.join(line_iter)
526
538
                else:
527
539
                    raise errors.IllegalMergeDirectivePayload(start)
528
540
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
563
575
        :param target_branch: The url of the branch to merge into
564
576
        :param include_patch: If true, include a preview patch
565
577
        :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.
 
578
        :param local_target_branch: the target branch, either itself or a local copy
 
579
        :param public_branch: location of a public branch containing
 
580
            the target revision.
569
581
        :param message: Message to use when committing the merge
570
582
        :return: The merge directive
571
583
 
580
592
            repository.lock_write()
581
593
            locked.append(repository)
582
594
            t_revision_id = revision_id
583
 
            if revision_id == 'null:':
 
595
            if revision_id == b'null:':
584
596
                t_revision_id = None
585
597
            t = testament.StrictTestament3.from_revision(repository,
586
598
                t_revision_id)
587
 
            submit_branch = _mod_branch.Branch.open(target_branch)
 
599
            if local_target_branch is None:
 
600
                submit_branch = _mod_branch.Branch.open(target_branch)
 
601
            else:
 
602
                submit_branch = local_target_branch
588
603
            submit_branch.lock_read()
589
604
            locked.append(submit_branch)
590
605
            if submit_branch.get_public_branch() is not None: