104
104
"""Print file with id `file_id` to stdout."""
106
106
sys.stdout.write(self.get_file_text(file_id))
114
def filter_unversioned_files(self, paths):
115
"""Filter out paths that are not versioned.
117
:return: set of paths.
119
# NB: we specifically *don't* call self.has_filename, because for
120
# WorkingTrees that can indicate files that exist on disk but that
122
pred = self.inventory.has_filename
123
return set((p for p in paths if not pred(p)))
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)
126
120
class RevisionTree(Tree):
127
121
"""Tree viewing a previous revision.
133
127
or at least passing a description to the constructor.
136
def __init__(self, branch, inv, revision_id):
137
self._branch = branch
138
self._weave_store = branch.weave_store
130
def __init__(self, weave_store, inv, revision_id):
131
self._weave_store = weave_store
139
132
self._inventory = inv
140
133
self._revision_id = revision_id
142
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
143
139
return self._weave_store.get_weave(file_id,
144
self._branch.get_transaction())
140
transactions.PassThroughTransaction())
146
143
def get_file_lines(self, file_id):
147
144
ie = self._inventory[file_id]
148
145
weave = self.get_weave(file_id)
149
return weave.get_lines(ie.revision)
146
return weave.get(ie.revision)
151
149
def get_file_text(self, file_id):
152
150
return ''.join(self.get_file_lines(file_id))
154
153
def get_file(self, file_id):
155
154
return StringIO(self.get_file_text(file_id))
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