~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge_directive.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

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
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
19
from StringIO import StringIO
19
20
import re
20
21
 
 
22
from bzrlib import lazy_import
 
23
lazy_import.lazy_import(globals(), """
21
24
from bzrlib import (
22
25
    branch as _mod_branch,
23
26
    diff,
 
27
    email_message,
24
28
    errors,
25
29
    gpg,
26
30
    hooks,
34
38
from bzrlib.bundle import (
35
39
    serializer as bundle_serializer,
36
40
    )
37
 
from bzrlib.email_message import EmailMessage
 
41
""")
38
42
 
39
43
 
40
44
class MergeRequestBodyParams(object):
56
60
    """Hooks for MergeDirective classes."""
57
61
 
58
62
    def __init__(self):
59
 
        hooks.Hooks.__init__(self)
60
 
        self.create_hook(hooks.HookPoint('merge_request_body',
 
63
        hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
 
64
        self.add_hook('merge_request_body',
61
65
            "Called with a MergeRequestBodyParams when a body is needed for"
62
66
            " a merge request.  Callbacks must return a body.  If more"
63
67
            " than one callback is registered, the output of one callback is"
64
 
            " provided to the next.", (1, 15, 0), False))
65
 
 
66
 
 
67
 
class _BaseMergeDirective(object):
 
68
            " provided to the next.", (1, 15, 0))
 
69
 
 
70
 
 
71
class BaseMergeDirective(object):
 
72
    """A request to perform a merge into a branch.
 
73
 
 
74
    This is the base class that all merge directive implementations 
 
75
    should derive from.
 
76
 
 
77
    :cvar multiple_output_files: Whether or not this merge directive 
 
78
        stores a set of revisions in more than one file
 
79
    """
68
80
 
69
81
    hooks = MergeDirectiveHooks()
70
82
 
 
83
    multiple_output_files = False
 
84
 
71
85
    def __init__(self, revision_id, testament_sha1, time, timezone,
72
 
                 target_branch, patch=None, source_branch=None, message=None,
73
 
                 bundle=None):
 
86
                 target_branch, patch=None, source_branch=None,
 
87
                 message=None, bundle=None):
74
88
        """Constructor.
75
89
 
76
90
        :param revision_id: The revision to merge
78
92
            merge.
79
93
        :param time: The current POSIX timestamp time
80
94
        :param timezone: The timezone offset
81
 
        :param target_branch: The branch to apply the merge to
 
95
        :param target_branch: Location of branch to apply the merge to
82
96
        :param patch: The text of a diff or bundle
83
97
        :param source_branch: A public location to merge the revision from
84
98
        :param message: The message to use when committing this merge
92
106
        self.source_branch = source_branch
93
107
        self.message = message
94
108
 
 
109
    def to_lines(self):
 
110
        """Serialize as a list of lines
 
111
 
 
112
        :return: a list of lines
 
113
        """
 
114
        raise NotImplementedError(self.to_lines)
 
115
 
 
116
    def to_files(self):
 
117
        """Serialize as a set of files.
 
118
 
 
119
        :return: List of tuples with filename and contents as lines
 
120
        """
 
121
        raise NotImplementedError(self.to_files)
 
122
 
 
123
    def get_raw_bundle(self):
 
124
        """Return the bundle for this merge directive.
 
125
 
 
126
        :return: bundle text or None if there is no bundle
 
127
        """
 
128
        return None
 
129
 
95
130
    def _to_lines(self, base_revision=False):
96
131
        """Serialize as a list of lines
97
132
 
111
146
        lines.append('# \n')
112
147
        return lines
113
148
 
 
149
    def write_to_directory(self, path):
 
150
        """Write this merge directive to a series of files in a directory.
 
151
 
 
152
        :param path: Filesystem path to write to
 
153
        """
 
154
        raise NotImplementedError(self.write_to_directory)
 
155
 
114
156
    @classmethod
