13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
from StringIO import StringIO
35
37
from bzrlib.email_message import EmailMessage
40
class MergeRequestBodyParams(object):
41
"""Parameter object for the merge_request_body hook."""
43
def __init__(self, body, orig_body, directive, to, basename, subject,
46
self.orig_body = orig_body
47
self.directive = directive
51
self.basename = basename
52
self.subject = subject
55
class MergeDirectiveHooks(hooks.Hooks):
56
"""Hooks for MergeDirective classes."""
59
hooks.Hooks.__init__(self)
60
self.create_hook(hooks.HookPoint('merge_request_body',
61
"Called with a MergeRequestBodyParams when a body is needed for"
62
" a merge request. Callbacks must return a body. If more"
63
" than one callback is registered, the output of one callback is"
64
" provided to the next.", (1, 15, 0), False))
38
67
class _BaseMergeDirective(object):
69
hooks = MergeDirectiveHooks()
40
71
def __init__(self, revision_id, testament_sha1, time, timezone,
41
72
target_branch, patch=None, source_branch=None, message=None,
210
241
except errors.RevisionNotPresent:
211
242
# At least one dependency isn't present. Try installing
212
243
# missing revisions from the submit branch
213
submit_branch = _mod_branch.Branch.open(self.target_branch)
246
_mod_branch.Branch.open(self.target_branch)
247
except errors.NotBranchError:
248
raise errors.TargetNotBranch(self.target_branch)
214
249
missing_revisions = []
215
250
bundle_revisions = set(r.revision_id for r in
216
251
info.real_revisions)
236
271
target_repo.fetch(source_branch.repository, self.revision_id)
237
272
return self.revision_id
274
def compose_merge_request(self, mail_client, to, body, branch, tree=None):
275
"""Compose a request to merge this directive.
277
:param mail_client: The mail client to use for composing this request.
278
:param to: The address to compose the request to.
279
:param branch: The Branch that was used to produce this directive.
280
:param tree: The Tree (if any) for the Branch used to produce this
283
basename = self.get_disk_name(branch)
285
if self.message is not None:
286
subject += self.message
288
revision = branch.repository.get_revision(self.revision_id)
289
subject += revision.get_summary()
290
if getattr(mail_client, 'supports_body', False):
292
for hook in self.hooks['merge_request_body']:
293
params = MergeRequestBodyParams(body, orig_body, self,
294
to, basename, subject, branch,
297
elif len(self.hooks['merge_request_body']) > 0:
298
trace.warning('Cannot run merge_request_body hooks because mail'
299
' client %s does not support message bodies.',
300
mail_client.__class__.__name__)
301
mail_client.compose_merge_request(to, subject,
302
''.join(self.to_lines()),
240
306
class MergeDirective(_BaseMergeDirective):
517
583
raise errors.PublicBranchOutOfDate(public_branch,
585
testament_sha1 = t.as_sha1()
520
587
for entry in reversed(locked):
522
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
523
patch, public_branch, message, bundle, base_revision_id)
589
return klass(revision_id, testament_sha1, time, timezone,
590
target_branch, patch, public_branch, message, bundle,
525
593
def _verify_patch(self, repository):
526
594
calculated_patch = self._generate_diff(repository, self.revision_id,
562
630
_format_registry = MergeDirectiveFormatRegistry()
563
631
_format_registry.register(MergeDirective)
564
632
_format_registry.register(MergeDirective2)
633
# 0.19 never existed. It got renamed to 0.90. But by that point, there were
634
# already merge directives in the wild that used 0.19. Registering with the old
635
# format string to retain compatibility with those merge directives.
565
636
_format_registry.register(MergeDirective2,
566
637
'Bazaar merge directive format 2 (Bazaar 0.19)')