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