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
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
19
# FIXME: I don't know if writing out the cache from the destructor is really a
20
# good idea, because destructors are considered poor taste in Python, and
21
# it's not predictable when it will be written out.
35
31
It is possible for a `WorkingTree` to have a filename which is
36
32
not listed in the Inventory and vice versa.
38
36
def __init__(self, basedir, inv):
39
from bzrlib.hashcache import HashCache
40
from bzrlib.trace import note, mutter
42
37
self._inventory = inv
43
38
self.basedir = basedir
44
39
self.path2id = inv.path2id
46
# update the whole cache up front and write to disk if anything changed;
47
# in the future we might want to do this more selectively
48
hc = self._hashcache = HashCache(basedir)
58
if self._hashcache.needs_write:
59
self._hashcache.write()
62
41
def __iter__(self):
63
42
"""Iterate through file_ids for this tree.
65
44
file_ids are in a WorkingTree if they are in the working inventory
66
45
and the working file exists.
47
self._update_statcache()
68
48
inv = self._inventory
69
for path, ie in inv.iter_entries():
70
if os.path.exists(self.abspath(path)):
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
55
if ((file_id in self._statcache)
56
or (os.path.exists(self.abspath(inv.id2path(file_id))))):
74
61
def __repr__(self):
75
62
return "<%s of %s>" % (self.__class__.__name__,
76
getattr(self, 'basedir', None))
80
65
def abspath(self, filename):
81
66
return os.path.join(self.basedir, filename)
97
82
def has_id(self, file_id):
98
83
# files that have been deleted are excluded
100
if not inv.has_id(file_id):
84
if not self.inventory.has_id(file_id):
102
path = inv.id2path(file_id)
103
return os.path.exists(self.abspath(path))
86
self._update_statcache()
87
if file_id in self._statcache:
89
return os.path.exists(self.abspath(self.id2path(file_id)))
106
92
__contains__ = has_id
95
def _update_statcache(self):
97
if not self._statcache:
98
self._statcache = statcache.update_cache(self.basedir, self.inventory)
109
100
def get_file_size(self, file_id):
110
# is this still called?
111
raise NotImplementedError()
102
return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
114
105
def get_file_sha1(self, file_id):
115
path = self._inventory.id2path(file_id)
116
return self._hashcache.get_sha1(path)
107
self._update_statcache()
108
return self._statcache[file_id][statcache.SC_SHA1]
119
111
def file_class(self, filename):