/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006-2010 Canonical Ltd
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""RevisionTree - a Tree implementation backed by repository data for a revision."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from . import (
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
22
    errors,
2255.2.83 by John Arbash Meinel
[merge] bzr.dev 2294
23
    revision,
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
24
    tree,
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from .sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
27
    BytesIO,
28
    )
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
29
30
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
31
class RevisionTree(tree.Tree):
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
32
    """Tree viewing a previous revision.
33
34
    File text can be retrieved from the text store.
35
    """
3008.1.13 by Michael Hudson
merge bzr.dev
36
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
37
    def __init__(self, repository, revision_id):
38
        self._repository = repository
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
39
        self._revision_id = revision_id
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
40
        self._rules_searcher = None
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
41
6110.6.1 by Jelmer Vernooij
Add Tree.has_versioned_directories.
42
    def has_versioned_directories(self):
43
        """See `Tree.has_versioned_directories`."""
44
        return self._repository._format.supports_versioned_directories
45
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
46
    def supports_tree_reference(self):
4370.3.2 by Ian Clatworthy
apply jam's review feedback
47
        return getattr(self._repository._format, "supports_tree_reference",
48
            False)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
49
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
50
    def get_parent_ids(self):
51
        """See Tree.get_parent_ids.
52
53
        A RevisionTree's parents match the revision graph.
54
        """
1908.11.3 by Robert Collins
Merge bzr.dev
55
        if self._revision_id in (None, revision.NULL_REVISION):
56
            parent_ids = []
1908.11.2 by Robert Collins
Implement WorkingTree interface conformance tests for
57
        else:
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
58
            parent_ids = self._repository.get_revision(
59
                self._revision_id).parent_ids
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
60
        return parent_ids
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
61
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
62
    def get_revision_id(self):
63
        """Return the revision id associated with this tree."""
64
        return self._revision_id
65
5793.2.3 by Jelmer Vernooij
Add a RevisionTree.get_file_revision() method.
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)
69
3774.1.1 by Aaron Bentley
Test Tree.get_file_text() and supply default implementation.
70
    def get_file_text(self, file_id, path=None):
6280.10.14 by Jelmer Vernooij
cope with slightly different behaviour.
71
        for (identifier, content) in self.iter_files_bytes([(file_id, None)]):
72
            ret = "".join(content)
73
        return ret
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
74
2743.3.5 by Ian Clatworthy
Incorporate feedback from abentley
75
    def get_file(self, file_id, path=None):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
76
        return BytesIO(self.get_file_text(file_id))
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
77
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
78
    def is_locked(self):
79
        return self._repository.is_locked()
80
81
    def lock_read(self):
82
        self._repository.lock_read()
83
        return self
84
85
    def __repr__(self):
86
        return '<%s instance at %x, rev_id=%r>' % (
87
            self.__class__.__name__, id(self), self._revision_id)
88
89
    def unlock(self):
90
        self._repository.unlock()
91
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
98
99
100
class InventoryRevisionTree(RevisionTree,tree.InventoryTree):
101
102
    def __init__(self, repository, inv, revision_id):
103
        RevisionTree.__init__(self, repository, revision_id)
104
        self._inventory = inv
