~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge_directive.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
from StringIO import StringIO
19
19
import re
20
20
 
 
21
from bzrlib import lazy_import
 
22
lazy_import.lazy_import(globals(), """
21
23
from bzrlib import (
22
24
    branch as _mod_branch,
23
25
    diff,
 
26
    email_message,
24
27
    errors,
25
28
    gpg,
 
29
    hooks,
26
30
    registry,
27
31
    revision as _mod_revision,
28
32
    rio,
29
33
    testament,
30
34
    timestamp,
 
35
    trace,
31
36
    )
32
37
from bzrlib.bundle import (
33
38
    serializer as bundle_serializer,
34
39
    )
35
 
from bzrlib.email_message import EmailMessage
36
 
 
37
 
 
38
 
class _BaseMergeDirective(object):
 
40
""")
 
41
 
 
42
 
 
43
class MergeRequestBodyParams(object):
 
44
    """Parameter object for the merge_request_body hook."""
 
45
 
 
46
    def __init__(self, body, orig_body, directive, to, basename, subject,
 
47
                 branch, tree=None):
 
48
        self.body = body
 
49
        self.orig_body = orig_body
 
50
        self.directive = directive
 
51
        self.branch = branch
 
52
        self.tree = tree
 
53
        self.to = to
 
54
        self.basename = basename
 
55
        self.subject = subject
 
56
 
 
57
 
 
58
class MergeDirectiveHooks(hooks.Hooks):
 
59
    """Hooks for MergeDirective classes."""
 
60
 
 
61
    def __init__(self):
 
62
        hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
 
63
        self.add_hook('merge_request_body',
 
64
            "Called with a MergeRequestBodyParams when a body is needed for"
 
65
            " a merge request.  Callbacks must return a body.  If more"
 
66
            " than one callback is registered, the output of one callback is"
 
67
            " provided to the next.", (1, 15, 0))
 
68
 
 
69
 
 
70
class BaseMergeDirective(object):
 
71
    """A request to perform a merge into a branch.
 
72
 
 
73
    This is the base class that all merge directive implementations 
 
74
    should derive from.
 
75
 
 
76
    :cvar multiple_output_files: Whether or not this merge directive 
 
77
        stores a set of revisions in more than one file
 
78
    """
 
79
 
 
80
    hooks = MergeDirectiveHooks()
 
81
 
 
82
    multiple_output_files = False
39
83
 
