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.
22
from errors import BzrCheckError
23
from trace import mutter
25
class WorkingTree(bzrlib.tree.Tree):
28
The inventory is held in the `Branch` working-inventory, and the
29
files are in a directory on disk.
31
It is possible for a `WorkingTree` to have a filename which is
32
not listed in the Inventory and vice versa.
34
def __init__(self, basedir, inv):
35
from bzrlib.hashcache import HashCache
36
from bzrlib.trace import note, mutter
39
self.basedir = basedir
40
self.path2id = inv.path2id
42
# update the whole cache up front and write to disk if anything changed;
43
# in the future we might want to do this more selectively
44
hc = self._hashcache = HashCache(basedir)
54
if self._hashcache.needs_write:
55
self._hashcache.write()
59
"""Iterate through file_ids for this tree.
61
file_ids are in a WorkingTree if they are in the working inventory
62
and the working file exists.
65
for path, ie in inv.iter_entries():
66
if os.path.exists(self.abspath(path)):
71
return "<%s of %s>" % (self.__class__.__name__,
72
getattr(self, 'basedir', None))
76
def abspath(self, filename):
77
return os.path.join(self.basedir, filename)
79
def has_filename(self, filename):
80
return os.path.exists(self.abspath(filename))
82
def get_file(self, file_id):
83
return self.get_file_byname(self.id2path(file_id))
85
def get_file_byname(self, filename):
86
return file(self.abspath(filename), 'rb')
88
def _get_store_filename(self, file_id):
89
## XXX: badly named; this isn't in the store at all
90
return self.abspath(self.id2path(file_id))
93
def has_id(self, file_id):
94
# files that have been deleted are excluded
96
if not inv.has_id(file_id):
98
path = inv.id2path(file_id)
99
return os.path.exists(self.abspath(path))
102
__contains__ = has_id
105
def get_file_size(self, file_id):
106
# is this still called?
107
raise NotImplementedError()
110
def get_file_sha1(self, file_id):
111
path = self._inventory.id2path(file_id)
112
return self._hashcache.get_sha1(path)
115
def file_class(self, filename):
116
if self.path2id(filename):
118
elif self.is_ignored(filename):
124
def list_files(self):
125
"""Recursively list all files as (path, class, kind, id).
127
Lists, but does not descend into unversioned directories.
129
This does not include files that have been deleted in this
132
Skips the control directory.
134
from osutils import appendpath, file_kind
137
inv = self._inventory
139
def descend(from_dir_relpath, from_dir_id, dp):
143
## TODO: If we find a subdirectory with its own .bzr
144
## directory, then that is a separate tree and we
145
## should exclude it.
146
if bzrlib.BZRDIR == f:
150
fp = appendpath(from_dir_relpath, f)
153
fap = appendpath(dp, f)
155
f_ie = inv.get_child(from_dir_id, f)
158
elif self.is_ignored(fp):
167
raise BzrCheckError("file %r entered as kind %r id %r, "
169
% (fap, f_ie.kind, f_ie.file_id, fk))
171
yield fp, c, fk, (f_ie and f_ie.file_id)
173
if fk != 'directory':
177
# don't descend unversioned directories
180
for ff in descend(fp, f_ie.file_id, fap):
183
for f in descend('', inv.root.file_id, self.basedir):
189
for subp in self.extras():
190
if not self.is_ignored(subp):
195
"""Yield all unknown files in this WorkingTree.
197
If there are any unknown directories then only the directory is
198
returned, not all its children. But if there are unknown files
199
under a versioned subdirectory, they are returned.
201
Currently returned depth-first, sorted by name within directories.
203
## TODO: Work from given directory downwards
204
from osutils import isdir, appendpath
206
for path, dir_entry in self.inventory.directories():
207
mutter("search for unknowns in %r" % path)
208
dirabs = self.abspath(path)
209
if not isdir(dirabs):
210
# e.g. directory deleted
214
for subf in os.listdir(dirabs):
216
and (subf not in dir_entry.children)):
221
subp = appendpath(path, subf)
225
def ignored_files(self):
226
"""Yield list of PATH, IGNORE_PATTERN"""
227
for subp in self.extras():
228
pat = self.is_ignored(subp)
233
def get_ignore_list(self):
234
"""Return list of ignore patterns.
236
Cached in the Tree object after the first call.
238
if hasattr(self, '_ignorelist'):
239
return self._ignorelist
241
l = bzrlib.DEFAULT_IGNORE[:]
242
if self.has_filename(bzrlib.IGNORE_FILENAME):
243
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
244
l.extend([line.rstrip("\n\r") for line in f.readlines()])
249
def is_ignored(self, filename):
250
r"""Check whether the filename matches an ignore pattern.
252
Patterns containing '/' or '\' need to match the whole path;
253
others match against only the last component.
255
If the file is ignored, returns the pattern which caused it to
256
be ignored, otherwise None. So this can simply be used as a
257
boolean if desired."""
259
# TODO: Use '**' to match directories, and other extended
260
# globbing stuff from cvs/rsync.
262
# XXX: fnmatch is actually not quite what we want: it's only
263
# approximately the same as real Unix fnmatch, and doesn't
264
# treat dotfiles correctly and allows * to match /.
265
# Eventually it should be replaced with something more
269
from osutils import splitpath
271
for pat in self.get_ignore_list():
272
if '/' in pat or '\\' in pat:
274
# as a special case, you can put ./ at the start of a
275
# pattern; this is good to match in the top-level
278
if (pat[:2] == './') or (pat[:2] == '.\\'):
282
if fnmatch.fnmatchcase(filename, newpat):
285
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):