/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-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
 
18
from __future__ import absolute_import
 
19
 
18
20
from .errors import (
19
21
    BzrError,
20
22
    )
21
23
 
22
 
import os
23
24
import re
24
25
 
25
26
 
543
544
            yield orig_line
544
545
            line_no += 1
545
546
        for hunk_line in hunk.lines:
546
 
            seen_patch.append(hunk_line.contents)
 
547
            seen_patch.append(str(hunk_line))
547
548
            if isinstance(hunk_line, InsertLine):
548
549
                yield hunk_line.contents
549
550
            elif isinstance(hunk_line, (ContextLine, RemoveLine)):
550
551
                orig_line = next(orig_lines)
551
552
                if orig_line != hunk_line.contents:
552
553
                    raise PatchConflict(line_no, orig_line,
553
 
                                        b''.join(seen_patch))
 
554
                                        b"".join(seen_patch))
554
555
                if isinstance(hunk_line, ContextLine):
555
556
                    yield orig_line
556
557
                else:
560
561
    if orig_lines is not None:
561
562
        for line in orig_lines:
562
563
            yield line
563
 
 
564
 
 
565
 
def apply_patches(tt, patches, prefix=1):
566
 
    """Apply patches to a TreeTransform.
567
 
 
568
 
    :param tt: TreeTransform instance
569
 
    :param patches: List of patches
570
 
    :param prefix: Number leading path segments to strip
571
 
    """
572
 
    def strip_prefix(p):
573
 
        return '/'.join(p.split('/')[1:])
574
 
 
575
 
    from breezy.bzr.generate_ids import gen_file_id
576
 
    # TODO(jelmer): Extract and set mode
577
 
    for patch in patches:
578
 
        if patch.oldname == b'/dev/null':
579
 
            trans_id = None
580
 
            orig_contents = b''
581
 
        else:
582
 
            oldname = strip_prefix(patch.oldname.decode())
583
 
            trans_id = tt.trans_id_tree_path(oldname)
584
 
            orig_contents = tt._tree.get_file_text(oldname)
585
 
            tt.delete_contents(trans_id)
586
 
 
587
 
        if patch.newname != b'/dev/null':
588
 
            newname = strip_prefix(patch.newname.decode())
589
 
            new_contents = iter_patched_from_hunks(
590
 
                orig_contents.splitlines(True), patch.hunks)
591
 
            if trans_id is None:
592
 
                parts = os.path.split(newname)
593
 
                trans_id = tt.root
594
 
                for part in parts[1:-1]:
595
 
                    trans_id = tt.new_directory(part, trans_id)
596
 
                tt.new_file(
597
 
                    parts[-1], trans_id, new_contents,
598
 
                    file_id=gen_file_id(newname))
599
 
            else:
600
 
                tt.create_file(new_contents, trans_id)
601
 
 
602
 
 
603
 
class AppliedPatches(object):
604
 
    """Context that provides access to a tree with patches applied.
605
 
    """
606
 
 
607
 
    def __init__(self, tree, patches, prefix=1):
608
 
        self.tree = tree
609
 
        self.patches = patches
610
 
        self.prefix = prefix
611
 
 
612
 
    def __enter__(self):
613
 
        self._tt = self.tree.preview_transform()
614
 
        apply_patches(self._tt, self.patches, prefix=self.prefix)
615
 
        return self._tt.get_preview_tree()
616
 
 
617
 
    def __exit__(self, exc_type, exc_value, exc_tb):
618
 
        self._tt.finalize()
619
 
        return False