72
60
def has_id(self, file_id):
73
61
return self.inventory.has_id(file_id)
75
def has_or_had_id(self, file_id):
76
if file_id == self.inventory.root.file_id:
78
return self.inventory.has_id(file_id)
83
return iter(self.inventory)
64
"""Return set of all ids in this tree."""
65
return self.inventory.id_set()
85
67
def id2path(self, file_id):
86
68
return self.inventory.id2path(file_id)
88
def kind(self, file_id):
89
raise NotImplementedError("subclasses must implement kind")
91
70
def _get_inventory(self):
92
71
return self._inventory
94
def get_file_by_path(self, path):
95
return self.get_file(self._inventory.path2id(path))
97
73
inventory = property(_get_inventory,
98
74
doc="Inventory of this Tree")
100
76
def _check_retrieved(self, ie, f):
103
fp = fingerprint_file(f)
106
if ie.text_size != None:
107
if ie.text_size != fp['size']:
108
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
77
# TODO: Test this check by damaging the store?
78
if ie.text_size is not None:
80
if fs != ie.text_size:
81
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
109
82
["inventory expects %d bytes" % ie.text_size,
110
"file is actually %d bytes" % fp['size'],
83
"file is actually %d bytes" % fs,
111
84
"store is probably damaged/corrupt"])
113
if ie.text_sha1 != fp['sha1']:
114
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
88
if ie.text_sha1 != f_hash:
89
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
115
90
["inventory expects %s" % ie.text_sha1,
116
"file is actually %s" % fp['sha1'],
91
"file is actually %s" % f_hash,
117
92
"store is probably damaged/corrupt"])
120
def print_file(self, file_id):
121
"""Print file with id `file_id` to stdout."""
123
sys.stdout.write(self.get_file_text(file_id))
129
"""What files are present in this tree and unknown.
131
:return: an iterator over the unknown files.
138
def filter_unversioned_files(self, paths):
139
"""Filter out paths that are not versioned.
141
:return: set of paths.
143
# NB: we specifically *don't* call self.has_filename, because for
144
# WorkingTrees that can indicate files that exist on disk but that
146
pred = self.inventory.has_filename
147
return set((p for p in paths if not pred(p)))
95
def export(self, dest):
96
"""Export this tree to a new directory.
98
`dest` should not exist, and will be created holding the
99
contents of this tree.
101
:todo: To handle subdirectories we need to create the
104
:note: If the export fails, the destination directory will be
105
left in a half-assed state.
108
mutter('export version %r' % self)
110
for dp, ie in inv.iter_entries():
112
fullpath = appendpath(dest, dp)
113
if kind == 'directory':
116
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
118
bailout("don't know how to export {%s} of kind %r", fid, kind)
119
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
123
class WorkingTree(Tree):
124
"""Working copy tree.
126
The inventory is held in the `Branch` working-inventory, and the
127
files are in a directory on disk.
129
It is possible for a `WorkingTree` to have a filename which is
130
not listed in the Inventory and vice versa.
132
def __init__(self, basedir, inv):
133
self._inventory = inv
134
self.basedir = basedir
135
self.path2id = inv.path2id
138
return "<%s of %s>" % (self.__class__.__name__,
141
def _rel(self, filename):
142
return os.path.join(self.basedir, filename)
144
def has_filename(self, filename):
145
return os.path.exists(self._rel(filename))
147
def get_file(self, file_id):
148
return self.get_file_byname(self.id2path(file_id))
150
def get_file_byname(self, filename):
151
return file(self._rel(filename), 'rb')
153
def _get_store_filename(self, file_id):
154
return self._rel(self.id2path(file_id))
156
def get_file_size(self, file_id):
157
return os.stat(self._get_store_filename(file_id))[ST_SIZE]
159
def get_file_sha1(self, file_id):
160
f = self.get_file(file_id)
164
def file_class(self, filename):
165
if self.path2id(filename):
167
elif self.is_ignored(filename):
173
def file_kind(self, filename):
174
if isfile(self._rel(filename)):
176
elif isdir(self._rel(filename)):
182
def list_files(self):
183
"""Recursively list all files as (path, class, kind, id).
185
Lists, but does not descend into unversioned directories.
187
This does not include files that have been deleted in this
190
Skips the control directory.
194
def descend(from_dir, from_dir_id, dp):
198
if bzrlib.BZRDIR == f:
202
fp = appendpath(from_dir, f)
205
fap = appendpath(dp, f)
207
f_ie = inv.get_child(from_dir_id, f)
210
elif self.is_ignored(fp):
219
bailout("file %r entered as kind %r id %r, now of kind %r"
220
% (fap, f_ie.kind, f_ie.file_id, fk))
222
yield fp, c, fk, (f_ie and f_ie.file_id)
224
if fk != 'directory':
228
# don't descend unversioned directories
231
for ff in descend(fp, f_ie.file_id, fap):
234
for f in descend('', None, self.basedir):
239
def unknowns(self, path='', dir_id=None):
240
"""Yield names of unknown files in this WorkingTree.
242
If there are any unknown directories then only the directory is
243
returned, not all its children. But if there are unknown files
244
under a versioned subdirectory, they are returned.
246
Currently returned depth-first, sorted by name within directories.
248
for fpath, fclass, fkind, fid in self.list_files():
253
def ignored_files(self):
254
for fpath, fclass, fkind, fid in self.list_files():
259
def get_ignore_list(self):
260
"""Return list of ignore patterns."""
261
if self.has_filename(bzrlib.IGNORE_FILENAME):
262
f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
263
return [line.rstrip("\n\r") for line in f.readlines()]
265
return bzrlib.DEFAULT_IGNORE
268
def is_ignored(self, filename):
269
"""Check whether the filename matches an ignore pattern.
271
Patterns containing '/' need to match the whole path; others
272
match against only the last component."""
273
## TODO: Take them from a file, not hardcoded
274
## TODO: Use extended zsh-style globs maybe?
275
## TODO: Use '**' to match directories?
276
for pat in self.get_ignore_list():
278
if fnmatch.fnmatchcase(filename, pat):
281
if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
150
289
class RevisionTree(Tree):
151
290
"""Tree viewing a previous revision.
153
292
File text can be retrieved from the text store.
155
TODO: Some kind of `__repr__` method, but a good one
294
:todo: Some kind of `__repr__` method, but a good one
156
295
probably means knowing the branch and revision number,
157
296
or at least passing a description to the constructor.
160
def __init__(self, branch, inv, revision_id):
161
# for compatability the 'branch' parameter has not been renamed to
162
# repository at this point. However, we should change RevisionTree's
163
# construction to always be via Repository and not via direct
164
# construction - this will mean that we can change the constructor
165
# with much less chance of breaking client code.
166
self._repository = branch
167
self._weave_store = branch.weave_store
299
def __init__(self, store, inv):
168
301
self._inventory = inv
169
self._revision_id = revision_id
171
def get_parent_ids(self):
172
"""See Tree.get_parent_ids.
174
A RevisionTree's parents match the revision graph.
176
parent_ids = self._repository.get_revision(self._revision_id).parent_ids
179
def get_revision_id(self):
180
"""Return the revision id associated with this tree."""
181
return self._revision_id
183
def get_weave(self, file_id):
184
return self._weave_store.get_weave(file_id,
185
self._repository.get_transaction())
187
def get_file_lines(self, file_id):
303
def get_file(self, file_id):
188
304
ie = self._inventory[file_id]
189
weave = self.get_weave(file_id)
190
return weave.get_lines(ie.revision)
192
def get_file_text(self, file_id):
193
return ''.join(self.get_file_lines(file_id))
195
def get_file(self, file_id):
196
return StringIO(self.get_file_text(file_id))
305
f = self._store[ie.text_id]
306
mutter(" get fileid{%s} from %r" % (file_id, self))
308
if ie.text_size is None:
309
note("warning: no text size recorded on %r" % ie)
310
self._check_retrieved(ie, f)
198
313
def get_file_size(self, file_id):
199
314
return self._inventory[file_id].text_size
201
def get_file_sha1(self, file_id, path=None):
202
ie = self._inventory[file_id]
203
if ie.kind == "file":
207
def get_file_mtime(self, file_id, path=None):
208
ie = self._inventory[file_id]
209
revision = self._repository.get_revision(ie.revision)
210
return revision.timestamp
212
def is_executable(self, file_id, path=None):
213
ie = self._inventory[file_id]
214
if ie.kind != "file":
216
return self._inventory[file_id].executable
316
def get_file_sha1(self, file_id):
317
ie = self._inventory[file_id]
218
320
def has_filename(self, filename):
219
321
return bool(self.inventory.path2id(filename))