115
157
    def from_objects(klass, repository, revision_id, time, timezone,
116
158
                 target_branch, patch_type='bundle',
124
166
        :param target_branch: The url of the branch to merge into
125
167
        :param patch_type: 'bundle', 'diff' or None, depending on the type of
126
168
            patch desired.
127
 
        :param local_target_branch: a local copy of the target branch
128
 
        :param public_branch: location of a public branch containing the target
129
 
            revision.
 
169
        :param local_target_branch: the submit branch, either itself or a local copy
 
170
        :param public_branch: location of a public branch containing
 
171
            the target revision.
130
172
        :param message: Message to use when committing the merge
131
173
        :return: The merge directive
132
174
 
140
182
        if revision_id == _mod_revision.NULL_REVISION:
141
183
            t_revision_id = None
142
184
        t = testament.StrictTestament3.from_revision(repository, t_revision_id)
143
 
        submit_branch = _mod_branch.Branch.open(target_branch)
 
185
        if local_target_branch is None:
 
186
            submit_branch = _mod_branch.Branch.open(target_branch)
 
187
        else:
 
188
            submit_branch = local_target_branch
144
189
        if submit_branch.get_public_branch() is not None:
145
190
            target_branch = submit_branch.get_public_branch()
146
191
        if patch_type is None:
203
248
        :param branch: The source branch, to get the signing strategy
204
249
        :return: a string
205
250
        """
206
 
        my_gpg = gpg.GPGStrategy(branch.get_config())
 
251
        my_gpg = gpg.GPGStrategy(branch.get_config_stack())
207
252
        return my_gpg.sign(''.join(self.to_lines()))
208
253
 
209
254
    def to_email(self, mail_to, branch, sign=False):
225
270
            body = self.to_signed(branch)
226
271
        else:
227
272
            body = ''.join(self.to_lines())
228
 
        message = EmailMessage(mail_from, mail_to, subject, body)
 
273
        message = email_message.EmailMessage(mail_from, mail_to, subject,
 
274
            body)
229
275
        return message
230
276
 
231
277
    def install_revisions(self, target_repo):
303
349
                                          basename, body)
304
350
 
305
351
 
306
 
class MergeDirective(_BaseMergeDirective):
 
352
class MergeDirective(BaseMergeDirective):
307
353
 
308
354
    """A request to perform a merge into a branch.
309
355
 
331
377
            merge.
332
378
        :param time: The current POSIX timestamp time
333
379
        :param timezone: The timezone offset
334
 
        :param target_branch: The branch to apply the merge to
 
380
        :param target_branch: Location of the branch to apply the merge to
335
381
        :param patch: The text of a diff or bundle
336
382
        :param patch_type: None, "diff" or "bundle", depending on the contents
337
383
            of patch
338
384
        :param source_branch: A public location to merge the revision from
339
385
        :param message: The message to use when committing this merge
340
386
        """
341
 
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
387
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
342
388
            timezone, target_branch, patch, source_branch, message)
343
389
        if patch_type not in (None, 'diff', 'bundle'):
344
390
            raise ValueError(patch_type)
428
474
        return None, self.revision_id, 'inapplicable'
429
475
 
430
476
 
431
 
class MergeDirective2(_BaseMergeDirective):
 
477
class MergeDirective2(BaseMergeDirective):
432
478
 
433
479
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
434
480
 
437
483
                 bundle=None, base_revision_id=None):
438
484
        if source_branch is None and bundle is None:
439
485
            raise errors.NoMergeSource()
440
 
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
486
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
441
487
            timezone, target_branch, patch, source_branch, message)
442
488
        self.bundle = bundle
443
489
        self.base_revision_id = base_revision_id
525
571
        :param target_branch: The url of the branch to merge into
526
572
        :param include_patch: If true, include a preview patch
527
573
        :param include_bundle: If true, include a bundle
528
 
        :param local_target_branch: a local copy of the target branch
529
 
        :param public_branch: location of a public branch containing the target
530
 
            revision.
 
574
        :param local_target_branch: the target branch, either itself or a local copy
 
575
        :param public_branch: location of a public branch containing
 
576
            the target revision.
531
577
        :param message: Message to use when committing the merge
532
578
        :return: The merge directive
533
579
 
546
592
                t_revision_id = None
547
593
            t = testament.StrictTestament3.from_revision(repository,
548
594
                t_revision_id)
549
 
            submit_branch = _mod_branch.Branch.open(target_branch)
 
595
            if local_target_branch is None:
 
596
                submit_branch = _mod_branch.Branch.open(target_branch)
 
597
            else:
 
598
                submit_branch = local_target_branch
550
599
            submit_branch.lock_read()
551
600
            locked.append(submit_branch)
552
601
            if submit_branch.get_public_branch() is not None: