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
17
from __future__ import absolute_import
18
19
from StringIO import StringIO
22
from bzrlib import lazy_import
23
lazy_import.lazy_import(globals(), """
21
24
from bzrlib import (
22
25
branch as _mod_branch,
27
32
revision as _mod_revision,
32
38
from bzrlib.bundle import (
33
39
serializer as bundle_serializer,
35
from bzrlib.email_message import EmailMessage
38
class _BaseMergeDirective(object):
44
class MergeRequestBodyParams(object):
45
"""Parameter object for the merge_request_body hook."""
47
def __init__(self, body, orig_body, directive, to, basename, subject,
50
self.orig_body = orig_body
51
self.directive = directive
55
self.basename = basename
56
self.subject = subject
59
class MergeDirectiveHooks(hooks.Hooks):
60
"""Hooks for MergeDirective classes."""
63
hooks.Hooks.__init__(self, "bzrlib.merge_directive", "BaseMergeDirective.hooks")
64
self.add_hook('merge_request_body',
65
"Called with a MergeRequestBodyParams when a body is needed for"
66
" a merge request. Callbacks must return a body. If more"
67
" 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
81
hooks = MergeDirectiveHooks()
83
multiple_output_files = False
40
85
def __init__(self, revision_id, testament_sha1, time, timezone,
41
target_branch, patch=None, source_branch=None, message=None,
86
target_branch, patch=None, source_branch=None,
87
message=None, bundle=None):
45
90
:param revision_id: The revision to merge
61
106
self.source_branch = source_branch
62
107
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
64
130
def _to_lines(self, base_revision=False):
65
131
"""Serialize as a list of lines
80
146
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)
84
157
def from_objects(klass, repository, revision_id, time, timezone,
85
158
target_branch, patch_type='bundle',
93
166
:param target_branch: The url of the branch to merge into
94
167
:param patch_type: 'bundle', 'diff' or None, depending on the type of
96
:param local_target_branch: a local copy of the target branch
97
:param public_branch: location of a public branch containing the target
169
:param local_target_branch: the submit branch, either itself or a local copy
170
:param public_branch: location of a public branch containing
99
172
:param message: Message to use when committing the merge
100
173
:return: The merge directive
136
212
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
137
213
patch, patch_type, public_branch, message)
215
def get_disk_name(self, branch):
216
"""Generate a suitable basename for storing this directive on disk
218
:param branch: The Branch this merge directive was generated fro
221
revno, revision_id = branch.last_revision_info()
222
if self.revision_id == revision_id:
225
revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
227
nick = re.sub('(\W+)', '-', branch.nick).strip('-')
228
return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
140
231
def _generate_diff(repository, revision_id, ancestor_id):
141
232
tree_1 = repository.revision_tree(ancestor_id)
190
282
StringIO(self.get_raw_bundle()))
191
283
# We don't use the bundle's target revision, because
192
284
# MergeDirective.revision_id is authoritative.
193
info.install_revisions(target_repo, stream_input=False)
286
info.install_revisions(target_repo, stream_input=False)
287
except errors.RevisionNotPresent:
288
# At least one dependency isn't present. Try installing
289
# missing revisions from the submit branch
292
_mod_branch.Branch.open(self.target_branch)
293
except errors.NotBranchError:
294
raise errors.TargetNotBranch(self.target_branch)
295
missing_revisions = []
296
bundle_revisions = set(r.revision_id for r in
298
for revision in info.real_revisions:
299
for parent_id in revision.parent_ids:
300
if (parent_id not in bundle_revisions and
301
not target_repo.has_revision(parent_id)):
302
missing_revisions.append(parent_id)
303
# reverse missing revisions to try to get heads first
305
unique_missing_set = set()
306
for revision in reversed(missing_revisions):
307
if revision in unique_missing_set:
309
unique_missing.append(revision)
310
unique_missing_set.add(revision)
311
for missing_revision in unique_missing:
312
target_repo.fetch(submit_branch.repository,
314
info.install_revisions(target_repo, stream_input=False)
195
316
source_branch = _mod_branch.Branch.open(self.source_branch)
196
317
target_repo.fetch(source_branch.repository, self.revision_id)
197
318
return self.revision_id
200
class MergeDirective(_BaseMergeDirective):
320
def compose_merge_request(self, mail_client, to, body, branch, tree=None):
321
"""Compose a request to merge this directive.
323
:param mail_client: The mail client to use for composing this request.
324
:param to: The address to compose the request to.
325
:param branch: The Branch that was used to produce this directive.
326
:param tree: The Tree (if any) for the Branch used to produce this
329
basename = self.get_disk_name(branch)
331
if self.message is not None:
332
subject += self.message
334
revision = branch.repository.get_revision(self.revision_id)
335
subject += revision.get_summary()
336
if getattr(mail_client, 'supports_body', False):
338
for hook in self.hooks['merge_request_body']:
339
params = MergeRequestBodyParams(body, orig_body, self,
340
to, basename, subject, branch,
343
elif len(self.hooks['merge_request_body']) > 0:
344
trace.warning('Cannot run merge_request_body hooks because mail'
345
' client %s does not support message bodies.',
346
mail_client.__class__.__name__)
347
mail_client.compose_merge_request(to, subject,
348
''.join(self.to_lines()),
352
class MergeDirective(BaseMergeDirective):
202
354
"""A request to perform a merge into a branch.
226
378
:param time: The current POSIX timestamp time
227
379
:param timezone: The timezone offset
228
:param target_branch: The branch to apply the merge to
380
:param target_branch: Location of the branch to apply the merge to
229
381
:param patch: The text of a diff or bundle
230
382
:param patch_type: None, "diff" or "bundle", depending on the contents
232
384
:param source_branch: A public location to merge the revision from
233
385
:param message: The message to use when committing this merge
235
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
387
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
236
388
timezone, target_branch, patch, source_branch, message)
237
assert patch_type in (None, 'diff', 'bundle'), patch_type
389
if patch_type not in (None, 'diff', 'bundle'):
390
raise ValueError(patch_type)
238
391
if patch_type != 'bundle' and source_branch is None:
239
392
raise errors.NoMergeSource()
240
393
if patch_type is not None and patch is None:
332
483
bundle=None, base_revision_id=None):
333
484
if source_branch is None and bundle is None:
334
485
raise errors.NoMergeSource()
335
_BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
486
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
336
487
timezone, target_branch, patch, source_branch, message)
337
488
self.bundle = bundle
338
489
self.base_revision_id = base_revision_id
420
571
:param target_branch: The url of the branch to merge into
421
572
:param include_patch: If true, include a preview patch
422
573
:param include_bundle: If true, include a bundle
423
:param local_target_branch: a local copy of the target branch
424
:param public_branch: location of a public branch containing the target
574
:param local_target_branch: the target branch, either itself or a local copy
575
:param public_branch: location of a public branch containing
426
577
:param message: Message to use when committing the merge
427
578
:return: The merge directive
476
630
raise errors.PublicBranchOutOfDate(public_branch,
632
testament_sha1 = t.as_sha1()
479
634
for entry in reversed(locked):
481
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
482
patch, public_branch, message, bundle, base_revision_id)
636
return klass(revision_id, testament_sha1, time, timezone,
637
target_branch, patch, public_branch, message, bundle,
484
640
def _verify_patch(self, repository):
485
641
calculated_patch = self._generate_diff(repository, self.revision_id,
521
677
_format_registry = MergeDirectiveFormatRegistry()
522
678
_format_registry.register(MergeDirective)
523
679
_format_registry.register(MergeDirective2)
680
# 0.19 never existed. It got renamed to 0.90. But by that point, there were
681
# already merge directives in the wild that used 0.19. Registering with the old
682
# format string to retain compatibility with those merge directives.
524
683
_format_registry.register(MergeDirective2,
525
684
'Bazaar merge directive format 2 (Bazaar 0.19)')