~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-30 22:27:17 UTC
  • Revision ID: mbp@sourcefrog.net-20050330222717-027b5837127b938d
experiment with new nested inventory file format
not used by default yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from inventory import Inventory
24
24
from trace import mutter, note
25
25
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
26
 
     joinpath, splitpath, appendpath, isdir, isfile, file_kind
 
26
     joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
27
27
from errors import bailout
28
28
import branch
29
29
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
73
73
                         doc="Inventory of this Tree")
74
74
 
75
75
    def _check_retrieved(self, ie, f):
76
 
        # TODO: Test this check by damaging the store?
 
76
        fp = fingerprint_file(f)
 
77
        f.seek(0)
 
78
        
77
79
        if ie.text_size is not None:
78
 
            fs = filesize(f)
79
 
            if fs != ie.text_size:
 
80
            if ie.text_size != fp['size']:
80
81
                bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
81
82
                        ["inventory expects %d bytes" % ie.text_size,
82
 
                         "file is actually %d bytes" % fs,
 
83
                         "file is actually %d bytes" % fp['size'],
83
84
                         "store is probably damaged/corrupt"])
84
85
 
85
 
        f_hash = sha_file(f)
86
 
        f.seek(0)
87
 
        if ie.text_sha1 != f_hash:
 
86
        if ie.text_sha1 != fp['sha1']:
88
87
            bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
89
88
                    ["inventory expects %s" % ie.text_sha1,
90
 
                     "file is actually %s" % f_hash,
 
89
                     "file is actually %s" % fp['sha1'],
91
90
                     "store is probably damaged/corrupt"])
92
91
 
93
92
 
175
174
            return '?'
176
175
 
177
176
 
178
 
    def file_kind(self, filename):
179
 
        if isfile(self.abspath(filename)):
180
 
            return 'file'
181
 
        elif isdir(self.abspath(filename)):
182
 
            return 'directory'
183
 
        else:
184
 
            return 'unknown'
185
 
 
186
 
 
187
177
    def list_files(self):
188
178
        """Recursively list all files as (path, class, kind, id).
189
179
 
200
190
            ls = os.listdir(dp)
201
191
            ls.sort()
202
192
            for f in ls:
 
193
                ## TODO: If we find a subdirectory with its own .bzr
 
194
                ## directory, then that is a separate tree and we
 
195
                ## should exclude it.
203
196
                if bzrlib.BZRDIR == f:
204
197
                    continue
205
198
 
241
234
            
242
235
 
243
236
 
244
 
    def unknowns(self, path='', dir_id=None):
245
 
        """Yield names of unknown files in this WorkingTree.
 
237
    def unknowns(self):
 
238
        for subp in self.extras():
 
239
            if not self.is_ignored(subp):
 
240
                yield subp
 
241
 
 
242
 
 
243
    def extras(self):
 
244
        """Yield all unknown files in this WorkingTree.
246
245
 
247
246
        If there are any unknown directories then only the directory is
248
247
        returned, not all its children.  But if there are unknown files
250
249
 
251
250
        Currently returned depth-first, sorted by name within directories.
252
251
        """
253
 
        for fpath, fclass, fkind, fid in self.list_files():
254
 
            if fclass == '?':
255
 
                yield fpath
 
252
        ## TODO: Work from given directory downwards
 
253
        
 
254
        for path, dir_entry in self.inventory.directories():
 
255
            mutter("search for unknowns in %r" % path)
 
256
            dirabs = self.abspath(path)
 
257
            if not isdir(dirabs):
 
258
                # e.g. directory deleted
 
259
                continue
 
260
 
 
261
            fl = []
 
262
            for subf in os.listdir(dirabs):
 
263
                if (subf != '.bzr'
 
264
                    and (subf not in dir_entry.children)):
 
265
                    fl.append(subf)
 
266
            
 
267
            fl.sort()
 
268
            for subf in fl:
 
269
                subp = appendpath(path, subf)
 
270
                yield subp
256
271
                
257
272
 
258
273
    def ignored_files(self):
259
 
        for fpath, fclass, fkind, fid in self.list_files():
260
 
            if fclass == 'I':
261
 
                yield fpath
 
274
        """Yield list of PATH, IGNORE_PATTERN"""
 
275
        for subp in self.extras():
 
276
            pat = self.is_ignored(subp)
 
277
            if pat != None:
 
278
                yield subp, pat
262
279
 
263
280
 
264
281
    def get_ignore_list(self):
265
 
        """Return list of ignore patterns."""
 
282
        """Return list of ignore patterns.
 
283
 
 
284
        Cached in the Tree object after the first call.
 
285
        """
 
286
        if hasattr(self, '_ignorelist'):
 
287
            return self._ignorelist
 
288
 
 
289
        l = bzrlib.DEFAULT_IGNORE[:]
266
290
        if self.has_filename(bzrlib.IGNORE_FILENAME):
267
291
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
268
 
            return [line.rstrip("\n\r") for line in f.readlines()]
269
 
        else:
270
 
            return bzrlib.DEFAULT_IGNORE
 
292
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
 
293
        self._ignorelist = l
 
294
        return l
271
295
 
272
296
 
273
297
    def is_ignored(self, filename):
274
298
        """Check whether the filename matches an ignore pattern.
275
299
 
276
300
        Patterns containing '/' need to match the whole path; others
277
 
        match against only the last component."""
278
 
        ## TODO: Take them from a file, not hardcoded
279
 
        ## TODO: Use extended zsh-style globs maybe?
280
 
        ## TODO: Use '**' to match directories?
 
301
        match against only the last component.
 
302
 
 
303
        If the file is ignored, returns the pattern which caused it to
 
304
        be ignored, otherwise None.  So this can simply be used as a
 
305
        boolean if desired."""
 
306
 
 
307
        ## TODO: Use '**' to match directories, and other extended globbing stuff from cvs/rsync.
 
308
        
281
309
        for pat in self.get_ignore_list():
282
310
            if '/' in pat:
283
 
                if fnmatch.fnmatchcase(filename, pat):
284
 
                    return True
 
311
                # as a special case, you can put ./ at the start of a pattern;
 
312
                # this is good to match in the top-level only;
 
313
                if pat[:2] == './':
 
314
                    newpat = pat[2:]
 
315
                else:
 
316
                    newpat = pat
 
317
                if fnmatch.fnmatchcase(filename, newpat):
 
318
                    return pat
285
319
            else:
286
320
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
287
 
                    return True
288
 
        return False
 
321
                    return pat
 
322
        return None
289
323
        
290
324
 
291
325
        
309
343
        ie = self._inventory[file_id]
310
344
        f = self._store[ie.text_id]
311
345
        mutter("  get fileid{%s} from %r" % (file_id, self))
312
 
        fs = filesize(f)
313
 
        if ie.text_size is None:
314
 
            note("warning: no text size recorded on %r" % ie)
315
346
        self._check_retrieved(ie, f)
316
347
        return f
317
348