20
21
from sets import Set
21
22
import os.path, os, fnmatch
23
from osutils import pumpfile, 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
28
24
from inventory import Inventory
29
25
from trace import mutter, note
26
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
27
joinpath, splitpath, appendpath, isdir, isfile, file_kind
30
28
from errors import bailout
30
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
80
78
doc="Inventory of this Tree")
82
80
def _check_retrieved(self, ie, f):
83
fp = fingerprint_file(f)
86
if ie.text_size != None:
87
if ie.text_size != fp['size']:
81
# TODO: Test this check by damaging the store?
82
if ie.text_size is not None:
84
if fs != ie.text_size:
88
85
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
89
86
["inventory expects %d bytes" % ie.text_size,
90
"file is actually %d bytes" % fp['size'],
87
"file is actually %d bytes" % fs,
91
88
"store is probably damaged/corrupt"])
93
if ie.text_sha1 != fp['sha1']:
92
if ie.text_sha1 != f_hash:
94
93
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
95
94
["inventory expects %s" % ie.text_sha1,
96
"file is actually %s" % fp['sha1'],
95
"file is actually %s" % f_hash,
97
96
"store is probably damaged/corrupt"])
100
def print_file(self, fileid):
101
"""Print file with id `fileid` to stdout."""
103
pumpfile(self.get_file(fileid), sys.stdout)
106
def export(self, dest):
99
def export(self, dest):
107
100
"""Export this tree to a new directory.
109
102
`dest` should not exist, and will be created holding the
110
103
contents of this tree.
112
TODO: To handle subdirectories we need to create the
105
:todo: To handle subdirectories we need to create the
113
106
directories first.
115
108
:note: If the export fails, the destination directory will be
126
119
elif kind == 'file':
127
120
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
129
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
122
bailout("don't know how to export {%s} of kind %r", fid, kind)
130
123
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
127
class WorkingTree(Tree):
128
"""Working copy tree.
130
The inventory is held in the `Branch` working-inventory, and the
131
files are in a directory on disk.
133
It is possible for a `WorkingTree` to have a filename which is
134
not listed in the Inventory and vice versa.
136
def __init__(self, basedir, inv):
137
self._inventory = inv
138
self.basedir = basedir
139
self.path2id = inv.path2id
142
return "<%s of %s>" % (self.__class__.__name__,
145
def _rel(self, filename):
146
return os.path.join(self.basedir, filename)
148
def has_filename(self, filename):
149
return os.path.exists(self._rel(filename))
151
def get_file(self, file_id):
152
return file(self._get_store_filename(file_id), 'rb')
154
def _get_store_filename(self, file_id):
155
return self._rel(self.id2path(file_id))
157
def get_file_size(self, file_id):
158
return os.stat(self._get_store_filename(file_id))[ST_SIZE]
160
def get_file_sha1(self, file_id):
161
f = self.get_file(file_id)
165
def file_class(self, filename):
166
if self.path2id(filename):
168
elif self.is_ignored(filename):
174
def file_kind(self, filename):
175
if isfile(self._rel(filename)):
177
elif isdir(self._rel(filename)):
183
def list_files(self):
184
"""Recursively list all files as (path, class, kind, id).
186
Lists, but does not descend into unversioned directories.
188
This does not include files that have been deleted in this
191
Skips the control directory.
195
def descend(from_dir, from_dir_id, dp):
199
if bzrlib.BZRDIR == f:
203
fp = appendpath(from_dir, f)
206
fap = appendpath(dp, f)
208
f_ie = inv.get_child(from_dir_id, f)
211
elif self.is_ignored(fp):
220
bailout("file %r entered as kind %r id %r, now of kind %r"
221
% (fap, f_ie.kind, f_ie.file_id, fk))
223
yield fp, c, fk, (f_ie and f_ie.file_id)
225
if fk != 'directory':
229
# don't descend unversioned directories
232
for ff in descend(fp, f_ie.file_id, fap):
235
for f in descend('', None, self.basedir):
240
def unknowns(self, path='', dir_id=None):
241
"""Yield names of unknown files in this WorkingTree.
243
If there are any unknown directories then only the directory is
244
returned, not all its children. But if there are unknown files
245
under a versioned subdirectory, they are returned.
247
Currently returned depth-first, sorted by name within directories.
249
for fpath, fclass, fkind, fid in self.list_files():
254
def ignored_files(self):
255
for fpath, fclass, fkind, fid in self.list_files():
260
def is_ignored(self, filename):
261
"""Check whether the filename matches an ignore pattern."""
262
## TODO: Take them from a file, not hardcoded
263
## TODO: Use extended zsh-style globs maybe?
264
## TODO: Use '**' to match directories?
265
## TODO: Patterns without / should match in subdirectories?
266
for i in bzrlib.DEFAULT_IGNORE:
267
if fnmatch.fnmatchcase(filename, i):
134
275
class RevisionTree(Tree):
135
276
"""Tree viewing a previous revision.
137
278
File text can be retrieved from the text store.
139
TODO: Some kind of `__repr__` method, but a good one
280
:todo: Some kind of `__repr__` method, but a good one
140
281
probably means knowing the branch and revision number,
141
282
or at least passing a description to the constructor.