/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.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:
31
31
 
32
32
from __future__ import absolute_import
33
33
 
34
 
from cStringIO import StringIO
 
34
import errno
35
35
import os
 
36
import re
36
37
import sys
37
38
 
38
 
from bzrlib.lazy_import import lazy_import
 
39
import breezy
 
40
 
 
41
from .lazy_import import lazy_import
39
42
lazy_import(globals(), """
40
43
from bisect import bisect_left
41
44
import collections
42
 
import errno
43
45
import itertools
44
46
import operator
45
47
import stat
46
 
import re
47
48
 
48
 
from bzrlib import (
 
49
from breezy import (
49
50
    branch,
50
51
    conflicts as _mod_conflicts,
51
52
    controldir,
70
71
    )
71
72
""")
72
73
 
73
 
# Explicitly import bzrlib.bzrdir so that the BzrProber
 
74
# Explicitly import breezy.bzrdir so that the BzrProber
74
75
# is guaranteed to be registered.
75
 
from bzrlib import (
 
76
from . import (
76
77
    bzrdir,
 
78
    osutils,
77
79
    symbol_versioning,
78
80
    )
79
 
 
80
 
from bzrlib.decorators import needs_read_lock, needs_write_lock
81
 
from bzrlib.i18n import gettext
82
 
from bzrlib.lock import LogicalLockResult
83
 
import bzrlib.mutabletree
84
 
from bzrlib.mutabletree import needs_tree_write_lock
85
 
from bzrlib import osutils
86
 
from bzrlib.osutils import (
 
81
from .decorators import needs_read_lock, needs_write_lock
 
82
from .i18n import gettext
 
83
from .lock import LogicalLockResult
 
84
from . import mutabletree
 
85
from .mutabletree import needs_tree_write_lock
 
86
from .osutils import (
87
87
    file_kind,
88
88
    isdir,
89
89
    normpath,
92
92
    safe_unicode,
93
93
    splitpath,
94
94
    )
95
 
from bzrlib.trace import mutter, note
96
 
from bzrlib.revision import CURRENT_REVISION
97
 
from bzrlib.symbol_versioning import (
 
95
from .revision import CURRENT_REVISION
 
96
from .sixish import (
 
97
    BytesIO,
 
98
    )
 
99
from .trace import mutter, note
 
100
from .symbol_versioning import (
98
101
    deprecated_passed,
99
102
    DEPRECATED_PARAMETER,
100
103
    )
164
167
        return ''
165
168
 
166
169
 
167
 
class WorkingTree(bzrlib.mutabletree.MutableTree,
 
170
class WorkingTree(mutabletree.MutableTree,
168
171
    controldir.ControlComponent):
169
172
    """Working copy tree.
170
173
 
265
268
    def get_config_stack(self):
266
269
        """Retrieve the config stack for this tree.
267
270
 
268
 
        :return: A ``bzrlib.config.Stack``
 
271
        :return: A ``breezy.config.Stack``
269
272
        """
270
273
        # For the moment, just provide the branch config stack.
271
274
        return self.branch.get_config_stack()
584
587
        # canonical size
585
588
        try:
586
589
            return os.path.getsize(self.id2abspath(file_id))
587
 
        except OSError, e:
 
590
        except OSError as e:
588
591
            if e.errno != errno.ENOENT:
589
592
                raise
590
593
            else:
598
601
                fullpath = normpath(self.abspath(f))
599
602
                try:
600
603
                    kinds[pos] = file_kind(fullpath)
601
 
                except OSError, e:
 
604
                except OSError as e:
602
605
                    if e.errno == errno.ENOENT:
603
606
                        raise errors.NoSuchFile(fullpath)
604
607
 
660
663
        abspath = self.abspath(path)
661
664
        try:
662
665
            stat_result = _lstat(abspath)
663
 
        except OSError, e:
 
666
        except OSError as e:
664
667
            if getattr(e, 'errno', None) == errno.ENOENT:
665
668
                # no file.
666
669
                return ('missing', None, None, None)
784
787
            of the branch when it is supplied. If None, to_revision defaults to
785
788
            branch.last_revision().
786
789
        """
787
 
        from bzrlib.merge import Merger, Merge3Merger
 
790
        from .merge import Merger, Merge3Merger
788
791
        merger = Merger(self.branch, this_tree=self)
789
792
        # check that there are no local alterations
790
793
        if not force and self.has_changes():
1083
1086
        ignore_globs = set()
1084
1087
        ignore_globs.update(ignores.get_runtime_ignores())
1085
1088
        ignore_globs.update(ignores.get_user_ignores())
1086
 
        if self.has_filename(bzrlib.IGNORE_FILENAME):
1087
 
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
 
1089
        if self.has_filename(breezy.IGNORE_FILENAME):
 
1090
            f = self.get_file_byname(breezy.IGNORE_FILENAME)
1088
1091
            try:
1089
1092
                ignore_globs.update(ignores.parse_ignore_file(f))
1090
1093
            finally:
1123
1126
        abspath = self.abspath(path)
1124
1127
        try:
1125
1128
            stat_value = os.lstat(abspath)
1126
 
        except OSError, e:
 
1129
        except OSError as e:
1127
1130
            if getattr(e, 'errno', None) == errno.ENOENT:
1128
1131
                stat_value = None
1129
1132
                kind = None
1166
1169
 
1167
1170
        This also locks the branch, and can be unlocked via self.unlock().
1168
1171
 
1169
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1172
        :return: A breezy.lock.LogicalLockResult.
1170
1173
        """
1171
1174
        raise NotImplementedError(self.lock_read)
1172
1175
 
1173
1176
    def lock_tree_write(self):
1174
1177
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
1175
1178
 
1176
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1179
        :return: A breezy.lock.LogicalLockResult.
1177
1180
        """
1178
1181
        raise NotImplementedError(self.lock_tree_write)
1179
1182
 
1180
1183
    def lock_write(self):
1181
1184
        """See MutableTree.lock_write, and WorkingTree.unlock.
1182
1185
 
1183
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1186
        :return: A breezy.lock.LogicalLockResult.
1184
1187
        """
1185
1188
        raise NotImplementedError(self.lock_write)
1186
1189
 
1330
1333
    @needs_tree_write_lock
1331
1334
    def revert(self, filenames=None, old_tree=None, backups=True,
1332
1335
               pb=None, report_changes=False):
1333
 
        from bzrlib.conflicts import resolve
 
1336
        from .conflicts import resolve
1334
1337
        if old_tree is None:
1335
1338
            basis_tree = self.basis_tree()
1336
1339
            basis_tree.lock_read()
1593
1596
        try:
1594
1597
            current_disk = disk_iterator.next()
1595
1598
            disk_finished = False
1596
 
        except OSError, e:
 
1599
        except OSError as e:
1597
1600
            if not (e.errno == errno.ENOENT or
1598
1601
                (sys.platform == 'win32' and e.errno == ERROR_PATH_NOT_FOUND)):
1599
1602
                raise
1768
1771
 
1769
1772
    def get_shelf_manager(self):
1770
1773
        """Return the ShelfManager for this WorkingTree."""
1771
 
        from bzrlib.shelf import ShelfManager
 
1774
        from .shelf import ShelfManager
1772
1775
        return ShelfManager(self, self._transport)
1773
1776
 
1774
1777
 
1775
1778
class InventoryWorkingTree(WorkingTree,
1776
 
        bzrlib.mutabletree.MutableInventoryTree):
 
1779
        mutabletree.MutableInventoryTree):
1777
1780
    """Base class for working trees that are inventory-oriented.
1778
1781
 
1779
1782
    The inventory is held in the `Branch` working-inventory, and the
1866
1869
 
1867
1870
        This also locks the branch, and can be unlocked via self.unlock().
1868
1871
 
1869
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1872
        :return: A breezy.lock.LogicalLockResult.
1870
1873
        """
1871
1874
        if not self.is_locked():
1872
1875
            self._reset_data()
1881
1884
    def lock_tree_write(self):
1882
1885
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
1883
1886
 
1884
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1887
        :return: A breezy.lock.LogicalLockResult.
1885
1888
        """
1886
1889
        if not self.is_locked():
1887
1890
            self._reset_data()
1896
1899
    def lock_write(self):
1897
1900
        """See MutableTree.lock_write, and WorkingTree.unlock.
1898
1901
 
1899
 
        :return: A bzrlib.lock.LogicalLockResult.
 
1902
        :return: A breezy.lock.LogicalLockResult.
1900
1903
        """
1901
1904
        if not self.is_locked():
1902
1905
            self._reset_data()
1921
1924
    # new Inventory object.
1922
1925
    @needs_tree_write_lock
1923
1926
    def set_inventory(self, new_inventory_list):
1924
 
        from bzrlib.inventory import (Inventory,
1925
 
                                      InventoryDirectory,
1926
 
                                      InventoryFile,
1927
 
                                      InventoryLink)
 
1927
        from .inventory import (
 
1928
            Inventory,
 
1929
            InventoryDirectory,
 
1930
            InventoryFile,
 
1931
            InventoryLink)
1928
1932
        inv = Inventory(self.get_root_id())
1929
1933
        for path, file_id, parent, kind in new_inventory_list:
1930
1934
            name = os.path.basename(path)
1944
1948
    def _write_basis_inventory(self, xml):
1945
1949
        """Write the basis inventory XML to the basis-inventory file"""
1946
1950
        path = self._basis_inventory_name()
1947
 
        sio = StringIO(xml)
 
1951
        sio = BytesIO(xml)
1948
1952
        self._transport.put_file(path, sio,
1949
1953
            mode=self.bzrdir._get_file_mode())
1950
1954
 
2146
2150
        The default implementation returns no refs, and is only suitable for
2147
2151
        trees that have no local caching and can commit on ghosts at any time.
2148
2152
 
2149
 
        :seealso: bzrlib.check for details about check_refs.
 
2153
        :seealso: breezy.check for details about check_refs.
2150
2154
        """
2151
2155
        return []
2152
2156
 
2202
2206
        # TODO: Maybe this should only write on dirty ?
2203
2207
        if self._control_files._lock_mode != 'w':
2204
2208
            raise errors.NotWriteLocked(self)
2205
 
        sio = StringIO()
 
2209
        sio = BytesIO()
2206
2210
        self._serialize(self._inventory, sio)
2207
2211
        sio.seek(0)
2208
2212
        self._transport.put_file('inventory', sio,
2215
2219
            path = self.id2path(file_id)
2216
2220
        try:
2217
2221
            return os.lstat(self.abspath(path)).st_mtime
2218
 
        except OSError, e:
 
2222
        except OSError as e:
2219
2223
            if e.errno == errno.ENOENT:
2220
2224
                raise errors.FileTimestampUnavailable(path)
2221
2225
            raise
2513
2517
            inv = self.root_inventory
2514
2518
            from_dir_id = inv.root.file_id
2515
2519
            from_dir_abspath = self.basedir
2516
 
        children = os.listdir(from_dir_abspath)
2517
 
        children.sort()
 
2520
        children = sorted(os.listdir(from_dir_abspath))
2518
2521
        # jam 20060527 The kernel sized tree seems equivalent whether we
2519
2522
        # use a deque and popleft to keep them sorted, or if we use a plain
2520
2523
        # list and just reverse() them.
2590
2593
 
2591
2594
                # But do this child first if recursing down
2592
2595
                if recursive:
2593
 
                    new_children = os.listdir(fap)
2594
 
                    new_children.sort()
 
2596
                    new_children = sorted(os.listdir(fap))
2595
2597
                    new_children = collections.deque(new_children)
2596
2598
                    stack.append((f_ie.file_id, fp, fap, new_children))
2597
2599
                    # Break out of inner loop,
2878
2880
                    entry.to_tail, entry.to_parent_id, entry.from_rel,
2879
2881
                    entry.from_tail, entry.from_parent_id,
2880
2882
                    entry.only_change_inv))
2881
 
            except errors.BzrMoveFailedError, e:
 
2883
            except errors.BzrMoveFailedError as e:
2882
2884
                raise errors.BzrMoveFailedError( '', '', "Rollback failed."
2883
2885
                        " The working tree is in an inconsistent state."
2884
2886
                        " Please consider doing a 'bzr revert'."
2895
2897
        if not entry.only_change_inv:
2896
2898
            try:
2897
2899
                osutils.rename(from_rel_abs, to_rel_abs)
2898
 
            except OSError, e:
 
2900
            except OSError as e:
2899
2901
                raise errors.BzrMoveFailedError(entry.from_rel,
2900
2902
                    entry.to_rel, e[1])
2901
2903
        if entry.change_id:
3215
3217
 
3216
3218
 
3217
3219
format_registry.register_lazy("Bazaar Working Tree Format 4 (bzr 0.15)\n",
3218
 
    "bzrlib.workingtree_4", "WorkingTreeFormat4")
 
3220
    "breezy.workingtree_4", "WorkingTreeFormat4")
3219
3221
format_registry.register_lazy("Bazaar Working Tree Format 5 (bzr 1.11)\n",
3220
 
    "bzrlib.workingtree_4", "WorkingTreeFormat5")
 
3222
    "breezy.workingtree_4", "WorkingTreeFormat5")
3221
3223
format_registry.register_lazy("Bazaar Working Tree Format 6 (bzr 1.14)\n",
3222
 
    "bzrlib.workingtree_4", "WorkingTreeFormat6")
 
3224
    "breezy.workingtree_4", "WorkingTreeFormat6")
3223
3225
format_registry.register_lazy("Bazaar-NG Working Tree format 3",
3224
 
    "bzrlib.workingtree_3", "WorkingTreeFormat3")
 
3226
    "breezy.workingtree_3", "WorkingTreeFormat3")
3225
3227
format_registry.set_default_key("Bazaar Working Tree Format 6 (bzr 1.14)\n")