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.
21
from errors import BzrCheckError
22
from trace import mutter
27
from bzrlib.osutils import appendpath, file_kind, isdir, splitpath
28
from bzrlib.errors import BzrCheckError
29
from bzrlib.trace import mutter
25
31
class WorkingTree(bzrlib.tree.Tree):
26
32
"""Working copy tree.
31
37
It is possible for a `WorkingTree` to have a filename which is
32
38
not listed in the Inventory and vice versa.
36
40
def __init__(self, basedir, inv):
41
from bzrlib.hashcache import HashCache
42
from bzrlib.trace import note, mutter
37
44
self._inventory = inv
38
45
self.basedir = basedir
39
46
self.path2id = inv.path2id
48
# update the whole cache up front and write to disk if anything changed;
49
# in the future we might want to do this more selectively
50
hc = self._hashcache = HashCache(basedir)
60
if self._hashcache.needs_write:
61
self._hashcache.write()
41
64
def __iter__(self):
42
65
"""Iterate through file_ids for this tree.
44
67
file_ids are in a WorkingTree if they are in the working inventory
45
68
and the working file exists.
47
self._update_statcache()
48
70
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))))):
71
for path, ie in inv.iter_entries():
72
if os.path.exists(self.abspath(path)):
61
76
def __repr__(self):
62
77
return "<%s of %s>" % (self.__class__.__name__,
78
getattr(self, 'basedir', None))
65
82
def abspath(self, filename):
66
83
return os.path.join(self.basedir, filename)
82
99
def has_id(self, file_id):
83
100
# files that have been deleted are excluded
84
if not self.inventory.has_id(file_id):
101
inv = self._inventory
102
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)))
104
path = inv.id2path(file_id)
105
return os.path.exists(self.abspath(path))
92
108
__contains__ = has_id
95
def _update_statcache(self):
97
if not self._statcache:
98
self._statcache = statcache.update_cache(self.basedir, self.inventory)
100
111
def get_file_size(self, file_id):
102
return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
112
# is this still called?
113
raise NotImplementedError()
105
116
def get_file_sha1(self, file_id):
107
self._update_statcache()
108
return self._statcache[file_id][statcache.SC_SHA1]
117
path = self._inventory.id2path(file_id)
118
return self._hashcache.get_sha1(path)
111
121
def file_class(self, filename):
128
138
Skips the control directory.
130
from osutils import appendpath, file_kind
140
inv = self._inventory
135
142
def descend(from_dir_relpath, from_dir_id, dp):
136
143
ls = os.listdir(dp)
197
204
Currently returned depth-first, sorted by name within directories.
199
206
## TODO: Work from given directory downwards
200
from osutils import isdir, appendpath
202
207
for path, dir_entry in self.inventory.directories():
203
208
mutter("search for unknowns in %r" % path)
204
209
dirabs = self.abspath(path)