1
# Copyright (C) 2007-2011 Canonical Ltd
1
# Copyright (C) 2007 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
19
18
from StringIO import StringIO
22
from bzrlib import lazy_import
23
lazy_import.lazy_import(globals(), """
24
21
from bzrlib import (
25
22
branch as _mod_branch,
38
34
from bzrlib.bundle import (
39
35
serializer as bundle_serializer,
37
from bzrlib.email_message import EmailMessage
44
40
class MergeRequestBodyParams(object):
60
56
"""Hooks for MergeDirective classes."""
62
58
def __init__(self):
63
hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
64
self.add_hook('merge_request_body',
59
hooks.Hooks.__init__(self)
60
self.create_hook(hooks.HookPoint('merge_request_body',
65
61
"Called with a MergeRequestBodyParams when a body is needed for"
66
62
" a merge request. Callbacks must return a body. If more"
67
63
" than one callback is registered, the output of one callback is"
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
64
" provided to the next.", (1, 15, 0), False))
67
class _BaseMergeDirective(object):
81
69
hooks = MergeDirectiveHooks()
83
multiple_output_files = False
85
71
def __init__(self, revision_id, testament_sha1, time, timezone,
86
target_branch, patch=None, source_branch=None,
87
message=None, bundle=None):
72
target_branch, patch=None, source_branch=None, message=None,
90
76
:param revision_id: The revision to merge
93
79
:param time: The current POSIX timestamp time
94
80
:param timezone: The timezone offset
95
:param target_branch: Location of branch to apply the merge to
81
:param target_branch: The branch to apply the merge to
96
82
:param patch: The text of a diff or bundle
97
83
:param source_branch: A public location to merge the revision from
98
84
:param message: The message to use when committing this merge
106
92
self.source_branch = source_branch
107
93
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
130
95
def _to_lines(self, base_revision=False):
131
96
"""Serialize as a list of lines
146
111
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)
157
115
def from_objects(klass, repository, revision_id, time, timezone,
158
116
target_branch, patch_type='bundle',
166
124
:param target_branch: The url of the branch to merge into
167
125
:param patch_type: 'bundle', 'diff' or None, depending on the type of
169
:param local_target_branch: the submit branch, either itself or a local copy
170
:param public_branch: location of a public branch containing
127
:param local_target_branch: a local copy of the target branch
128
:param public_branch: location of a public branch containing the target
172
130
:param message: Message to use when committing the merge
173
131
:return: The merge directive
182
140
if revision_id == _mod_revision.NULL_REVISION:
183
141
t_revision_id = None
184
142
t = testament.StrictTestament3.from_revision(repository, t_revision_id)
185
if local_target_branch is None:
186
submit_branch = _mod_branch.Branch.open(target_branch)
188
submit_branch = local_target_branch
143
submit_branch = _mod_branch.Branch.open(target_branch)
189
144
if submit_branch.get_public_branch() is not None:
190
145
target_branch = submit_branch.get_public_branch()
191
146
if patch_type is None:
248
203
:param branch: The source branch, to get the signing strategy
249
204
:return: a string
251
my_gpg = gpg.GPGStrategy(branch.get_config_stack())
206
my_gpg = gpg.GPGStrategy(branch.get_config())
252
207
return my_gpg.sign(''.join(self.to_lines()))
254
209
def to_email(self, mail_to, branch, sign=False):
260
215
:param sign: If True, gpg-sign the email
261
216
:return: an email message
263
mail_from = branch.get_config_stack().get('email')
218
mail_from = branch.get_config().username()
264
219
if self.message is not None:
265
220
subject = self.message
270
225
body = self.to_signed(branch)
272
227
body = ''.join(self.to_lines())
273
message = email_message.EmailMessage(mail_from, mail_to, subject,
228
message = EmailMessage(mail_from, mail_to, subject, body)
277
231
def install_revisions(self, target_repo):
378
332
:param time: The current POSIX timestamp time
379
333
:param timezone: The timezone offset
380
:param target_branch: Location of the branch to apply the merge to
334
:param target_branch: The branch to apply the merge to
381
335
:param patch: The text of a diff or bundle
382
336
:param patch_type: None, "diff" or "bundle", depending on the contents
384
338
:param source_branch: A public location to merge the revision from
385
339
:param message: The message to use when committing this merge
387
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
341
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
388
342
timezone, target_branch, patch, source_branch, message)
389
343
if patch_type not in (None, 'diff', 'bundle'):
390
344
raise ValueError(patch_type)
417
371
:return: a MergeRequest
419
373
line_iter = iter(lines)
421
374
for line in line_iter:
422
375
if line.startswith('# Bazaar merge directive format '):
423
return _format_registry.get(line[2:].rstrip())._from_lines(
425
firstline = firstline or line.strip()
426
raise errors.NotAMergeDirective(firstline)
379
raise errors.NotAMergeDirective(lines[0])
381
raise errors.NotAMergeDirective('')
382
return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
429
385
def _from_lines(klass, line_iter):
474
430
return None, self.revision_id, 'inapplicable'
477
class MergeDirective2(BaseMergeDirective):
433
class MergeDirective2(_BaseMergeDirective):
479
435
_format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
483
439
bundle=None, base_revision_id=None):
484
440
if source_branch is None and bundle is None:
485
441
raise errors.NoMergeSource()
486
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
442
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
487
443
timezone, target_branch, patch, source_branch, message)
488
444
self.bundle = bundle
489
445
self.base_revision_id = base_revision_id
571
527
:param target_branch: The url of the branch to merge into
572
528
:param include_patch: If true, include a preview patch
573
529
:param include_bundle: If true, include a bundle
574
:param local_target_branch: the target branch, either itself or a local copy
575
:param public_branch: location of a public branch containing
530
:param local_target_branch: a local copy of the target branch
531
:param public_branch: location of a public branch containing the target
577
533
:param message: Message to use when committing the merge
578
534
:return: The merge directive
592
548
t_revision_id = None
593
549
t = testament.StrictTestament3.from_revision(repository,
595
if local_target_branch is None:
596
submit_branch = _mod_branch.Branch.open(target_branch)
598
submit_branch = local_target_branch
551
submit_branch = _mod_branch.Branch.open(target_branch)
599
552
submit_branch.lock_read()
600
553
locked.append(submit_branch)
601
554
if submit_branch.get_public_branch() is not None:
630
583
raise errors.PublicBranchOutOfDate(public_branch,
632
testament_sha1 = t.as_sha1()
634
586
for entry in reversed(locked):
636
return klass(revision_id, testament_sha1, time, timezone,
637
target_branch, patch, public_branch, message, bundle,
588
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
589
patch, public_branch, message, bundle, base_revision_id)
640
591
def _verify_patch(self, repository):
641
592
calculated_patch = self._generate_diff(repository, self.revision_id,