~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

  • Committer: Martin Packman
  • Date: 2011-12-19 10:37:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6394.
  • Revision ID: martin.packman@canonical.com-20111219103757-b85as9n9pb7e6qvn
Add tests for deprecated unicode wrapper functions in win32utils

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
31
31
import errno
32
32
import stat
33
33
 
34
 
import bzrlib
35
34
from bzrlib import (
36
35
    bzrdir,
37
36
    cache_utf8,
 
37
    config,
 
38
    conflicts as _mod_conflicts,
38
39
    debug,
39
40
    dirstate,
40
41
    errors,
 
42
    filters as _mod_filters,
41
43
    generate_ids,
42
44
    osutils,
43
45
    revision as _mod_revision,
46
48
    transform,
47
49
    views,
48
50
    )
49
 
import bzrlib.branch
50
 
import bzrlib.ui
51
51
""")
52
52
 
53
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
54
 
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
55
54
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
 
55
from bzrlib.lock import LogicalLockResult
 
56
from bzrlib.lockable_files import LockableFiles
 
57
from bzrlib.lockdir import LockDir
56
58
from bzrlib.mutabletree import needs_tree_write_lock
57
59
from bzrlib.osutils import (
58
60
    file_kind,
61
63
    realpath,
62
64
    safe_unicode,
63
65
    )
64
 
from bzrlib.trace import mutter
65
66
from bzrlib.transport.local import LocalTransport
66
 
from bzrlib.tree import InterTree
67
 
from bzrlib.tree import Tree
68
 
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
69
 
 
70
 
 
71
 
class DirStateWorkingTree(WorkingTree3):
 
67
from bzrlib.tree import (
 
68
    InterTree,
 
69
    InventoryTree,
 
70
    )
 
71
from bzrlib.workingtree import (
 
72
    InventoryWorkingTree,
 
73
    WorkingTree,
 
74
    WorkingTreeFormatMetaDir,
 
75
    )
 
76
 
 
77
 
 
78
class DirStateWorkingTree(InventoryWorkingTree):
 
79
 
72
80
    def __init__(self, basedir,
73
81
                 branch,
74
82
                 _control_files=None,
84
92
        self._format = _format
85
93
        self.bzrdir = _bzrdir
86
94
        basedir = safe_unicode(basedir)
87
 
        mutter("opening working tree %r", basedir)
 
95
        trace.mutter("opening working tree %r", basedir)
88
96
        self._branch = branch
89
97
        self.basedir = realpath(basedir)
90
98
        # if branch is at our basedir and is a format 6 or less
124
132
            state.add(f, file_id, kind, None, '')
125
133
        self._make_dirty(reset_inventory=True)
126
134
 
 
135
    def _get_check_refs(self):
 
136
        """Return the references needed to perform a check of this tree."""
 
137
        return [('trees', self.last_revision())]
 
138
 
127
139
    def _make_dirty(self, reset_inventory):
128
140
        """Make the tree state dirty.
129
141
 
181
193
 
182
194
    def _comparison_data(self, entry, path):
183
195
        kind, executable, stat_value = \
184
 
            WorkingTree3._comparison_data(self, entry, path)
 
196
            WorkingTree._comparison_data(self, entry, path)
185
197
        # it looks like a plain directory, but it's really a reference -- see
186
198
        # also kind()
187
199
        if (self._repo_supports_tree_reference and kind == 'directory'
193
205
    def commit(self, message=None, revprops=None, *args, **kwargs):
194
206
        # mark the tree as dirty post commit - commit
195
207
        # can change the current versioned list by doing deletes.
196
 
        result = WorkingTree3.commit(self, message, revprops, *args, **kwargs)
 
208
        result = WorkingTree.commit(self, message, revprops, *args, **kwargs)
197
209
        self._make_dirty(reset_inventory=True)
198
210
        return result
199
211
 
218
230
        local_path = self.bzrdir.get_workingtree_transport(None
219
231
            ).local_abspath('dirstate')
220
232
        self._dirstate = dirstate.DirState.on_file(local_path,
221
 
            self._sha1_provider())
 
233
            self._sha1_provider(), self._worth_saving_limit())
