21
20
from sets import Set
22
21
import os.path, os, fnmatch
23
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
24
joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
26
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
24
28
from inventory import Inventory
25
29
from trace import mutter, note
26
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
27
joinpath, splitpath, appendpath, isdir, isfile, file_kind
28
30
from errors import bailout
30
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
74
75
doc="Inventory of this Tree")
76
77
def _check_retrieved(self, ie, f):
77
# TODO: Test this check by damaging the store?
78
fp = fingerprint_file(f)
78
81
if ie.text_size is not None:
80
if fs != ie.text_size:
82
if ie.text_size != fp['size']:
81
83
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
82
84
["inventory expects %d bytes" % ie.text_size,
83
"file is actually %d bytes" % fs,
85
"file is actually %d bytes" % fp['size'],
84
86
"store is probably damaged/corrupt"])
88
if ie.text_sha1 != f_hash:
88
if ie.text_sha1 != fp['sha1']:
89
89
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
90
90
["inventory expects %s" % ie.text_sha1,
91
"file is actually %s" % f_hash,
91
"file is actually %s" % fp['sha1'],
92
92
"store is probably damaged/corrupt"])
138
138
return "<%s of %s>" % (self.__class__.__name__,
141
def _rel(self, filename):
141
def abspath(self, filename):
142
142
return os.path.join(self.basedir, filename)
144
144
def has_filename(self, filename):
145
return os.path.exists(self._rel(filename))
145
return os.path.exists(self.abspath(filename))
147
147
def get_file(self, file_id):
148
148
return self.get_file_byname(self.id2path(file_id))
150
150
def get_file_byname(self, filename):
151
return file(self._rel(filename), 'rb')
151
return file(self.abspath(filename), 'rb')
153
153
def _get_store_filename(self, file_id):
154
return self._rel(self.id2path(file_id))
154
return self.abspath(self.id2path(file_id))
156
156
def has_id(self, file_id):
157
157
# files that have been deleted are excluded
158
158
if not self.inventory.has_id(file_id):
160
return os.access(self._rel(self.inventory.id2path(file_id)), os.F_OK)
160
return os.access(self.abspath(self.inventory.id2path(file_id)), os.F_OK)
162
162
def get_file_size(self, file_id):
163
163
return os.stat(self._get_store_filename(file_id))[ST_SIZE]
245
def unknowns(self, path='', dir_id=None):
246
"""Yield names of unknown files in this WorkingTree.
240
for subp in self.extras():
241
if not self.is_ignored(subp):
246
"""Yield all unknown files in this WorkingTree.
248
248
If there are any unknown directories then only the directory is
249
249
returned, not all its children. But if there are unknown files
252
252
Currently returned depth-first, sorted by name within directories.
254
for fpath, fclass, fkind, fid in self.list_files():
254
## TODO: Work from given directory downwards
256
for path, dir_entry in self.inventory.directories():
257
mutter("search for unknowns in %r" % path)
258
dirabs = self.abspath(path)
259
if not isdir(dirabs):
260
# e.g. directory deleted
264
for subf in os.listdir(dirabs):
266
and (subf not in dir_entry.children)):
271
subp = appendpath(path, subf)
259
275
def ignored_files(self):
260
for fpath, fclass, fkind, fid in self.list_files():
276
"""Yield list of PATH, IGNORE_PATTERN"""
277
for subp in self.extras():
278
pat = self.is_ignored(subp)
265
283
def get_ignore_list(self):
266
"""Return list of ignore patterns."""
284
"""Return list of ignore patterns.
286
Cached in the Tree object after the first call.
288
if hasattr(self, '_ignorelist'):
289
return self._ignorelist
291
l = bzrlib.DEFAULT_IGNORE[:]
267
292
if self.has_filename(bzrlib.IGNORE_FILENAME):
268
293
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
269
return [line.rstrip("\n\r") for line in f.readlines()]
271
return bzrlib.DEFAULT_IGNORE
294
l.extend([line.rstrip("\n\r") for line in f.readlines()])
274
299
def is_ignored(self, filename):
275
300
"""Check whether the filename matches an ignore pattern.
277
302
Patterns containing '/' need to match the whole path; others
278
match against only the last component."""
279
## TODO: Take them from a file, not hardcoded
280
## TODO: Use extended zsh-style globs maybe?
281
## TODO: Use '**' to match directories?
303
match against only the last component.
305
If the file is ignored, returns the pattern which caused it to
306
be ignored, otherwise None. So this can simply be used as a
307
boolean if desired."""
309
## TODO: Use '**' to match directories, and other extended globbing stuff from cvs/rsync.
282
311
for pat in self.get_ignore_list():
284
if fnmatch.fnmatchcase(filename, pat):
313
# as a special case, you can put ./ at the start of a pattern;
314
# this is good to match in the top-level only;
319
if fnmatch.fnmatchcase(filename, newpat):
287
322
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):