~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-06-01 07:45:28 UTC
  • Revision ID: mbp@sourcefrog.net-20050601074528-54509f4b1d505f55
- ignore .git files as well

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
18
17
 
19
18
import os
20
19
    
21
20
import bzrlib.tree
22
21
from errors import BzrCheckError
23
22
from trace import mutter
 
23
import statcache
24
24
 
25
25
class WorkingTree(bzrlib.tree.Tree):
26
26
    """Working copy tree.
31
31
    It is possible for a `WorkingTree` to have a filename which is
32
32
    not listed in the Inventory and vice versa.
33
33
    """
 
34
    _statcache = None
 
35
    
34
36
    def __init__(self, basedir, inv):
35
 
        from bzrlib.hashcache import HashCache
36
 
        from bzrlib.trace import note, mutter
37
 
 
38
37
        self._inventory = inv
39
38
        self.basedir = basedir
40
39
        self.path2id = inv.path2id
41
 
 
42
 
        # update the whole cache up front and write to disk if anything changed;
43
 
        # in the future we might want to do this more selectively
44
 
        hc = self._hashcache = HashCache(basedir)
45
 
        hc.read()
46
 
        for path, ie in inv.iter_entries():
47
 
            hc.get_sha1(path)
48
 
 
49
 
        if hc.needs_write:
50
 
            mutter("write hc")
51
 
            hc.write()
52
 
 
53
 
 
 
40
        self._update_statcache()
54
41
 
55
42
    def __iter__(self):
56
43
        """Iterate through file_ids for this tree.
59
46
        and the working file exists.
60
47
        """
61
48
        inv = self._inventory
62
 
        for path, ie in inv.iter_entries():
63
 
            if os.path.exists(self.abspath(path)):
64
 
                yield ie.file_id
 
49
        for file_id in self._inventory:
 
50
            # TODO: This is slightly redundant; we should be able to just
 
51
            # check the statcache but it only includes regular files.
 
52
            # only include files which still exist on disk
 
53
            ie = inv[file_id]
 
54
            if ie.kind == 'file':
 
55
                if ((file_id in self._statcache)
 
56
                    or (os.path.exists(self.abspath(inv.id2path(file_id))))):
 
57
                    yield file_id
 
58
 
65
59
 
66
60
 
67
61
    def __repr__(self):
68
62
        return "<%s of %s>" % (self.__class__.__name__,
69
63
                               self.basedir)
70
64
 
71
 
 
72
 
 
73
65
    def abspath(self, filename):
74
66
        return os.path.join(self.basedir, filename)
75
67
 
89
81
                
90
82
    def has_id(self, file_id):
91
83
        # files that have been deleted are excluded
92
 
        inv = self._inventory
93
 
        if not inv.has_id(file_id):
 
84
        if not self.inventory.has_id(file_id):
94
85
            return False
95
 
        path = inv.id2path(file_id)
96
 
        return os.path.exists(self.abspath(path))
 
86
        if file_id in self._statcache:
 
87
            return True
 
88
        return os.path.exists(self.abspath(self.id2path(file_id)))
97
89
 
98
90
 
99
91
    __contains__ = has_id
100
92
    
101
93
 
 
94
    def _update_statcache(self):
 
95
        import statcache
 
96
        if not self._statcache:
 
97
            self._statcache = statcache.update_cache(self.basedir, self.inventory)
 
98
 
102
99
    def get_file_size(self, file_id):
103
 
        # is this still called?
104
 
        raise NotImplementedError()
 
100
        import os, stat
 
101
        return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
105
102
 
106
103
 
107
104
    def get_file_sha1(self, file_id):
108
 
        path = self._inventory.id2path(file_id)
109
 
        return self._hashcache.get_sha1(path)
 
105
        return self._statcache[file_id][statcache.SC_SHA1]
110
106
 
111
107
 
112
108
    def file_class(self, filename):
131
127
        from osutils import appendpath, file_kind
132
128
        import os
133
129
 
134
 
        inv = self._inventory
 
130
        inv = self.inventory
135
131
 
136
132
        def descend(from_dir_relpath, from_dir_id, dp):
137
133
            ls = os.listdir(dp)