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
19
from io import BytesIO
17
from __future__ import absolute_import
19
from StringIO import StringIO
22
from . import lazy_import
22
from brzlib import lazy_import
23
23
lazy_import.lazy_import(globals(), """
25
25
branch as _mod_branch,
31
32
revision as _mod_revision,
36
from breezy.bzr import (
39
from breezy.bzr.bundle import (
38
from brzlib.bundle import (
40
39
serializer as bundle_serializer,
48
44
class MergeRequestBodyParams(object):
64
60
"""Hooks for MergeDirective classes."""
66
62
def __init__(self):
67
hooks.Hooks.__init__(self, "breezy.merge_directive",
68
"BaseMergeDirective.hooks")
63
hooks.Hooks.__init__(self, "brzlib.merge_directive", "BaseMergeDirective.hooks")
64
self.add_hook('merge_request_body',
71
65
"Called with a MergeRequestBodyParams when a body is needed for"
72
66
" a merge request. Callbacks must return a body. If more"
73
67
" than one callback is registered, the output of one callback is"
77
71
class BaseMergeDirective(object):
78
72
"""A request to perform a merge into a branch.
80
This is the base class that all merge directive implementations
74
This is the base class that all merge directive implementations
81
75
should derive from.
83
:cvar multiple_output_files: Whether or not this merge directive
77
:cvar multiple_output_files: Whether or not this merge directive
84
78
stores a set of revisions in more than one file
147
141
stanza.add(key, self.__dict__[key])
148
142
if base_revision:
149
143
stanza.add('base_revision_id', self.base_revision_id)
150
lines = [b'# ' + self._format_string + b'\n']
144
lines = ['# ' + self._format_string + '\n']
151
145
lines.extend(rio.to_patch_lines(stanza))
152
lines.append(b'# \n')
155
149
def write_to_directory(self, path):
163
157
def from_objects(klass, repository, revision_id, time, timezone,
164
target_branch, patch_type='bundle',
165
local_target_branch=None, public_branch=None, message=None):
158
target_branch, patch_type='bundle',
159
local_target_branch=None, public_branch=None, message=None):
166
160
"""Generate a merge directive from various objects
168
162
:param repository: The repository containing the revision
205
199
submit_revision_id)
206
200
type_handler = {'bundle': klass._generate_bundle,
207
201
'diff': klass._generate_diff,
208
None: lambda x, y, z: None}
202
None: lambda x, y, z: None }
209
203
patch = type_handler[patch_type](repository, revision_id,
218
212
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
219
patch, patch_type, public_branch, message)
213
patch, patch_type, public_branch, message)
221
215
def get_disk_name(self, branch):
222
216
"""Generate a suitable basename for storing this directive on disk
228
222
if self.revision_id == revision_id:
232
revno = branch.revision_id_to_dotted_revno(self.revision_id)
233
except errors.NoSuchRevision:
235
nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
225
revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
227
nick = re.sub('(\W+)', '-', branch.nick).strip('-')
236
228
return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
239
231
def _generate_diff(repository, revision_id, ancestor_id):
240
232
tree_1 = repository.revision_tree(ancestor_id)
241
233
tree_2 = repository.revision_tree(revision_id)
243
235
diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='')
244
236
return s.getvalue()
247
239
def _generate_bundle(repository, revision_id, ancestor_id):
249
241
bundle_serializer.write_bundle(repository, revision_id,
251
243
return s.getvalue()
257
249
:return: a string
259
251
my_gpg = gpg.GPGStrategy(branch.get_config_stack())
260
return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
252
return my_gpg.sign(''.join(self.to_lines()))
262
254
def to_email(self, mail_to, branch, sign=False):
263
255
"""Serialize as an email message.
278
270
body = self.to_signed(branch)
280
body = b''.join(self.to_lines())
272
body = ''.join(self.to_lines())
281
273
message = email_message.EmailMessage(mail_from, mail_to, subject,
285
277
def install_revisions(self, target_repo):
287
279
if not target_repo.has_revision(self.revision_id):
288
280
if self.patch_type == 'bundle':
289
281
info = bundle_serializer.read_bundle(
290
BytesIO(self.get_raw_bundle()))
282
StringIO(self.get_raw_bundle()))
291
283
# We don't use the bundle's target revision, because
292
284
# MergeDirective.revision_id is authoritative.
305
297
info.real_revisions)
306
298
for revision in info.real_revisions:
307
299
for parent_id in revision.parent_ids:
308
if (parent_id not in bundle_revisions
309
and not target_repo.has_revision(parent_id)):
300
if (parent_id not in bundle_revisions and
301
not target_repo.has_revision(parent_id)):
310
302
missing_revisions.append(parent_id)
311
303
# reverse missing revisions to try to get heads first
312
304
unique_missing = []
351
343
elif len(self.hooks['merge_request_body']) > 0:
352
344
trace.warning('Cannot run merge_request_body hooks because mail'
353
345
' client %s does not support message bodies.',
354
mail_client.__class__.__name__)
346
mail_client.__class__.__name__)
355
347
mail_client.compose_merge_request(to, subject,
356
b''.join(self.to_lines()),
348
''.join(self.to_lines()),
373
365
directly using the standard patch program.
376
_format_string = b'Bazaar merge directive format 1'
368
_format_string = 'Bazaar merge directive format 1'
378
370
def __init__(self, revision_id, testament_sha1, time, timezone,
379
371
target_branch, patch=None, patch_type=None,
393
385
:param message: The message to use when committing this merge
395
387
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
396
timezone, target_branch, patch, source_branch, message)
388
timezone, target_branch, patch, source_branch, message)
397
389
if patch_type not in (None, 'diff', 'bundle'):
398
390
raise ValueError(patch_type)
399
391
if patch_type != 'bundle' and source_branch is None:
460
452
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
461
if 'testament_sha1' in kwargs:
462
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
463
453
return MergeDirective(time=time, timezone=timezone,
464
454
patch_type=patch_type, patch=patch, **kwargs)
487
477
class MergeDirective2(BaseMergeDirective):
489
_format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
479
_format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
491
481
def __init__(self, revision_id, testament_sha1, time, timezone,
492
482
target_branch, patch=None, source_branch=None, message=None,
494
484
if source_branch is None and bundle is None:
495
485
raise errors.NoMergeSource()
496
486
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
497
timezone, target_branch, patch, source_branch, message)
487
timezone, target_branch, patch, source_branch, message)
498
488
self.bundle = bundle
499
489
self.base_revision_id = base_revision_id
527
start = next(line_iter)
517
start = line_iter.next()
528
518
except StopIteration:
531
if start.startswith(b'# Begin patch'):
521
if start.startswith('# Begin patch'):
533
523
for line in line_iter:
534
if line.startswith(b'# Begin bundle'):
524
if line.startswith('# Begin bundle'):
537
527
patch_lines.append(line)
540
patch = b''.join(patch_lines)
530
patch = ''.join(patch_lines)
541
531
if start is not None:
542
if start.startswith(b'# Begin bundle'):
543
bundle = b''.join(line_iter)
532
if start.startswith('# Begin bundle'):
533
bundle = ''.join(line_iter)
545
535
raise errors.IllegalMergeDirectivePayload(start)
546
536
time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
554
544
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
555
545
kwargs['base_revision_id'] =\
556
546
kwargs['base_revision_id'].encode('utf-8')
557
if 'testament_sha1' in kwargs:
558
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
559
547
return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
562
550
def to_lines(self):
563
551
lines = self._to_lines(base_revision=True)
564
552
if self.patch is not None:
565
lines.append(b'# Begin patch\n')
553
lines.append('# Begin patch\n')
566
554
lines.extend(self.patch.splitlines(True))
567
555
if self.bundle is not None:
568
lines.append(b'# Begin bundle\n')
556
lines.append('# Begin bundle\n')
569
557
lines.extend(self.bundle.splitlines(True))
573
561
def from_objects(klass, repository, revision_id, time, timezone,
574
target_branch, include_patch=True, include_bundle=True,
575
local_target_branch=None, public_branch=None, message=None,
576
base_revision_id=None):
562
target_branch, include_patch=True, include_bundle=True,
563
local_target_branch=None, public_branch=None, message=None,
564
base_revision_id=None):
577
565
"""Generate a merge directive from various objects
579
567
:param repository: The repository containing the revision
595
583
If the message is not supplied, the message from revision_id will be
596
584
used for the commit.
598
with contextlib.ExitStack() as exit_stack:
599
exit_stack.enter_context(repository.lock_write())
588
repository.lock_write()
589
locked.append(repository)
600
590
t_revision_id = revision_id
601
if revision_id == b'null:':
591
if revision_id == 'null:':
602
592
t_revision_id = None
603
593
t = testament.StrictTestament3.from_revision(repository,
605
595
if local_target_branch is None:
606
596
submit_branch = _mod_branch.Branch.open(target_branch)
608
598
submit_branch = local_target_branch
609
exit_stack.enter_context(submit_branch.lock_read())
599
submit_branch.lock_read()
600
locked.append(submit_branch)
610
601
if submit_branch.get_public_branch() is not None:
611
602
target_branch = submit_branch.get_public_branch()
612
603
submit_revision_id = submit_branch.last_revision()
627
618
if include_bundle:
628
bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
619
bundle = klass._generate_bundle(repository, revision_id,
620
ancestor_id).encode('base-64')
633
624
if public_branch is not None and not include_bundle:
634
625
public_branch_obj = _mod_branch.Branch.open(public_branch)
635
exit_stack.enter_context(public_branch_obj.lock_read())
626
public_branch_obj.lock_read()
627
locked.append(public_branch_obj)
636
628
if not public_branch_obj.repository.has_revision(
638
630
raise errors.PublicBranchOutOfDate(public_branch,
640
632
testament_sha1 = t.as_sha1()
634
for entry in reversed(locked):
641
636
return klass(revision_id, testament_sha1, time, timezone,
642
target_branch, patch, public_branch, message, bundle,
637
target_branch, patch, public_branch, message, bundle,
645
640
def _verify_patch(self, repository):
646
641
calculated_patch = self._generate_diff(repository, self.revision_id,
647
642
self.base_revision_id)
648
643
# Convert line-endings to UNIX
649
stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
650
calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
644
stored_patch = re.sub('\r\n?', '\n', self.patch)
645
calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
651
646
# Strip trailing whitespace
652
calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
653
stored_patch = re.sub(b' *\n', b'\n', stored_patch)
647
calculated_patch = re.sub(' *\n', '\n', calculated_patch)
648
stored_patch = re.sub(' *\n', '\n', stored_patch)
654
649
return (calculated_patch == stored_patch)
656
651
def get_merge_request(self, repository):