/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/revisiontree.py

MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""RevisionTree - a Tree implementation backed by repository data for a revision."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import (
 
22
    errors,
 
23
    osutils,
 
24
    revision,
 
25
    symbol_versioning,
 
26
    tree,
 
27
    )
 
28
from bzrlib.decorators import cleanup_method
 
29
 
 
30
 
 
31
class RevisionTree(tree.Tree):
 
32
    """Tree viewing a previous revision.
 
33
 
 
34
    File text can be retrieved from the text store.
 
35
    """
 
36
 
 
37
    def __init__(self, branch, inv, revision_id):
 
38
        # for compatability the 'branch' parameter has not been renamed to
 
39
        # repository at this point. However, we should change RevisionTree's
 
40
        # construction to always be via Repository and not via direct
 
41
        # construction - this will mean that we can change the constructor
 
42
        # with much less chance of breaking client code.
 
43
        self._repository = branch
 
44
        self._inventory = inv
 
45
        self._revision_id = revision_id
 
46
        self._rules_searcher = None
 
47
 
 
48
    def supports_tree_reference(self):
 
49
        return getattr(self._repository._format, "supports_tree_reference",
 
50
            False)
 
51
 
 
52
    def get_parent_ids(self):
 
53
        """See Tree.get_parent_ids.
 
54
 
 
55
        A RevisionTree's parents match the revision graph.
 
56
        """
 
57
        if self._revision_id in (None, revision.NULL_REVISION):
 
58
            parent_ids = []
 
59
        else:
 
60
            parent_ids = self._repository.get_revision(
 
61
                self._revision_id).parent_ids
 
62
        return parent_ids
 
63
 
 
64
    def get_revision_id(self):
 
65
        """Return the revision id associated with this tree."""
 
66
        return self._revision_id
 
67
 
 
68
    def get_file_text(self, file_id, path=None):
 
69
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
70
        return ''.join(content)
 
71
 
 
72
    def get_file(self, file_id, path=None):
 
73
        return StringIO(self.get_file_text(file_id))
 
74
 
 
75
    def iter_files_bytes(self, desired_files):
 
76
        """See Tree.iter_files_bytes.
 
77
 
 
78
        This version is implemented on top of Repository.extract_files_bytes"""
 
79
        repo_desired_files = [(f, self.inventory[f].revision, i)
 
80
                              for f, i in desired_files]
 
81
        try:
 
82
            for result in self._repository.iter_files_bytes(repo_desired_files):
 
83
                yield result
 
84
        except errors.RevisionNotPresent, e:
 
85
            raise errors.NoSuchFile(e.revision_id)
 
86
 
 
87
    def annotate_iter(self, file_id,
 
88
                      default_revision=revision.CURRENT_REVISION):
 
89
        """See Tree.annotate_iter"""
 
90
        text_key = (file_id, self.inventory[file_id].revision)
 
91
        annotator = self._repository.texts.get_annotator()
 
92
        annotations = annotator.annotate_flat(text_key)
 
93
        return [(key[-1], line) for key, line in annotations]
 
94
 
 
95
    def get_file_size(self, file_id):
 
96
        """See Tree.get_file_size"""
 
97
        return self._inventory[file_id].text_size
 
98
 
 
99
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
100
        ie = self._inventory[file_id]
 
101
        if ie.kind == "file":
 
102
            return ie.text_sha1
 
103
        return None
 
104
 
 
105
    def get_file_mtime(self, file_id, path=None):
 
106
        ie = self._inventory[file_id]
 
107
        revision = self._repository.get_revision(ie.revision)
 
108
        return revision.timestamp
 
109
 
 
110
    def is_executable(self, file_id, path=None):
 
111
        ie = self._inventory[file_id]
 
112
        if ie.kind != "file":
 
113
            return None
 
114
        return ie.executable
 
115
 
 
116
    def has_filename(self, filename):
 