222
234
        return self._dirstate
223
235
 
224
236
    def _sha1_provider(self):
233
245
        else:
234
246
            return None
235
247
 
 
248
    def _worth_saving_limit(self):
 
249
        """How many hash changes are ok before we must save the dirstate.
 
250
 
 
251
        :return: an integer. -1 means never save.
 
252
        """
 
253
        # FIXME: We want a WorkingTreeStack here -- vila 20110812
 
254
        conf = config.BranchStack(self.branch)
 
255
        return conf.get('bzr.workingtree.worth_saving_limit')
 
256
 
236
257
    def filter_unversioned_files(self, paths):
237
258
        """Filter out paths that are versioned.
238
259
 
368
389
        state = self.current_dirstate()
369
390
        if stat_value is None:
370
391
            try:
371
 
                stat_value = os.lstat(file_abspath)
 
392
                stat_value = osutils.lstat(file_abspath)
372
393
            except OSError, e:
373
394
                if e.errno == errno.ENOENT:
374
395
                    return None
477
498
            self._must_be_locked()
478
499
            if not path:
479
500
                path = self.id2path(file_id)
480
 
            mode = os.lstat(self.abspath(path)).st_mode
 
501
            mode = osutils.lstat(self.abspath(path)).st_mode
481
502
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
482
503
 
483
504
    def all_file_ids(self):
567
588
            return _mod_revision.NULL_REVISION
568
589
 
569
590
    def lock_read(self):
570
 
        """See Branch.lock_read, and WorkingTree.unlock."""
 
591
        """See Branch.lock_read, and WorkingTree.unlock.
 
592
 
 
593
        :return: A bzrlib.lock.LogicalLockResult.
 
594
        """
571
595
        self.branch.lock_read()
572
596
        try:
573
597
            self._control_files.lock_read()
586
610
        except:
587
611
            self.branch.unlock()
588
612
            raise
 
613
        return LogicalLockResult(self.unlock)
589
614
 
590
615
    def _lock_self_write(self):
591
616
        """This should be called after the branch is locked."""
606
631
        except:
607
632
            self.branch.unlock()
608
633
            raise
 
634
        return LogicalLockResult(self.unlock)
609
635
 
610
636
    def lock_tree_write(self):
611
 
        """See MutableTree.lock_tree_write, and WorkingTree.unlock."""
 
637
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
 
638
 
 
639
        :return: A bzrlib.lock.LogicalLockResult.
 
640
        """
612
641
        self.branch.lock_read()
613
 
        self._lock_self_write()
 
642
        return self._lock_self_write()
614
643
 
615
644
    def lock_write(self):
616
 
        """See MutableTree.lock_write, and WorkingTree.unlock."""
 
645
        """See MutableTree.lock_write, and WorkingTree.unlock.
 
646
 
 
647
        :return: A bzrlib.lock.LogicalLockResult.
 
648
        """
617
649
        self.branch.lock_write()
618
 
        self._lock_self_write()
 
650
        return self._lock_self_write()
619
651
 
620
652
    @needs_tree_write_lock
621
653
    def move(self, from_paths, to_dir, after=False):
838
870
                rollback_rename()
839
871
                raise
840
872
            result.append((from_rel, to_rel))
841
 
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
 
873
            state._mark_modified()
842
874
            self._make_dirty(reset_inventory=False)
843
875
 
844
876
        return result
937
969
                    all_versioned = False
938
970
                    break
939
971
            if not all_versioned:
940
 
                raise errors.PathsNotVersionedError(paths)
 
972
                raise errors.PathsNotVersionedError(
 
973
                    [p.decode('utf-8') for p in paths])
