~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisiontree.py

  • Committer: Aaron Bentley
  • Date: 2007-01-16 13:12:54 UTC
  • mto: (2230.3.47 branch6)
  • mto: This revision was merged to the branch mainline in revision 2290.
  • Revision ID: aaron.bentley@utoronto.ca-20070116131254-sjruli93timappd4
work in progress bind stuff

Show diffs side-by-side

added added

removed removed

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