40
84
    def __init__(self, revision_id, testament_sha1, time, timezone,
41
85
                 target_branch, patch=None, source_branch=None, message=None,
61
105
        self.source_branch = source_branch
62
106
        self.message = message
63
107
 
 
108
    def to_lines(self):
 
109
        """Serialize as a list of lines
 
110
 
 
111
        :return: a list of lines
 
112
        """
 
113
        raise NotImplementedError(self.to_lines)
 
114
 
 
115
    def to_files(self):
 
116
        """Serialize as a set of files.
 
117
 
 
118
        :return: List of tuples with filename and contents as lines
 
119
        """
 
120
        raise NotImplementedError(self.to_files)
 
121
 
 
122
    def get_raw_bundle(self):
 
123
        """Return the bundle for this merge directive.
 
124
 
 
125
        :return: bundle text or None if there is no bundle
 
126
        """
 
127
        return None
 
128
 
64
129
    def _to_lines(self, base_revision=False):
65
130
        """Serialize as a list of lines
66
131
 
80
145
        lines.append('# \n')
81
146
        return lines
82
147
 
 
148
    def write_to_directory(self, path):
 
149
        """Write this merge directive to a series of files in a directory.
 
150
 
 
151
        :param path: Filesystem path to write to
 
152
        """
 
153
        raise NotImplementedError(self.write_to_directory)
 
154
 
83
155
    @classmethod
84
156
    def from_objects(klass, repository, revision_id, time, timezone,
85
157
                 target_branch, patch_type='bundle',
194
266
            body = self.to_signed(branch)
195
267
        else:
196
268
            body = ''.join(self.to_lines())
197
 
        message = EmailMessage(mail_from, mail_to, subject, body)
 
269
        message = email_message.EmailMessage(mail_from, mail_to, subject,
 
270
            body)
198
271
        return message
199
272
 
200
273
    def install_revisions(self, target_repo):
240
313
                target_repo.fetch(source_branch.repository, self.revision_id)
241
314
        return self.revision_id
242
315
 
243
 
 
244
 
class MergeDirective(_BaseMergeDirective):
 
316
    def compose_merge_request(self, mail_client, to, body, branch, tree=None):
 
317
        """Compose a request to merge this directive.
 
318
 
 
319
        :param mail_client: The mail client to use for composing this request.
 
320
        :param to: The address to compose the request to.
 
321
        :param branch: The Branch that was used to produce this directive.
 
322
        :param tree: The Tree (if any) for the Branch used to produce this
 
323
            directive.
 
324
        """
 
325
        basename = self.get_disk_name(branch)
 
326
        subject = '[MERGE] '
 
327
        if self.message is not None:
 
328
            subject += self.message
 
329
        else:
 
330
            revision = branch.repository.get_revision(self.revision_id)
 
331
            subject += revision.get_summary()
 
332
        if getattr(mail_client, 'supports_body', False):
 
333
            orig_body = body
 
334
            for hook in self.hooks['merge_request_body']:
 
335
                params = MergeRequestBodyParams(body, orig_body, self,
 
336
                                                to, basename, subject, branch,
 
337
                                                tree)
 
338
                body = hook(params)
 
339
        elif len(self.hooks['merge_request_body']) > 0:
 
340
            trace.warning('Cannot run merge_request_body hooks because mail'
 
341
                          ' client %s does not support message bodies.',
 
342
                        mail_client.__class__.__name__)
 
343
        mail_client.compose_merge_request(to, subject,
 
344
                                          ''.join(self.to_lines()),
 
345
                                          basename, body)
 
346
 
 
347
 
 
348
class MergeDirective(BaseMergeDirective):
245
349
 
246
350
    """A request to perform a merge into a branch.
247
351
 
276
380
        :param source_branch: A public location to merge the revision from
277
381
        :param message: The message to use when committing this merge
278
382
        """
279
 
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
383
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
280
384
            timezone, target_branch, patch, source_branch, message)
281
385
        if patch_type not in (None, 'diff', 'bundle'):
282
386
            raise ValueError(patch_type)
309
413
        :return: a MergeRequest
310
414
        """
311
415
        line_iter = iter(lines)
 
416
        firstline = ""
312
417
        for line in line_iter:
313
418
            if line.startswith('# Bazaar merge directive format '):
314
 
                break
315
 
        else:
316
 
            if len(lines) > 0:
317
 
                raise errors.NotAMergeDirective(lines[0])
318
 
            else:
319
 
                raise errors.NotAMergeDirective('')
320
 
        return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
 
419
                return _format_registry.get(line[2:].rstrip())._from_lines(
 
420
                    line_iter)
 
421
            firstline = firstline or line.strip()
 
422
        raise errors.NotAMergeDirective(firstline)
321
423
 
322
424
    @classmethod
323
425
    def _from_lines(klass, line_iter):
368
470
        return None, self.revision_id, 'inapplicable'
369
471
 
370
472
 
371
 
class MergeDirective2(_BaseMergeDirective):
 
473
class MergeDirective2(BaseMergeDirective):
372
474
 
373
475
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
374
476
 
377
479
                 bundle=None, base_revision_id=None):
378
480
        if source_branch is None and bundle is None:
379
481
            raise errors.NoMergeSource()
380
 
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
482
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
381
483
            timezone, target_branch, patch, source_branch, message)
382
484
        self.bundle = bundle
383
485
        self.base_revision_id = base_revision_id
520
622
                    revision_id):
521
623
                    raise errors.PublicBranchOutOfDate(public_branch,
522
624
                                                       revision_id)
 
625
            testament_sha1 = t.as_sha1()
523
626
        finally:
524
627
            for entry in reversed(locked):
525
628
                entry.unlock()
526
 
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
527
 
            patch, public_branch, message, bundle, base_revision_id)
 
629
        return klass(revision_id, testament_sha1, time, timezone,
 
630
            target_branch, patch, public_branch, message, bundle,
 
631
            base_revision_id)
528
632
 
529
633
    def _verify_patch(self, repository):
530
634
        calculated_patch = self._generate_diff(repository, self.revision_id,
566
670
_format_registry = MergeDirectiveFormatRegistry()
567
671
_format_registry.register(MergeDirective)
568
672
_format_registry.register(MergeDirective2)
 
673
# 0.19 never existed.  It got renamed to 0.90.  But by that point, there were
 
674
# already merge directives in the wild that used 0.19. Registering with the old
 
675
# format string to retain compatibility with those merge directives.
569
676
_format_registry.register(MergeDirective2,
570
677
                          'Bazaar merge directive format 2 (Bazaar 0.19)')