~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge_directive.py

  • Committer: Alexander Belchenko
  • Date: 2007-09-25 22:13:08 UTC
  • mfrom: (2831.8.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 2867.
  • Revision ID: bialix@ukr.net-20070925221308-aqaqq1u2qv6kpl2z
merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from __future__ import absolute_import
18
17
 
19
18
from StringIO import StringIO
20
19
import re
21
20
 
22
 
from bzrlib import lazy_import
23
 
lazy_import.lazy_import(globals(), """
24
21
from bzrlib import (
25
22
    branch as _mod_branch,
26
23
    diff,
27
 
    email_message,
28
24
    errors,
29
25
    gpg,
30
 
    hooks,
31
26
    registry,
32
27
    revision as _mod_revision,
33
28
    rio,
34
29
    testament,
35
30
    timestamp,
36
 
    trace,
37
31
    )
38
32
from bzrlib.bundle import (
39
33
    serializer as bundle_serializer,
40
34
    )
41
 
""")
42
 
 
43
 
 
44
 
class MergeRequestBodyParams(object):
45
 
    """Parameter object for the merge_request_body hook."""
46
 
 
47
 
    def __init__(self, body, orig_body, directive, to, basename, subject,
48
 
                 branch, tree=None):
49
 
        self.body = body
50
 
        self.orig_body = orig_body
51
 
        self.directive = directive
52
 
        self.branch = branch
53
 
        self.tree = tree
54
 
        self.to = to
55
 
        self.basename = basename
56
 
        self.subject = subject
57
 
 
58
 
 
59
 
class MergeDirectiveHooks(hooks.Hooks):
60
 
    """Hooks for MergeDirective classes."""
61
 
 
62
 
    def __init__(self):
63
 
        hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
64
 
        self.add_hook('merge_request_body',
65
 
            "Called with a MergeRequestBodyParams when a body is needed for"
66
 
            " a merge request.  Callbacks must return a body.  If more"
67
 
            " than one callback is registered, the output of one callback is"
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
 
    """
80
 
 
81
 
    hooks = MergeDirectiveHooks()
82
 
 
83
 
    multiple_output_files = False
 
35
from bzrlib.email_message import EmailMessage
 
36
 
 
37
 
 
38
class _BaseMergeDirective(object):
84
39
 
85
40
    def __init__(self, revision_id, testament_sha1, time, timezone,
86
 
                 target_branch, patch=None, source_branch=None,
87
 
                 message=None, bundle=None):
 
41
                 target_branch, patch=None, source_branch=None, message=None,
 
42
                 bundle=None):
88
43
        """Constructor.
89
44
 
90
45
        :param revision_id: The revision to merge
92
47
            merge.
93
48
        :param time: The current POSIX timestamp time
94
49
        :param timezone: The timezone offset
95
 
        :param target_branch: Location of branch to apply the merge to
 
50
        :param target_branch: The branch to apply the merge to
96
51
        :param patch: The text of a diff or bundle
97
52
        :param source_branch: A public location to merge the revision from
98
53
        :param message: The message to use when committing this merge
106
61
        self.source_branch = source_branch
107
62
        self.message = message
108
63
 
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
 
 
130
64
    def _to_lines(self, base_revision=False):
131
65
        """Serialize as a list of lines
132
66
 
146
80
        lines.append('# \n')
147
81
        return lines
148
82
 
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
 
 
156
83
    @classmethod
157
84
    def from_objects(klass, repository, revision_id, time, timezone,
158
85
                 target_branch, patch_type='bundle',
166
93
        :param target_branch: The url of the branch to merge into
167
94
        :param patch_type: 'bundle', 'diff' or None, depending on the type of
168
95
            patch desired.
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.
 
96
        :param local_target_branch: a local copy of the target branch
 
97
        :param public_branch: location of a public branch containing the target
 
98
            revision.
172
99
        :param message: Message to use when committing the merge
173
100
        :return: The merge directive
174
101
 
182
109
        if revision_id == _mod_revision.NULL_REVISION:
183
110
            t_revision_id = None
184
111
        t = testament.StrictTestament3.from_revision(repository, t_revision_id)
185
 
        if local_target_branch is None:
186
 
            submit_branch = _mod_branch.Branch.open(target_branch)
187
 
        else:
188
 
            submit_branch = local_target_branch
 
112
        submit_branch = _mod_branch.Branch.open(target_branch)
189
113
        if submit_branch.get_public_branch() is not None:
190
114
            target_branch = submit_branch.get_public_branch()
191
115
        if patch_type is None:
212
136
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
213
137
            patch, patch_type, public_branch, message)
214
138
 
215
 
    def get_disk_name(self, branch):
216
 
        """Generate a suitable basename for storing this directive on disk
217
 
 
218
 
        :param branch: The Branch this merge directive was generated fro
219
 
        :return: A string
220
 
        """
221
 
        revno, revision_id = branch.last_revision_info()
222
 
        if self.revision_id == revision_id:
223
 
            revno = [revno]
224
 
        else:
225
 
            revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
226
 
                ['merge'])
227
 
        nick = re.sub('(\W+)', '-', branch.nick).strip('-')
228
 
        return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
229
 
 
230
139
    @staticmethod
231
140
    def _generate_diff(repository, revision_id, ancestor_id):
232
141
        tree_1 = repository.revision_tree(ancestor_id)
248
157
        :param branch: The source branch, to get the signing strategy
249
158
        :return: a string
250
159
        """
251
 
        my_gpg = gpg.GPGStrategy(branch.get_config_stack())
 
160
        my_gpg = gpg.GPGStrategy(branch.get_config())
252
161
        return my_gpg.sign(''.join(self.to_lines()))
253
162
 
254
163
    def to_email(self, mail_to, branch, sign=False):
260
169
        :param sign: If True, gpg-sign the email
261
170
        :return: an email message
262
171
        """
263
 
        mail_from = branch.get_config_stack().get('email')
 
172
        mail_from = branch.get_config().username()
264
173
        if self.message is not None:
265
174
            subject = self.message
266
175
        else:
270
179
            body = self.to_signed(branch)
271
180
        else:
272
181
            body = ''.join(self.to_lines())
273
 
        message = email_message.EmailMessage(mail_from, mail_to, subject,
274
 
            body)
 
182
        message = EmailMessage(mail_from, mail_to, subject, body)
275
183
        return message
276
184
 
277
185
    def install_revisions(self, target_repo):
282
190
                    StringIO(self.get_raw_bundle()))
283
191
                # We don't use the bundle's target revision, because
284
192
                # MergeDirective.revision_id is authoritative.
285
 
                try:
286
 
                    info.install_revisions(target_repo, stream_input=False)
287
 
                except errors.RevisionNotPresent:
288
 
                    # At least one dependency isn't present.  Try installing
289
 
                    # missing revisions from the submit branch
290
 
                    try:
291
 
                        submit_branch = \
292
 
                            _mod_branch.Branch.open(self.target_branch)
293
 
                    except errors.NotBranchError:
294
 
                        raise errors.TargetNotBranch(self.target_branch)
295
 
                    missing_revisions = []
296
 
                    bundle_revisions = set(r.revision_id for r in
297
 
                                           info.real_revisions)
298
 
                    for revision in info.real_revisions:
299
 
                        for parent_id in revision.parent_ids:
300
 
                            if (parent_id not in bundle_revisions and
301
 
                                not target_repo.has_revision(parent_id)):
302
 
                                missing_revisions.append(parent_id)
303
 
                    # reverse missing revisions to try to get heads first
304
 
                    unique_missing = []
305
 
                    unique_missing_set = set()
306
 
                    for revision in reversed(missing_revisions):
307
 
                        if revision in unique_missing_set:
308
 
                            continue
309
 
                        unique_missing.append(revision)
310
 
                        unique_missing_set.add(revision)
311
 
                    for missing_revision in unique_missing:
312
 
                        target_repo.fetch(submit_branch.repository,
313
 
                                          missing_revision)
314
 
                    info.install_revisions(target_repo, stream_input=False)
 
193
                info.install_revisions(target_repo, stream_input=False)
315
194
            else:
316
195
                source_branch = _mod_branch.Branch.open(self.source_branch)
317
196
                target_repo.fetch(source_branch.repository, self.revision_id)
318
197
        return self.revision_id
319
198
 
320
 
    def compose_merge_request(self, mail_client, to, body, branch, tree=None):
321
 
        """Compose a request to merge this directive.
322
 
 
323
 
        :param mail_client: The mail client to use for composing this request.
324
 
        :param to: The address to compose the request to.
325
 
        :param branch: The Branch that was used to produce this directive.
326
 
        :param tree: The Tree (if any) for the Branch used to produce this
327
 
            directive.
328
 
        """
329
 
        basename = self.get_disk_name(branch)
330
 
        subject = '[MERGE] '
331
 
        if self.message is not None:
332
 
            subject += self.message
333
 
        else:
334
 
            revision = branch.repository.get_revision(self.revision_id)
335
 
            subject += revision.get_summary()
336
 
        if getattr(mail_client, 'supports_body', False):
337
 
            orig_body = body
338
 
            for hook in self.hooks['merge_request_body']:
339
 
                params = MergeRequestBodyParams(body, orig_body, self,
340
 
                                                to, basename, subject, branch,
341
 
                                                tree)
342
 
                body = hook(params)
343
 
        elif len(self.hooks['merge_request_body']) > 0:
344
 
            trace.warning('Cannot run merge_request_body hooks because mail'
345
 
                          ' client %s does not support message bodies.',
346
 
                        mail_client.__class__.__name__)
347
 
        mail_client.compose_merge_request(to, subject,
348
 
                                          ''.join(self.to_lines()),
349
 
                                          basename, body)
350
 
 
351
 
 
352
 
class MergeDirective(BaseMergeDirective):
 
199
 
 
200
class MergeDirective(_BaseMergeDirective):
353
201
 
354
202
    """A request to perform a merge into a branch.
355
203
 
377
225
            merge.
378
226
        :param time: The current POSIX timestamp time
379
227
        :param timezone: The timezone offset
380
 
        :param target_branch: Location of the branch to apply the merge to
 
228
        :param target_branch: The branch to apply the merge to
381
229
        :param patch: The text of a diff or bundle
382
230
        :param patch_type: None, "diff" or "bundle", depending on the contents
383
231
            of patch
384
232
        :param source_branch: A public location to merge the revision from
385
233
        :param message: The message to use when committing this merge
386
234
        """
387
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
235
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
388
236
            timezone, target_branch, patch, source_branch, message)
389
 
        if patch_type not in (None, 'diff', 'bundle'):
390
 
            raise ValueError(patch_type)
 
237
        assert patch_type in (None, 'diff', 'bundle'), patch_type
391
238
        if patch_type != 'bundle' and source_branch is None:
392
239
            raise errors.NoMergeSource()
393
240
        if patch_type is not None and patch is None:
417
264
        :return: a MergeRequest
418
265
        """
419
266
        line_iter = iter(lines)
420
 
        firstline = ""
421
267
        for line in line_iter:
422
268
            if line.startswith('# Bazaar merge directive format '):
423
 
                return _format_registry.get(line[2:].rstrip())._from_lines(
424
 
                    line_iter)
425
 
            firstline = firstline or line.strip()
426
 
        raise errors.NotAMergeDirective(firstline)
 
269
                break
 
270
        else:
 
271
            if len(lines) > 0:
 
272
                raise errors.NotAMergeDirective(lines[0])
 
273
            else:
 
274
                raise errors.NotAMergeDirective('')
 
275
        return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
427
276
 
428
277
    @classmethod
429
278
    def _from_lines(klass, line_iter):
474
323
        return None, self.revision_id, 'inapplicable'
475
324
 
476
325
 
477
 
class MergeDirective2(BaseMergeDirective):
 
326
class MergeDirective2(_BaseMergeDirective):
478
327
 
479
328
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
480
329
 
483
332
                 bundle=None, base_revision_id=None):
484
333
        if source_branch is None and bundle is None:
485
334
            raise errors.NoMergeSource()
486
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
335
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
487
336
            timezone, target_branch, patch, source_branch, message)
