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

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

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