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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
from StringIO import StringIO
21
from bzrlib import lazy_import
22
lazy_import.lazy_import(globals(), """
23
21
from bzrlib import (
24
22
branch as _mod_branch,
31
27
revision as _mod_revision,
37
32
from bzrlib.bundle import (
38
33
serializer as bundle_serializer,
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
35
from bzrlib.email_message import EmailMessage
38
class _BaseMergeDirective(object):
84
40
def __init__(self, revision_id, testament_sha1, time, timezone,
85
41
target_branch, patch=None, source_branch=None, message=None,
105
61
self.source_branch = source_branch
106
62
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
129
64
def _to_lines(self, base_revision=False):
130
65
"""Serialize as a list of lines
145
80
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)
156
84
def from_objects(klass, repository, revision_id, time, timezone,
157
85
target_branch, patch_type='bundle',
266
194
body = self.to_signed(branch)
268
196
body = ''.join(self.to_lines())
269
message = email_message.EmailMessage(mail_from, mail_to, subject,
197
message = EmailMessage(mail_from, mail_to, subject, body)
273
200
def install_revisions(self, target_repo):
313
240
target_repo.fetch(source_branch.repository, self.revision_id)
314
241
return self.revision_id
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):
244
class MergeDirective(_BaseMergeDirective):
350
246
"""A request to perform a merge into a branch.
380
276
:param source_branch: A public location to merge the revision from
381
277
:param message: The message to use when committing this merge
383
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
279
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
384
280
timezone, target_branch, patch, source_branch, message)
385
281
if patch_type not in (None, 'diff', 'bundle'):
386
282
raise ValueError(patch_type)
413
309
:return: a MergeRequest
415
311
line_iter = iter(lines)
417
312
for line in line_iter:
418
313
if line.startswith('# Bazaar merge directive format '):
419
return _format_registry.get(line[2:].rstrip())._from_lines(
421
firstline = firstline or line.strip()
422
raise errors.NotAMergeDirective(firstline)
317
raise errors.NotAMergeDirective(lines[0])
319
raise errors.NotAMergeDirective('')
320
return _format_registry.get(line[2:].rstrip())._from_lines(line_iter)
425
323
def _from_lines(klass, line_iter):
479
377
bundle=None, base_revision_id=None):
480
378
if source_branch is None and bundle is None:
481
379
raise errors.NoMergeSource()
482
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
380
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
483
381
timezone, target_branch, patch, source_branch, message)
484
382
self.bundle = bundle
485
383
self.base_revision_id = base_revision_id
623
521
raise errors.PublicBranchOutOfDate(public_branch,
625
testament_sha1 = t.as_sha1()
627
524
for entry in reversed(locked):
629
return klass(revision_id, testament_sha1, time, timezone,
630
target_branch, patch, public_branch, message, bundle,
526
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
527
patch, public_branch, message, bundle, base_revision_id)
633
529
def _verify_patch(self, repository):
634
530
calculated_patch = self._generate_diff(repository, self.revision_id,
670
566
_format_registry = MergeDirectiveFormatRegistry()
671
567
_format_registry.register(MergeDirective)
672
568
_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.
676
569
_format_registry.register(MergeDirective2,
677
570
'Bazaar merge directive format 2 (Bazaar 0.19)')