941
974
        # -- remove redundancy in supplied paths to prevent over-scanning --
942
975
        search_paths = osutils.minimum_path_selection(paths)
943
976
        # sketch:
992
1025
            found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
993
1026
            for dir_name in split_paths:
994
1027
                if dir_name not in found_dir_names:
995
 
                    raise errors.PathsNotVersionedError(paths)
 
1028
                    raise errors.PathsNotVersionedError(
 
1029
                        [p.decode('utf-8') for p in paths])
996
1030
 
997
1031
        for dir_name_id, trees_info in found.iteritems():
998
1032
            for index in search_indexes:
1101
1135
                        _mod_revision.NULL_REVISION)))
1102
1136
                ghosts.append(rev_id)
1103
1137
            accepted_revisions.add(rev_id)
1104
 
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
 
1138
        updated = False
 
1139
        if (len(real_trees) == 1
 
1140
            and not ghosts
 
1141
            and self.branch.repository._format.fast_deltas
 
1142
            and isinstance(real_trees[0][1],
 
1143
                revisiontree.InventoryRevisionTree)
 
1144
            and self.get_parent_ids()):
 
1145
            rev_id, rev_tree = real_trees[0]
 
1146
            basis_id = self.get_parent_ids()[0]
 
1147
            # There are times when basis_tree won't be in
 
1148
            # self.branch.repository, (switch, for example)
 
1149
            try:
 
1150
                basis_tree = self.branch.repository.revision_tree(basis_id)
 
1151
            except errors.NoSuchRevision:
 
1152
                # Fall back to the set_parent_trees(), since we can't use
 
1153
                # _make_delta if we can't get the RevisionTree
 
1154
                pass
 
1155
            else:
 
1156
                delta = rev_tree.inventory._make_delta(basis_tree.inventory)
 
1157
                dirstate.update_basis_by_delta(delta, rev_id)
 
1158
                updated = True
 
1159
        if not updated:
 
1160
            dirstate.set_parent_trees(real_trees, ghosts=ghosts)
1105
1161
        self._make_dirty(reset_inventory=False)
1106
1162
 
1107
1163
    def _set_root_id(self, file_id):
1127
1183
 
1128
1184
    def unlock(self):
1129
1185
        """Unlock in format 4 trees needs to write the entire dirstate."""
1130
 
        # do non-implementation specific cleanup
1131
 
        self._cleanup()
1132
 
 
1133
1186
        if self._control_files._lock_count == 1:
 
1187
            # do non-implementation specific cleanup
 
1188
            self._cleanup()
 
1189
 
1134
1190
            # eventually we should do signature checking during read locks for
1135
1191
            # dirstate updates.
1136
1192
            if self._control_files._lock_mode == 'w':
1235
1291
        # have to change the legacy inventory too.
1236
1292
        if self._inventory is not None:
1237
1293
            for file_id in file_ids:
1238
 
                self._inventory.remove_recursive_id(file_id)
 
1294
                if self._inventory.has_id(file_id):
 
1295
                    self._inventory.remove_recursive_id(file_id)
1239
1296
 
1240
1297
    @needs_tree_write_lock
1241
1298
    def rename_one(self, from_rel, to_rel, after=False):
1242
1299
        """See WorkingTree.rename_one"""
1243
1300
        self.flush()
1244
 
        WorkingTree.rename_one(self, from_rel, to_rel, after)
 
1301
        super(DirStateWorkingTree, self).rename_one(from_rel, to_rel, after)
1245
1302
 
1246
1303
    @needs_tree_write_lock
1247
1304
    def apply_inventory_delta(self, changes):
1280
1337
            self._inventory = inv
1281
1338
        self.flush()
1282
1339
 
 
1340
    @needs_tree_write_lock
 
1341
    def reset_state(self, revision_ids=None):
 
