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
19
from io import BytesIO
21
22
from . import lazy_import
62
64
"""Hooks for MergeDirective classes."""
64
66
def __init__(self):
65
hooks.Hooks.__init__(self, "breezy.merge_directive", "BaseMergeDirective.hooks")
66
self.add_hook('merge_request_body',
67
hooks.Hooks.__init__(self, "breezy.merge_directive",
68
"BaseMergeDirective.hooks")
67
71
"Called with a MergeRequestBodyParams when a body is needed for"
68
72
" a merge request. Callbacks must return a body. If more"
69
73
" than one callback is registered, the output of one callback is"
73
77
class BaseMergeDirective(object):
74
78
"""A request to perform a merge into a branch.
76
This is the base class that all merge directive implementations
80
This is the base class that all merge directive implementations
77
81
should derive from.
79
:cvar multiple_output_files: Whether or not this merge directive
83
:cvar multiple_output_files: Whether or not this merge directive
80
84
stores a set of revisions in more than one file
143
147
stanza.add(key, self.__dict__[key])
144
148
if base_revision:
145
149
stanza.add('base_revision_id', self.base_revision_id)
146
lines = ['# ' + self._format_string + '\n']
150
lines = [b'# ' + self._format_string + b'\n']
147
151
lines.extend(rio.to_patch_lines(stanza))
152
lines.append(b'# \n')
151
155
def write_to_directory(self, path):
159
163
def from_objects(klass, repository, revision_id, time, timezone,
160
target_branch, patch_type='bundle',
161
local_target_branch=None, public_branch=None, message=None):
164
target_branch, patch_type='bundle',
165
local_target_branch=None, public_branch=None, message=None):
162
166
"""Generate a merge directive from various objects
164
168
:param repository: The repository containing the revision
201
205
submit_revision_id)
202
206
type_handler = {'bundle': klass._generate_bundle,
203
207
'diff': klass._generate_diff,
204
None: lambda x, y, z: None }
208
None: lambda x, y, z: None}
205
209
patch = type_handler[patch_type](repository, revision_id,
214
218
return klass(revision_id, t.as_sha1(), time, timezone, target_branch,
215
patch, patch_type, public_branch, message)
219
patch, patch_type, public_branch, message)
217
221
def get_disk_name(self, branch):
218
222
"""Generate a suitable basename for storing this directive on disk
224
228
if self.revision_id == revision_id:
227
revno = branch.get_revision_id_to_revno_map().get(self.revision_id,
232
revno = branch.revision_id_to_dotted_revno(self.revision_id)
233
except errors.NoSuchRevision:
229
235
nick = re.sub('(\\W+)', '-', branch.nick).strip('-')
230
236
return '%s-%s' % (nick, '.'.join(str(n) for n in revno))
251
257
:return: a string
253
259
my_gpg = gpg.GPGStrategy(branch.get_config_stack())
254
return my_gpg.sign(''.join(self.to_lines()))
260
return my_gpg.sign(b''.join(self.to_lines()), gpg.MODE_CLEAR)
256
262
def to_email(self, mail_to, branch, sign=False):
257
263
"""Serialize as an email message.
272
278
body = self.to_signed(branch)
274
body = ''.join(self.to_lines())
280
body = b''.join(self.to_lines())
275
281
message = email_message.EmailMessage(mail_from, mail_to, subject,
279
285
def install_revisions(self, target_repo):
299
305
info.real_revisions)
300
306
for revision in info.real_revisions:
301
307
for parent_id in revision.parent_ids:
302
if (parent_id not in bundle_revisions and
303
not target_repo.has_revision(parent_id)):
308
if (parent_id not in bundle_revisions
309
and not target_repo.has_revision(parent_id)):
304
310
missing_revisions.append(parent_id)
305
311
# reverse missing revisions to try to get heads first
306
312
unique_missing = []
345
351
elif len(self.hooks['merge_request_body']) > 0:
346
352
trace.warning('Cannot run merge_request_body hooks because mail'
347
353
' client %s does not support message bodies.',
348
mail_client.__class__.__name__)
354
mail_client.__class__.__name__)
349
355
mail_client.compose_merge_request(to, subject,
350
''.join(self.to_lines()),
356
b''.join(self.to_lines()),
367
373
directly using the standard patch program.
370
_format_string = 'Bazaar merge directive format 1'
376
_format_string = b'Bazaar merge directive format 1'
372
378
def __init__(self, revision_id, testament_sha1, time, timezone,
373
379
target_branch, patch=None, patch_type=None,
387
393
:param message: The message to use when committing this merge
389
395
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
390
timezone, target_branch, patch, source_branch, message)
396
timezone, target_branch, patch, source_branch, message)
391
397
if patch_type not in (None, 'diff', 'bundle'):
392
398
raise ValueError(patch_type)
393
399
if patch_type != 'bundle' and source_branch is None:
454
460
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
461
if 'testament_sha1' in kwargs:
462
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
455
463
return MergeDirective(time=time, timezone=timezone,
456
464
patch_type=patch_type, patch=patch, **kwargs)
479
487
class MergeDirective2(BaseMergeDirective):
481
_format_string = 'Bazaar merge directive format 2 (Bazaar 0.90)'
489
_format_string = b'Bazaar merge directive format 2 (Bazaar 0.90)'
483
491
def __init__(self, revision_id, testament_sha1, time, timezone,
484
492
target_branch, patch=None, source_branch=None, message=None,
486
494
if source_branch is None and bundle is None:
487
495
raise errors.NoMergeSource()
488
496
BaseMergeDirective.__init__(self, revision_id, testament_sha1, time,
489
timezone, target_branch, patch, source_branch, message)
497
timezone, target_branch, patch, source_branch, message)
490
498
self.bundle = bundle
491
499
self.base_revision_id = base_revision_id
520
528
except StopIteration:
523
if start.startswith('# Begin patch'):
531
if start.startswith(b'# Begin patch'):
525
533
for line in line_iter:
526
if line.startswith('# Begin bundle'):
534
if line.startswith(b'# Begin bundle'):
529
537
patch_lines.append(line)
532
patch = ''.join(patch_lines)
540
patch = b''.join(patch_lines)
533
541
if start is not None:
534
if start.startswith('# Begin bundle'):
535
bundle = ''.join(line_iter)
542
if start.startswith(b'# Begin bundle'):
543
bundle = b''.join(line_iter)
537
545
raise errors.IllegalMergeDirectivePayload(start)
538
546
time, timezone = timestamp.parse_patch_date(stanza.get('timestamp'))
546
554
kwargs['revision_id'] = kwargs['revision_id'].encode('utf-8')
547
555
kwargs['base_revision_id'] =\
548
556
kwargs['base_revision_id'].encode('utf-8')
557
if 'testament_sha1' in kwargs:
558
kwargs['testament_sha1'] = kwargs['testament_sha1'].encode('ascii')
549
559
return klass(time=time, timezone=timezone, patch=patch, bundle=bundle,
552
562
def to_lines(self):
553
563
lines = self._to_lines(base_revision=True)
554
564
if self.patch is not None:
555
lines.append('# Begin patch\n')
565
lines.append(b'# Begin patch\n')
556
566
lines.extend(self.patch.splitlines(True))
557
567
if self.bundle is not None:
558
lines.append('# Begin bundle\n')
568
lines.append(b'# Begin bundle\n')
559
569
lines.extend(self.bundle.splitlines(True))
563
573
def from_objects(klass, repository, revision_id, time, timezone,
564
target_branch, include_patch=True, include_bundle=True,
565
local_target_branch=None, public_branch=None, message=None,
566
base_revision_id=None):
574
target_branch, include_patch=True, include_bundle=True,
575
local_target_branch=None, public_branch=None, message=None,
576
base_revision_id=None):
567
577
"""Generate a merge directive from various objects
569
579
:param repository: The repository containing the revision
585
595
If the message is not supplied, the message from revision_id will be
586
596
used for the commit.
590
repository.lock_write()
591
locked.append(repository)
598
with contextlib.ExitStack() as exit_stack:
599
exit_stack.enter_context(repository.lock_write())
592
600
t_revision_id = revision_id
593
if revision_id == 'null:':
601
if revision_id == b'null:':
594
602
t_revision_id = None
595
603
t = testament.StrictTestament3.from_revision(repository,
597
605
if local_target_branch is None:
598
606
submit_branch = _mod_branch.Branch.open(target_branch)
600
608
submit_branch = local_target_branch
601
submit_branch.lock_read()
602
locked.append(submit_branch)
609
exit_stack.enter_context(submit_branch.lock_read())
603
610
if submit_branch.get_public_branch() is not None:
604
611
target_branch = submit_branch.get_public_branch()
605
612
submit_revision_id = submit_branch.last_revision()
620
627
if include_bundle:
621
bundle = klass._generate_bundle(repository, revision_id,
622
ancestor_id).encode('base-64')
628
bundle = base64.b64encode(klass._generate_bundle(repository, revision_id,
626
633
if public_branch is not None and not include_bundle:
627
634
public_branch_obj = _mod_branch.Branch.open(public_branch)
628
public_branch_obj.lock_read()
629
locked.append(public_branch_obj)
635
exit_stack.enter_context(public_branch_obj.lock_read())
630
636
if not public_branch_obj.repository.has_revision(
632
638
raise errors.PublicBranchOutOfDate(public_branch,
634
640
testament_sha1 = t.as_sha1()
636
for entry in reversed(locked):
638
641
return klass(revision_id, testament_sha1, time, timezone,
639
target_branch, patch, public_branch, message, bundle,
642
target_branch, patch, public_branch, message, bundle,
642
645
def _verify_patch(self, repository):
643
646
calculated_patch = self._generate_diff(repository, self.revision_id,
644
647
self.base_revision_id)
645
648
# Convert line-endings to UNIX
646
stored_patch = re.sub('\r\n?', '\n', self.patch)
647
calculated_patch = re.sub('\r\n?', '\n', calculated_patch)
649
stored_patch = re.sub(b'\r\n?', b'\n', self.patch)
650
calculated_patch = re.sub(b'\r\n?', b'\n', calculated_patch)
648
651
# Strip trailing whitespace
649
calculated_patch = re.sub(' *\n', '\n', calculated_patch)
650
stored_patch = re.sub(' *\n', '\n', stored_patch)
652
calculated_patch = re.sub(b' *\n', b'\n', calculated_patch)
653
stored_patch = re.sub(b' *\n', b'\n', stored_patch)
651
654
return (calculated_patch == stored_patch)
653
656
def get_merge_request(self, repository):