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',
208
136
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
209
137
patch, patch_type, public_branch, message)
211
def get_disk_name(self, branch):
212
"""Generate a suitable basename for storing this directive on disk
214
:param branch: The Branch this merge directive was generated fro
217
revno, revision_id = branch.last_revision_info()
218
if self.revision_id == revision_id:
221
revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
223
nick = re.sub('(\W+)', '-', branch.nick).strip('-')
224
return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
227
140
def _generate_diff(repository, revision_id, ancestor_id):
228
141
tree_1 = repository.revision_tree(ancestor_id)
278
190
StringIO(self.get_raw_bundle()))
279
191
# We don't use the bundle's target revision, because
280
192
# MergeDirective.revision_id is authoritative.
282
info.install_revisions(target_repo, stream_input=False)
283
except errors.RevisionNotPresent:
284
# At least one dependency isn't present. Try installing
285
# missing revisions from the submit branch
288
_mod_branch.Branch.open(self.target_branch)
289
except errors.NotBranchError:
290
raise errors.TargetNotBranch(self.target_branch)
291
missing_revisions = []
292
bundle_revisions = set(r.revision_id for r in
294
for revision in info.real_revisions:
295
for parent_id in revision.parent_ids:
296
if (parent_id not in bundle_revisions and
297
not target_repo.has_revision(parent_id)):
298
missing_revisions.append(parent_id)
299
# reverse missing revisions to try to get heads first
301
unique_missing_set = set()
302
for revision in reversed(missing_revisions):
303
if revision in unique_missing_set:
305
unique_missing.append(revision)
306
unique_missing_set.add(revision)
307
for missing_revision in unique_missing:
308
target_repo.fetch(submit_branch.repository,
310
info.install_revisions(target_repo, stream_input=False)
193
info.install_revisions(target_repo, stream_input=False)
312
195
source_branch = _mod_branch.Branch.open(self.source_branch)
313
196
target_repo.fetch(source_branch.repository, self.revision_id)
314
197
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):
200
class MergeDirective(_BaseMergeDirective):
350
202
"""A request to perform a merge into a branch.
380
232
:param source_branch: A public location to merge the revision from
381
233
:param message: The message to use when committing this merge
383
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
235
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
384
236
timezone, target_branch, patch, source_branch, message)
385
if patch_type not in (None, 'diff', 'bundle'):
386
raise ValueError(patch_type)
237
assert patch_type in (None, 'diff', 'bundle'), patch_type
387
238
if patch_type != 'bundle' and source_branch is None:
388
239
raise errors.NoMergeSource()
389
240
if patch_type is not None and patch is None:
479
332
bundle=None, base_revision_id=None):
480
333
if source_branch is None and bundle is None:
481
334
raise errors.NoMergeSource()
482
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
335
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
483
336
timezone, target_branch, patch, source_branch, message)
484
337
self.bundle = bundle
485
338
self.base_revision_id = base_revision_id
623
476
raise errors.PublicBranchOutOfDate(public_branch,
625
testament_sha1 = t.as_sha1()
627
479
for entry in reversed(locked):
629
return klass(revision_id, testament_sha1, time, timezone,
630
target_branch, patch, public_branch, message, bundle,
481
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
482
patch, public_branch, message, bundle, base_revision_id)
633
484
def _verify_patch(self, repository):
634
485
calculated_patch = self._generate_diff(repository, self.revision_id,
670
521
_format_registry = MergeDirectiveFormatRegistry()
671
522
_format_registry.register(MergeDirective)
672
523
_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
524
_format_registry.register(MergeDirective2,
677
525
'Bazaar merge directive format 2 (Bazaar 0.19)')