/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: 2007-03-07 20:14:06 UTC
  • mto: (2323.6.9 0.15-integration)
  • mto: This revision was merged to the branch mainline in revision 2330.
  • Revision ID: abentley@panoramicfeedback.com-20070307201406-5bxjy8wlmmcxy0eh
Fix failing tests

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
    _format_string = 'Bazaar merge directive format experimental-1'
 
37
 
 
38
    def __init__(self, revision_id, testament_sha1, time, timezone,
 
39
                 target_branch, patch=None, patch_type=None,
 
40
                 source_branch=None, message=None):
 
41
        assert patch_type in (None, 'diff', 'bundle')
 
42
        if patch_type != 'bundle' and source_branch is None:
 
43
            raise errors.NoMergeSource()
 
44
        if patch_type is not None and patch is None:
 
45
            raise errors.PatchMissing(patch_type)
 
46
        self.revision_id = revision_id
 
47
        self.testament_sha1 = testament_sha1
 
48
        self.time = time
 
49
        self.timezone = timezone
 
50
        self.target_branch = target_branch
 
51
        self.patch = patch
 
52
        self.patch_type = patch_type
 
53
        self.source_branch = source_branch
 
54
        self.message = message
 
55
 
 
56
    @classmethod
 
57
    def from_lines(klass, lines):
 
58
        assert lines[0].startswith('# ' + klass._format_string + '\n')
 
59
        line_iter = iter(lines[1:])
 
60
        stanza = rio.read_patch_stanza(line_iter)
 
61
        patch_lines = list(line_iter)
 
62
        if len(patch_lines) == 0:
 
63
            patch = None
 
64
        else:
 
65
            patch = ''.join(patch_lines)
 
66
        try:
 
67
            bundle_serializer.read_bundle(StringIO(patch))
 
68
        except errors.NotABundle:
 
69
            patch_type = 'diff'
 
70
        else:
 
71
            patch_type = 'bundle'
 
72
        time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
 
73
        kwargs = {}
 
74
        for key in ('revision_id', 'testament_sha1', 'target_branch',
 
75
                    'source_branch', 'message'):
 
76
            try:
 
77
                kwargs[key] = stanza.get(key)
 
78
            except KeyError:
 
79
                pass
 
80
        return MergeDirective(time=time, timezone=timezone,
 
81
                              patch_type=patch_type, patch=patch, **kwargs)
 
82
 
 
83
    def to_lines(self):
 
84
        time_str = timestamp.format_patch_date(self.time, self.timezone)
 
85
        stanza = rio.Stanza(revision_id=self.revision_id, timestamp=time_str,
 
86
                            target_branch=self.target_branch,
 
87
                            testament_sha1=self.testament_sha1)
 
88
        for key in ('source_branch', 'message'):
 
89
            if self.__dict__[key] is not None:
 
90
                stanza.add(key, self.__dict__[key])
 
91
        lines = ['# ' + self._format_string + '\n']
 
92
        lines.extend(rio.to_patch_lines(stanza))
 
93
        lines.append('# \n')
 
94
        if self.patch is not None:
 
95
            lines.extend(self.patch.splitlines(True))
 
96
        return lines
 
97
 
 
98
    def to_signed(self, branch):
 
99
        my_gpg = gpg.GPGStrategy(branch.get_config())
 
100
        return my_gpg.sign(''.join(self.to_lines()))
 
101
 
 
102
    def to_email(self, mail_to, branch, sign=False):
 
103
        mail_from = branch.get_config().username()
 
104
        message = Message.Message()
 
105
        message['To'] = mail_to
 
106
        message['From'] = mail_from
 
107
        if self.message is not None:
 
108
            message['Subject'] = self.message
 
109
        else:
 
110
            revision = branch.repository.get_revision(self.revision_id)
 
111
            message['Subject'] = revision.message
 
112
        if sign:
 
113
            body = self.to_signed(branch)
 
114
        else:
 
115
            body = ''.join(self.to_lines())
 
116
        message.set_payload(body)
 
117
        return message
 
118
 
 
119
    @classmethod
 
120
    def from_objects(klass, repository, revision_id, time, timezone,
 
121
                 target_branch, patch_type='bundle',
 
122
                 local_target_branch=None, public_branch=None, message=None):
 
123
        t = testament.StrictTestament3.from_revision(repository, revision_id)
 
124
        if patch_type is None:
 
125
            patch = None
 
126
        else:
 
127
            submit_branch = _mod_branch.Branch.open(target_branch)
 
128
            submit_revision_id = submit_branch.last_revision()
 
129
            repository.fetch(submit_branch.repository, submit_revision_id)
 
130
            ancestor_id = _mod_revision.common_ancestor(revision_id,
 
131
                                                        submit_revision_id,
 
132
                                                        repository)
 
133
            if patch_type == 'bundle':
 
134
                s = StringIO()
 
135
                bundle_serializer.write_bundle(repository, revision_id,
 
136
                                               ancestor_id, s)
 
137
                patch = s.getvalue()
 
138
            elif patch_type == 'diff':
 
139
                patch = klass._generate_diff(repository, revision_id,
 
140
                                             ancestor_id)
 
141
 
 
142
            if public_branch is not None and patch_type != 'bundle':
 
143
                public_branch_obj = _mod_branch.Branch.open(public_branch)
 
144
                if not public_branch_obj.repository.has_revision(revision_id):
 
145
                    raise errors.PublicBranchOutOfDate(public_branch,
 
146
                                                       revision_id)
 
147
 
 
148
        return MergeDirective(revision_id, t.as_sha1(), time, timezone,
 
149
                              target_branch, patch, patch_type, public_branch,
 
150
                              message)
 
151
 
 
152
    @staticmethod
 
153
    def _generate_diff(repository, revision_id, ancestor_id):
 
154
        tree_1 = repository.revision_tree(ancestor_id)
 
155
        tree_2 = repository.revision_tree(revision_id)
 
156
        s = StringIO()
 
157
        diff.show_diff_trees(tree_1, tree_2, s)
 
158
        return s.getvalue()