1342
        """Reset the state of the working tree.
 
1343
 
 
1344
        This does a hard-reset to a last-known-good state. This is a way to
 
1345
        fix if something got corrupted (like the .bzr/checkout/dirstate file)
 
1346
        """
 
1347
        if revision_ids is None:
 
1348
            revision_ids = self.get_parent_ids()
 
1349
        if not revision_ids:
 
1350
            base_tree = self.branch.repository.revision_tree(
 
1351
                _mod_revision.NULL_REVISION)
 
1352
            trees = []
 
1353
        else:
 
1354
            trees = zip(revision_ids,
 
1355
                        self.branch.repository.revision_trees(revision_ids))
 
1356
            base_tree = trees[0][1]
 
1357
        state = self.current_dirstate()
 
1358
        # We don't support ghosts yet
 
1359
        state.set_state_from_scratch(base_tree.inventory, trees, [])
 
1360
 
1283
1361
 
1284
1362
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1285
1363
 
1290
1368
        """See dirstate.SHA1Provider.sha1()."""
1291
1369
        filters = self.tree._content_filter_stack(
1292
1370
            self.tree.relpath(osutils.safe_unicode(abspath)))
1293
 
        return internal_size_sha_file_byname(abspath, filters)[1]
 
1371
        return _mod_filters.internal_size_sha_file_byname(abspath, filters)[1]
1294
1372
 
1295
1373
    def stat_and_sha1(self, abspath):
1296
1374
        """See dirstate.SHA1Provider.stat_and_sha1()."""
1300
1378
        try:
1301
1379
            statvalue = os.fstat(file_obj.fileno())
1302
1380
            if filters:
1303
 
                file_obj = filtered_input_file(file_obj, filters)
 
1381
                file_obj = _mod_filters.filtered_input_file(file_obj, filters)
1304
1382
            sha1 = osutils.size_sha_file(file_obj)[1]
1305
1383
        finally:
1306
1384
            file_obj.close()
1317
1395
    def _file_content_summary(self, path, stat_result):
1318
1396
        # This is to support the somewhat obsolete path_content_summary method
1319
1397
        # with content filtering: see
1320
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/415508>.
 
1398
        # <https://bugs.launchpad.net/bzr/+bug/415508>.
1321
1399
        #
1322
1400
        # If the dirstate cache is up to date and knows the hash and size,
1323
1401
        # return that.
1336
1414
class WorkingTree4(DirStateWorkingTree):
1337
1415
    """This is the Format 4 working tree.
1338
1416
 
1339
 
    This differs from WorkingTree3 by:
 
1417
    This differs from WorkingTree by:
1340
1418
     - Having a consolidated internal dirstate, stored in a
1341
1419
       randomly-accessible sorted file on disk.
1342
1420
     - Not having a regular inventory attribute.  One can be synthesized
1370
1448
        return views.PathBasedViews(self)
1371
1449
 
1372
1450
 
1373
 
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
 
1451
class DirStateWorkingTreeFormat(WorkingTreeFormatMetaDir):
 
1452
 
 
1453
    missing_parent_conflicts = True
 
1454
 
 
1455
    supports_versioned_directories = True
 
1456
 
 
1457
    _lock_class = LockDir
 
1458
    _lock_file_name = 'lock'
 
1459
 
 
1460
    def _open_control_files(self, a_bzrdir):
 
1461
        transport = a_bzrdir.get_workingtree_transport(None)
 
1462
        return LockableFiles(transport, self._lock_file_name,
 
1463
                             self._lock_class)
1374
1464
 
1375
1465
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1376
1466
                   accelerator_tree=None, hardlink=False):
1377
1467
        """See WorkingTreeFormat.initialize().
1378
1468
 
1379
1469
        :param revision_id: allows creating a working tree at a different
1380
 
        revision than the branch is at.
 
1470
            revision than the branch is at.
1381
1471
        :param accelerator_tree: A tree which can be used for retrieving file
1382
1472
            contents more quickly than the revision tree, i.e. a workingtree.
