~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisiontree.py

  • Committer: Jelmer Vernooij
  • Date: 2009-06-09 00:59:51 UTC
  • mto: (4443.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 4444.
  • Revision ID: jelmer@samba.org-20090609005951-apv900cdk35o2ygh
Move squashing of XML-invalid characters to XMLSerializer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2007 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
20
20
 
21
21
from bzrlib import (
22
22
    errors,
 
23
    osutils,
23
24
    revision,
 
25
    symbol_versioning,
24
26
    tree,
25
27
    )
26
28
 
31
33
    File text can be retrieved from the text store.
32
34
    """
33
35
 
34
 
    def __init__(self, repository, revision_id):
35
 
        self._repository = repository
 
36
    def __init__(self, branch, inv, revision_id):
 
37
        # for compatability the 'branch' parameter has not been renamed to
 
38
        # repository at this point. However, we should change RevisionTree's
 
39
        # construction to always be via Repository and not via direct
 
40
        # construction - this will mean that we can change the constructor
 
41
        # with much less chance of breaking client code.
 
42
        self._repository = branch
 
43
        self._inventory = inv
36
44
        self._revision_id = revision_id
37
45
        self._rules_searcher = None
38
46
 
56
64
        """Return the revision id associated with this tree."""
57
65
        return self._revision_id
58
66
 
59
 
    def get_file_revision(self, file_id, path=None):
60
 
        """Return the revision id in which a file was last changed."""
61
 
        raise NotImplementedError(self.get_file_revision)
62
 
 
63
67
    def get_file_text(self, file_id, path=None):
64
68
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
65
69
        return ''.join(content)
67
71
    def get_file(self, file_id, path=None):
68
72
        return StringIO(self.get_file_text(file_id))
69
73
 
70
 
    def is_locked(self):
71
 
        return self._repository.is_locked()
72
 
 
73
 
    def lock_read(self):
74
 
        self._repository.lock_read()
75
 
        return self
76
 
 
77
 
    def __repr__(self):
78
 
        return '<%s instance at %x, rev_id=%r>' % (
79
 
            self.__class__.__name__, id(self), self._revision_id)
80
 
 
81
 
    def unlock(self):
82
 
        self._repository.unlock()
83
 
 
84
 
    def _get_rules_searcher(self, default_searcher):
85
 
        """See Tree._get_rules_searcher."""
86
 
        if self._rules_searcher is None:
87
 
            self._rules_searcher = super(RevisionTree,
88
 
                self)._get_rules_searcher(default_searcher)
89
 
        return self._rules_searcher
90
 
 
91
 
 
92
 
class InventoryRevisionTree(RevisionTree,tree.InventoryTree):
93
 
 
94
 
    def __init__(self, repository, inv, revision_id):
95
 
        RevisionTree.__init__(self, repository, revision_id)
96
 
        self._inventory = inv
97
 
 
98
 
    def get_file_mtime(self, file_id, path=None):
99
 
        ie = self._inventory[file_id]
 
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]
100
80
        try:
101
 
            revision = self._repository.get_revision(ie.revision)
102
 
        except errors.NoSuchRevision:
103
 
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
104
 
        return revision.timestamp
 
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):
 
88
        """See Tree.annotate_iter"""
 
89
        text_key = (file_id, self.inventory[file_id].revision)
 
90
        annotations = self._repository.texts.annotate(text_key)
 
91
        return [(key[-1], line) for key, line in annotations]
105
92
 
106
93
    def get_file_size(self, file_id):
 
94
        """See Tree.get_file_size"""
107
95
        return self._inventory[file_id].text_size
108
96
 
109
97
    def get_file_sha1(self, file_id, path=None, stat_value=None):
112
100
            return ie.text_sha1
113
101
        return None
114
102
 
115
 
    def get_file_revision(self, file_id, path=None):
 
103
    def get_file_mtime(self, file_id, path=None):
116
104
        ie = self._inventory[file_id]
117
 
        return ie.revision
 
105
        revision = self._repository.get_revision(ie.revision)
 
106
        return revision.timestamp
118
107
 
119
108
    def is_executable(self, file_id, path=None):
120
109
        ie = self._inventory[file_id]
121
110
        if ie.kind != "file":
122
 
            return False
 
111
            return None
123
112
        return ie.executable
124
113
 
125
114
    def has_filename(self, filename):
126
115
        return bool(self.inventory.path2id(filename))
127
116
 
128
 
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
117
    def list_files(self, include_root=False):
129
118
        # The only files returned by this are those from the version
130
 
        inv = self.inventory
131
 
        if from_dir is None:
132
 
            from_dir_id = None
133
 
        else:
134
 
            from_dir_id = inv.path2id(from_dir)
135
 
            if from_dir_id is None:
136
 
                # Directory not versioned
137
 
                return
138
 
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
139
 
        if inv.root is not None and not include_root and from_dir is None:
 
119
        entries = self.inventory.iter_entries()
 
120
        # skip the root for compatability with the current apis.
 
121
        if self.inventory.root is not None and not include_root:
140
122
            # skip the root for compatability with the current apis.
141
123
            entries.next()
142
124
        for path, entry in entries:
143
125
            yield path, 'V', entry.kind, entry.file_id, entry
144
126
 
145
 
    def get_symlink_target(self, file_id, path=None):
 
127
    def get_symlink_target(self, file_id):
146
128
        ie = self._inventory[file_id]
147
129
        # Inventories store symlink targets in unicode
148
130
        return ie.symlink_target
179
161
    def _file_size(self, entry, stat_value):
180
162
        return entry.text_size
181
163
 
 
164
    def _get_ancestors(self, default_revision):
 
165
        return set(self._repository.get_ancestry(self._revision_id,
 
166
                                                 topo_sorted=False))
 
167
 
 
168
    def lock_read(self):
 
169
        self._repository.lock_read()
 
170
 
 
171
    def __repr__(self):
 
172
        return '<%s instance at %x, rev_id=%r>' % (
 
173
            self.__class__.__name__, id(self), self._revision_id)
 
174
 
 
175
    def unlock(self):
 
176
        self._repository.unlock()
 
177
 
182
178
    def walkdirs(self, prefix=""):
183
179
        _directory = 'directory'
184
180
        inv = self.inventory
208
204
                if dir[2] == _directory:
209
205
                    pending.append(dir)
210
206
 
211
 
    def iter_files_bytes(self, desired_files):
212
 
        """See Tree.iter_files_bytes.
213
 
 
214
 
        This version is implemented on top of Repository.extract_files_bytes"""
215
 
        repo_desired_files = [(f, self.get_file_revision(f), i)
216
 
                              for f, i in desired_files]
217
 
        try:
218
 
            for result in self._repository.iter_files_bytes(repo_desired_files):
219
 
                yield result
220
 
        except errors.RevisionNotPresent, e:
221
 
            raise errors.NoSuchFile(e.revision_id)
222
 
 
223
 
    def annotate_iter(self, file_id,
224
 
                      default_revision=revision.CURRENT_REVISION):
225
 
        """See Tree.annotate_iter"""
226
 
        text_key = (file_id, self.get_file_revision(file_id))
227
 
        annotator = self._repository.texts.get_annotator()
228
 
        annotations = annotator.annotate_flat(text_key)
229
 
        return [(key[-1], line) for key, line in annotations]
 
207
    def _get_rules_searcher(self, default_searcher):
 
208
        """See Tree._get_rules_searcher."""
 
209
        if self._rules_searcher is None:
 
210
            self._rules_searcher = super(RevisionTree,
 
211
                self)._get_rules_searcher(default_searcher)
 
212
        return self._rules_searcher
230
213
 
231
214
 
232
215
class InterCHKRevisionTree(tree.InterTree):
251
234
        lookup_trees = [self.source]
252
235
        if extra_trees:
253
236
             lookup_trees.extend(extra_trees)
254
 
        # The ids of items we need to examine to insure delta consistency.
255
 
        precise_file_ids = set()
256
 
        discarded_changes = {}
257
237
        if specific_files == []:
258
238
            specific_file_ids = []
259
239
        else:
260
240
            specific_file_ids = self.target.paths2ids(specific_files,
261
241
                lookup_trees, require_versioned=require_versioned)
 
242
 
262
243
        # FIXME: It should be possible to delegate include_unchanged handling
263
244
        # to CHKInventory.iter_changes and do a better job there -- vila
264
245
        # 20090304
265
 
        changed_file_ids = set()
 
246
        if include_unchanged:
 
247
            changed_file_ids = []
266
248
        for result in self.target.inventory.iter_changes(self.source.inventory):
267
 
            if specific_file_ids is not None:
268
 
                file_id = result[0]
269
 
                if file_id not in specific_file_ids:
270
 
                    # A change from the whole tree that we don't want to show yet.
271
 
                    # We may find that we need to show it for delta consistency, so
272
 
                    # stash it.
273
 
                    discarded_changes[result[0]] = result
274
 
                    continue
275
 
                new_parent_id = result[4][1]
276
 
                precise_file_ids.add(new_parent_id)
 
249
            if (specific_file_ids is not None
 
250
                and not result[0] in specific_file_ids):
 
251
                # CHKMap.iter_changes is clean and fast. Better filter out
 
252
                # the specific files *after* it did its job.
 
253
                continue
277
254
            yield result
278
 
            changed_file_ids.add(result[0])
279
 
        if specific_file_ids is not None:
280
 
            for result in self._handle_precise_ids(precise_file_ids,
281
 
                changed_file_ids, discarded_changes=discarded_changes):
282
 
                yield result
 
255
            if include_unchanged:
 
256
                # Keep track of yielded results (cheaper than building the
 
257
                # whole inventory).
 
258
                changed_file_ids.append(result[0])
283
259
        if include_unchanged:
284
260
            # CHKMap avoid being O(tree), so we go to O(tree) only if
285
261
            # required to.