~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge_directive.py

  • Committer: Ian Clatworthy
  • Date: 2009-07-22 14:07:56 UTC
  • mto: (4568.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4569.
  • Revision ID: ian.clatworthy@canonical.com-20090722140756-rx3dbtf3rlubfy4r
Improve the names and location of the quick reference cards

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
18
18
from StringIO import StringIO
19
19
import re
20
20
 
21
 
from bzrlib import lazy_import
22
 
lazy_import.lazy_import(globals(), """
23
21
from bzrlib import (
24
22
    branch as _mod_branch,
25
23
    diff,
26
 
    email_message,
27
24
    errors,
28
25
    gpg,
29
26
    hooks,
37
34
from bzrlib.bundle import (
38
35
    serializer as bundle_serializer,
39
36
    )
40
 
""")
 
37
from bzrlib.email_message import EmailMessage
41
38
 
42
39
 
43
40
class MergeRequestBodyParams(object):
59
56
    """Hooks for MergeDirective classes."""
60
57
 
61
58
    def __init__(self):
62
 
        hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
63
 
        self.add_hook('merge_request_body',
 
59
        hooks.Hooks.__init__(self)
 
60
        self.create_hook(hooks.HookPoint('merge_request_body',
64
61
            "Called with a MergeRequestBodyParams when a body is needed for"
65
62
            " a merge request.  Callbacks must return a body.  If more"
66
63
            " 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
 
    """
 
64
            " provided to the next.", (1, 15, 0), False))
 
65
 
 
66
 
 
67
class _BaseMergeDirective(object):
79
68
 
80
69
    hooks = MergeDirectiveHooks()
81
70
 
82
 
    multiple_output_files = False
83
 
 
84
71
    def __init__(self, revision_id, testament_sha1, time, timezone,
85
72
                 target_branch, patch=None, source_branch=None, message=None,
86
73
                 bundle=None):
105
92
        self.source_branch = source_branch
106
93
        self.message = message
107
94
 
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
 
 
129
95
    def _to_lines(self, base_revision=False):
130
96
        """Serialize as a list of lines
131
97
 
145
111
        lines.append('# \n')
146
112
        return lines
147
113
 
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
 
 
155
114
    @classmethod
156
115
    def from_objects(klass, repository, revision_id, time, timezone,
157
116
                 target_branch, patch_type='bundle',
266
225
            body = self.to_signed(branch)
267
226
        else:
268
227
            body = ''.join(self.to_lines())
269
 
        message = email_message.EmailMessage(mail_from, mail_to, subject,
270
 
            body)
 
228
        message = EmailMessage(mail_from, mail_to, subject, body)
271
229
        return message
272
230
 
273
231
    def install_revisions(self, target_repo):
345
303
                                          basename, body)
346
304
 
347
305
 
348
 
class MergeDirective(BaseMergeDirective):
 
306
class MergeDirective(_BaseMergeDirective):
349
307
 
350
308
    """A request to perform a merge into a branch.
351
309
 
380
338
        :param source_branch: A public location to merge the revision from
381
339
        :param message: The message to use when committing this merge
382
340
        """
383
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
341
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
384
342
            timezone, target_branch, patch, source_branch, message)
385
343
        if patch_type not in (None, 'diff', 'bundle'):
386
344
            raise ValueError(patch_type)
413
371
        :return: a MergeRequest
414
372
        """
415
373
        line_iter = iter(lines)
416
 
        firstline = ""
417
374
        for line in line_iter:
418
375
            if line.startswith('# Bazaar merge directive format '):
419
 
                return _format_registry.get(line[2:].rstrip())._from_lines(
420
 
                    line_iter)
421
 
            firstline = firstline or line.strip()
422
 
        raise errors.NotAMergeDirective(firstline)
 
376
                break
 
377
        else:
 
378
            if len(lines) > 0:
 
379
                raise errors.NotAMergeDirective(lines[0])
 
380
            else:
 
381
                raise errors.NotAMergeDirective('')
 
382
        return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
423
383
 
424
384
    @classmethod
425
385
    def _from_lines(klass, line_iter):
470
430
        return None, self.revision_id, 'inapplicable'
471
431
 
472
432
 
473
 
class MergeDirective2(BaseMergeDirective):
 
433
class MergeDirective2(_BaseMergeDirective):
474
434
 
475
435
    _format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
476
436
 
479
439
                 bundle=None, base_revision_id=None):
480
440
        if source_branch is None and bundle is None:
481
441
            raise errors.NoMergeSource()
482
 
        BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
 
442
        _BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
483
443
            timezone, target_branch, patch, source_branch, message)
484
444
        self.bundle = bundle
485
445
        self.base_revision_id = base_revision_id
622
582
                    revision_id):
623
583
                    raise errors.PublicBranchOutOfDate(public_branch,
624
584
                                                       revision_id)
625
 
            testament_sha1 = t.as_sha1()
626
585
        finally:
627
586
            for entry in reversed(locked):
628
587
                entry.unlock()
629
 
        return klass(revision_id, testament_sha1, time, timezone,
630
 
            target_branch, patch, public_branch, message, bundle,
631
 
            base_revision_id)
 
588
        return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
 
589
            patch, public_branch, message, bundle, base_revision_id)
632
590
 
633
591
    def _verify_patch(self, repository):
634
592
        calculated_patch = self._generate_diff(repository, self.revision_id,