1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
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
31
class WorkingTree(bzrlib.tree.Tree):
34
The inventory is held in the `Branch` working-inventory, and the
35
files are in a directory on disk.
37
It is possible for a `WorkingTree` to have a filename which is
38
not listed in the Inventory and vice versa.
40
def __init__(self, basedir, inv):
41
from bzrlib.hashcache import HashCache
42
from bzrlib.trace import note, mutter
45
self.basedir = basedir
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()
65
"""Iterate through file_ids for this tree.
67
file_ids are in a WorkingTree if they are in the working inventory
68
and the working file exists.
71
for path, ie in inv.iter_entries():
72
if bzrlib.osutils.lexists(self.abspath(path)):
77
return "<%s of %s>" % (self.__class__.__name__,
78
getattr(self, 'basedir', None))
82
def abspath(self, filename):
83
return os.path.join(self.basedir, filename)
85
def has_filename(self, filename):
86
return bzrlib.osutils.lexists(self.abspath(filename))
88
def get_file(self, file_id):
89
return self.get_file_byname(self.id2path(file_id))
91
def get_file_byname(self, filename):
92
return file(self.abspath(filename), 'rb')
94
def _get_store_filename(self, file_id):
95
## XXX: badly named; this isn't in the store at all
96
return self.abspath(self.id2path(file_id))
99
def has_id(self, file_id):
100
# files that have been deleted are excluded
101
inv = self._inventory
102
if not inv.has_id(file_id):
104
path = inv.id2path(file_id)
105
return bzrlib.osutils.lexists(self.abspath(path))
108
__contains__ = has_id
111
def get_file_size(self, file_id):
112
# is this still called?
113
raise NotImplementedError()
115
def get_file_sha1(self, file_id):
116
path = self._inventory.id2path(file_id)
117
return self._hashcache.get_sha1(path)
119
def get_symlink_target(self, file_id):
120
return os.readlink(self.id2path(file_id))
122
def file_class(self, filename):
123
if self.path2id(filename):
125
elif self.is_ignored(filename):
131
def list_files(self):
132
"""Recursively list all files as (path, class, kind, id).
134
Lists, but does not descend into unversioned directories.
136
This does not include files that have been deleted in this
139
Skips the control directory.
141
inv = self._inventory
143
def descend(from_dir_relpath, from_dir_id, dp):
147
## TODO: If we find a subdirectory with its own .bzr
148
## directory, then that is a separate tree and we
149
## should exclude it.
150
if bzrlib.BZRDIR == f:
154
fp = appendpath(from_dir_relpath, f)
157
fap = appendpath(dp, f)
159
f_ie = inv.get_child(from_dir_id, f)
162
elif self.is_ignored(fp):
171
raise BzrCheckError("file %r entered as kind %r id %r, "
173
% (fap, f_ie.kind, f_ie.file_id, fk))
175
yield fp, c, fk, (f_ie and f_ie.file_id)
177
if fk != 'directory':
181
# don't descend unversioned directories
184
for ff in descend(fp, f_ie.file_id, fap):
187
for f in descend('', inv.root.file_id, self.basedir):
193
for subp in self.extras():
194
if not self.is_ignored(subp):
199
"""Yield all unknown files in this WorkingTree.
201
If there are any unknown directories then only the directory is
202
returned, not all its children. But if there are unknown files
203
under a versioned subdirectory, they are returned.
205
Currently returned depth-first, sorted by name within directories.
207
## TODO: Work from given directory downwards
208
for path, dir_entry in self.inventory.directories():
209
mutter("search for unknowns in %r" % path)
210
dirabs = self.abspath(path)
211
if not isdir(dirabs):
212
# e.g. directory deleted
216
for subf in os.listdir(dirabs):
218
and (subf not in dir_entry.children)):
223
subp = appendpath(path, subf)
227
def ignored_files(self):
228
"""Yield list of PATH, IGNORE_PATTERN"""
229
for subp in self.extras():
230
pat = self.is_ignored(subp)
235
def get_ignore_list(self):
236
"""Return list of ignore patterns.
238
Cached in the Tree object after the first call.
240
if hasattr(self, '_ignorelist'):
241
return self._ignorelist
243
l = bzrlib.DEFAULT_IGNORE[:]
244
if self.has_filename(bzrlib.IGNORE_FILENAME):
245
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
246
l.extend([line.rstrip("\n\r") for line in f.readlines()])
251
def is_ignored(self, filename):
252
r"""Check whether the filename matches an ignore pattern.
254
Patterns containing '/' or '\' need to match the whole path;
255
others match against only the last component.
257
If the file is ignored, returns the pattern which caused it to
258
be ignored, otherwise None. So this can simply be used as a
259
boolean if desired."""
261
# TODO: Use '**' to match directories, and other extended
262
# globbing stuff from cvs/rsync.
264
# XXX: fnmatch is actually not quite what we want: it's only
265
# approximately the same as real Unix fnmatch, and doesn't
266
# treat dotfiles correctly and allows * to match /.
267
# Eventually it should be replaced with something more
270
for pat in self.get_ignore_list():
271
if '/' in pat or '\\' in pat:
273
# as a special case, you can put ./ at the start of a
274
# pattern; this is good to match in the top-level
277
if (pat[:2] == './') or (pat[:2] == '.\\'):
281
if fnmatch.fnmatchcase(filename, newpat):
284
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):