105
106
    def get_file_mtime(self, file_id, path=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
107
        inv, inv_file_id = self._unpack_file_id(file_id)
108
        ie = inv[inv_file_id]
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
109
        try:
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
110
            revision = self._repository.get_revision(ie.revision)
111
        except errors.NoSuchRevision:
112
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
113
        return revision.timestamp
1551.9.16 by Aaron Bentley
Implement Tree.annotate_iter for RevisionTree and WorkingTree
114
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
115
    def get_file_size(self, file_id):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
116
        inv, inv_file_id = self._unpack_file_id(file_id)
117
        return inv[inv_file_id].text_size
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
118
2012.1.7 by Aaron Bentley
Get tree._iter_changed down to ~ 1 stat per file
119
    def get_file_sha1(self, file_id, path=None, stat_value=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
120
        inv, inv_file_id = self._unpack_file_id(file_id)
121
        ie = inv[inv_file_id]
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
122
        if ie.kind == "file":
123
            return ie.text_sha1
124
        return None
125
5793.2.3 by Jelmer Vernooij
Add a RevisionTree.get_file_revision() method.
126
    def get_file_revision(self, file_id, path=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
127
        inv, inv_file_id = self._unpack_file_id(file_id)
128
        ie = inv[inv_file_id]
5793.2.3 by Jelmer Vernooij
Add a RevisionTree.get_file_revision() method.
129
        return ie.revision
130
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
131
    def is_executable(self, file_id, path=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
132
        inv, inv_file_id = self._unpack_file_id(file_id)
133
        ie = inv[inv_file_id]
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
134
        if ie.kind != "file":
5050.57.1 by Aaron Bentley
Make is_executable treat symlinks and directories the same across tree types.
135
            return False
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
136
        return ie.executable
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
137
138
    def has_filename(self, filename):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
139
        return bool(self.path2id(filename))
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
140
4370.5.2 by Ian Clatworthy
extend list_files() with from_dir and recursive parameters
141
    def list_files(self, include_root=False, from_dir=None, recursive=True):
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
142
        # The only files returned by this are those from the version
4370.5.2 by Ian Clatworthy
extend list_files() with from_dir and recursive parameters
143
        if from_dir is None:
144
            from_dir_id = None
6405.2.5 by Jelmer Vernooij
Add root_inventory.
145
            inv = self.root_inventory
4370.5.2 by Ian Clatworthy
extend list_files() with from_dir and recursive parameters
146
        else:
6405.2.2 by Jelmer Vernooij
Use _path2inv_file_id.
147
            inv, from_dir_id = self._path2inv_file_id(from_dir)
4370.5.3 by Ian Clatworthy
handle unversioned directories
148
            if from_dir_id is None:
149
                # Directory not versioned
150
                return
4370.5.2 by Ian Clatworthy
extend list_files() with from_dir and recursive parameters
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:
1910.2.56 by Aaron Bentley
More work on bundles
153
            # skip the root for compatability with the current apis.
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
154
            next(entries)
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
155
        for path, entry in entries:
156
            yield path, 'V', entry.kind, entry.file_id, entry
157
5858.1.1 by Jelmer Vernooij
Support optional path argument to Tree.get_symlink_target.
158
    def get_symlink_target(self, file_id, path=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
159
        inv, inv_file_id = self._unpack_file_id(file_id)
160
        ie = inv[inv_file_id]
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
161
        # Inventories store symlink targets in unicode
162
        return ie.symlink_target
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
163
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
164
    def get_reference_revision(self, file_id, path=None):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
165
        inv, inv_file_id = self._unpack_file_id(file_id)
166
        return inv[inv_file_id].reference_revision
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
167
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
168
    def get_root_id(self):
6405.2.5 by Jelmer Vernooij
Add root_inventory.
169
        if self.root_inventory.root:
170
            return self.root_inventory.root.file_id
2255.2.166 by Martin Pool
(broken) Add Tree.get_root_id() & test
171
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
172
    def kind(self, file_id):
6405.2.1 by Jelmer Vernooij
Allow passing in tuples as file ids in various places.
173
        inv, inv_file_id = self._unpack_file_id(file_id)
174
        return inv[inv_file_id].kind
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
175
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
176
    def path_content_summary(self, path):
177
        """See Tree.path_content_summary."""
6405.2.2 by Jelmer Vernooij
Use _path2inv_file_id.
178
        inv, file_id = self._path2inv_file_id(path)
179
        if file_id is None:
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
180
            return ('missing', None, None, None)
6405.2.2 by Jelmer Vernooij
Use _path2inv_file_id.
181
        entry = inv[file_id]
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
182
        kind = entry.kind
183
        if kind == 'file':
184
            return (kind, entry.text_size, entry.executable, entry.text_sha1)
185
        elif kind == 'symlink':
186
            return (kind, None, None, entry.symlink_target)
187
        else:
188
            return (kind, None, None, None)
189
2012.1.7 by Aaron Bentley
Get tree._iter_changed down to ~ 1 stat per file
190
    def _comparison_data(self, entry, path):
191
        if entry is None:
2012.1.15 by Aaron Bentley
Minor tweaks
192
            return None, False, None
2012.1.7 by Aaron Bentley
Get tree._iter_changed down to ~ 1 stat per file
193
        return entry.kind, entry.executable, None
194
195
    def _file_size(self, entry, stat_value):
196
        return entry.text_size
197
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
198
    def walkdirs(self, prefix=""):
199
        _directory = 'directory'
6405.2.2 by Jelmer Vernooij
Use _path2inv_file_id.
200
        inv, top_id = self._path2inv_file_id(prefix)
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
201
        if top_id is None:
202
            pending = []
203
        else:
204
            pending = [(prefix, '', _directory, None, top_id, None)]
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
205
        while pending:
206
            dirblock = []
207
            currentdir = pending.pop()
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
208
            # 0 - relpath, 1- basename, 2- kind, 3- stat, id, v-kind
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
209
            if currentdir[0]:
210
                relroot = currentdir[0] + '/'
211
            else:
212
                relroot = ""
213
            # FIXME: stash the node in pending
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
214
            entry = inv[currentdir[4]]
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
215
            for name, child in entry.sorted_children():
216
                toppath = relroot + name
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
217
                dirblock.append((toppath, name, child.kind, None,
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
218
                    child.file_id, child.kind
219
                    ))
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
220
            yield (currentdir[0], entry.file_id), dirblock
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
221
            # push the user specified dirs from dirblock
222
            for dir in reversed(dirblock):
223
                if dir[2] == _directory:
224
                    pending.append(dir)
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
225
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
226
    def iter_files_bytes(self, desired_files):
227
        """See Tree.iter_files_bytes.
228
6280.10.17 by Jelmer Vernooij
Fix NoSuchFile error message, method name of Repository.iter_file_bytes.
229
        This version is implemented on top of Repository.iter_files_bytes"""
5793.2.3 by Jelmer Vernooij
Add a RevisionTree.get_file_revision() method.
230
        repo_desired_files = [(f, self.get_file_revision(f), i)
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
231
                              for f, i in desired_files]
232
        try:
233
            for result in self._repository.iter_files_bytes(repo_desired_files):
234
                yield result
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
235
        except errors.RevisionNotPresent as e:
6280.10.17 by Jelmer Vernooij
Fix NoSuchFile error message, method name of Repository.iter_file_bytes.
236
            raise errors.NoSuchFile(e.file_id)
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
237
238
    def annotate_iter(self, file_id,
239
                      default_revision=revision.CURRENT_REVISION):
240
        """See Tree.annotate_iter"""
5793.2.3 by Jelmer Vernooij
Add a RevisionTree.get_file_revision() method.
241
        text_key = (file_id, self.get_file_revision(file_id))
5793.2.2 by Jelmer Vernooij
Split inventory-specific code out of RevisionTree into InventoryRevisionTree.
242
        annotator = self._repository.texts.get_annotator()
243
        annotations = annotator.annotate_flat(text_key)
244
        return [(key[-1], line) for key, line in annotations]
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
245
6470.1.1 by Jelmer Vernooij
Use inventories directly in fewer places.
246
    def __eq__(self, other):
247
        if self is other:
248
            return True
249
        if isinstance(other, InventoryRevisionTree):
250
            return (self.root_inventory == other.root_inventory)
251
        return False
252
253
    def __ne__(self, other):
254
        return not (self == other)
255
256
    def __hash__(self):
257
        raise ValueError('not hashable')
258
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
259
260
class InterCHKRevisionTree(tree.InterTree):
261
    """Fast path optimiser for RevisionTrees with CHK inventories."""
262
263
    @staticmethod
264
    def is_compatible(source, target):
265
        if (isinstance(source, RevisionTree)
266
            and isinstance(target, RevisionTree)):
267
            try:
268
                # Only CHK inventories have id_to_entry attribute
6405.2.5 by Jelmer Vernooij
Add root_inventory.
269
                source.root_inventory.id_to_entry
270
                target.root_inventory.id_to_entry
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
271
                return True
272
            except AttributeError:
273
                pass
274
        return False
275
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]
280
        if extra_trees:
281
             lookup_trees.extend(extra_trees)
4570.2.3 by Robert Collins
Change the way iter_changes treats specific files to prevent InconsistentDeltas.
282
        # The ids of items we need to examine to insure delta consistency.
283
        precise_file_ids = set()
284
        discarded_changes = {}
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
285
        if specific_files == []:
286
            specific_file_ids = []
287
        else:
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
292
        # 20090304
4570.2.3 by Robert Collins
Change the way iter_changes treats specific files to prevent InconsistentDeltas.
293
        changed_file_ids = set()
6405.2.5 by Jelmer Vernooij
Add root_inventory.
294
        # FIXME: nested tree support
295
        for result in self.target.root_inventory.iter_changes(
296
                self.source.root_inventory):
4570.2.3 by Robert Collins
Change the way iter_changes treats specific files to prevent InconsistentDeltas.
297
            if specific_file_ids is not None:
298
                file_id = result[0]
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
302
                    # stash it.
303
                    discarded_changes[result[0]] = result
304
                    continue
305
                new_parent_id = result[4][1]
306
                precise_file_ids.add(new_parent_id)
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
307
            yield result
4570.2.3 by Robert Collins
Change the way iter_changes treats specific files to prevent InconsistentDeltas.
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):
312
                yield result
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
313
        if include_unchanged:
314
            # CHKMap avoid being O(tree), so we go to O(tree) only if
315
            # required to.
316
            # Now walk the whole inventory, excluding the already yielded
317
            # file ids
6405.2.10 by Jelmer Vernooij
Fix more tests.
318
            # FIXME: Support nested trees
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
319
            changed_file_ids = set(changed_file_ids)
6405.2.10 by Jelmer Vernooij
Fix more tests.
320
            for relpath, entry in self.target.root_inventory.iter_entries():
4241.6.7 by Vincent Ladeuil
Add InterCHKRevisionTree
321
                if (specific_file_ids is not None
322
                    and not entry.file_id in specific_file_ids):
323
                    continue
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))
333
334
335
tree.InterTree.register_optimiser(InterCHKRevisionTree)