~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisiontree.py

  • Committer: Robert Collins
  • Date: 2010-05-06 07:48:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506074822-0bsgf2j4h8jx0xkk
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
our first in-tree matcher. See the module docstring for details.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

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