488
337
        self.bundle = bundle
489
338
        self.base_revision_id = base_revision_id
571
420
        :param target_branch: The url of the branch to merge into
572
421
        :param include_patch: If true, include a preview patch
573
422
        :param include_bundle: If true, include a bundle
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.
 
423
        :param local_target_branch: a local copy of the target branch
 
424
        :param public_branch: location of a public branch containing the target
 
425
            revision.
577
426
        :param message: Message to use when committing the merge
578
427
        :return: The merge directive
579
428
 
592
441
                t_revision_id = None
593
442
            t = testament.StrictTestament3.from_revision(repository,
594
443
                t_revision_id)
595
 
            if local_target_branch is None:
596
 
                submit_branch = _mod_branch.Branch.open(target_branch)
597
 
            else:
598
 
                submit_branch = local_target_branch
 
444
            submit_branch = _mod_branch.Branch.open(target_branch)
599
445
            submit_branch.lock_read()
600
446
            locked.append(submit_branch)
601
447
            if submit_branch.get_public_branch() is not None:
629
475
                    revision_id):
630
476
                    raise errors.PublicBranchOutOfDate(public_branch,
631
477
                                                       revision_id)
632
 
            testament_sha1 = t.as_sha1()
633
478
        finally:
634
479
            for entry in reversed(locked):
635
480
                entry.unlock()
636
 
        return klass(revision_id, testament_sha1, time, timezone,
637
 
            target_branch, patch, public_branch, message, bundle,
638
 
            base_revision_id)
 
481
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
 
482
            patch, public_branch, message, bundle, base_revision_id)
639
483
 
640
484
    def _verify_patch(self, repository):
641
485
        calculated_patch = self._generate_diff(repository, self.revision_id,
677
521
_format_registry = MergeDirectiveFormatRegistry()
678
522
_format_registry.register(MergeDirective)
679
523
_format_registry.register(MergeDirective2)
680
 
# 0.19 never existed.  It got renamed to 0.90.  But by that point, there were
681
 
# already merge directives in the wild that used 0.19. Registering with the old
682
 
# format string to retain compatibility with those merge directives.
683
524
_format_registry.register(MergeDirective2,
684
525
                          'Bazaar merge directive format 2 (Bazaar 0.19)')