1
# Copyright (C) 2006-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""RevisionTree - a Tree implementation backed by repository data for a revision."""
19
from __future__ import absolute_import
31
class RevisionTree(tree.Tree):
32
"""Tree viewing a previous revision.
34
File text can be retrieved from the text store.
37
def __init__(self, repository, revision_id):
38
self._repository = repository
39
self._revision_id = revision_id
40
self._rules_searcher = None
42
def has_versioned_directories(self):
43
"""See `Tree.has_versioned_directories`."""
44
return self._repository._format.supports_versioned_directories
46
def supports_tree_reference(self):
47
return getattr(self._repository._format, "supports_tree_reference",
50
def get_parent_ids(self):
51
"""See Tree.get_parent_ids.
53
A RevisionTree's parents match the revision graph.
55
if self._revision_id in (None, revision.NULL_REVISION):
58
parent_ids = self._repository.get_revision(
59
self._revision_id).parent_ids
62
def get_revision_id(self):
63
"""Return the revision id associated with this tree."""
64
return self._revision_id
66
def get_file_revision(self, file_id, path=None):
67
"""Return the revision id in which a file was last changed."""
68
raise NotImplementedError(self.get_file_revision)
70
def get_file_text(self, file_id, path=None):
71
for (identifier, content) in self.iter_files_bytes([(file_id, None)]):
72
ret = "".join(content)
75
def get_file(self, file_id, path=None):
76
return BytesIO(self.get_file_text(file_id))
79
return self._repository.is_locked()
82
self._repository.lock_read()
86
return '<%s instance at %x, rev_id=%r>' % (
87
self.__class__.__name__, id(self), self._revision_id)
90
self._repository.unlock()
92
def _get_rules_searcher(self, default_searcher):
93
"""See Tree._get_rules_searcher."""
94
if self._rules_searcher is None:
95
self._rules_searcher = super(RevisionTree,
96
self)._get_rules_searcher(default_searcher)
97
return self._rules_searcher
100
class InventoryRevisionTree(RevisionTree,tree.InventoryTree):
102
def __init__(self, repository, inv, revision_id):
103
RevisionTree.__init__(self, repository, revision_id)
104
self._inventory = inv
106
def get_file_mtime(self, file_id, path=None):
107
inv, inv_file_id = self._unpack_file_id(file_id)
108
ie = inv[inv_file_id]
110
revision = self._repository.get_revision(ie.revision)
111
except errors.NoSuchRevision:
112
raise errors.FileTimestampUnavailable(self.id2path(file_id))
113
return revision.timestamp
115
def get_file_size(self, file_id):
116
inv, inv_file_id = self._unpack_file_id(file_id)
117
return inv[inv_file_id].text_size
119
def get_file_sha1(self, file_id, path=None, stat_value=None):
120
inv, inv_file_id = self._unpack_file_id(file_id)
121
ie = inv[inv_file_id]
122
if ie.kind == "file":
126
def get_file_revision(self, file_id, path=None):
127
inv, inv_file_id = self._unpack_file_id(file_id)
128
ie = inv[inv_file_id]
131
def is_executable(self, file_id, path=None):
132
inv, inv_file_id = self._unpack_file_id(file_id)
133
ie = inv[inv_file_id]
134
if ie.kind != "file":
138
def has_filename(self, filename):
139
return bool(self.path2id(filename))
141
def list_files(self, include_root=False, from_dir=None, recursive=True):
142
# The only files returned by this are those from the version
145
inv = self.root_inventory
147
inv, from_dir_id = self._path2inv_file_id(from_dir)
148
if from_dir_id is None:
149
# Directory not versioned
151
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
152
if inv.root is not None and not include_root and from_dir is None:
153
# skip the root for compatability with the current apis.
155
for path, entry in entries:
156
yield path, 'V', entry.kind, entry.file_id, entry
158
def get_symlink_target(self, file_id, path=None):
159
inv, inv_file_id = self._unpack_file_id(file_id)
160
ie = inv[inv_file_id]
161
# Inventories store symlink targets in unicode
162
return ie.symlink_target
164
def get_reference_revision(self, file_id, path=None):
165
inv, inv_file_id = self._unpack_file_id(file_id)
166
return inv[inv_file_id].reference_revision
168
def get_root_id(self):
169
if self.root_inventory.root:
170
return self.root_inventory.root.file_id
172
def kind(self, file_id):
173
inv, inv_file_id = self._unpack_file_id(file_id)
174
return inv[inv_file_id].kind
176
def path_content_summary(self, path):
177
"""See Tree.path_content_summary."""
178
inv, file_id = self._path2inv_file_id(path)
180
return ('missing', None, None, None)
184
return (kind, entry.text_size, entry.executable, entry.text_sha1)
185
elif kind == 'symlink':
186
return (kind, None, None, entry.symlink_target)
188
return (kind, None, None, None)
190
def _comparison_data(self, entry, path):
192
return None, False, None
193
return entry.kind, entry.executable, None
195
def _file_size(self, entry, stat_value):
196
return entry.text_size
198
def walkdirs(self, prefix=""):
199
_directory = 'directory'
200
inv, top_id = self._path2inv_file_id(prefix)
204
pending = [(prefix, '', _directory, None, top_id, None)]
207
currentdir = pending.pop()
208
# 0 - relpath, 1- basename, 2- kind, 3- stat, id, v-kind
210
relroot = currentdir[0] + '/'
213
# FIXME: stash the node in pending
214
entry = inv[currentdir[4]]
215
for name, child in entry.sorted_children():
216
toppath = relroot + name
217
dirblock.append((toppath, name, child.kind, None,
218
child.file_id, child.kind
220
yield (currentdir[0], entry.file_id), dirblock
221
# push the user specified dirs from dirblock
222
for dir in reversed(dirblock):
223
if dir[2] == _directory:
226
def iter_files_bytes(self, desired_files):
227
"""See Tree.iter_files_bytes.
229
This version is implemented on top of Repository.iter_files_bytes"""
230
repo_desired_files = [(f, self.get_file_revision(f), i)
231
for f, i in desired_files]
233
for result in self._repository.iter_files_bytes(repo_desired_files):
235
except errors.RevisionNotPresent as e:
236
raise errors.NoSuchFile(e.file_id)
238
def annotate_iter(self, file_id,
239
default_revision=revision.CURRENT_REVISION):
240
"""See Tree.annotate_iter"""
241
text_key = (file_id, self.get_file_revision(file_id))
242
annotator = self._repository.texts.get_annotator()
243
annotations = annotator.annotate_flat(text_key)
244
return [(key[-1], line) for key, line in annotations]
246
def __eq__(self, other):
249
if isinstance(other, InventoryRevisionTree):
250
return (self.root_inventory == other.root_inventory)
253
def __ne__(self, other):
254
return not (self == other)
257
raise ValueError('not hashable')
260
class InterCHKRevisionTree(tree.InterTree):
261
"""Fast path optimiser for RevisionTrees with CHK inventories."""
264
def is_compatible(source, target):
265
if (isinstance(source, RevisionTree)
266
and isinstance(target, RevisionTree)):
268
# Only CHK inventories have id_to_entry attribute
269
source.root_inventory.id_to_entry
270
target.root_inventory.id_to_entry
272
except AttributeError:
276
def iter_changes(self, include_unchanged=False,
277
specific_files=None, pb=None, extra_trees=[],
278
require_versioned=True, want_unversioned=False):
279
lookup_trees = [self.source]
281
lookup_trees.extend(extra_trees)
282
# The ids of items we need to examine to insure delta consistency.
283
precise_file_ids = set()
284
discarded_changes = {}
285
if specific_files == []:
286
specific_file_ids = []
288
specific_file_ids = self.target.paths2ids(specific_files,
289
lookup_trees, require_versioned=require_versioned)
290
# FIXME: It should be possible to delegate include_unchanged handling
291
# to CHKInventory.iter_changes and do a better job there -- vila
293
changed_file_ids = set()
294
# FIXME: nested tree support
295
for result in self.target.root_inventory.iter_changes(
296
self.source.root_inventory):
297
if specific_file_ids is not None:
299
if file_id not in specific_file_ids:
300
# A change from the whole tree that we don't want to show yet.
301
# We may find that we need to show it for delta consistency, so
303
discarded_changes[result[0]] = result
305
new_parent_id = result[4][1]
306
precise_file_ids.add(new_parent_id)
308
changed_file_ids.add(result[0])
309
if specific_file_ids is not None:
310
for result in self._handle_precise_ids(precise_file_ids,
311
changed_file_ids, discarded_changes=discarded_changes):
313
if include_unchanged:
314
# CHKMap avoid being O(tree), so we go to O(tree) only if
316
# Now walk the whole inventory, excluding the already yielded
318
# FIXME: Support nested trees
319
changed_file_ids = set(changed_file_ids)
320
for relpath, entry in self.target.root_inventory.iter_entries():
321
if (specific_file_ids is not None
322
and not entry.file_id in specific_file_ids):
324
if not entry.file_id in changed_file_ids:
325
yield (entry.file_id,
326
(relpath, relpath), # Not renamed
327
False, # Not modified
328
(True, True), # Still versioned
329
(entry.parent_id, entry.parent_id),
330
(entry.name, entry.name),
331
(entry.kind, entry.kind),
332
(entry.executable, entry.executable))
335
tree.InterTree.register_optimiser(InterCHKRevisionTree)