117
        return bool(self.inventory.path2id(filename))
 
118
 
 
119
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
120
        # The only files returned by this are those from the version
 
121
        inv = self.inventory
 
122
        if from_dir is None:
 
123
            from_dir_id = None
 
124
        else:
 
125
            from_dir_id = inv.path2id(from_dir)
 
126
            if from_dir_id is None:
 
127
                # Directory not versioned
 
128
                return
 
129
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
 
130
        if inv.root is not None and not include_root and from_dir is None:
 
131
            # skip the root for compatability with the current apis.
 
132
            entries.next()
 
133
        for path, entry in entries:
 
134
            yield path, 'V', entry.kind, entry.file_id, entry
 
135
 
 
136
    def get_symlink_target(self, file_id):
 
137
        ie = self._inventory[file_id]
 
138
        # Inventories store symlink targets in unicode
 
139
        return ie.symlink_target
 
140
 
 
141
    def get_reference_revision(self, file_id, path=None):
 
142
        return self.inventory[file_id].reference_revision
 
143
 
 
144
    def get_root_id(self):
 
145
        if self.inventory.root:
 
146
            return self.inventory.root.file_id
 
147
 
 
148
    def kind(self, file_id):
 
149
        return self._inventory[file_id].kind
 
150
 
 
151
    def path_content_summary(self, path):
 
152
        """See Tree.path_content_summary."""
 
153
        id = self.inventory.path2id(path)
 
154
        if id is None:
 
155
            return ('missing', None, None, None)
 
156
        entry = self._inventory[id]
 
157
        kind = entry.kind
 
158
        if kind == 'file':
 
159
            return (kind, entry.text_size, entry.executable, entry.text_sha1)
 
160
        elif kind == 'symlink':
 
161
            return (kind, None, None, entry.symlink_target)
 
162
        else:
 
163
            return (kind, None, None, None)
 
164
 
 
165
    def _comparison_data(self, entry, path):
 
166
        if entry is None:
 
167
            return None, False, None
 
168
        return entry.kind, entry.executable, None
 
169
 
 
170
    def _file_size(self, entry, stat_value):
 
171
        return entry.text_size
 
172
 
 
173
    def _get_ancestors(self, default_revision):
 
174
        return set(self._repository.get_ancestry(self._revision_id,
 
175
                                                 topo_sorted=False))
 
176
 
 
177
    def lock_read(self):
 
178
        self._repository.lock_read()
 
179
 
 
180
    def __repr__(self):
 
181
        return '<%s instance at %x, rev_id=%r>' % (
 
182
            self.__class__.__name__, id(self), self._revision_id)
 
183
 
 
184
    @cleanup_method
 
185
    def unlock(self):
 
186
        self._repository.unlock()
 
187
 
 
188
    def walkdirs(self, prefix=""):
 
189
        _directory = 'directory'
 
190
        inv = self.inventory
 
191
        top_id = inv.path2id(prefix)
 
192
        if top_id is None:
 
193
            pending = []
 
194
        else:
 
195
            pending = [(prefix, '', _directory, None, top_id, None)]
 
196
        while pending:
 
197
            dirblock = []
 
198
            currentdir = pending.pop()
 
199
            # 0 - relpath, 1- basename, 2- kind, 3- stat, id, v-kind
 
200
            if currentdir[0]:
 
201
                relroot = currentdir[0] + '/'
 
202
            else:
 
203
                relroot = ""
 
204
            # FIXME: stash the node in pending
 
205
            entry = inv[currentdir[4]]
 
206
            for name, child in entry.sorted_children():
 
207
                toppath = relroot + name
 
208
                dirblock.append((toppath, name, child.kind, None,
 
209
                    child.file_id, child.kind
 
210
                    ))
 
211
            yield (currentdir[0], entry.file_id), dirblock
 
212
            # push the user specified dirs from dirblock
 
213
            for dir in reversed(dirblock):
 