1383
1473
            The revision tree will be used for cases where accelerator_tree's
1476
1566
        :param wt: the WorkingTree object
1477
1567
        """
1478
1568
 
 
1569
    def open(self, a_bzrdir, _found=False):
 
1570
        """Return the WorkingTree object for a_bzrdir
 
1571
 
 
1572
        _found is a private parameter, do not use it. It is used to indicate
 
1573
               if format probing has already been done.
 
1574
        """
 
1575
        if not _found:
 
1576
            # we are being called directly and must probe.
 
1577
            raise NotImplementedError
 
1578
        if not isinstance(a_bzrdir.transport, LocalTransport):
 
1579
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
 
1580
        wt = self._open(a_bzrdir, self._open_control_files(a_bzrdir))
 
1581
        return wt
 
1582
 
1479
1583
    def _open(self, a_bzrdir, control_files):
1480
1584
        """Open the tree itself.
1481
1585
 
1506
1610
    This format:
1507
1611
        - exists within a metadir controlling .bzr
1508
1612
        - includes an explicit version marker for the workingtree control
1509
 
          files, separate from the BzrDir format
 
1613
          files, separate from the ControlDir format
1510
1614
        - modifies the hash cache format
1511
1615
        - is new in bzr 0.15
1512
1616
        - uses a LockDir to guard access to it.
1516
1620
 
1517
1621
    _tree_class = WorkingTree4
1518
1622
 
1519
 
    def get_format_string(self):
 
1623
    @classmethod
 
1624
    def get_format_string(cls):
1520
1625
        """See WorkingTreeFormat.get_format_string()."""
1521
1626
        return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
1522
1627
 
1533
1638
 
1534
1639
    _tree_class = WorkingTree5
1535
1640
 
1536
 
    def get_format_string(self):
 
1641
    @classmethod
 
1642
    def get_format_string(cls):
1537
1643
        """See WorkingTreeFormat.get_format_string()."""
1538
1644
        return "Bazaar Working Tree Format 5 (bzr 1.11)\n"
1539
1645
 
1553
1659
 
1554
1660
    _tree_class = WorkingTree6
1555
1661
 
1556
 
    def get_format_string(self):
 
1662
    @classmethod
 
1663
    def get_format_string(cls):
1557
1664
        """See WorkingTreeFormat.get_format_string()."""
1558
1665
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
1559
1666
 
1572
1679
        return True
1573
1680
 
1574
1681
 
1575
 
class DirStateRevisionTree(Tree):
 
