547
546
for hunk_line in hunk.lines:
548
seen_patch.append(hunk_line.contents)
547
seen_patch.append(str(hunk_line))
549
548
if isinstance(hunk_line, InsertLine):
550
549
yield hunk_line.contents
551
550
elif isinstance(hunk_line, (ContextLine, RemoveLine)):
552
551
orig_line = next(orig_lines)
553
552
if orig_line != hunk_line.contents:
554
553
raise PatchConflict(line_no, orig_line,
555
b''.join(seen_patch))
554
b"".join(seen_patch))
556
555
if isinstance(hunk_line, ContextLine):
562
561
if orig_lines is not None:
563
562
for line in orig_lines:
567
def apply_patches(tt, patches, prefix=1):
568
"""Apply patches to a TreeTransform.
570
:param tt: TreeTransform instance
571
:param patches: List of patches
572
:param prefix: Number leading path segments to strip
575
return '/'.join(p.split('/')[1:])
577
from breezy.bzr.generate_ids import gen_file_id
578
# TODO(jelmer): Extract and set mode
579
for patch in patches:
580
if patch.oldname == b'/dev/null':
584
oldname = strip_prefix(patch.oldname.decode())
585
trans_id = tt.trans_id_tree_path(oldname)
586
orig_contents = tt._tree.get_file_text(oldname)
587
tt.delete_contents(trans_id)
589
if patch.newname != b'/dev/null':
590
newname = strip_prefix(patch.newname.decode())
591
new_contents = iter_patched_from_hunks(
592
orig_contents.splitlines(True), patch.hunks)
594
parts = os.path.split(newname)
596
for part in parts[1:-1]:
597
trans_id = tt.new_directory(part, trans_id)
599
parts[-1], trans_id, new_contents,
600
file_id=gen_file_id(newname))
602
tt.create_file(new_contents, trans_id)
605
class AppliedPatches(object):
606
"""Context that provides access to a tree with patches applied.
609
def __init__(self, tree, patches, prefix=1):
611
self.patches = patches
615
from .transform import TransformPreview
616
self._tt = TransformPreview(self.tree)
617
apply_patches(self._tt, self.patches, prefix=self.prefix)
618
return self._tt.get_preview_tree()
620
def __exit__(self, exc_type, exc_value, exc_tb):