/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-11 08:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511083616-b8fjb19zomwupid0
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.

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