/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/patches.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    BzrError,
22
22
    )
23
23
 
24
 
import os
25
24
import re
26
25
 
27
26
 
545
544
            yield orig_line
546
545
            line_no += 1
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):
557
556
                    yield orig_line
558
557
                else:
562
561
    if orig_lines is not None:
563
562
        for line in orig_lines:
564
563
            yield line
565
 
 
566
 
 
567
 
def apply_patches(tt, patches, prefix=1):
568
 
    """Apply patches to a TreeTransform.
569
 
 
570
 
    :param tt: TreeTransform instance
571
 
    :param patches: List of patches
572
 
    :param prefix: Number leading path segments to strip
573
 
    """
574
 
    def strip_prefix(p):
575
 
        return '/'.join(p.split('/')[1:])
576
 
 
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':
581
 
            trans_id = None
582
 
            orig_contents = b''
583
 
        else:
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)
588
 
 
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)
593
 
            if trans_id is None:
594
 
                parts = os.path.split(newname)
595
 
                trans_id = tt.root
596
 
                for part in parts[1:-1]:
597
 
                    trans_id = tt.new_directory(part, trans_id)
598
 
                tt.new_file(
599
 
                    parts[-1], trans_id, new_contents,
600
 
                    file_id=gen_file_id(newname))
601
 
            else:
602
 
                tt.create_file(new_contents, trans_id)
603
 
 
604
 
 
605
 
class AppliedPatches(object):
606
 
    """Context that provides access to a tree with patches applied.
607
 
    """
608
 
 
609
 
    def __init__(self, tree, patches, prefix=1):
610
 
        self.tree = tree
611
 
        self.patches = patches
612
 
        self.prefix = prefix
613
 
 
614
 
    def __enter__(self):
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()
619
 
 
620
 
    def __exit__(self, exc_type, exc_value, exc_tb):
621
 
        self._tt.finalize()
622
 
        return False