/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: Canonical.com Patch Queue Manager
  • Date: 2007-03-28 06:58:22 UTC
  • mfrom: (2379.2.3 hpss-chroot)
  • Revision ID: pqm@pqm.ubuntu.com-20070328065822-999550a858a3ced3
(robertc) Fix chroot urls to not expose the url of the transport they are protecting, allowing regular url operations to work on them. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
 
 
18
from email import Message
 
19
from StringIO import StringIO
 
20
 
 
21
from bzrlib import (
 
22
    branch as _mod_branch,
 
23
    diff,
 
24
    errors,
 
25
    gpg,
 
26
    revision as _mod_revision,
 
27
    rio,
 
28
    testament,
 
29
    timestamp,
 
30
    )
 
31
from bzrlib.bundle import serializer as bundle_serializer
 
32
 
 
33
 
 
34
class MergeDirective(object):
 
35
 
 
36
    """A request to perform a merge into a branch.
 
37
 
 
38
    Designed to be serialized and mailed.  It provides all the information
 
39
    needed to perform a merge automatically, by providing at minimum a revision
 
40
    bundle or the location of a branch.
 
41
 
 
42
    The serialization format is robust against certain common forms of
 
43
    deterioration caused by mailing.
 
44
 
 
45
    The format is also designed to be patch-compatible.  If the directive
 
46
    includes a diff or revision bundle, it should be possible to apply it
 
47
    directly using the standard patch program.
 
48
    """
 
49
 
 
50
    _format_string = 'Bazaar merge directive format 1'
 
51
 
 
52
    def __init__(self, revision_id, testament_sha1, time, timezone,
 
53
                 target_branch, patch=None, patch_type=None,
 
54
                 source_branch=None, message=None):
 
55
        """Constructor.
 
56
 
 
57
        :param revision_id: The revision to merge
 
58
        :param testament_sha1: The sha1 of the testament of the revision to
 
59
            merge.
 
60
        :param time: The current POSIX timestamp time
 
61
        :param timezone: The timezone offset
 
62
        :param target_branch: The branch to apply the merge to
 
63
        :param patch: The text of a diff or bundle
 
64
        :param patch_type: None, "diff" or "bundle", depending on the contents
 
65
            of patch
 
66
        :param source_branch: A public location to merge the revision from
 
67
        :param message: The message to use when committing this merge
 
68
        """
 
69
        assert patch_type in (None, 'diff', 'bundle')
 
70
        if patch_type != 'bundle' and source_branch is None:
 
71
            raise errors.NoMergeSource()
 
72
        if patch_type is not None and patch is None:
 
73
            raise errors.PatchMissing(patch_type)
 
74
        self.revision_id = revision_id
 
75
        self.testament_sha1 = testament_sha1
 
76
        self.time = time
 
77
        self.timezone = timezone
 
78
        self.target_branch = target_branch
 
79
        self.patch = patch
 
80
        self.patch_type = patch_type
 
81
        self.source_branch = source_branch
 
82
        self.message = message
 
83
 
 
84
    @classmethod
 
85
    def from_lines(klass, lines):
 
86
        """Deserialize a MergeRequest from an iterable of lines
 
87
 
 
88
        :param lines: An iterable of lines
 
89
        :return: a MergeRequest
 
90
        """
 
91
        line_iter = iter(lines)
 
92
        for line in line_iter:
 
93
            if line.startswith('# ' + klass._format_string):
 
94
                break
 
95
        else:
 
96
            if len(lines) > 0:
 
97
                raise errors.NotAMergeDirective(lines[0])
 
98
            else:
 
99
                raise errors.NotAMergeDirective('')
 
100
        stanza = rio.read_patch_stanza(line_iter)
 
101
        patch_lines = list(line_iter)
 
102
        if len(patch_lines) == 0:
 
103
            patch = None
 
104
            patch_type = None
 
105
        else:
 
106
            patch = ''.join(patch_lines)
 
107
            try:
 
108
                bundle_serializer.read_bundle(StringIO(patch))
 
109
            except errors.NotABundle:
 
110
                patch_type = 'diff'
 
111
            else:
 
112
                patch_type = 'bundle'
 
113
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
 
114
        kwargs = {}
 
115
        for key in ('revision_id', 'testament_sha1', 'target_branch',
 
116
                    'source_branch', 'message'):
 
117
            try:
 
118
                kwargs[key] = stanza.get(key)
 
119
            except KeyError:
 
120
                pass
 
121
        kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
 
122
        return MergeDirective(time=time, timezone=timezone,
 
123
                              patch_type=patch_type, patch=patch, **kwargs)
 
124
 
 
125
    def to_lines(self):
 
126
        """Serialize as a list of lines
 
127
 
 
128
        :return: a list of lines
 
129
        """
 
130
        time_str = timestamp.format_patch_date(self.time, self.timezone)
 
131
        stanza = rio.Stanza(revision_id=self.revision_id, timestamp=time_str,
 
132
                            target_branch=self.target_branch,
 
133
                            testament_sha1=self.testament_sha1)
 
134
        for key in ('source_branch', 'message'):
 
135
            if self.__dict__[key] is not None:
 
136
                stanza.add(key, self.__dict__[key])
 
