/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: Aaron Bentley
  • Date: 2009-05-11 18:35:20 UTC
  • mto: This revision was merged to the branch mainline in revision 4351.
  • Revision ID: aaron@aaronbentley.com-20090511183520-xtvepoauy7zjtb7x
Move hook to MergeDirective, implement MergeDirective.compose_merge_request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 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
29
29
    rio,
30
30
    testament,
31
31
    timestamp,
32
 
    trace,
33
32
    )
34
33
from bzrlib.bundle import (
35
34
    serializer as bundle_serializer,
38
37
 
39
38
 
40
39
class MergeRequestBodyParams(object):
41
 
    """Parameter object for the merge_request_body hook."""
42
40
 
43
41
    def __init__(self, body, orig_body, directive, to, basename, subject,
44
42
                 branch, tree=None):
53
51
 
54
52
 
55
53
class MergeDirectiveHooks(hooks.Hooks):
56
 
    """Hooks for MergeDirective classes."""
57
54
 
58
55
    def __init__(self):
59
56
        hooks.Hooks.__init__(self)
64
61
            " provided to the next.", (1, 15, 0), False))
65
62
 
66
63
 
67
 
class BaseMergeDirective(object):
68
 
    """A request to perform a merge into a branch.
69
 
 
70
 
    This is the base class that all merge directive implementations 
71
 
    should derive from.
72
 
 
73
 
    :cvar multiple_output_files: Whether or not this merge directive 
74
 
        stores a set of revisions in more than one file
75
 
    """
 
64
class _BaseMergeDirective(object):
76
65
 
77
66
    hooks = MergeDirectiveHooks()
78
67
 
79
 
    multiple_output_files = False
80
 
 
81
68
    def __init__(self, revision_id, testament_sha1, time, timezone,
82
69
                 target_branch, patch=None, source_branch=None, message=None,
83
70
                 bundle=None):
102
89
        self.source_branch = source_branch
103
90
        self.message = message
104
91
 
105
 
    def to_lines(self):
106
 
        """Serialize as a list of lines
107
 
 
108
 
        :return: a list of lines
109
 
        """
110
 
        raise NotImplementedError(self.to_lines)
111
 
 
112
 
    def to_files(self):
113
 
        """Serialize as a set of files.
114
 
 
115
 
        :return: List of tuples with filename and contents as lines
116
 
        """
117
 
        raise NotImplementedError(self.to_files)
118
 
 
119
 
    def get_raw_bundle(self):
120
 
        """Return the bundle for this merge directive.
121
 
 
122
 
        :return: bundle text or None if there is no bundle
123
 
        """
124
 
        return None
125
 
 
126
92
    def _to_lines(self, base_revision=False):
127
93
        """Serialize as a list of lines
128
94
 
142
108
        lines.append('# \n')
143
109
        return lines
144
110
 
145
 
    def write_to_directory(self, path):
146
 
        """Write this merge directive to a series of files in a directory.
147
 
 
148
 
        :param path: Filesystem path to write to
149
 
        """
150
 
        raise NotImplementedError(self.write_to_directory)
151
 
 
152
111
    @classmethod
153
112
    def from_objects(klass, repository, revision_id, time, timezone,
154
113
                 target_branch, patch_type='bundle',
310
269
        return self.revision_id
311
270
 
312
271
    def compose_merge_request(self, mail_client, to, body, branch, tree=None):
313
 
        """Compose a request to merge this directive.
314
 
 
315
 
        :param mail_client: The mail client to use for composing this request.
316
 
        :param to: The address to compose the request to.
317
 
        :param branch: The Branch that was used to produce this directive.
318
 
        :param tree: The Tree (if any) for the Branch used to produce this
319
 
            directive.
320
 
        """
321
272
        basename = self.get_disk_name(branch)
322
273
        subject = '[MERGE] '
323
274
        if self.message is not None:
325
276
        else:
326
277
            revision = branch.repository.get_revision(self.revision_id)
327
278
            subject += revision.get_summary()
328
 
        if getattr(mail_client, 'supports_body', False):
329
 
            orig_body = body
330
 
            for hook in self.hooks['merge_request_body']:
331
 
                params = MergeRequestBodyParams(body, orig_body, self,
332
 
                                                to, basename, subject, branch,
333
 
                                                tree)
334
 
                body = hook(params)
335
 
        elif len(self.hooks['merge_request_body']) > 0:
336
 
            trace.warning('Cannot run merge_request_body hooks because mail'
337
 
                          ' client %s does not support message bodies.',
338
 
                        mail_client.__class__.__name__)
 
279
        orig_body = body
 
280
        for hook in self.hooks['merge_request_body']:
 
281
            params = MergeRequestBodyParams(body, orig_body, self,
 
282
                                            to, basename, subject, branch,
 
283
                                            tree)
 
284
            body = hook(params)
339
285
        mail_client.compose_merge_request(to, subject,
340
286
                                          ''.join(self.to_lines()),
341
287
                                          basename, body)
342
288
 
343
289
 
344
 
class MergeDirective(BaseMergeDirective):
 
290
class MergeDirective(_BaseMergeDirective):
345
291
 
346
292
    """A request to perform a merge into a branch.
347
293
 
376
322
        :param source_branch: A public location to merge the revision from
377
323
        :param message: The message to use when committing this merge
378
324
        """
379
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
325
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
380
326
            timezone, target_branch, patch, source_branch, message)
381
327
        if patch_type not in (None, 'diff', 'bundle'):
382
328
            raise ValueError(patch_type)
409
355
        :return: a MergeRequest
410
356
        """
411
357
        line_iter = iter(lines)
412
 
        firstline = ""
413
358
        for line in line_iter:
414
359
            if line.startswith('# Bazaar merge directive format '):
415
 
                return _format_registry.get(line[2:].rstrip())._from_lines(
416
 
                    line_iter)
417
 
            firstline = firstline or line.strip()
418
 
        raise errors.NotAMergeDirective(firstline)
 
360
                break
 
361
        else:
 
362
            if len(lines) > 0:
 
363
                raise errors.NotAMergeDirective(lines[0])
 
364
            else:
 
365
                raise errors.NotAMergeDirective('')
 
366
        return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
419
367
 
420
368
    @classmethod
421
369
    def _from_lines(klass, line_iter):
466
414
        return None, self.revision_id, 'inapplicable'
467
415
 
468
416
 
469
 
class MergeDirective2(BaseMergeDirective):
 
417
class MergeDirective2(_BaseMergeDirective):
470
418
 
471
419
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
472
420
 
475
423
                 bundle=None, base_revision_id=None):
476
424
        if source_branch is None and bundle is None:
477
425
            raise errors.NoMergeSource()
478
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
426
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
479
427
            timezone, target_branch, patch, source_branch, message)
480
428
        self.bundle = bundle
481
429
        self.base_revision_id = base_revision_id
618
566
                    revision_id):
619
567
                    raise errors.PublicBranchOutOfDate(public_branch,
620
568
                                                       revision_id)
621
 
            testament_sha1 = t.as_sha1()
622
569
        finally:
623
570
            for entry in reversed(locked):
624
571
                entry.unlock()
625
 
        return klass(revision_id, testament_sha1, time, timezone,
626
 
            target_branch, patch, public_branch, message, bundle,
627
 
            base_revision_id)
 
572
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
 
573
            patch, public_branch, message, bundle, base_revision_id)
628
574
 
629
575
    def _verify_patch(self, repository):
630
576
        calculated_patch = self._generate_diff(repository, self.revision_id,