~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-08-24 21:59:21 UTC
  • mfrom: (5363.2.22 controldir-1)
  • Revision ID: pqm@pqm.ubuntu.com-20100824215921-p4nheij9k4x6i1jw
(jelmer) Split generic interface code out of bzrlib.bzrdir.BzrDir into
 bzrlib.controldir.ControlDir. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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
53
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
54
54
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
55
55
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
56
 
import bzrlib.mutabletree
 
56
from bzrlib.lock import LogicalLockResult
57
57
from bzrlib.mutabletree import needs_tree_write_lock
58
58
from bzrlib.osutils import (
59
59
    file_kind,
568
568
            return _mod_revision.NULL_REVISION
569
569
 
570
570
    def lock_read(self):
571
 
        """See Branch.lock_read, and WorkingTree.unlock."""
 
571
        """See Branch.lock_read, and WorkingTree.unlock.
 
572
 
 
573
        :return: A bzrlib.lock.LogicalLockResult.
 
574
        """
572
575
        self.branch.lock_read()
573
576
        try:
574
577
            self._control_files.lock_read()
587
590
        except:
588
591
            self.branch.unlock()
589
592
            raise
 
593
        return LogicalLockResult(self.unlock)
590
594
 
591
595
    def _lock_self_write(self):
592
596
        """This should be called after the branch is locked."""
607
611
        except:
608
612
            self.branch.unlock()
609
613
            raise
 
614
        return LogicalLockResult(self.unlock)
610
615
 
611
616
    def lock_tree_write(self):
612
 
        """See MutableTree.lock_tree_write, and WorkingTree.unlock."""
 
617
        """See MutableTree.lock_tree_write, and WorkingTree.unlock.
 
618
 
 
619
        :return: A bzrlib.lock.LogicalLockResult.
 
620
        """
613
621
        self.branch.lock_read()
614
 
        self._lock_self_write()
 
622
        return self._lock_self_write()
615
623
 
616
624
    def lock_write(self):
617
 
        """See MutableTree.lock_write, and WorkingTree.unlock."""
 
625
        """See MutableTree.lock_write, and WorkingTree.unlock.
 
626
 
 
627
        :return: A bzrlib.lock.LogicalLockResult.
 
628
        """
618
629
        self.branch.lock_write()
619
 
        self._lock_self_write()
 
630
        return self._lock_self_write()
620
631
 
621
632
    @needs_tree_write_lock
622
633
    def move(self, from_paths, to_dir, after=False):
1236
1247
        # have to change the legacy inventory too.
1237
1248
        if self._inventory is not None:
1238
1249
            for file_id in file_ids:
1239
 
                self._inventory.remove_recursive_id(file_id)
 
1250
                if self._inventory.has_id(file_id):
 
1251
                    self._inventory.remove_recursive_id(file_id)
1240
1252
 
1241
1253
    @needs_tree_write_lock
1242
1254
    def rename_one(self, from_rel, to_rel, after=False):
1267
1279
        if self._dirty:
1268
1280
            raise AssertionError("attempting to write an inventory when the "
1269
1281
                "dirstate is dirty will lose pending changes")
1270
 
        self.current_dirstate().set_state_from_inventory(inv)
1271
 
        self._make_dirty(reset_inventory=False)
1272
 
        if self._inventory is not None:
 
1282
        had_inventory = self._inventory is not None
 
1283
        # Setting self._inventory = None forces the dirstate to regenerate the
 
1284
        # working inventory. We do this because self.inventory may be inv, or
 
1285
        # may have been modified, and either case would prevent a clean delta
 
1286
        # being created.
 
1287
        self._inventory = None
 
1288
        # generate a delta,
 
1289
        delta = inv._make_delta(self.inventory)
 
1290
        # and apply it.
 
1291
        self.apply_inventory_delta(delta)
 
1292
        if had_inventory:
1273
1293
            self._inventory = inv
1274
1294
        self.flush()
1275
1295
 
1300
1320
        return statvalue, sha1
1301
1321
 
1302
1322
 
 
1323
class ContentFilteringDirStateWorkingTree(DirStateWorkingTree):
 
1324
    """Dirstate working tree that supports content filtering.
 
1325
 
 
1326
    The dirstate holds the hash and size of the canonical form of the file, 
 
1327
    and most methods must return that.
 
1328
    """
 
1329
 
 
1330
    def _file_content_summary(self, path, stat_result):
 
1331
        # This is to support the somewhat obsolete path_content_summary method
 
1332
        # with content filtering: see
 
1333
        # <https://bugs.launchpad.net/bzr/+bug/415508>.
 
1334
        #
 
1335
        # If the dirstate cache is up to date and knows the hash and size,
 
1336
        # return that.
 
1337
        # Otherwise if there are no content filters, return the on-disk size
 
1338
        # and leave the hash blank.
 
1339
        # Otherwise, read and filter the on-disk file and use its size and
 
1340
        # hash.
 
1341
        #
 
1342
        # The dirstate doesn't store the size of the canonical form so we
 
1343
        # can't trust it for content-filtered trees.  We just return None.
 
1344
        dirstate_sha1 = self._dirstate.sha1_from_stat(path, stat_result)
 
1345
        executable = self._is_executable_from_path_and_stat(path, stat_result)
 
1346
        return ('file', None, executable, dirstate_sha1)
 
1347
 
 
1348
 
1303
1349
class WorkingTree4(DirStateWorkingTree):
1304
1350
    """This is the Format 4 working tree.
1305
1351
 
1313
1359
    """
1314
1360
 
1315
1361
 
1316
 
class WorkingTree5(DirStateWorkingTree):
 
1362
class WorkingTree5(ContentFilteringDirStateWorkingTree):
1317
1363
    """This is the Format 5 working tree.
1318
1364
 
1319
1365
    This differs from WorkingTree4 by:
1323
1369
    """
1324
1370
 
1325
1371
 
1326
 
class WorkingTree6(DirStateWorkingTree):
 
1372
class WorkingTree6(ContentFilteringDirStateWorkingTree):
1327
1373
    """This is the Format 6 working tree.
1328
1374
 
1329
1375
    This differs from WorkingTree5 by:
1338
1384
 
1339
1385
 
1340
1386
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
 
1387
 
1341
1388
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1342
1389
                   accelerator_tree=None, hardlink=False):
1343
1390
        """See WorkingTreeFormat.initialize().
1413
1460
                if basis_root_id is not None:
1414
1461
                    wt._set_root_id(basis_root_id)
1415
1462
                    wt.flush()
1416
 
                # If content filtering is supported, do not use the accelerator
1417
 
                # tree - the cost of transforming the content both ways and
1418
 
                # checking for changed content can outweight the gains it gives.
1419
 
                # Note: do NOT move this logic up higher - using the basis from
1420
 
                # the accelerator tree is still desirable because that can save
1421
 
                # a minute or more of processing on large trees!
1422
 
                # The original tree may not have the same content filters
1423
 
                # applied so we can't safely build the inventory delta from
1424
 
                # the source tree.
1425
1463
                if wt.supports_content_filtering():
1426
 
                    if hardlink:
1427
 
                        # see https://bugs.edge.launchpad.net/bzr/+bug/408193
1428
 
                        trace.warning("hardlinking working copy files is not currently "
1429
 
                            "supported in %r" % (wt,))
1430
 
                    accelerator_tree = None
 
1464
                    # The original tree may not have the same content filters
 
1465
                    # applied so we can't safely build the inventory delta from
 
1466
                    # the source tree.
1431
1467
                    delta_from_tree = False
1432
1468
                else:
1433
1469
                    delta_from_tree = True
1550
1586
 
1551
1587
 
1552
1588
class DirStateRevisionTree(Tree):
1553
 
    """A revision tree pulling the inventory from a dirstate."""
 
1589
    """A revision tree pulling the inventory from a dirstate.
 
1590
    
 
1591
    Note that this is one of the historical (ie revision) trees cached in the
 
1592
    dirstate for easy access, not the workingtree.
 
1593
    """
1554
1594
 
1555
1595
    def __init__(self, dirstate, revision_id, repository):
1556
1596
        self._dirstate = dirstate
1698
1738
                elif kind == 'directory':
1699
1739
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1700
1740
                elif kind == 'symlink':
1701
 
                    inv_entry.executable = False
1702
 
                    inv_entry.text_size = None
1703
1741
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1704
1742
                elif kind == 'tree-reference':
1705
1743
                    inv_entry.reference_revision = fingerprint or None
1728
1766
            return None
1729
1767
        parent_index = self._get_parent_index()
1730
1768
        last_changed_revision = entry[1][parent_index][4]
1731
 
        return self._repository.get_revision(last_changed_revision).timestamp
 
1769
        try:
 
1770
            rev = self._repository.get_revision(last_changed_revision)
 
1771
        except errors.NoSuchRevision:
 
1772
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
 
1773
        return rev.timestamp
1732
1774
 
1733
1775
    def get_file_sha1(self, file_id, path=None, stat_value=None):
1734
1776
        entry = self._get_entry(file_id=file_id, path=path)
1801
1843
        entry = self._get_entry(file_id=file_id)[1]
1802
1844
        if entry is None:
1803
1845
            raise errors.NoSuchId(tree=self, file_id=file_id)
1804
 
        return dirstate.DirState._minikind_to_kind[entry[1][0]]
 
1846
        parent_index = self._get_parent_index()
 
1847
        return dirstate.DirState._minikind_to_kind[entry[parent_index][0]]
1805
1848
 
1806
1849
    def stored_kind(self, file_id):
1807
1850
        """See Tree.stored_kind"""
1827
1870
            return None
1828
1871
        return ie.executable
1829
1872
 
 
1873
    def is_locked(self):
 
1874
        return self._locked
 
1875
 
1830
1876
    def list_files(self, include_root=False, from_dir=None, recursive=True):
1831
1877
        # We use a standard implementation, because DirStateRevisionTree is
1832
1878
        # dealing with one of the parents of the current state
1845
1891
            yield path, 'V', entry.kind, entry.file_id, entry
1846
1892
 
1847
1893
    def lock_read(self):
1848
 
        """Lock the tree for a set of operations."""
 
1894
        """Lock the tree for a set of operations.
 
1895
 
 
1896
        :return: A bzrlib.lock.LogicalLockResult.
 
1897
        """
1849
1898
        if not self._locked:
1850
1899
            self._repository.lock_read()
1851
1900
            if self._dirstate._lock_token is None:
1852
1901
                self._dirstate.lock_read()
1853
1902
                self._dirstate_locked = True
1854
1903
        self._locked += 1
 
1904
        return LogicalLockResult(self.unlock)
1855
1905
 
1856
1906
    def _must_be_locked(self):
1857
1907
        if not self._locked:
1947
1997
        return result
1948
1998
 
1949
1999
    @classmethod
1950
 
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source, target):
 
2000
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source,
 
2001
                                                  target):
1951
2002
        from bzrlib.tests.test__dirstate_helpers import \
1952
 
            CompiledDirstateHelpersFeature
1953
 
        if not CompiledDirstateHelpersFeature.available():
1954
 
            from bzrlib.tests import UnavailableFeature
1955
 
            raise UnavailableFeature(CompiledDirstateHelpersFeature)
 
2003
            compiled_dirstate_helpers_feature
 
2004
        test_case.requireFeature(compiled_dirstate_helpers_feature)
1956
2005
        from bzrlib._dirstate_helpers_pyx import ProcessEntryC
1957
2006
        result = klass.make_source_parent_tree(source, target)
1958
2007
        result[1]._iter_changes = ProcessEntryC
1989
2038
            output. An unversioned file is defined as one with (False, False)
1990
2039
            for the versioned pair.
1991
2040
        """
1992
 
        # NB: show_status depends on being able to pass in non-versioned files
1993
 
        # and report them as unknown
1994
2041
        # TODO: handle extra trees in the dirstate.
1995
2042
        if (extra_trees or specific_files == []):
1996
2043
            # we can't fast-path these cases (yet)