214
                if dir[2] == _directory:
 
215
                    pending.append(dir)
 
216
 
 
217
    def _get_rules_searcher(self, default_searcher):
 
218
        """See Tree._get_rules_searcher."""
 
219
        if self._rules_searcher is None:
 
220
            self._rules_searcher = super(RevisionTree,
 
221
                self)._get_rules_searcher(default_searcher)
 
222
        return self._rules_searcher
 
223
 
 
224
 
 
225
class InterCHKRevisionTree(tree.InterTree):
 
226
    """Fast path optimiser for RevisionTrees with CHK inventories."""
 
227
 
 
228
    @staticmethod
 
229
    def is_compatible(source, target):
 
230
        if (isinstance(source, RevisionTree)
 
231
            and isinstance(target, RevisionTree)):
 
232
            try:
 
233
                # Only CHK inventories have id_to_entry attribute
 
234
                source.inventory.id_to_entry
 
235
                target.inventory.id_to_entry
 
236
                return True
 
237
            except AttributeError:
 
238
                pass
 
239
        return False
 
240
 
 
241
    def iter_changes(self, include_unchanged=False,
 
242
                     specific_files=None, pb=None, extra_trees=[],
 
243
                     require_versioned=True, want_unversioned=False):
 
244
        lookup_trees = [self.source]
 
245
        if extra_trees:
 
246
             lookup_trees.extend(extra_trees)
 
247
        # The ids of items we need to examine to insure delta consistency.
 
248
        precise_file_ids = set()
 
249
        discarded_changes = {}
 
250
        if specific_files == []:
 
251
            specific_file_ids = []
 
252
        else:
 
253
            specific_file_ids = self.target.paths2ids(specific_files,
 
254
                lookup_trees, require_versioned=require_versioned)
 
255
        # FIXME: It should be possible to delegate include_unchanged handling
 
256
        # to CHKInventory.iter_changes and do a better job there -- vila
 
257
        # 20090304
 
258
        changed_file_ids = set()
 
259
        for result in self.target.inventory.iter_changes(self.source.inventory):
 
260
            if specific_file_ids is not None:
 
261
                file_id = result[0]
 
262
                if file_id not in specific_file_ids:
 
263
                    # A change from the whole tree that we don't want to show yet.
 
264
                    # We may find that we need to show it for delta consistency, so
 
265
                    # stash it.
 
266
                    discarded_changes[result[0]] = result
 
267
                    continue
 
268
                new_parent_id = result[4][1]
 
269
                precise_file_ids.add(new_parent_id)
 
270
            yield result
 
271
            changed_file_ids.add(result[0])
 
272
        if specific_file_ids is not None:
 
273
            for result in self._handle_precise_ids(precise_file_ids,
 
274
                changed_file_ids, discarded_changes=discarded_changes):
 
275
                yield result
 
276
        if include_unchanged:
 
277
            # CHKMap avoid being O(tree), so we go to O(tree) only if
 
278
            # required to.
 
279
            # Now walk the whole inventory, excluding the already yielded
 
280
            # file ids
 
281
            changed_file_ids = set(changed_file_ids)
 
282
            for relpath, entry in self.target.inventory.iter_entries():
 
283
                if (specific_file_ids is not None
 
284
                    and not entry.file_id in specific_file_ids):
 
285
                    continue
 
286
                if not entry.file_id in changed_file_ids:
 
287
                    yield (entry.file_id,
 
288
                           (relpath, relpath), # Not renamed
 
289
                           False, # Not modified
 
290
                           (True, True), # Still  versioned
 
291
                           (entry.parent_id, entry.parent_id),
 
292
                           (entry.name, entry.name),
 
293
                           (entry.kind, entry.kind),
 
294
                           (entry.executable, entry.executable))
 
295
 
 
296
 
 
297
tree.InterTree.register_optimiser(InterCHKRevisionTree)