1
# Copyright (C) 2007 Canonical Ltd
1
# Copyright (C) 2007-2011 Canonical Ltd
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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
18
19
from StringIO import StringIO
22
from bzrlib import lazy_import
23
lazy_import.lazy_import(globals(), """
21
24
from bzrlib import (
22
25
branch as _mod_branch,
34
38
from bzrlib.bundle import (
35
39
serializer as bundle_serializer,
37
from bzrlib.email_message import EmailMessage
40
44
class MergeRequestBodyParams(object):
56
60
"""Hooks for MergeDirective classes."""
58
62
def __init__(self):
59
hooks.Hooks.__init__(self)
60
self.create_hook(hooks.HookPoint('merge_request_body',
63
hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
64
self.add_hook('merge_request_body',
61
65
"Called with a MergeRequestBodyParams when a body is needed for"
62
66
" a merge request. Callbacks must return a body. If more"
63
67
" than one callback is registered, the output of one callback is"
64
" provided to the next.", (1, 15, 0), False))
67
class _BaseMergeDirective(object):
68
" provided to the next.", (1, 15, 0))
71
class BaseMergeDirective(object):
72
"""A request to perform a merge into a branch.
74
This is the base class that all merge directive implementations
77
:cvar multiple_output_files: Whether or not this merge directive
78
stores a set of revisions in more than one file
69
81
hooks = MergeDirectiveHooks()
83
multiple_output_files = False
71
85
def __init__(self, revision_id, testament_sha1, time, timezone,
72
target_branch, patch=None, source_branch=None, message=None,
86
target_branch, patch=None, source_branch=None,
87
message=None, bundle=None):
76
90
:param revision_id: The revision to merge
79
93
:param time: The current POSIX timestamp time
80
94
:param timezone: The timezone offset
81
:param target_branch: The branch to apply the merge to
95
:param target_branch: Location of branch to apply the merge to
82
96
:param patch: The text of a diff or bundle
83
97
:param source_branch: A public location to merge the revision from
84
98
:param message: The message to use when committing this merge
92
106
self.source_branch = source_branch
93
107
self.message = message
110
"""Serialize as a list of lines
112
:return: a list of lines
114
raise NotImplementedError(self.to_lines)
117
"""Serialize as a set of files.
119
:return: List of tuples with filename and contents as lines
121
raise NotImplementedError(self.to_files)
123
def get_raw_bundle(self):
124
"""Return the bundle for this merge directive.
126
:return: bundle text or None if there is no bundle
95
130
def _to_lines(self, base_revision=False):
96
131
"""Serialize as a list of lines
111
146
lines.append('# \n')
149
def write_to_directory(self, path):
150
"""Write this merge directive to a series of files in a directory.
152
:param path: Filesystem path to write to
154
raise NotImplementedError(self.write_to_directory)
115
157
def from_objects(klass, repository, revision_id, time, timezone,
116
158
target_branch, patch_type='bundle',
124
166
:param target_branch: The url of the branch to merge into
125
167
:param patch_type: 'bundle', 'diff' or None, depending on the type of
127
:param local_target_branch: a local copy of the target branch
128
:param public_branch: location of a public branch containing the target
169
:param local_target_branch: the submit branch, either itself or a local copy
170
:param public_branch: location of a public branch containing
130
172
:param message: Message to use when committing the merge
131
173
:return: The merge directive
140
182
if revision_id == _mod_revision.NULL_REVISION:
141
183
t_revision_id = None
142
184
t = testament.StrictTestament3.from_revision(repository, t_revision_id)
143
submit_branch = _mod_branch.Branch.open(target_branch)
185
if local_target_branch is None:
186
submit_branch = _mod_branch.Branch.open(target_branch)
188
submit_branch = local_target_branch
144
189
if submit_branch.get_public_branch() is not None:
145
190
target_branch = submit_branch.get_public_branch()
146
191
if patch_type is None:
203
248
:param branch: The source branch, to get the signing strategy
204
249
:return: a string
206
my_gpg = gpg.GPGStrategy(branch.get_config())
251
my_gpg = gpg.GPGStrategy(branch.get_config_stack())
207
252
return my_gpg.sign(''.join(self.to_lines()))
209
254
def to_email(self, mail_to, branch, sign=False):
225
270
body = self.to_signed(branch)
227
272
body = ''.join(self.to_lines())
228
message = EmailMessage(mail_from, mail_to, subject, body)
273
message = email_message.EmailMessage(mail_from, mail_to, subject,
231
277
def install_revisions(self, target_repo):
332
378
:param time: The current POSIX timestamp time
333
379
:param timezone: The timezone offset
334
:param target_branch: The branch to apply the merge to
380
:param target_branch: Location of the branch to apply the merge to
335
381
:param patch: The text of a diff or bundle
336
382
:param patch_type: None, "diff" or "bundle", depending on the contents
338
384
:param source_branch: A public location to merge the revision from
339
385
:param message: The message to use when committing this merge
341
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
387
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
342
388
timezone, target_branch, patch, source_branch, message)
343
389
if patch_type not in (None, 'diff', 'bundle'):
344
390
raise ValueError(patch_type)
428
474
return None, self.revision_id, 'inapplicable'
431
class MergeDirective2(_BaseMergeDirective):
477
class MergeDirective2(BaseMergeDirective):
433
479
_format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
437
483
bundle=None, base_revision_id=None):
438
484
if source_branch is None and bundle is None:
439
485
raise errors.NoMergeSource()
440
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
486
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
441
487
timezone, target_branch, patch, source_branch, message)
442
488
self.bundle = bundle
443
489
self.base_revision_id = base_revision_id
525
571
:param target_branch: The url of the branch to merge into
526
572
:param include_patch: If true, include a preview patch
527
573
:param include_bundle: If true, include a bundle
528
:param local_target_branch: a local copy of the target branch
529
:param public_branch: location of a public branch containing the target
574
:param local_target_branch: the target branch, either itself or a local copy
575
:param public_branch: location of a public branch containing
531
577
:param message: Message to use when committing the merge
532
578
:return: The merge directive
546
592
t_revision_id = None
547
593
t = testament.StrictTestament3.from_revision(repository,
549
submit_branch = _mod_branch.Branch.open(target_branch)
595
if local_target_branch is None:
596
submit_branch = _mod_branch.Branch.open(target_branch)
598
submit_branch = local_target_branch
550
599
submit_branch.lock_read()
551
600
locked.append(submit_branch)
552
601
if submit_branch.get_public_branch() is not None: