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.
31
35
It is possible for a `WorkingTree` to have a filename which is
32
36
not listed in the Inventory and vice versa.
36
38
def __init__(self, basedir, inv):
39
from bzrlib.hashcache import HashCache
40
from bzrlib.trace import note, mutter
37
42
self._inventory = inv
38
43
self.basedir = basedir
39
44
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()
41
62
def __iter__(self):
42
63
"""Iterate through file_ids for this tree.
44
65
file_ids are in a WorkingTree if they are in the working inventory
45
66
and the working file exists.
47
self._update_statcache()
48
68
inv = self._inventory
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))))):
69
for path, ie in inv.iter_entries():
70
if os.path.exists(self.abspath(path)):
61
74
def __repr__(self):
62
75
return "<%s of %s>" % (self.__class__.__name__,
76
getattr(self, 'basedir', None))
65
80
def abspath(self, filename):
66
81
return os.path.join(self.basedir, filename)
82
97
def has_id(self, file_id):
83
98
# files that have been deleted are excluded
84
if not self.inventory.has_id(file_id):
100
if not inv.has_id(file_id):
86
self._update_statcache()
87
if file_id in self._statcache:
89
return os.path.exists(self.abspath(self.id2path(file_id)))
102
path = inv.id2path(file_id)
103
return os.path.exists(self.abspath(path))
92
106
__contains__ = has_id
95
def _update_statcache(self):
97
if not self._statcache:
98
self._statcache = statcache.update_cache(self.basedir, self.inventory)
100
109
def get_file_size(self, file_id):
102
return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
110
# is this still called?
111
raise NotImplementedError()
105
114
def get_file_sha1(self, file_id):
107
self._update_statcache()
108
return self._statcache[file_id][statcache.SC_SHA1]
115
path = self._inventory.id2path(file_id)
116
return self._hashcache.get_sha1(path)
111
119
def file_class(self, filename):