57
64
def has_id(self, file_id):
58
65
return self.inventory.has_id(file_id)
63
return iter(self.inventory)
68
"""Return set of all ids in this tree."""
69
return self.inventory.id_set()
65
71
def id2path(self, file_id):
66
72
return self.inventory.id2path(file_id)
68
74
def _get_inventory(self):
69
75
return self._inventory
71
def get_file_by_path(self, path):
72
return self.get_file(self._inventory.path2id(path))
74
77
inventory = property(_get_inventory,
75
78
doc="Inventory of this Tree")
77
80
def _check_retrieved(self, ie, f):
78
fp = fingerprint_file(f)
81
if ie.text_size != None:
82
if ie.text_size != fp['size']:
83
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),
84
86
["inventory expects %d bytes" % ie.text_size,
85
"file is actually %d bytes" % fp['size'],
87
"file is actually %d bytes" % fs,
86
88
"store is probably damaged/corrupt"])
88
if ie.text_sha1 != fp['sha1']:
89
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),
90
94
["inventory expects %s" % ie.text_sha1,
91
"file is actually %s" % fp['sha1'],
95
"file is actually %s" % f_hash,
92
96
"store is probably damaged/corrupt"])
95
def print_file(self, fileid):
96
"""Print file with id `fileid` to stdout."""
98
pumpfile(self.get_file(fileid), sys.stdout)
101
def export(self, dest, format='dir', root=None):
102
"""Export this tree."""
104
exporter = exporters[format]
106
from bzrlib.errors import BzrCommandError
107
raise BzrCommandError("export format %r not supported" % format)
108
exporter(self, dest, root)
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):
112
275
class RevisionTree(Tree):
113
276
"""Tree viewing a previous revision.
115
278
File text can be retrieved from the text store.
117
TODO: Some kind of `__repr__` method, but a good one
280
:todo: Some kind of `__repr__` method, but a good one
118
281
probably means knowing the branch and revision number,
119
282
or at least passing a description to the constructor.
221
def find_renames(old_inv, new_inv):
222
for file_id in old_inv:
223
if file_id not in new_inv:
225
old_name = old_inv.id2path(file_id)
226
new_name = new_inv.id2path(file_id)
227
if old_name != new_name:
228
yield (old_name, new_name)
232
######################################################################
235
def dir_exporter(tree, dest, root):
236
"""Export this tree to a new directory.
238
`dest` should not exist, and will be created holding the
239
contents of this tree.
241
TODO: To handle subdirectories we need to create the
244
:note: If the export fails, the destination directory will be
245
left in a half-assed state.
249
mutter('export version %r' % tree)
251
for dp, ie in inv.iter_entries():
253
fullpath = appendpath(dest, dp)
254
if kind == 'directory':
257
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
259
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
260
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
261
exporters['dir'] = dir_exporter
268
def get_root_name(dest):
269
"""Get just the root name for a tarball.
271
>>> get_root_name('mytar.tar')
273
>>> get_root_name('mytar.tar.bz2')
275
>>> get_root_name('tar.tar.tar.tgz')
277
>>> get_root_name('bzr-0.0.5.tar.gz')
279
>>> get_root_name('a/long/path/mytar.tgz')
281
>>> get_root_name('../parent/../dir/other.tbz2')
284
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
285
dest = os.path.basename(dest)
287
if dest.endswith(end):
288
return dest[:-len(end)]
290
def tar_exporter(tree, dest, root, compression=None):
291
"""Export this tree to a new tar file.
293
`dest` will be created holding the contents of this tree; if it
294
already exists, it will be clobbered, like with "tar -c".
296
from time import time
298
compression = str(compression or '')
300
root = get_root_name(dest)
302
ball = tarfile.open(dest, 'w:' + compression)
303
except tarfile.CompressionError, e:
304
raise BzrError(str(e))
305
mutter('export version %r' % tree)
307
for dp, ie in inv.iter_entries():
308
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
309
item = tarfile.TarInfo(os.path.join(root, dp))
310
# TODO: would be cool to actually set it to the timestamp of the
311
# revision it was last changed
313
if ie.kind == 'directory':
314
item.type = tarfile.DIRTYPE
319
elif ie.kind == 'file':
320
item.type = tarfile.REGTYPE
321
fileobj = tree.get_file(ie.file_id)
322
item.size = _find_file_size(fileobj)
325
raise BzrError("don't know how to export {%s} of kind %r" %
326
(ie.file_id, ie.kind))
328
ball.addfile(item, fileobj)
330
exporters['tar'] = tar_exporter
332
def tgz_exporter(tree, dest, root):
333
tar_exporter(tree, dest, root, compression='gz')
334
exporters['tgz'] = tgz_exporter
336
def tbz_exporter(tree, dest, root):
337
tar_exporter(tree, dest, root, compression='bz2')
338
exporters['tbz2'] = tbz_exporter
341
def _find_file_size(fileobj):
342
offset = fileobj.tell()
345
size = fileobj.tell()
347
# gzip doesn't accept second argument to seek()
351
nread = len(fileobj.read())