137
        lines = ['# ' + self._format_string + '\n']
 
138
        lines.extend(rio.to_patch_lines(stanza))
 
139
        lines.append('# \n')
 
140
        if self.patch is not None:
 
141
            lines.extend(self.patch.splitlines(True))
 
142
        return lines
 
143
 
 
144
    def to_signed(self, branch):
 
145
        """Serialize as a signed string.
 
146
 
 
147
        :param branch: The source branch, to get the signing strategy
 
148
        :return: a string
 
149
        """
 
150
        my_gpg = gpg.GPGStrategy(branch.get_config())
 
151
        return my_gpg.sign(''.join(self.to_lines()))
 
152
 
 
153
    def to_email(self, mail_to, branch, sign=False):
 
154
        """Serialize as an email message.
 
155
 
 
156
        :param mail_to: The address to mail the message to
 
157
        :param branch: The source branch, to get the signing strategy and
 
158
            source email address
 
159
        :param sign: If True, gpg-sign the email
 
160
        :return: an email message
 
161
        """
 
162
        mail_from = branch.get_config().username()
 
163
        message = Message.Message()
 
164
        message['To'] = mail_to
 
165
        message['From'] = mail_from
 
166
        if self.message is not None:
 
167
            message['Subject'] = self.message
 
168
        else:
 
169
            revision = branch.repository.get_revision(self.revision_id)
 
170
            message['Subject'] = revision.message
 
171
        if sign:
 
172
            body = self.to_signed(branch)
 
173
        else:
 
174
            body = ''.join(self.to_lines())
 
175
        message.set_payload(body)
 
176
        return message
 
177
 
 
178
    @classmethod
 
179
    def from_objects(klass, repository, revision_id, time, timezone,
 
180
                 target_branch, patch_type='bundle',
 
181
                 local_target_branch=None, public_branch=None, message=None):
 
182
        """Generate a merge directive from various objects
 
183
 
 
184
        :param repository: The repository containing the revision
 
185
        :param revision_id: The revision to merge
 
186
        :param time: The POSIX timestamp of the date the request was issued.
 
187
        :param timezone: The timezone of the request
 
188
        :param target_branch: The url of the branch to merge into
 
189
        :param patch_type: 'bundle', 'diff' or None, depending on the type of
 
190
            patch desired.
 
191
        :param local_target_branch: a local copy of the target branch
 
192
        :param public_branch: location of a public branch containing the target
 
193
            revision.
 
194
        :param message: Message to use when committing the merge
 
195
        :return: The merge directive
 
196
 
 
197
        The public branch is always used if supplied.  If the patch_type is
 
198
        not 'bundle', the public branch must be supplied, and will be verified.
 
199
 
 
200
        If the message is not supplied, the message from revision_id will be
 
201
        used for the commit.
 
202
        """
 
203
        t = testament.StrictTestament3.from_revision(repository, revision_id)
 
204
        submit_branch = _mod_branch.Branch.open(target_branch)
 
205
        if submit_branch.get_public_branch() is not None:
 
206
            target_branch = submit_branch.get_public_branch()
 
207
        if patch_type is None:
 
208
            patch = None
 
209
        else:
 
210
            submit_revision_id = submit_branch.last_revision()
 
211
            repository.fetch(submit_branch.repository, submit_revision_id)
 
212
            ancestor_id = _mod_revision.common_ancestor(revision_id,
 
213
                                                        submit_revision_id,
 
214
                                                        repository)
 
215
            type_handler = {'bundle': klass._generate_bundle,
 
216
                            'diff': klass._generate_diff,
 
217
                            None: lambda x, y, z: None }
 
218
            patch = type_handler[patch_type](repository, revision_id,
 
219
                                             ancestor_id)
 
220
            if patch_type == 'bundle':
 
221
                s = StringIO()
 
222
                bundle_serializer.write_bundle(repository, revision_id,
 
223
                                               ancestor_id, s)
 
224
                patch = s.getvalue()
 
225
            elif patch_type == 'diff':
 
226
                patch = klass._generate_diff(repository, revision_id,
 
227
                                             ancestor_id)
 
228
 
 
229
            if public_branch is not None and patch_type != 'bundle':
 
230
                public_branch_obj = _mod_branch.Branch.open(public_branch)
 
231
                if not public_branch_obj.repository.has_revision(revision_id):
 
232
                    raise errors.PublicBranchOutOfDate(public_branch,
 
233
                                                       revision_id)
 
234
 
 
235
        return MergeDirective(revision_id, t.as_sha1(), time, timezone,
 
236
                              target_branch, patch, patch_type, public_branch,
 
237
                              message)
 
238
 
 
239
    @staticmethod
 
240
    def _generate_diff(repository, revision_id, ancestor_id):
 
241
        tree_1 = repository.revision_tree(ancestor_id)
 
242
        tree_2 = repository.revision_tree(revision_id)
 
243
        s = StringIO()
 
244
        diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
 
245
        return s.getvalue()
 
246
 
 
247
    @staticmethod
 
248
    def _generate_bundle(repository, revision_id, ancestor_id):
 
249
        s = StringIO()
 
250
        bundle_serializer.write_bundle(repository, revision_id,
 
251
                                       ancestor_id, s)
 
252
        return s.getvalue()