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