1682
class DirStateRevisionTree(InventoryTree):
1576
1683
    """A revision tree pulling the inventory from a dirstate.
1577
1684
    
1578
1685
    Note that this is one of the historical (ie revision) trees cached in the
1597
1704
    def annotate_iter(self, file_id,
1598
1705
                      default_revision=_mod_revision.CURRENT_REVISION):
1599
1706
        """See Tree.annotate_iter"""
1600
 
        text_key = (file_id, self.inventory[file_id].revision)
 
1707
        text_key = (file_id, self.get_file_revision(file_id))
1601
1708
        annotations = self._repository.texts.annotate(text_key)
1602
1709
        return [(key[-1], line) for (key, line) in annotations]
1603
1710
 
1604
 
    def _get_ancestors(self, default_revision):
1605
 
        return set(self._repository.get_ancestry(self._revision_id,
1606
 
                                                 topo_sorted=False))
1607
1711
    def _comparison_data(self, entry, path):
1608
1712
        """See Tree._comparison_data."""
1609
1713
        if entry is None:
1725
1829
                elif kind == 'directory':
1726
1830
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1727
1831
                elif kind == 'symlink':
1728
 
                    inv_entry.executable = False
1729
 
                    inv_entry.text_size = None
1730
1832
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1731
1833
                elif kind == 'tree-reference':
1732
1834
                    inv_entry.reference_revision = fingerprint or None
1752
1854
        # Make sure the file exists
1753
1855
        entry = self._get_entry(file_id, path=path)
1754
1856
        if entry == (None, None): # do we raise?
1755
 
            return None
 
1857
            raise errors.NoSuchId(self, file_id)
1756
1858
        parent_index = self._get_parent_index()
1757
1859
        last_changed_revision = entry[1][parent_index][4]
1758
1860
        try:
1769
1871
            return parent_details[1]
1770
1872
        return None
1771
1873
 
 
1874
    @needs_read_lock
 
1875
    def get_file_revision(self, file_id):
 
1876
        return self.inventory[file_id].revision
 
1877
 
1772
1878
    def get_file(self, file_id, path=None):
1773
1879
        return StringIO(self.get_file_text(file_id))
1774
1880
 
1797
1903
                                       identifier))
1798
1904
        return self._repository.iter_files_bytes(repo_desired_files)
1799
1905
 
1800
 
    def get_symlink_target(self, file_id):
 
1906
    def get_symlink_target(self, file_id, path=None):
1801
1907
        entry = self._get_entry(file_id=file_id)
1802
1908
        parent_index = self._get_parent_index()
1803
1909
        if entry[1][parent_index][0] != 'l':
1856
1962
    def is_executable(self, file_id, path=None):
1857
1963
        ie = self.inventory[file_id]
1858
1964
        if ie.kind != "file":
1859
 
            return None
 
1965
            return False
1860
1966
        return ie.executable
1861
1967
 
 
1968
    def is_locked(self):
 
1969
        return self._locked
 
1970
 
1862
1971
    def list_files(self, include_root=False, from_dir=None, recursive=True):
1863
1972
        # We use a standard implementation, because DirStateRevisionTree is
1864
1973
        # dealing with one of the parents of the current state
1877
1986
            yield path, 'V', entry.kind, entry.file_id, entry
1878
1987
 
1879
1988
    def lock_read(self):
1880
 
        """Lock the tree for a set of operations."""
 
1989
        """Lock the tree for a set of operations.
 
1990
 
 
1991
        :return: A bzrlib.lock.LogicalLockResult.
 
1992
        """
1881
1993
        if not self._locked:
1882
1994
            self._repository.lock_read()
1883
1995
            if self._dirstate._lock_token is None:
1884
1996
                self._dirstate.lock_read()
1885
1997
                self._dirstate_locked = True
1886
1998
        self._locked += 1
 
1999
        return LogicalLockResult(self.unlock)
1887
2000
 
1888
2001
    def _must_be_locked(self):
1889
2002
        if not self._locked:
1968
2081
    def make_source_parent_tree(source, target):
1969
2082
        """Change the source tree into a parent of the target."""
1970
2083
        revid = source.commit('record tree')
1971
 
        target.branch.repository.fetch(source.branch.repository, revid)
 
2084
        target.branch.fetch(source.branch, revid)
1972
2085
        target.set_parent_ids([revid])
1973
2086
        return target.basis_tree(), target
1974
2087
 
2066
2179
                path_entries = state._entries_for_path(path)
2067
2180
                if not path_entries:
2068
2181
                    # this specified path is not present at all: error
2069
 
                    not_versioned.append(path)
 
2182
                    not_versioned.append(path.decode('utf-8'))
2070
2183
                    continue
2071
2184
                found_versioned = False
2072
2185
                # for each id at this path
2080
2193
                if not found_versioned:
2081
2194
                    # none of the indexes was not 'absent' at all ids for this
2082
2195
                    # path.
2083
 
                    not_versioned.append(path)
 
2196
                    not_versioned.append(path.decode('utf-8'))
2084
2197
            if len(not_versioned) > 0:
2085
2198
                raise errors.PathsNotVersionedError(not_versioned)
2086
2199
        # -- remove redundancy in supplied specific_files to prevent over-scanning --