21
21
from cStringIO import StringIO
24
from bzrlib.trace import mutter, note
25
24
from bzrlib.errors import BzrError, BzrCheckError
26
25
from bzrlib.inventory import Inventory
27
from bzrlib.osutils import appendpath, fingerprint_file
26
from bzrlib.osutils import fingerprint_file
27
import bzrlib.revision
28
from bzrlib.trace import mutter, note
32
30
class Tree(object):
33
31
"""Abstract file tree.
51
49
trees or versioned trees.
53
"""Get a list of the conflicts in the tree.
55
Each conflict is an instance of bzrlib.conflicts.Conflict.
59
def get_parent_ids(self):
60
"""Get the parent ids for this tree.
62
:return: a list of parent ids. [] is returned to indicate
63
a tree with no parents.
64
:raises: BzrError if the parents are not known.
66
raise NotImplementedError(self.get_parent_ids)
54
68
def has_filename(self, filename):
55
69
"""True if the tree has given filename."""
56
70
raise NotImplementedError()
99
121
"""Print file with id `file_id` to stdout."""
101
123
sys.stdout.write(self.get_file_text(file_id))
104
def export(self, dest, format='dir', root=None):
105
"""Export this tree."""
107
exporter = exporters[format]
109
from bzrlib.errors import BzrCommandError
110
raise BzrCommandError("export format %r not supported" % format)
111
exporter(self, dest, root)
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)))
115
150
class RevisionTree(Tree):
116
151
"""Tree viewing a previous revision.
122
157
or at least passing a description to the constructor.
125
def __init__(self, weave_store, inv, revision_id):
126
self._weave_store = weave_store
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
127
168
self._inventory = inv
128
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
130
183
def get_weave(self, file_id):
131
return self._weave_store.get_weave(file_id)
184
return self._weave_store.get_weave(file_id,
185
self._repository.get_transaction())
134
187
def get_file_lines(self, file_id):
135
188
ie = self._inventory[file_id]
136
189
weave = self.get_weave(file_id)
137
return weave.get(ie.revision)
190
return weave.get_lines(ie.revision)
140
192
def get_file_text(self, file_id):
141
193
return ''.join(self.get_file_lines(file_id))
144
195
def get_file(self, file_id):
145
196
return StringIO(self.get_file_text(file_id))
147
198
def get_file_size(self, file_id):
148
199
return self._inventory[file_id].text_size
150
def get_file_sha1(self, file_id):
201
def get_file_sha1(self, file_id, path=None):
151
202
ie = self._inventory[file_id]
152
203
if ie.kind == "file":
153
204
return ie.text_sha1
155
def is_executable(self, file_id):
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":
156
216
return self._inventory[file_id].executable
158
218
def has_filename(self, filename):
167
227
ie = self._inventory[file_id]
168
228
return ie.symlink_target;
230
def kind(self, file_id):
231
return self._inventory[file_id].kind
234
self._repository.lock_read()
237
self._repository.unlock()
171
240
class EmptyTree(Tree):
172
242
def __init__(self):
173
243
self._inventory = Inventory()
245
def get_parent_ids(self):
246
"""See Tree.get_parent_ids.
248
An EmptyTree always has NULL_REVISION as the only parent.
175
252
def get_symlink_target(self, file_id):
178
255
def has_filename(self, filename):
258
def kind(self, file_id):
259
assert self._inventory[file_id].kind == "root_directory"
260
return "root_directory"
181
262
def list_files(self):
184
265
def __contains__(self, file_id):
185
266
return file_id in self._inventory
187
def get_file_sha1(self, file_id):
268
def get_file_sha1(self, file_id, path=None):
188
269
assert self._inventory[file_id].kind == "root_directory"
258
######################################################################
261
def dir_exporter(tree, dest, root):
262
"""Export this tree to a new directory.
264
`dest` should not exist, and will be created holding the
265
contents of this tree.
267
TODO: To handle subdirectories we need to create the
270
:note: If the export fails, the destination directory will be
271
left in a half-assed state.
275
mutter('export version %r' % tree)
277
for dp, ie in inv.iter_entries():
278
ie.put_on_disk(dest, dp, tree)
280
exporters['dir'] = dir_exporter
287
def get_root_name(dest):
288
"""Get just the root name for a tarball.
290
>>> get_root_name('mytar.tar')
292
>>> get_root_name('mytar.tar.bz2')
294
>>> get_root_name('tar.tar.tar.tgz')
296
>>> get_root_name('bzr-0.0.5.tar.gz')
298
>>> get_root_name('a/long/path/mytar.tgz')
300
>>> get_root_name('../parent/../dir/other.tbz2')
303
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
304
dest = os.path.basename(dest)
306
if dest.endswith(end):
307
return dest[:-len(end)]
309
def tar_exporter(tree, dest, root, compression=None):
310
"""Export this tree to a new tar file.
312
`dest` will be created holding the contents of this tree; if it
313
already exists, it will be clobbered, like with "tar -c".
315
from time import time
317
compression = str(compression or '')
319
root = get_root_name(dest)
321
ball = tarfile.open(dest, 'w:' + compression)
322
except tarfile.CompressionError, e:
323
raise BzrError(str(e))
324
mutter('export version %r' % tree)
326
for dp, ie in inv.iter_entries():
327
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
328
item, fileobj = ie.get_tar_item(root, dp, now, tree)
329
ball.addfile(item, fileobj)
332
exporters['tar'] = tar_exporter
334
def tgz_exporter(tree, dest, root):
335
tar_exporter(tree, dest, root, compression='gz')
336
exporters['tgz'] = tgz_exporter
338
def tbz_exporter(tree, dest, root):
339
tar_exporter(tree, dest, root, compression='bz2')
340
exporters['tbz2'] = tbz_exporter