58
64
def has_id(self, file_id):
59
65
return self.inventory.has_id(file_id)
61
def has_or_had_id(self, file_id):
62
if file_id == self.inventory.root.file_id:
64
return self.inventory.has_id(file_id)
69
return iter(self.inventory)
68
"""Return set of all ids in this tree."""
69
return self.inventory.id_set()
71
71
def id2path(self, file_id):
72
72
return self.inventory.id2path(file_id)
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))
109
def export(self, dest, format='dir', root=None):
110
"""Export this tree."""
112
exporter = exporters[format]
114
from bzrlib.errors import BzrCommandError
115
raise BzrCommandError("export format %r not supported" % format)
116
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):
120
275
class RevisionTree(Tree):
121
276
"""Tree viewing a previous revision.
123
278
File text can be retrieved from the text store.
125
TODO: Some kind of `__repr__` method, but a good one
280
:todo: Some kind of `__repr__` method, but a good one
126
281
probably means knowing the branch and revision number,
127
282
or at least passing a description to the constructor.
130
def __init__(self, weave_store, inv, revision_id):
131
self._weave_store = weave_store
285
def __init__(self, store, inv):
132
287
self._inventory = inv
133
self._revision_id = revision_id
135
def get_weave(self, file_id):
136
# FIXME: RevisionTree should be given a branch
137
# not a store, or the store should know the branch.
138
import bzrlib.transactions as transactions
139
return self._weave_store.get_weave(file_id,
140
transactions.PassThroughTransaction())
143
def get_file_lines(self, file_id):
289
def get_file(self, file_id):
144
290
ie = self._inventory[file_id]
145
weave = self.get_weave(file_id)
146
return weave.get(ie.revision)
149
def get_file_text(self, file_id):
150
return ''.join(self.get_file_lines(file_id))
153
def get_file(self, file_id):
154
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)
156
299
def get_file_size(self, file_id):
157
300
return self._inventory[file_id].text_size
159
302
def get_file_sha1(self, file_id):
160
303
ie = self._inventory[file_id]
161
if ie.kind == "file":
164
def is_executable(self, file_id):
165
ie = self._inventory[file_id]
166
if ie.kind != "file":
168
return self._inventory[file_id].executable
170
306
def has_filename(self, filename):
171
307
return bool(self.inventory.path2id(filename))
261
def find_renames(old_inv, new_inv):
262
for file_id in old_inv:
263
if file_id not in new_inv:
265
old_name = old_inv.id2path(file_id)
266
new_name = new_inv.id2path(file_id)
267
if old_name != new_name:
268
yield (old_name, new_name)
272
######################################################################
275
def dir_exporter(tree, dest, root):
276
"""Export this tree to a new directory.
278
`dest` should not exist, and will be created holding the
279
contents of this tree.
281
TODO: To handle subdirectories we need to create the
284
:note: If the export fails, the destination directory will be
285
left in a half-assed state.
289
mutter('export version %r' % tree)
291
for dp, ie in inv.iter_entries():
292
ie.put_on_disk(dest, dp, tree)
294
exporters['dir'] = dir_exporter
301
def get_root_name(dest):
302
"""Get just the root name for a tarball.
304
>>> get_root_name('mytar.tar')
306
>>> get_root_name('mytar.tar.bz2')
308
>>> get_root_name('tar.tar.tar.tgz')
310
>>> get_root_name('bzr-0.0.5.tar.gz')
312
>>> get_root_name('a/long/path/mytar.tgz')
314
>>> get_root_name('../parent/../dir/other.tbz2')
317
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
318
dest = os.path.basename(dest)
320
if dest.endswith(end):
321
return dest[:-len(end)]
323
def tar_exporter(tree, dest, root, compression=None):
324
"""Export this tree to a new tar file.
326
`dest` will be created holding the contents of this tree; if it
327
already exists, it will be clobbered, like with "tar -c".
329
from time import time
331
compression = str(compression or '')
333
root = get_root_name(dest)
335
ball = tarfile.open(dest, 'w:' + compression)
336
except tarfile.CompressionError, e:
337
raise BzrError(str(e))
338
mutter('export version %r' % tree)
340
for dp, ie in inv.iter_entries():
341
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
342
item, fileobj = ie.get_tar_item(root, dp, now, tree)
343
ball.addfile(item, fileobj)
346
exporters['tar'] = tar_exporter
348
def tgz_exporter(tree, dest, root):
349
tar_exporter(tree, dest, root, compression='gz')
350
exporters['tgz'] = tgz_exporter
352
def tbz_exporter(tree, dest, root):
353
tar_exporter(tree, dest, root, compression='bz2')
354
exporters['tbz2'] = tbz_exporter