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
21
from bzrlib import lazy_import
22
lazy_import.lazy_import(globals(), """
21
23
from bzrlib import (
22
24
branch as _mod_branch,
27
31
revision as _mod_revision,
32
37
from bzrlib.bundle import (
33
38
serializer as bundle_serializer,
35
from bzrlib.email_message import EmailMessage
38
class _BaseMergeDirective(object):
43
class MergeRequestBodyParams(object):
44
"""Parameter object for the merge_request_body hook."""
46
def __init__(self, body, orig_body, directive, to, basename, subject,
49
self.orig_body = orig_body
50
self.directive = directive
54
self.basename = basename
55
self.subject = subject
58
class MergeDirectiveHooks(hooks.Hooks):
59
"""Hooks for MergeDirective classes."""
62
hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
63
self.add_hook('merge_request_body',
64
"Called with a MergeRequestBodyParams when a body is needed for"
65
" a merge request. Callbacks must return a body. If more"
66
" than one callback is registered, the output of one callback is"
67
" provided to the next.", (1, 15, 0))
70
class BaseMergeDirective(object):
71
"""A request to perform a merge into a branch.
73
This is the base class that all merge directive implementations
76
:cvar multiple_output_files: Whether or not this merge directive
77
stores a set of revisions in more than one file
80
hooks = MergeDirectiveHooks()
82
multiple_output_files = False
40
84
def __init__(self, revision_id, testament_sha1, time, timezone,
41
85
target_branch, patch=None, source_branch=None, message=None,
61
105
self.source_branch = source_branch
62
106
self.message = message
109
"""Serialize as a list of lines
111
:return: a list of lines
113
raise NotImplementedError(self.to_lines)
116
"""Serialize as a set of files.
118
:return: List of tuples with filename and contents as lines
120
raise NotImplementedError(self.to_files)
122
def get_raw_bundle(self):
123
"""Return the bundle for this merge directive.
125
:return: bundle text or None if there is no bundle
64
129
def _to_lines(self, base_revision=False):
65
130
"""Serialize as a list of lines
80
145
lines.append('# \n')
148
def write_to_directory(self, path):
149
"""Write this merge directive to a series of files in a directory.
151
:param path: Filesystem path to write to
153
raise NotImplementedError(self.write_to_directory)
84
156
def from_objects(klass, repository, revision_id, time, timezone,
85
157
target_branch, patch_type='bundle',
194
266
body = self.to_signed(branch)
196
268
body = ''.join(self.to_lines())
197
message = EmailMessage(mail_from, mail_to, subject, body)
269
message = email_message.EmailMessage(mail_from, mail_to, subject,
200
273
def install_revisions(self, target_repo):
240
313
target_repo.fetch(source_branch.repository, self.revision_id)
241
314
return self.revision_id
244
class MergeDirective(_BaseMergeDirective):
316
def compose_merge_request(self, mail_client, to, body, branch, tree=None):
317
"""Compose a request to merge this directive.
319
:param mail_client: The mail client to use for composing this request.
320
:param to: The address to compose the request to.
321
:param branch: The Branch that was used to produce this directive.
322
:param tree: The Tree (if any) for the Branch used to produce this
325
basename = self.get_disk_name(branch)
327
if self.message is not None:
328
subject += self.message
330
revision = branch.repository.get_revision(self.revision_id)
331
subject += revision.get_summary()
332
if getattr(mail_client, 'supports_body', False):
334
for hook in self.hooks['merge_request_body']:
335
params = MergeRequestBodyParams(body, orig_body, self,
336
to, basename, subject, branch,
339
elif len(self.hooks['merge_request_body']) > 0:
340
trace.warning('Cannot run merge_request_body hooks because mail'
341
' client %s does not support message bodies.',
342
mail_client.__class__.__name__)
343
mail_client.compose_merge_request(to, subject,
344
''.join(self.to_lines()),
348
class MergeDirective(BaseMergeDirective):
246
350
"""A request to perform a merge into a branch.
276
380
:param source_branch: A public location to merge the revision from
277
381
:param message: The message to use when committing this merge
279
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
383
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
280
384
timezone, target_branch, patch, source_branch, message)
281
385
if patch_type not in (None, 'diff', 'bundle'):
282
386
raise ValueError(patch_type)
309
413
:return: a MergeRequest
311
415
line_iter = iter(lines)
312
417
for line in line_iter:
313
418
if line.startswith('# Bazaar merge directive format '):
317
raise errors.NotAMergeDirective(lines[0])
319
raise errors.NotAMergeDirective('')
320
return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
419
return _format_registry.get(line[2:].rstrip())._from_lines(
421
firstline = firstline or line.strip()
422
raise errors.NotAMergeDirective(firstline)
323
425
def _from_lines(klass, line_iter):
377
479
bundle=None, base_revision_id=None):
378
480
if source_branch is None and bundle is None:
379
481
raise errors.NoMergeSource()
380
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
482
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
381
483
timezone, target_branch, patch, source_branch, message)
382
484
self.bundle = bundle
383
485
self.base_revision_id = base_revision_id
521
623
raise errors.PublicBranchOutOfDate(public_branch,
625
testament_sha1 = t.as_sha1()
524
627
for entry in reversed(locked):
526
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
527
patch, public_branch, message, bundle, base_revision_id)
629
return klass(revision_id, testament_sha1, time, timezone,
630
target_branch, patch, public_branch, message, bundle,
529
633
def _verify_patch(self, repository):
530
634
calculated_patch = self._generate_diff(repository, self.revision_id,
566
670
_format_registry = MergeDirectiveFormatRegistry()
567
671
_format_registry.register(MergeDirective)
568
672
_format_registry.register(MergeDirective2)
673
# 0.19 never existed. It got renamed to 0.90. But by that point, there were
674
# already merge directives in the wild that used 0.19. Registering with the old
675
# format string to retain compatibility with those merge directives.
569
676
_format_registry.register(MergeDirective2,
570
677
'Bazaar merge directive format 2 (Bazaar 0.19)')