/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/workingtree_4.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
from __future__ import absolute_import
26
26
 
27
 
from cStringIO import StringIO
28
27
import os
29
28
import sys
30
29
 
31
 
from bzrlib.lazy_import import lazy_import
 
30
from .lazy_import import lazy_import
32
31
lazy_import(globals(), """
33
32
import errno
34
33
import stat
35
34
 
36
 
from bzrlib import (
 
35
from breezy import (
37
36
    bzrdir,
38
37
    cache_utf8,
 
38
    cleanup,
39
39
    config,
40
40
    conflicts as _mod_conflicts,
41
41
    controldir,
53
53
    )
54
54
""")
55
55
 
56
 
from bzrlib.decorators import needs_read_lock, needs_write_lock
57
 
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
58
 
from bzrlib.lock import LogicalLockResult
59
 
from bzrlib.lockable_files import LockableFiles
60
 
from bzrlib.lockdir import LockDir
61
 
from bzrlib.mutabletree import (
 
56
from .decorators import needs_read_lock, needs_write_lock
 
57
from .inventory import Inventory, ROOT_ID, entry_factory
 
58
from .lock import LogicalLockResult
 
59
from .lockable_files import LockableFiles
 
60
from .lockdir import LockDir
 
61
from .mutabletree import (
62
62
    MutableTree,
63
63
    needs_tree_write_lock,
64
64
    )
65
 
from bzrlib.osutils import (
 
65
from .osutils import (
66
66
    file_kind,
67
67
    isdir,
68
68
    pathjoin,
69
69
    realpath,
70
70
    safe_unicode,
71
71
    )
72
 
from bzrlib.symbol_versioning import (
 
72
from .sixish import (
 
73
    BytesIO,
 
74
    )
 
75
from .symbol_versioning import (
73
76
    deprecated_in,
74
77
    deprecated_method,
75
78
    )
76
 
from bzrlib.transport.local import LocalTransport
77
 
from bzrlib.tree import (
 
79
from .transport.local import LocalTransport
 
80
from .tree import (
78
81
    InterTree,
79
82
    InventoryTree,
80
83
    )
81
 
from bzrlib.workingtree import (
 
84
from .workingtree import (
82
85
    InventoryWorkingTree,
83
86
    WorkingTree,
84
87
    WorkingTreeFormatMetaDir,
399
402
        if stat_value is None:
400
403
            try:
401
404
                stat_value = osutils.lstat(file_abspath)
402
 
            except OSError, e:
 
405
            except OSError as e:
403
406
                if e.errno == errno.ENOENT:
404
407
                    return None
405
408
                else:
556
559
                # path is missing on disk.
557
560
                continue
558
561
 
559
 
    def _observed_sha1(self, file_id, path, (sha1, statvalue)):
 
562
    def _observed_sha1(self, file_id, path, sha_and_stat):
560
563
        """See MutableTree._observed_sha1."""
561
564
        state = self.current_dirstate()
562
565
        entry = self._get_entry(file_id=file_id, path=path)
563
 
        state._observed_sha1(entry, sha1, statvalue)
 
566
        state._observed_sha1(entry, *sha_and_stat)
564
567
 
565
568
    def kind(self, file_id):
566
569
        """Return the kind of a file.
598
601
    def lock_read(self):
599
602
        """See Branch.lock_read, and WorkingTree.unlock.
600
603
 
601
 
        :return: A bzrlib.lock.LogicalLockResult.
 
604
        :return: A breezy.lock.LogicalLockResult.
602
605
        """
603
606
        self.branch.lock_read()
604
607
        try:
644
647
    def lock_tree_write(self):
645
648
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
646
649
 
647
 
        :return: A bzrlib.lock.LogicalLockResult.
 
650
        :return: A breezy.lock.LogicalLockResult.
648
651
        """
649
652
        self.branch.lock_read()
650
653
        return self._lock_self_write()
652
655
    def lock_write(self):
653
656
        """See MutableTree.lock_write, and WorkingTree.unlock.
654
657
 
655
 
        :return: A bzrlib.lock.LogicalLockResult.
 
658
        :return: A breezy.lock.LogicalLockResult.
656
659
        """
657
660
        self.branch.lock_write()
658
661
        return self._lock_self_write()
698
701
        else:
699
702
            update_inventory = False
700
703
 
701
 
        rollbacks = []
 
704
        # GZ 2017-03-28: The rollbacks variable was shadowed in the loop below
 
705
        # missing those added here, but there's also no test coverage for this.
 
706
        rollbacks = cleanup.ObjectWithCleanups()
702
707
        def move_one(old_entry, from_path_utf8, minikind, executable,
703
708
                     fingerprint, packed_stat, size,
704
709
                     to_block, to_key, to_path_utf8):
705
710
            state._make_absent(old_entry)
706
711
            from_key = old_entry[0]
707
 
            rollbacks.append(
708
 
                lambda:state.update_minimal(from_key,
709
 
                    minikind,
710
 
                    executable=executable,
711
 
                    fingerprint=fingerprint,
712
 
                    packed_stat=packed_stat,
713
 
                    size=size,
714
 
                    path_utf8=from_path_utf8))
 
712
            rollbacks.add_cleanup(
 
713
                state.update_minimal,
 
714
                from_key,
 
715
                minikind,
 
716
                executable=executable,
 
717
                fingerprint=fingerprint,
 
718
                packed_stat=packed_stat,
 
719
                size=size,
 
720
                path_utf8=from_path_utf8)
715
721
            state.update_minimal(to_key,
716
722
                    minikind,
717
723
                    executable=executable,
721
727
                    path_utf8=to_path_utf8)
722
728
            added_entry_index, _ = state._find_entry_index(to_key, to_block[1])
723
729
            new_entry = to_block[1][added_entry_index]
724
 
            rollbacks.append(lambda:state._make_absent(new_entry))
 
730
            rollbacks.add_cleanup(state._make_absent, new_entry)
725
731
 
726
732
        for from_rel in from_paths:
727
733
            # from_rel is 'pathinroot/foo/bar'
766
772
                elif not after:
767
773
                    raise errors.RenameFailedFilesExist(from_rel, to_rel)
768
774
 
769
 
            rollbacks = []
770
 
            def rollback_rename():
771
 
                """A single rename has failed, roll it back."""
772
 
                # roll back everything, even if we encounter trouble doing one
773
 
                # of them.
774
 
                #
775
 
                # TODO: at least log the other exceptions rather than just
776
 
                # losing them mbp 20070307
777
 
                exc_info = None
778
 
                for rollback in reversed(rollbacks):
779
 
                    try:
780
 
                        rollback()
781
 
                    except Exception, e:
782
 
                        exc_info = sys.exc_info()
783
 
                if exc_info:
784
 
                    raise exc_info[0], exc_info[1], exc_info[2]
785
 
 
786
775
            # perform the disk move first - its the most likely failure point.
787
776
            if move_file:
788
777
                from_rel_abs = self.abspath(from_rel)
789
778
                to_rel_abs = self.abspath(to_rel)
790
779
                try:
791
780
                    osutils.rename(from_rel_abs, to_rel_abs)
792
 
                except OSError, e:
 
781
                except OSError as e:
793
782
                    raise errors.BzrMoveFailedError(from_rel, to_rel, e[1])
794
 
                rollbacks.append(lambda: osutils.rename(to_rel_abs, from_rel_abs))
 
783
                rollbacks.add_cleanup(osutils.rename, to_rel_abs, from_rel_abs)
795
784
            try:
796
785
                # perform the rename in the inventory next if needed: its easy
797
786
                # to rollback
800
789
                    from_entry = inv[from_id]
801
790
                    current_parent = from_entry.parent_id
802
791
                    inv.rename(from_id, to_dir_id, from_tail)
803
 
                    rollbacks.append(
804
 
                        lambda: inv.rename(from_id, current_parent, from_tail))
 
792
                    rollbacks.add_cleanup(
 
793
                        inv.rename, from_id, current_parent, from_tail)
805
794
                # finally do the rename in the dirstate, which is a little
806
795
                # tricky to rollback, but least likely to need it.
807
796
                old_block_index, old_entry_index, dir_present, file_present = \
875
864
                                                to_path_utf8)
876
865
                    update_dirblock(from_rel_utf8, to_key, to_rel_utf8)
877
866
            except:
878
 
                rollback_rename()
 
867
                rollbacks.cleanup_now()
879
868
                raise
880
869
            result.append((from_rel, to_rel))
881
870
            state._mark_modified()
1899
1888
        return inv[inv_file_id].revision
1900
1889
 
1901
1890
    def get_file(self, file_id, path=None):
1902
 
        return StringIO(self.get_file_text(file_id))
 
1891
        return BytesIO(self.get_file_text(file_id))
1903
1892
 
1904
1893
    def get_file_size(self, file_id):
1905
1894
        """See Tree.get_file_size"""
2032
2021
    def lock_read(self):
2033
2022
        """Lock the tree for a set of operations.
2034
2023
 
2035
 
        :return: A bzrlib.lock.LogicalLockResult.
 
2024
        :return: A breezy.lock.LogicalLockResult.
2036
2025
        """
2037
2026
        if not self._locked:
2038
2027
            self._repository.lock_read()
2123
2112
    def __init__(self, source, target):
2124
2113
        super(InterDirStateTree, self).__init__(source, target)
2125
2114
        if not InterDirStateTree.is_compatible(source, target):
2126
 
            raise Exception, "invalid source %r and target %r" % (source, target)
 
2115
            raise Exception("invalid source %r and target %r" % (source, target))
2127
2116
 
2128
2117
    @staticmethod
2129
2118
    def make_source_parent_tree(source, target):
2142
2131
    @classmethod
2143
2132
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source,
2144
2133
                                                  target):
2145
 
        from bzrlib.tests.test__dirstate_helpers import \
 
2134
        from .tests.test__dirstate_helpers import \
2146
2135
            compiled_dirstate_helpers_feature
2147
2136
        test_case.requireFeature(compiled_dirstate_helpers_feature)
2148
 
        from bzrlib._dirstate_helpers_pyx import ProcessEntryC
 
2137
        from ._dirstate_helpers_pyx import ProcessEntryC
2149
2138
        result = klass.make_source_parent_tree(source, target)
2150
2139
        result[1]._iter_changes = ProcessEntryC
2151
2140
        return result
2214
2203
                specific_files_utf8.add(path.encode('utf8'))
2215
2204
            specific_files = specific_files_utf8
2216
2205
        else:
2217
 
            specific_files = set([''])
 
2206
            specific_files = {''}
2218
2207
        # -- specific_files is now a utf8 path set --
2219
2208
 
2220
2209
        # -- get the state object and prepare it.