14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
18
from StringIO import StringIO
22
from . import lazy_import
23
lazy_import.lazy_import(globals(), """
22
25
branch as _mod_branch,
28
32
revision as _mod_revision,
34
from bzrlib.bundle import (
37
from breezy.bzr import (
40
from breezy.bzr.bundle import (
35
41
serializer as bundle_serializer,
37
from bzrlib.email_message import EmailMessage
52
class IllegalMergeDirectivePayload(errors.BzrError):
53
"""A merge directive contained something other than a patch or bundle"""
55
_fmt = "Bad merge directive payload %(start)r"
57
def __init__(self, start):
40
62
class MergeRequestBodyParams(object):
56
78
"""Hooks for MergeDirective classes."""
58
80
def __init__(self):
59
hooks.Hooks.__init__(self)
60
self.create_hook(hooks.HookPoint('merge_request_body',
81
hooks.Hooks.__init__(self, "breezy.merge_directive",
82
"BaseMergeDirective.hooks")
61
85
"Called with a MergeRequestBodyParams when a body is needed for"
62
86
" a merge request. Callbacks must return a body. If more"
63
87
" than one callback is registered, the output of one callback is"
64
" provided to the next.", (1, 15, 0), False))
88
" provided to the next.", (1, 15, 0))
67
91
class BaseMergeDirective(object):
68
92
"""A request to perform a merge into a branch.
70
This is the base class that all merge directive implementations
94
This is the base class that all merge directive implementations
71
95
should derive from.
73
:cvar multiple_output_files: Whether or not this merge directive
97
:cvar multiple_output_files: Whether or not this merge directive
74
98
stores a set of revisions in more than one file
79
103
multiple_output_files = False
81
105
def __init__(self, revision_id, testament_sha1, time, timezone,
82
target_branch, patch=None, source_branch=None, message=None,
106
target_branch, patch=None, source_branch=None,
107
message=None, bundle=None):
86
110
:param revision_id: The revision to merge
89
113
:param time: The current POSIX timestamp time
90
114
:param timezone: The timezone offset
91
:param target_branch: The branch to apply the merge to
115
:param target_branch: Location of branch to apply the merge to
92
116
:param patch: The text of a diff or bundle
93
117
:param source_branch: A public location to merge the revision from
94
118
:param message: The message to use when committing this merge
137
161
stanza.add(key, self.__dict__[key])
138
162
if base_revision:
139
163
stanza.add('base_revision_id', self.base_revision_id)
140
lines = ['# ' + self._format_string + '\n']
164
lines = [b'# ' + self._format_string + b'\n']
141
165
lines.extend(rio.to_patch_lines(stanza))
166
lines.append(b'# \n')
145
169
def write_to_directory(self, path):
153
177
def from_objects(klass, repository, revision_id, time, timezone,
154
target_branch, patch_type='bundle',
155
local_target_branch=None, public_branch=None, message=None):
178
target_branch, patch_type='bundle',
179
local_target_branch=None, public_branch=None, message=None):
156
180
"""Generate a merge directive from various objects
158
182
:param repository: The repository containing the revision
162
186
:param target_branch: The url of the branch to merge into
163
187
:param patch_type: 'bundle', 'diff' or None, depending on the type of
165
:param local_target_branch: a local copy of the target branch
166
:param public_branch: location of a public branch containing the target
189
:param local_target_branch: the submit branch, either itself or a local copy
190
:param public_branch: location of a public branch containing
168
192
:param message: Message to use when committing the merge
169
193
:return: The merge directive
178
202
if revision_id == _mod_revision.NULL_REVISION:
179
203
t_revision_id = None
180
204
t = testament.StrictTestament3.from_revision(repository, t_revision_id)
181
submit_branch = _mod_branch.Branch.open(target_branch)
205
if local_target_branch is None:
206
submit_branch = _mod_branch.Branch.open(target_branch)
208
submit_branch = local_target_branch
182
209
if submit_branch.get_public_branch() is not None:
183
210
target_branch = submit_branch.get_public_branch()
184
211
if patch_type is None:
192
219
submit_revision_id)
193
220
type_handler = {'bundle': klass._generate_bundle,
194
221
'diff': klass._generate_diff,
195
None: lambda x, y, z: None }
222
None: lambda x, y, z: None}
196
223
patch = type_handler[patch_type](repository, revision_id,
205
232
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
206
patch, patch_type, public_branch, message)
233
patch, patch_type, public_branch, message)
208
235
def get_disk_name(self, branch):
209
236
"""Generate a suitable basename for storing this directive on disk
215
242
if self.revision_id == revision_id:
218
revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
220
nick = re.sub('(\W+)', '-', branch.nick).strip('-')
246
revno = branch.revision_id_to_dotted_revno(self.revision_id)
247
except errors.NoSuchRevision:
249
nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
221
250
return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
224
253
def _generate_diff(repository, revision_id, ancestor_id):
225
254
tree_1 = repository.revision_tree(ancestor_id)
226
255
tree_2 = repository.revision_tree(revision_id)
228
257
diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
229
258
return s.getvalue()
232
261
def _generate_bundle(repository, revision_id, ancestor_id):
234
263
bundle_serializer.write_bundle(repository, revision_id,
236
265
return s.getvalue()
241
270
:param branch: The source branch, to get the signing strategy
242
271
:return: a string
244
my_gpg = gpg.GPGStrategy(branch.get_config())
245
return my_gpg.sign(''.join(self.to_lines()))
273
my_gpg = gpg.GPGStrategy(branch.get_config_stack())
274
return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
247
276
def to_email(self, mail_to, branch, sign=False):
248
277
"""Serialize as an email message.
263
292
body = self.to_signed(branch)
265
body = ''.join(self.to_lines())
266
message = EmailMessage(mail_from, mail_to, subject, body)
294
body = b''.join(self.to_lines())
295
message = email_message.EmailMessage(mail_from, mail_to, subject,
269
299
def install_revisions(self, target_repo):
271
301
if not target_repo.has_revision(self.revision_id):
272
302
if self.patch_type == 'bundle':
273
303
info = bundle_serializer.read_bundle(
274
StringIO(self.get_raw_bundle()))
304
BytesIO(self.get_raw_bundle()))
275
305
# We don't use the bundle's target revision, because
276
306
# MergeDirective.revision_id is authoritative.
289
319
info.real_revisions)
290
320
for revision in info.real_revisions:
291
321
for parent_id in revision.parent_ids:
292
if (parent_id not in bundle_revisions and
293
not target_repo.has_revision(parent_id)):
322
if (parent_id not in bundle_revisions
323
and not target_repo.has_revision(parent_id)):
294
324
missing_revisions.append(parent_id)
295
325
# reverse missing revisions to try to get heads first
296
326
unique_missing = []
335
365
elif len(self.hooks['merge_request_body']) > 0:
336
366
trace.warning('Cannot run merge_request_body hooks because mail'
337
367
' client %s does not support message bodies.',
338
mail_client.__class__.__name__)
368
mail_client.__class__.__name__)
339
369
mail_client.compose_merge_request(to, subject,
340
''.join(self.to_lines()),
370
b''.join(self.to_lines()),
357
387
directly using the standard patch program.
360
_format_string = 'Bazaar merge directive format 1'
390
_format_string = b'Bazaar merge directive format 1'
362
392
def __init__(self, revision_id, testament_sha1, time, timezone,
363
393
target_branch, patch=None, patch_type=None,
370
400
:param time: The current POSIX timestamp time
371
401
:param timezone: The timezone offset
372
:param target_branch: The branch to apply the merge to
402
:param target_branch: Location of the branch to apply the merge to
373
403
:param patch: The text of a diff or bundle
374
404
:param patch_type: None, "diff" or "bundle", depending on the contents
377
407
:param message: The message to use when committing this merge
379
409
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
380
timezone, target_branch, patch, source_branch, message)
410
timezone, target_branch, patch, source_branch, message)
381
411
if patch_type not in (None, 'diff', 'bundle'):
382
412
raise ValueError(patch_type)
383
413
if patch_type != 'bundle' and source_branch is None:
444
474
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
475
if 'testament_sha1' in kwargs:
476
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
445
477
return MergeDirective(time=time, timezone=timezone,
446
478
patch_type=patch_type, patch=patch, **kwargs)
469
501
class MergeDirective2(BaseMergeDirective):
471
_format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
503
_format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
473
505
def __init__(self, revision_id, testament_sha1, time, timezone,
474
506
target_branch, patch=None, source_branch=None, message=None,
476
508
if source_branch is None and bundle is None:
477
509
raise errors.NoMergeSource()
478
510
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
479
timezone, target_branch, patch, source_branch, message)
511
timezone, target_branch, patch, source_branch, message)
480
512
self.bundle = bundle
481
513
self.base_revision_id = base_revision_id
509
start = line_iter.next()
541
start = next(line_iter)
510
542
except StopIteration:
513
if start.startswith('# Begin patch'):
545
if start.startswith(b'# Begin patch'):
515
547
for line in line_iter:
516
if line.startswith('# Begin bundle'):
548
if line.startswith(b'# Begin bundle'):
519
551
patch_lines.append(line)
522
patch = ''.join(patch_lines)
554
patch = b''.join(patch_lines)
523
555
if start is not None:
524
if start.startswith('# Begin bundle'):
525
bundle = ''.join(line_iter)
556
if start.startswith(b'# Begin bundle'):
557
bundle = b''.join(line_iter)
527
raise errors.IllegalMergeDirectivePayload(start)
559
raise IllegalMergeDirectivePayload(start)
528
560
time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
530
562
for key in ('revision_id', 'testament_sha1', 'target_branch',
536
568
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
537
569
kwargs['base_revision_id'] =\
538
570
kwargs['base_revision_id'].encode('utf-8')
571
if 'testament_sha1' in kwargs:
572
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
539
573
return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
542
576
def to_lines(self):
543
577
lines = self._to_lines(base_revision=True)
544
578
if self.patch is not None:
545
lines.append('# Begin patch\n')
579
lines.append(b'# Begin patch\n')
546
580
lines.extend(self.patch.splitlines(True))
547
581
if self.bundle is not None:
548
lines.append('# Begin bundle\n')
582
lines.append(b'# Begin bundle\n')
549
583
lines.extend(self.bundle.splitlines(True))
553
587
def from_objects(klass, repository, revision_id, time, timezone,
554
target_branch, include_patch=True, include_bundle=True,
555
local_target_branch=None, public_branch=None, message=None,
556
base_revision_id=None):
588
target_branch, include_patch=True, include_bundle=True,
589
local_target_branch=None, public_branch=None, message=None,
590
base_revision_id=None):
557
591
"""Generate a merge directive from various objects
559
593
:param repository: The repository containing the revision
563
597
:param target_branch: The url of the branch to merge into
564
598
:param include_patch: If true, include a preview patch
565
599
:param include_bundle: If true, include a bundle
566
:param local_target_branch: a local copy of the target branch
567
:param public_branch: location of a public branch containing the target
600
:param local_target_branch: the target branch, either itself or a local copy
601
:param public_branch: location of a public branch containing
569
603
:param message: Message to use when committing the merge
570
604
:return: The merge directive
575
609
If the message is not supplied, the message from revision_id will be
576
610
used for the commit.
580
repository.lock_write()
581
locked.append(repository)
612
with cleanup.ExitStack() as exit_stack:
613
exit_stack.enter_context(repository.lock_write())
582
614
t_revision_id = revision_id
583
if revision_id == 'null:':
615
if revision_id == b'null:':
584
616
t_revision_id = None
585
617
t = testament.StrictTestament3.from_revision(repository,
587
submit_branch = _mod_branch.Branch.open(target_branch)
588
submit_branch.lock_read()
589
locked.append(submit_branch)
619
if local_target_branch is None:
620
submit_branch = _mod_branch.Branch.open(target_branch)
622
submit_branch = local_target_branch
623
exit_stack.enter_context(submit_branch.lock_read())
590
624
if submit_branch.get_public_branch() is not None:
591
625
target_branch = submit_branch.get_public_branch()
592
626
submit_revision_id = submit_branch.last_revision()
607
641
if include_bundle:
608
bundle = klass._generate_bundle(repository, revision_id,
609
ancestor_id).encode('base-64')
642
bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
613
647
if public_branch is not None and not include_bundle:
614
648
public_branch_obj = _mod_branch.Branch.open(public_branch)
615
public_branch_obj.lock_read()
616
locked.append(public_branch_obj)
649
exit_stack.enter_context(public_branch_obj.lock_read())
617
650
if not public_branch_obj.repository.has_revision(
619
652
raise errors.PublicBranchOutOfDate(public_branch,
621
654
testament_sha1 = t.as_sha1()
623
for entry in reversed(locked):
625
655
return klass(revision_id, testament_sha1, time, timezone,
626
target_branch, patch, public_branch, message, bundle,
656
target_branch, patch, public_branch, message, bundle,
629
659
def _verify_patch(self, repository):
630
660
calculated_patch = self._generate_diff(repository, self.revision_id,
631
661
self.base_revision_id)
632
662
# Convert line-endings to UNIX
633
stored_patch = re.sub('\r\n?', '\n', self.patch)
634
calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
663
stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
664
calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
635
665
# Strip trailing whitespace
636
calculated_patch = re.sub(' *\n', '\n', calculated_patch)
637
stored_patch = re.sub(' *\n', '\n', stored_patch)
666
calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
667
stored_patch = re.sub(b' *\n', b'\n', stored_patch)
638
668
return (calculated_patch == stored_patch)
640
670
def get_merge_request(self, repository):