55
64
def has_id(self, file_id):
56
65
return self.inventory.has_id(file_id)
58
def has_or_had_id(self, file_id):
59
if file_id == self.inventory.root.file_id:
61
return self.inventory.has_id(file_id)
66
return iter(self.inventory)
68
"""Return set of all ids in this tree."""
69
return self.inventory.id_set()
68
71
def id2path(self, file_id):
69
72
return self.inventory.id2path(file_id)
71
def kind(self, file_id):
72
raise NotImplementedError("subclasses must implement kind")
74
74
def _get_inventory(self):
75
75
return self._inventory
77
def get_file_by_path(self, path):
78
return self.get_file(self._inventory.path2id(path))
80
77
inventory = property(_get_inventory,
81
78
doc="Inventory of this Tree")
83
80
def _check_retrieved(self, ie, f):
86
fp = fingerprint_file(f)
89
if ie.text_size != None:
90
if ie.text_size != fp['size']:
91
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
81
# TODO: Test this check by damaging the store?
82
if ie.text_size is not None:
84
if fs != ie.text_size:
85
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
92
86
["inventory expects %d bytes" % ie.text_size,
93
"file is actually %d bytes" % fp['size'],
87
"file is actually %d bytes" % fs,
94
88
"store is probably damaged/corrupt"])
96
if ie.text_sha1 != fp['sha1']:
97
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
92
if ie.text_sha1 != f_hash:
93
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
98
94
["inventory expects %s" % ie.text_sha1,
99
"file is actually %s" % fp['sha1'],
95
"file is actually %s" % f_hash,
100
96
"store is probably damaged/corrupt"])
103
def print_file(self, file_id):
104
"""Print file with id `file_id` to stdout."""
106
sys.stdout.write(self.get_file_text(file_id))
99
def export(self, dest):
100
"""Export this tree to a new directory.
102
`dest` should not exist, and will be created holding the
103
contents of this tree.
105
:todo: To handle subdirectories we need to create the
108
:note: If the export fails, the destination directory will be
109
left in a half-assed state.
112
mutter('export version %r' % self)
114
for dp, ie in inv.iter_entries():
116
fullpath = appendpath(dest, dp)
117
if kind == 'directory':
120
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
122
bailout("don't know how to export {%s} of kind %r", fid, kind)
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):
109
275
class RevisionTree(Tree):
110
276
"""Tree viewing a previous revision.
112
278
File text can be retrieved from the text store.
114
TODO: Some kind of `__repr__` method, but a good one
280
:todo: Some kind of `__repr__` method, but a good one
115
281
probably means knowing the branch and revision number,
116
282
or at least passing a description to the constructor.
119
def __init__(self, weave_store, inv, revision_id):
120
self._weave_store = weave_store
285
def __init__(self, store, inv):
121
287
self._inventory = inv
122
self._revision_id = revision_id
124
def get_weave(self, file_id):
125
# FIXME: RevisionTree should be given a branch
126
# not a store, or the store should know the branch.
127
import bzrlib.transactions as transactions
128
return self._weave_store.get_weave(file_id,
129
transactions.PassThroughTransaction())
132
def get_file_lines(self, file_id):
289
def get_file(self, file_id):
133
290
ie = self._inventory[file_id]
134
weave = self.get_weave(file_id)
135
return weave.get(ie.revision)
138
def get_file_text(self, file_id):
139
return ''.join(self.get_file_lines(file_id))
142
def get_file(self, file_id):
143
return StringIO(self.get_file_text(file_id))
291
f = self._store[ie.text_id]
292
mutter(" get fileid{%s} from %r" % (file_id, self))
294
if ie.text_size is None:
295
note("warning: no text size recorded on %r" % ie)
296
self._check_retrieved(ie, f)
145
299
def get_file_size(self, file_id):
146
300
return self._inventory[file_id].text_size
148
302
def get_file_sha1(self, file_id):
149
303
ie = self._inventory[file_id]
150
if ie.kind == "file":
153
def is_executable(self, file_id):
154
ie = self._inventory[file_id]
155
if ie.kind != "file":
157
return self._inventory[file_id].executable
159
306
def has_filename(self, filename):
160
307
return bool(self.inventory.path2id(filename))