/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5752.3.8 by John Arbash Meinel
Merge bzr.dev 5764 to resolve release-notes (aka NEWS) conflicts
1
# Copyright (C) 2005-2011 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1 by mbp at sourcefrog
import from baz patch-364
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1 by mbp at sourcefrog
import from baz patch-364
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1 by mbp at sourcefrog
import from baz patch-364
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
1 by mbp at sourcefrog
import from baz patch-364
16
17
"""Tree classes, representing directory at point in time.
18
"""
19
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
20
from .lazy_import import lazy_import
5745.3.1 by Jelmer Vernooij
Use lazy imports in bzrlib.tree.
21
lazy_import(globals(), """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
from breezy.i18n import gettext
5745.3.1 by Jelmer Vernooij
Use lazy imports in bzrlib.tree.
23
""")
24
6731.1.1 by Jelmer Vernooij
Move FileTimestampUnavailable to breezy.tree.
25
from . import (
26
    errors,
6754.8.3 by Jelmer Vernooij
Use context manager in decorators.
27
    lock,
6731.1.1 by Jelmer Vernooij
Move FileTimestampUnavailable to breezy.tree.
28
    osutils,
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
29
    revision as _mod_revision,
7467.4.15 by Jelmer Vernooij
Split out InventoryInterTree.
30
    trace,
6731.1.1 by Jelmer Vernooij
Move FileTimestampUnavailable to breezy.tree.
31
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
32
from .inter import InterObject
1 by mbp at sourcefrog
import from baz patch-364
33
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
34
6731.1.1 by Jelmer Vernooij
Move FileTimestampUnavailable to breezy.tree.
35
class FileTimestampUnavailable(errors.BzrError):
36
37
    _fmt = "The filestamp for %(path)s is not available."
38
39
    internal_error = True
40
41
    def __init__(self, path):
42
        self.path = path
43
44
7358.4.1 by Jelmer Vernooij
Add Tree.get_nested_tree.
45
class MissingNestedTree(errors.BzrError):
46
47
    _fmt = "The nested tree for %(path)s can not be resolved."""
48
49
    def __init__(self, path):
50
        self.path = path
51
52
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
53
class TreeEntry(object):
54
    """An entry that implements the minimum interface used by commands.
55
    """
56
7322.3.1 by Jelmer Vernooij
Use slots on default Tree objects.
57
    __slots__ = []
58
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
59
    def __eq__(self, other):
6857 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-ie.
60
        # yes, this is ugly, TODO: best practice __eq__ style.
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
61
        return (isinstance(other, TreeEntry)
62
                and other.__class__ == self.__class__)
63
7143.19.2 by Jelmer Vernooij
Some test fixes.
64
    kind = None
65
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
66
    def kind_character(self):
67
        return "???"
68
7397.2.1 by Jelmer Vernooij
Add TreeEntry.is_unmodified.
69
    def is_unmodified(self, other):
70
        """Does this entry reference the same entry?
71
72
        This is mostly the same as __eq__, but returns False
73
        for entries without enough information (i.e. revision is None)
74
        """
75
        return False
76
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
77
78
class TreeDirectory(TreeEntry):
79
    """See TreeEntry. This is a directory in a working tree."""
80
7322.3.1 by Jelmer Vernooij
Use slots on default Tree objects.
81
    __slots__ = []
82
7143.19.2 by Jelmer Vernooij
Some test fixes.
83
    kind = 'directory'
84
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
85
    def kind_character(self):
86
        return "/"
87
88
89
class TreeFile(TreeEntry):
90
    """See TreeEntry. This is a regular file in a working tree."""
91
7322.3.1 by Jelmer Vernooij
Use slots on default Tree objects.
92
    __slots__ = []
93
7143.19.2 by Jelmer Vernooij
Some test fixes.
94
    kind = 'file'
95
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
96
    def kind_character(self):
97
        return ''
98
99
100
class TreeLink(TreeEntry):
101
    """See TreeEntry. This is a symlink in a working tree."""
102
7322.3.1 by Jelmer Vernooij
Use slots on default Tree objects.
103
    __slots__ = []
104
7143.19.2 by Jelmer Vernooij
Some test fixes.
105
    kind = 'symlink'
106
6846.6.1 by Jelmer Vernooij
Move Tree{Link,File,Directory,Entry} to breezy.tree.
107
    def kind_character(self):
108
        return ''
109
110
6926.2.1 by Jelmer Vernooij
Some tree reference fixes.
111
class TreeReference(TreeEntry):
112
    """See TreeEntry. This is a reference to a nested tree in a working tree."""
113
7322.3.1 by Jelmer Vernooij
Use slots on default Tree objects.
114
    __slots__ = []
115
7143.19.2 by Jelmer Vernooij
Some test fixes.
116
    kind = 'tree-reference'
117
6926.2.1 by Jelmer Vernooij
Some tree reference fixes.
118
    def kind_character(self):
7143.19.2 by Jelmer Vernooij
Some test fixes.
119
        return '+'
6926.2.1 by Jelmer Vernooij
Some tree reference fixes.
120
121
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
122
class TreeChange(object):
123
    """Describes the changes between the same item in two different trees."""
124
7322.1.5 by Jelmer Vernooij
Fix slice handling.
125
    __slots__ = ['file_id', 'path', 'changed_content', 'versioned', 'parent_id',
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
126
                 'name', 'kind', 'executable', 'copied']
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
127
7322.1.5 by Jelmer Vernooij
Fix slice handling.
128
    def __init__(self, file_id, path, changed_content, versioned, parent_id,
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
129
                 name, kind, executable, copied=False):
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
130
        self.file_id = file_id
7322.1.4 by Jelmer Vernooij
Use singular for consistency.
131
        self.path = path
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
132
        self.changed_content = changed_content
133
        self.versioned = versioned
7322.1.5 by Jelmer Vernooij
Fix slice handling.
134
        self.parent_id = parent_id
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
135
        self.name = name
136
        self.kind = kind
137
        self.executable = executable
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
138
        self.copied = copied
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
139
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
140
    def __repr__(self):
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
141
        return "%s%r" % (self.__class__.__name__, self._as_tuple())
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
142
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
143
    def __len__(self):
7322.1.3 by Jelmer Vernooij
Simplify.
144
        return len(self.__slots__)
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
145
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
146
    def _as_tuple(self):
7322.1.4 by Jelmer Vernooij
Use singular for consistency.
147
        return (self.file_id, self.path, self.changed_content, self.versioned,
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
148
                self.parent_id, self.name, self.kind, self.executable, self.copied)
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
149
150
    def __eq__(self, other):
151
        if isinstance(other, TreeChange):
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
152
            return self._as_tuple() == other._as_tuple()
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
153
        if isinstance(other, tuple):
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
154
            return self._as_tuple() == other
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
155
        return False
156
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
157
    def __lt__(self, other):
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
158
        return self._as_tuple() < other._as_tuple()
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
159
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
160
    def meta_modified(self):
7358.11.9 by Jelmer Vernooij
Fix other tests.
161
        if self.versioned == (True, True):
7358.11.3 by Jelmer Vernooij
TreeDelta holds TreeChange objects rather than tuples of various sizes.
162
            return (self.executable[0] != self.executable[1])
163
        return False
164
7324.2.1 by Jelmer Vernooij
Use find_previous_path in InterTree.iter_changes.
165
    def is_reparented(self):
166
        return self.parent_id[0] != self.parent_id[1]
167
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
168
    def discard_new(self):
169
        return self.__class__(
170
            self.file_id, (self.path[0], None), self.changed_content,
171
            (self.versioned[0], None), (self.parent_id[0], None),
172
            (self.name[0], None), (self.kind[0], None),
7358.17.1 by Jelmer Vernooij
Add TreeDelta.copied and TreeChange.copied fields.
173
            (self.executable[0], None),
174
            copied=False)
7322.1.7 by Jelmer Vernooij
Fix remaining tests.
175
7322.1.1 by Jelmer Vernooij
Add TreeChange object.
176
558 by Martin Pool
- All top-level classes inherit from object
177
class Tree(object):
1 by mbp at sourcefrog
import from baz patch-364
178
    """Abstract file tree.
179
180
    There are several subclasses:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
181
1 by mbp at sourcefrog
import from baz patch-364
182
    * `WorkingTree` exists as files on disk editable by the user.
183
184
    * `RevisionTree` is a tree as recorded at some point in the past.
185
186
    Trees can be compared, etc, regardless of whether they are working
187
    trees or versioned trees.
188
    """
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
189
6883.5.17 by Jelmer Vernooij
Add Tree.supports_rename_tracking().
190
    def supports_rename_tracking(self):
191
        """Whether this tree supports rename tracking.
192
193
        This defaults to True, but some implementations may want to override
194
        it.
195
        """
196
        return True
197
6110.6.1 by Jelmer Vernooij
Add Tree.has_versioned_directories.
198
    def has_versioned_directories(self):
199
        """Whether this tree can contain explicitly versioned directories.
200
201
        This defaults to True, but some implementations may want to override
202
        it.
203
        """
204
        return True
205
7122.6.3 by Jelmer Vernooij
Merge trunk.
206
    def supports_symlinks(self):
207
        """Does this tree support symbolic links?
208
        """
209
        return osutils.has_symlinks()
210
1852.9.6 by Robert Collins
Merge the change from Tree.compare to Tree.changes_from.
211
    def changes_from(self, other, want_unchanged=False, specific_files=None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
212
                     extra_trees=None, require_versioned=False, include_root=False,
213
                     want_unversioned=False):
1852.8.8 by Robert Collins
change Tree.compare to Tree.changes_from - its better for the common case.
214
        """Return a TreeDelta of the changes from other to this tree.
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
215
216
        :param other: A tree to compare with.
217
        :param specific_files: An optional list of file paths to restrict the
218
            comparison to. When mapping filenames to ids, all matches in all
219
            trees (including optional extra_trees) are used, and all children of
220
            matched directories are included.
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
221
        :param want_unchanged: An optional boolean requesting the inclusion of
222
            unchanged entries in the result.
223
        :param extra_trees: An optional list of additional trees to use when
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
224
            mapping the contents of specific_files (paths) to their identities.
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
225
        :param require_versioned: An optional boolean (defaults to False). When
226
            supplied and True all the 'specific_files' must be versioned, or
227
            a PathsNotVersionedError will be thrown.
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
228
        :param want_unversioned: Scan for unversioned paths.
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
229
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
230
        The comparison will be performed by an InterTree object looked up on
1852.8.4 by Robert Collins
Hook InterTree into Tree.
231
        self and other.
232
        """
1852.8.8 by Robert Collins
change Tree.compare to Tree.changes_from - its better for the common case.
233
        # Martin observes that Tree.changes_from returns a TreeDelta and this
234
        # may confuse people, because the class name of the returned object is
235
        # a synonym of the object referenced in the method name.
1852.9.6 by Robert Collins
Merge the change from Tree.compare to Tree.changes_from.
236
        return InterTree.get(other, self).compare(
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
237
            want_unchanged=want_unchanged,
238
            specific_files=specific_files,
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
239
            extra_trees=extra_trees,
240
            require_versioned=require_versioned,
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
241
            include_root=include_root,
242
            want_unversioned=want_unversioned,
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
243
            )
2012.1.1 by Aaron Bentley
Implement change iterator
244
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
245
    def iter_changes(self, from_tree, include_unchanged=False,
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
246
                     specific_files=None, pb=None, extra_trees=None,
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
247
                     require_versioned=True, want_unversioned=False):
4988.10.6 by John Arbash Meinel
Fix bug #304182 by adding a trivial docstring to Tree.iter_changes
248
        """See InterTree.iter_changes"""
2012.1.5 by Aaron Bentley
Implement specific file id and dangling id handling
249
        intertree = InterTree.get(from_tree, self)
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
250
        return intertree.iter_changes(include_unchanged, specific_files, pb,
7122.6.5 by Jelmer Vernooij
More improvements, add tests.
251
                                      extra_trees, require_versioned,
252
                                      want_unversioned=want_unversioned)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
253
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
254
    def conflicts(self):
255
        """Get a list of the conflicts in the tree.
256
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
257
        Each conflict is an instance of breezy.conflicts.Conflict.
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
258
        """
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
259
        from . import conflicts as _mod_conflicts
2748.2.1 by Lukáš Lalinsky
Return ConflictsList() instead of [] from Tree.conflicts.
260
        return _mod_conflicts.ConflictList()
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
261
2255.7.91 by Robert Collins
Move unknown detection in long status into the delta creation, saving a tree-scan.
262
    def extras(self):
263
        """For trees that can have unversioned files, return all such paths."""
264
        return []
265
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
266
    def get_parent_ids(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
267
        """Get the parent ids for this tree.
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
268
269
        :return: a list of parent ids. [] is returned to indicate
270
        a tree with no parents.
271
        :raises: BzrError if the parents are not known.
272
        """
273
        raise NotImplementedError(self.get_parent_ids)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
274
1 by mbp at sourcefrog
import from baz patch-364
275
    def has_filename(self, filename):
276
        """True if the tree has given filename."""
2818.2.1 by Ian Clatworthy
minor tree & dirstate code cleanups
277
        raise NotImplementedError(self.has_filename)
1 by mbp at sourcefrog
import from baz patch-364
278
2255.7.97 by Robert Collins
Teach delta.report_changes about unversioned files, removing all inventory access during status --short.
279
    def is_ignored(self, filename):
280
        """Check whether the filename is ignored by this tree.
281
282
        :param filename: The relative filename within the tree.
283
        :return: True if the filename is ignored.
284
        """
285
        return False
286
3146.8.16 by Aaron Bentley
Updates from review
287
    def all_file_ids(self):
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
288
        """Iterate through all file ids, including ids for missing files."""
5837.2.1 by Jelmer Vernooij
Deprecate Tree.__iter__.
289
        raise NotImplementedError(self.all_file_ids)
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
290
6825.5.1 by Jelmer Vernooij
Implement Tree.all_versioned_paths.
291
    def all_versioned_paths(self):
292
        """Iterate through all paths, including paths for missing files."""
293
        raise NotImplementedError(self.all_versioned_paths)
294
7450.1.1 by Jelmer Vernooij
Support recurse argument to id2path.
295
    def id2path(self, file_id, recurse='down'):
2255.11.5 by Martin Pool
Tree.id2path should raise NoSuchId, not return None.
296
        """Return the path for a file id.
297
298
        :raises NoSuchId:
299
        """
5777.3.1 by Jelmer Vernooij
Split InventoryTree out of Tree.
300
        raise NotImplementedError(self.id2path)
1 by mbp at sourcefrog
import from baz patch-364
301
7404.3.2 by Jelmer Vernooij
Merge rename of flag to recurse_nested.
302
    def iter_entries_by_dir(self, specific_files=None, recurse_nested=False):
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
303
        """Walk the tree in 'by_dir' order.
304
3363.2.23 by Aaron Bentley
Fix iter_entries_by_dir ordering
305
        This will yield each entry in the tree as a (path, entry) tuple.
306
        The order that they are yielded is:
307
308
        Directories are walked in a depth-first lexicographical order,
309
        however, whenever a directory is reached, all of its direct child
310
        nodes are yielded in  lexicographical order before yielding the
311
        grandchildren.
312
313
        For example, in the tree::
314
315
           a/
316
             b/
317
               c
318
             d/
319
               e
320
           f/
321
             g
322
3363.5.4 by Aaron Bentley
Fix iteration order of iter_entries_by_dir
323
        The yield order (ignoring root) would be::
5891.1.3 by Andrew Bennetts
Move docstring formatting fixes.
324
3363.2.23 by Aaron Bentley
Fix iter_entries_by_dir ordering
325
          a, f, a/b, a/d, a/b/c, a/d/e, f/g
7404.3.1 by Jelmer Vernooij
Add follow_tree_references argument to Tree.iter_entries_by_dir.
326
7404.3.2 by Jelmer Vernooij
Merge rename of flag to recurse_nested.
327
        If recurse_nested is enabled then nested trees are included as if
7404.3.1 by Jelmer Vernooij
Add follow_tree_references argument to Tree.iter_entries_by_dir.
328
        they were a part of the tree. If is disabled then TreeReference
329
        objects (without any children) are yielded.
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
330
        """
5777.3.1 by Jelmer Vernooij
Split InventoryTree out of Tree.
331
        raise NotImplementedError(self.iter_entries_by_dir)
1852.6.9 by Robert Collins
Add more test trees to the tree-implementations tests.
332
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
333
    def iter_child_entries(self, path):
6471.1.2 by Jelmer Vernooij
Add Tree.iter_child_entries.
334
        """Iterate over the children of a directory or tree reference.
335
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
336
        :param path: Path of the directory
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
337
        :raise NoSuchFile: When the path does not exist
6471.1.3 by Jelmer Vernooij
Add Tree.iter_child_entries.
338
        :return: Iterator over entries in the directory
6471.1.2 by Jelmer Vernooij
Add Tree.iter_child_entries.
339
        """
340
        raise NotImplementedError(self.iter_child_entries)
341
7404.2.1 by Jelmer Vernooij
Add a follow_tree_references argument to Tree.list_files.
342
    def list_files(self, include_root=False, from_dir=None, recursive=True,
7404.2.3 by Jelmer Vernooij
s/follow_tree_references/recurse_nested/g
343
                   recurse_nested=False):
5793.2.1 by Jelmer Vernooij
Add Tree.list_files stub.
344
        """List all files in this tree.
345
346
        :param include_root: Whether to include the entry for the tree root
347
        :param from_dir: Directory under which to list files
348
        :param recursive: Whether to list files recursively
7404.2.3 by Jelmer Vernooij
s/follow_tree_references/recurse_nested/g
349
        :param recurse_nested: enter nested trees
7143.19.5 by Jelmer Vernooij
Undo removal of kind.
350
        :return: iterator over tuples of
351
            (path, versioned, kind, inventory entry)
5793.2.1 by Jelmer Vernooij
Add Tree.list_files stub.
352
        """
353
        raise NotImplementedError(self.list_files)
354
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.
355
    def iter_references(self):
4370.3.2 by Ian Clatworthy
apply jam's review feedback
356
        if self.supports_tree_reference():
357
            for path, entry in self.iter_entries_by_dir():
358
                if entry.kind == 'tree-reference':
7350.2.1 by Jelmer Vernooij
Drop file_id return value from Tree.iter_references.
359
                    yield path
2100.3.27 by Aaron Bentley
Enable nested commits
360
7404.2.1 by Jelmer Vernooij
Add a follow_tree_references argument to Tree.list_files.
361
    def get_containing_nested_tree(self, path):
362
        """Find the nested tree that contains a path.
363
364
        :return: tuple with (nested tree and path inside the nested tree)
365
        """
366
        for nested_path in self.iter_references():
367
            nested_path += '/'
368
            if path.startswith(nested_path):
369
                nested_tree = self.get_nested_tree(nested_path)
370
                return nested_tree, path[len(nested_path):]
371
        else:
372
            return None, None
373
7358.4.1 by Jelmer Vernooij
Add Tree.get_nested_tree.
374
    def get_nested_tree(self, path):
375
        """Open the nested tree at the specified path.
376
377
        :param path: Path from which to resolve tree reference.
378
        :return: A Tree object for the nested tree
379
        :raise MissingNestedTree: If the nested tree can not be resolved
380
        """
381
        raise NotImplementedError(self.get_nested_tree)
382
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
383
    def kind(self, path):
2255.2.159 by Martin Pool
reference-trees in dirstate pass all tests.
384
        raise NotImplementedError("Tree subclass %s must implement kind"
7143.15.2 by Jelmer Vernooij
Run autopep8.
385
                                  % self.__class__.__name__)
1465 by Robert Collins
Bugfix the new pull --clobber to not generate spurious conflicts.
386
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
387
    def stored_kind(self, path):
388
        """File kind stored for this path.
3146.8.4 by Aaron Bentley
Eliminate direct use of inventory from transform application
389
3146.8.15 by Aaron Bentley
Cleanup and docs
390
        May not match kind on disk for working trees.  Always available
391
        for versioned files, even when the file itself is missing.
3146.8.4 by Aaron Bentley
Eliminate direct use of inventory from transform application
392
        """
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
393
        return self.kind(path)
3146.8.4 by Aaron Bentley
Eliminate direct use of inventory from transform application
394
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
395
    def path_content_summary(self, path):
396
        """Get a summary of the information about path.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
397
4595.11.2 by Martin Pool
Clarify contract of path_content_summary
398
        All the attributes returned are for the canonical form, not the
399
        convenient form (if content filters are in use.)
400
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
401
        :param path: A relative path within the tree.
402
        :return: A tuple containing kind, size, exec, sha1-or-link.
403
            Kind is always present (see tree.kind()).
7195.5.1 by Martin
Fix remaining whitespace lint in codebase
404
            size is present if kind is file and the size of the
4595.11.13 by Martin Pool
Remove get_kind_and_executable_by_path; go back to using plain path_content_summary
405
                canonical form can be cheaply determined, None otherwise.
2776.1.7 by Robert Collins
* New method on ``bzrlib.tree.Tree`` ``path_content_summary`` provides a
406
            exec is None unless kind is file and the platform supports the 'x'
407
                bit.
408
            sha1-or-link is the link target if kind is symlink, or the sha1 if
409
                it can be obtained without reading the file.
410
        """
411
        raise NotImplementedError(self.path_content_summary)
412
7447.3.3 by Jelmer Vernooij
Fix some blackbox tests.
413
    def get_reference_revision(self, path, branch=None):
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
414
        raise NotImplementedError("Tree subclass %s must implement "
415
                                  "get_reference_revision"
7143.15.2 by Jelmer Vernooij
Run autopep8.
416
                                  % self.__class__.__name__)
2100.3.20 by Aaron Bentley
Implement tree comparison for tree references
417
2012.1.7 by Aaron Bentley
Get tree._iter_changed down to ~ 1 stat per file
418
    def _comparison_data(self, entry, path):
2012.1.15 by Aaron Bentley
Minor tweaks
419
        """Return a tuple of kind, executable, stat_value for a file.
420
421
        entry may be None if there is no inventory entry for the file, but
422
        path must always be supplied.
423
424
        kind is None if there is no file present (even if an inventory id is
425
        present).  executable is False for non-file entries.
426
        """
2012.1.7 by Aaron Bentley
Get tree._iter_changed down to ~ 1 stat per file
427
        raise NotImplementedError(self._comparison_data)
428
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
429
    def get_file(self, path):
430
        """Return a file object for the file path in the tree.
2772.2.1 by Ian Clatworthy
(Ian Clatworthy) Quicker initial commit - skip SHAing twice & skip path lookup as we know it
431
        """
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
432
        raise NotImplementedError(self.get_file)
2255.7.36 by John Arbash Meinel
All trees should implement get_file_mtime()
433
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
434
    def get_file_with_stat(self, path):
435
        """Get a file handle and stat object for path.
4354.4.7 by Aaron Bentley
Move MutableTree.get_file_with_stat to Tree.get_file_with_stat.
436
437
        The default implementation returns (self.get_file, None) for backwards
438
        compatibility.
439
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
440
        :param path: The path of the file.
4354.4.7 by Aaron Bentley
Move MutableTree.get_file_with_stat to Tree.get_file_with_stat.
441
        :return: A tuple (file_handle, stat_value_or_None). If the tree has
442
            no stat facility, or need for a stat cache feedback during commit,
443
            it may return None for the second element of the tuple.
444
        """
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
445
        return (self.get_file(path), None)
4354.4.5 by Aaron Bentley
Ensure Tree.get_file_with_stat is provided.
446
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
447
    def get_file_text(self, path):
3774.1.1 by Aaron Bentley
Test Tree.get_file_text() and supply default implementation.
448
        """Return the byte content of a file.
449
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
450
        :param path: The path of the file.
6006.3.1 by Martin Pool
Start adding ContentFilterTree
451
452
        :returns: A single byte string for the whole file.
3774.1.1 by Aaron Bentley
Test Tree.get_file_text() and supply default implementation.
453
        """
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
454
        with self.get_file(path) as my_file:
3774.1.1 by Aaron Bentley
Test Tree.get_file_text() and supply default implementation.
455
            return my_file.read()
456
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
457
    def get_file_lines(self, path):
3774.1.2 by Aaron Bentley
Test Tree.get_file_lines, provide a default implementation
458
        """Return the content of a file, as lines.
459
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
460
        :param path: The path of the file.
3774.1.2 by Aaron Bentley
Test Tree.get_file_lines, provide a default implementation
461
        """
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
462
        return osutils.split_lines(self.get_file_text(path))
3774.1.2 by Aaron Bentley
Test Tree.get_file_lines, provide a default implementation
463
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
464
    def get_file_verifier(self, path, stat_value=None):
5906.1.2 by Jelmer Vernooij
Add Tree.get_file_verifier.
465
        """Return a verifier for a file.
466
467
        The default implementation returns a sha1.
468
469
        :param path: The path that this file can be found at.
470
            These must point to the same object.
471
        :param stat_value: Optional stat value for the object
472
        :return: Tuple with verifier name and verifier data
473
        """
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
474
        return ("SHA1", self.get_file_sha1(path, stat_value=stat_value))
5906.1.2 by Jelmer Vernooij
Add Tree.get_file_verifier.
475
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
476
    def get_file_sha1(self, path, stat_value=None):
5777.1.3 by Jelmer Vernooij
Add stub for Tree.get_file_sha1.
477
        """Return the SHA1 file for a file.
478
5906.1.4 by Jelmer Vernooij
Add InterTree.file_contents_match.
479
        :note: callers should use get_file_verifier instead
480
            where possible, as the underlying repository implementation may
481
            have quicker access to a non-sha1 verifier.
482
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
483
        :param path: The path that this file can be found at.
5883.1.2 by Jelmer Vernooij
Support stat_value argument to get_file_sha1
484
        :param stat_value: Optional stat value for the object
5777.1.3 by Jelmer Vernooij
Add stub for Tree.get_file_sha1.
485
        """
486
        raise NotImplementedError(self.get_file_sha1)
487
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
488
    def get_file_mtime(self, path):
2255.7.36 by John Arbash Meinel
All trees should implement get_file_mtime()
489
        """Return the modification time for a file.
490
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
491
        :param path: The path that this file can be found at.
2255.7.36 by John Arbash Meinel
All trees should implement get_file_mtime()
492
        """
493
        raise NotImplementedError(self.get_file_mtime)
494
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
495
    def get_file_size(self, path):
3363.3.4 by Aaron Bentley
Add get_file_size to Tree interface
496
        """Return the size of a file in bytes.
497
498
        This applies only to regular files.  If invoked on directories or
499
        symlinks, it will return None.
500
        """
501
        raise NotImplementedError(self.get_file_size)
502
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
503
    def is_executable(self, path):
5777.1.4 by Jelmer Vernooij
Stub for is_executable.
504
        """Check if a file is executable.
505
6809.4.4 by Jelmer Vernooij
Swap arguments for Tree.is_executable.
506
        :param path: The path that this file can be found at.
5777.1.4 by Jelmer Vernooij
Stub for is_executable.
507
        """
508
        raise NotImplementedError(self.is_executable)
509
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
510
    def iter_files_bytes(self, desired_files):
2708.1.6 by Aaron Bentley
Turn extract_files_bytes into an iterator
511
        """Iterate through file contents.
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
512
2708.1.10 by Aaron Bentley
Update docstrings
513
        Files will not necessarily be returned in the order they occur in
514
        desired_files.  No specific order is guaranteed.
515
516
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
517
        value supplied by the caller as part of desired_files.  It should
518
        uniquely identify the file version in the caller's context.  (Examples:
519
        an index number or a TreeTransform trans_id.)
520
521
        bytes_iterator is an iterable of bytestrings for the file.  The
522
        kind of iterable and length of the bytestrings are unspecified, but for
523
        this implementation, it is a tuple containing a single bytestring with
524
        the complete text of the file.
525
6874.2.1 by Jelmer Vernooij
Make Tree.iter_files_bytes() take paths rather than file_ids.
526
        :param desired_files: a list of (path, identifier) pairs
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
527
        """
6874.2.1 by Jelmer Vernooij
Make Tree.iter_files_bytes() take paths rather than file_ids.
528
        for path, identifier in desired_files:
2708.1.10 by Aaron Bentley
Update docstrings
529
            # We wrap the string in a tuple so that we can return an iterable
530
            # of bytestrings.  (Technically, a bytestring is also an iterable
531
            # of bytestrings, but iterating through each character is not
532
            # performant.)
6874.2.1 by Jelmer Vernooij
Make Tree.iter_files_bytes() take paths rather than file_ids.
533
            cur_file = (self.get_file_text(path),)
2708.1.6 by Aaron Bentley
Turn extract_files_bytes into an iterator
534
            yield identifier, cur_file
2708.1.1 by Aaron Bentley
Implement Tree.extract_files
535
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
536
    def get_symlink_target(self, path):
537
        """Get the target for a given path.
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
538
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
539
        It is assumed that the caller already knows that path is referencing
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
540
        a symlink.
5858.1.1 by Jelmer Vernooij
Support optional path argument to Tree.get_symlink_target.
541
        :param path: The path of the file.
2255.2.134 by John Arbash Meinel
Add a tree-test for get_symlink_target
542
        :return: The path the symlink points to.
543
        """
544
        raise NotImplementedError(self.get_symlink_target)
545
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
546
    def annotate_iter(self, path,
3224.1.2 by John Arbash Meinel
Updated the base Tree.annotate_iter() since all implemenations take an optional kwarg.
547
                      default_revision=_mod_revision.CURRENT_REVISION):
2818.2.1 by Ian Clatworthy
minor tree & dirstate code cleanups
548
        """Return an iterator of revision_id, line tuples.
1551.9.18 by Aaron Bentley
Updates from review comments
549
1551.9.16 by Aaron Bentley
Implement Tree.annotate_iter for RevisionTree and WorkingTree
550
        For working trees (and mutable trees in general), the special
551
        revision_id 'current:' will be used for lines that are new in this
552
        tree, e.g. uncommitted changes.
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
553
        :param path: The file to produce an annotated version from
3224.1.2 by John Arbash Meinel
Updated the base Tree.annotate_iter() since all implemenations take an optional kwarg.
554
        :param default_revision: For lines that don't match a basis, mark them
555
            with this revision id. Not all implementations will make use of
556
            this value.
1551.9.16 by Aaron Bentley
Implement Tree.annotate_iter for RevisionTree and WorkingTree
557
        """
558
        raise NotImplementedError(self.annotate_iter)
559
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
560
    def path2id(self, path):
561
        """Return the id for path in this tree."""
5777.3.1 by Jelmer Vernooij
Split InventoryTree out of Tree.
562
        raise NotImplementedError(self.path2id)
1 by mbp at sourcefrog
import from baz patch-364
563
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
564
    def is_versioned(self, path):
565
        """Check whether path is versioned.
566
567
        :param path: Path to check
568
        :return: boolean
569
        """
6856 by Jelmer Vernooij
Merge lp:~jelmer/brz/is-versioned.
570
        return self.path2id(path) is not None
6852.3.1 by Jelmer Vernooij
add Tree.is_versioned.
571
6885.5.3 by Jelmer Vernooij
Add a Tree.find_related_paths_across_trees.
572
    def find_related_paths_across_trees(self, paths, trees=[],
7143.15.2 by Jelmer Vernooij
Run autopep8.
573
                                        require_versioned=True):
6885.5.3 by Jelmer Vernooij
Add a Tree.find_related_paths_across_trees.
574
        """Find related paths in tree corresponding to specified filenames in any
575
        of `lookup_trees`.
576
577
        All matches in all trees will be used, and all children of matched
578
        directories will be used.
579
580
        :param paths: The filenames to find related paths for (if None, returns
581
            None)
582
        :param trees: The trees to find file_ids within
583
        :param require_versioned: if true, all specified filenames must occur in
584
            at least one tree.
585
        :return: a set of paths for the specified filenames and their children
586
            in `tree`
587
        """
6885.5.4 by Jelmer Vernooij
Move paths2ids to InventoryTree.
588
        raise NotImplementedError(self.find_related_paths_across_trees)
3363.12.2 by Aaron Bentley
Implement tree.iter_children to instead of adjusting InventoryEntry handling
589
1543.1.1 by Denys Duchier
lock operations for trees - use them for diff
590
    def lock_read(self):
5200.3.6 by Robert Collins
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.
591
        """Lock this tree for multiple read only operations.
6468.3.1 by Jelmer Vernooij
Use iter_children in a couple more places.
592
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
593
        :return: A breezy.lock.LogicalLockResult.
5200.3.6 by Robert Collins
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.
594
        """
6754.8.3 by Jelmer Vernooij
Use context manager in decorators.
595
        return lock.LogicalLockResult(self.unlock)
1543.1.1 by Denys Duchier
lock operations for trees - use them for diff
596
1908.11.1 by Robert Collins
Add a new method ``Tree.revision_tree`` which allows access to cached
597
    def revision_tree(self, revision_id):
598
        """Obtain a revision tree for the revision revision_id.
599
600
        The intention of this method is to allow access to possibly cached
601
        tree data. Implementors of this method should raise NoSuchRevision if
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
602
        the tree is not locally available, even if they could obtain the
603
        tree via a repository or some other means. Callers are responsible
1908.11.1 by Robert Collins
Add a new method ``Tree.revision_tree`` which allows access to cached
604
        for finding the ultimate source for a revision tree.
605
606
        :param revision_id: The revision_id of the requested tree.
607
        :return: A Tree.
608
        :raises: NoSuchRevision if the tree cannot be obtained.
609
        """
610
        raise errors.NoSuchRevisionInTree(self, revision_id)
611
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
612
    def unknowns(self):
613
        """What files are present in this tree and unknown.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
614
1773.2.1 by Robert Collins
Teach all trees about unknowns, conflicts and get_parent_ids.
615
        :return: an iterator over the unknown files.
616
        """
617
        return iter([])
618
1543.1.1 by Denys Duchier
lock operations for trees - use them for diff
619
    def unlock(self):
620
        pass
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
621
622
    def filter_unversioned_files(self, paths):
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
623
        """Filter out paths that are versioned.
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
624
625
        :return: set of paths.
626
        """
6974.1.1 by Jelmer Vernooij
Provide a sensible default implementaton of Tree.filter_unversioned_files.
627
        # NB: we specifically *don't* call self.has_filename, because for
628
        # WorkingTrees that can indicate files that exist on disk but that
629
        # are not versioned.
6974.1.2 by Jelmer Vernooij
Avoid unnecessary parentheses.
630
        return set(p for p in paths if not self.is_versioned(p))
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
631
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
632
    def walkdirs(self, prefix=""):
633
        """Walk the contents of this tree from path down.
634
635
        This yields all the data about the contents of a directory at a time.
636
        After each directory has been yielded, if the caller has mutated the
637
        list to exclude some directories, they are then not descended into.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
638
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
639
        The data yielded is of the form:
1852.15.7 by Robert Collins
Start testing behaviour of unknowns in WorkingTree.walkdirs.
640
        ((directory-relpath, directory-path-from-root, directory-fileid),
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
641
        [(relpath, basename, kind, lstat, path_from_tree_root, file_id,
1852.15.7 by Robert Collins
Start testing behaviour of unknowns in WorkingTree.walkdirs.
642
          versioned_kind), ...]),
643
         - directory-relpath is the containing dirs relpath from prefix
644
         - directory-path-from-root is the containing dirs path from /
645
         - directory-fileid is the id of the directory if it is versioned.
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
646
         - relpath is the relative path within the subtree being walked.
647
         - basename is the basename
648
         - kind is the kind of the file now. If unknonwn then the file is not
649
           present within the tree - but it may be recorded as versioned. See
650
           versioned_kind.
651
         - lstat is the stat data *if* the file was statted.
652
         - path_from_tree_root is the path from the root of the tree.
2818.2.1 by Ian Clatworthy
minor tree & dirstate code cleanups
653
         - file_id is the file_id if the entry is versioned.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
654
         - versioned_kind is the kind of the file as last recorded in the
1852.15.3 by Robert Collins
Add a first-cut Tree.walkdirs method.
655
           versioning system. If 'unknown' the file is not versioned.
656
        One of 'kind' and 'versioned_kind' must not be 'unknown'.
657
658
        :param prefix: Start walking from prefix within the tree rather than
659
        at the root. This allows one to walk a subtree but get paths that are
660
        relative to a tree rooted higher up.
661
        :return: an iterator over the directory data.
662
        """
663
        raise NotImplementedError(self.walkdirs)
664
3368.2.45 by Ian Clatworthy
add and use supports_content_filtering API
665
    def supports_content_filtering(self):
666
        return False
667
6883.5.3 by Jelmer Vernooij
Add find_previous_path.
668
    def _content_filter_stack(self, path=None):
3368.2.45 by Ian Clatworthy
add and use supports_content_filtering API
669
        """The stack of content filters for a path if filtering is supported.
3368.2.47 by Ian Clatworthy
merge bzr.dev r4042
670
3368.2.4 by Ian Clatworthy
make content filter lookup a tree responsibility
671
        Readers will be applied in first-to-last order.
672
        Writers will be applied in last-to-first order.
3368.2.5 by Ian Clatworthy
incorporate jameinel's review feedback
673
        Either the path or the file-id needs to be provided.
674
675
        :param path: path relative to the root of the tree
676
            or None if unknown
677
        :return: the list of filters - [] if there are none
3368.2.4 by Ian Clatworthy
make content filter lookup a tree responsibility
678
        """
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
679
        from . import debug, filters
3368.2.16 by Ian Clatworthy
add real implementation of Tree.get_filter_stack
680
        filter_pref_names = filters._get_registered_names()
681
        if len(filter_pref_names) == 0:
682
            return []
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
683
        prefs = next(self.iter_search_rules([path], filter_pref_names))
3368.2.30 by Ian Clatworthy
add -Dfilters support
684
        stk = filters._get_filter_stack_for(prefs)
685
        if 'filters' in debug.debug_flags:
7143.15.2 by Jelmer Vernooij
Run autopep8.
686
            trace.note(
687
                gettext("*** {0} content-filter: {1} => {2!r}").format(path, prefs, stk))
3368.2.30 by Ian Clatworthy
add -Dfilters support
688
        return stk
3368.2.4 by Ian Clatworthy
make content filter lookup a tree responsibility
689
3368.2.45 by Ian Clatworthy
add and use supports_content_filtering API
690
    def _content_filter_stack_provider(self):
691
        """A function that returns a stack of ContentFilters.
692
693
        The function takes a path (relative to the top of the tree) and a
694
        file-id as parameters.
695
696
        :return: None if content filtering is not supported by this tree.
697
        """
698
        if self.supports_content_filtering():
7476.3.1 by Jelmer Vernooij
Disable file ids for content filter stack.
699
            return self._content_filter_stack
3368.2.45 by Ian Clatworthy
add and use supports_content_filtering API
700
        else:
701
            return None
702
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
703
    def iter_search_rules(self, path_names, pref_names=None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
704
                          _default_searcher=None):
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
705
        """Find the preferences for filenames in a tree.
706
707
        :param path_names: an iterable of paths to find attributes for.
708
          Paths are given relative to the root of the tree.
709
        :param pref_names: the list of preferences to lookup - None for all
710
        :param _default_searcher: private parameter to assist testing - don't use
711
        :return: an iterator of tuple sequences, one per path-name.
712
          See _RulesSearcher.get_items for details on the tuple sequence.
713
        """
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
714
        from . import rules
4324.4.1 by Marius Kruger
Make it possible to blackboxtest rules
715
        if _default_searcher is None:
716
            _default_searcher = rules._per_user_searcher
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
717
        searcher = self._get_rules_searcher(_default_searcher)
718
        if searcher is not None:
3398.1.34 by Ian Clatworthy
changed API design as requested by jam during review
719
            if pref_names is not None:
720
                for path in path_names:
721
                    yield searcher.get_selected_items(path, pref_names)
722
            else:
723
                for path in path_names:
724
                    yield searcher.get_items(path)
3398.1.24 by Ian Clatworthy
make iter_search_rules a tree method
725
726
    def _get_rules_searcher(self, default_searcher):
727
        """Get the RulesSearcher for this tree given the default one."""
728
        searcher = default_searcher
729
        return searcher
730
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
731
    def archive(self, format, name, root='', subdir=None,
732
                force_mtime=None):
6968.2.1 by Jelmer Vernooij
Add Tree.archive, which streams an archived version of a tree.
733
        """Create an archive of this tree.
734
6995 by Jelmer Vernooij
Merge lp:~jelmer/brz/hpss-archive.
735
        :param format: Format name (e.g. 'tar')
6968.2.1 by Jelmer Vernooij
Add Tree.archive, which streams an archived version of a tree.
736
        :param name: target file name
737
        :param root: Root directory name (or None)
738
        :param subdir: Subdirectory to export (or None)
739
        :return: Iterator over archive chunks
740
        """
6968.2.10 by Jelmer Vernooij
Review comments.
741
        from .archive import create_archive
6968.2.1 by Jelmer Vernooij
Add Tree.archive, which streams an archived version of a tree.
742
        with self.lock_read():
6968.2.7 by Jelmer Vernooij
Add breezy.archive.
743
            return create_archive(format, self, name, root,
7143.15.2 by Jelmer Vernooij
Run autopep8.
744
                                  subdir, force_mtime=force_mtime)
6968.2.1 by Jelmer Vernooij
Add Tree.archive, which streams an archived version of a tree.
745
6929.3.3 by Jelmer Vernooij
Add Tree.versionable_kind.
746
    @classmethod
747
    def versionable_kind(cls, kind):
748
        """Check if this tree support versioning a specific file kind."""
749
        return (kind in ('file', 'directory', 'symlink', 'tree-reference'))
750
7490.77.2 by Jelmer Vernooij
Split out git and bzr-specific transforms.
751
    def preview_transform(self, pb=None):
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
752
        """Obtain a transform object."""
7490.77.2 by Jelmer Vernooij
Split out git and bzr-specific transforms.
753
        raise NotImplementedError(self.preview_transform)
754
1852.7.1 by Robert Collins
Move RevisionTree out of tree.py.
755
1852.8.2 by Robert Collins
Add InterTree class to represent InterTree operations.
756
class InterTree(InterObject):
757
    """This class represents operations taking place between two Trees.
758
759
    Its instances have methods like 'compare' and contain references to the
760
    source and target trees these operations are to be carried out on.
761
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
762
    Clients of breezy should not need to use InterTree directly, rather they
1852.8.2 by Robert Collins
Add InterTree class to represent InterTree operations.
763
    should use the convenience methods on Tree such as 'Tree.compare()' which
764
    will pass through to InterTree as appropriate.
765
    """
766
4585.1.18 by Jelmer Vernooij
Add note saying that InterTree will not be tested if the from and to formats are not set.
767
    # Formats that will be used to test this InterTree. If both are
768
    # None, this InterTree will not be tested (e.g. because a complex
769
    # setup is required)
770
    _matching_from_tree_format = None
771
    _matching_to_tree_format = None
772
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
773
    _optimisers = []
1852.8.2 by Robert Collins
Add InterTree class to represent InterTree operations.
774
5837.1.2 by Jelmer Vernooij
Register defaults as first optimiser for existing Inter objects.
775
    @classmethod
776
    def is_compatible(kls, source, target):
777
        # The default implementation is naive and uses the public API, so
778
        # it works for all trees.
779
        return True
780
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
781
    def compare(self, want_unchanged=False, specific_files=None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
782
                extra_trees=None, require_versioned=False, include_root=False,
783
                want_unversioned=False):
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
784
        """Return the changes from source to target.
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
785
786
        :return: A TreeDelta.
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
787
        :param specific_files: An optional list of file paths to restrict the
788
            comparison to. When mapping filenames to ids, all matches in all
789
            trees (including optional extra_trees) are used, and all children of
790
            matched directories are included.
791
        :param want_unchanged: An optional boolean requesting the inclusion of
792
            unchanged entries in the result.
793
        :param extra_trees: An optional list of additional trees to use when
794
            mapping the contents of specific_files (paths) to file_ids.
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
795
        :param require_versioned: An optional boolean (defaults to False). When
796
            supplied and True all the 'specific_files' must be versioned, or
797
            a PathsNotVersionedError will be thrown.
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
798
        :param want_unversioned: Scan for unversioned paths.
1852.8.3 by Robert Collins
Implement an InterTreeTestProvider and a trivial test_compare test case.
799
        """
7490.79.2 by Jelmer Vernooij
Add Tree.preview_transform.
800
        from . import delta
2255.2.105 by Robert Collins
Unfuck InterTree.compare which I broke with the paths2ids implementation.
801
        trees = (self.source,)
1852.11.1 by Robert Collins
Deprecate compare_trees and move its body to InterTree.changes_from.
802
        if extra_trees is not None:
803
            trees = trees + tuple(extra_trees)
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
804
        with self.lock_read():
805
            return delta._compare_trees(self.source, self.target, want_unchanged,
7143.15.2 by Jelmer Vernooij
Run autopep8.
806
                                        specific_files, include_root, extra_trees=extra_trees,
807
                                        require_versioned=require_versioned,
808
                                        want_unversioned=want_unversioned)
2012.1.1 by Aaron Bentley
Implement change iterator
809
3254.1.1 by Aaron Bentley
Make Tree.iter_changes a public method
810
    def iter_changes(self, include_unchanged=False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
811
                     specific_files=None, pb=None, extra_trees=[],
812
                     require_versioned=True, want_unversioned=False):
2012.1.1 by Aaron Bentley
Implement change iterator
813
        """Generate an iterator of changes between trees.
814
815
        A tuple is returned:
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
816
        (file_id, (path_in_source, path_in_target),
817
         changed_content, versioned, parent, name, kind,
2012.1.1 by Aaron Bentley
Implement change iterator
818
         executable)
819
2255.7.96 by Robert Collins
Change _iter_changes interface to yield both old and new paths.
820
        Changed_content is True if the file's content has changed.  This
821
        includes changes to its kind, and to a symlink's target.
2012.1.1 by Aaron Bentley
Implement change iterator
822
2012.1.15 by Aaron Bentley
Minor tweaks
823
        versioned, parent, name, kind, executable are tuples of (from, to).
824
        If a file is missing in a tree, its kind is None.
2012.1.1 by Aaron Bentley
Implement change iterator
825
2255.2.122 by Robert Collins
Alter intertree implementation tests to let dirstate inter-trees be correctly parameterised.
826
        Iteration is done in parent-to-child order, relative to the target
827
        tree.
2255.2.149 by Robert Collins
Crufty but existing _iter_changes implementation for WorkingTreeFormat4.
828
829
        There is no guarantee that all paths are in sorted order: the
830
        requirement to expand the search due to renames may result in children
831
        that should be found early being found late in the search, after
832
        lexically later results have been returned.
833
        :param require_versioned: Raise errors.PathsNotVersionedError if a
834
            path in the specific_files list is not versioned in one of
835
            source, target or extra_trees.
4570.2.3 by Robert Collins
Change the way iter_changes treats specific files to prevent InconsistentDeltas.
836
        :param specific_files: An optional list of file paths to restrict the
837
            comparison to. When mapping filenames to ids, all matches in all
838
            trees (including optional extra_trees) are used, and all children
839
            of matched directories are included. The parents in the target tree
840
            of the specific files up to and including the root of the tree are
841
            always evaluated for changes too.
2255.7.85 by Robert Collins
Teach _iter_changes to gather unversioned path details upon request.
842
        :param want_unversioned: Should unversioned files be returned in the
843
            output. An unversioned file is defined as one with (False, False)
844
            for the versioned pair.
2012.1.1 by Aaron Bentley
Implement change iterator
845
        """
7467.4.15 by Jelmer Vernooij
Split out InventoryInterTree.
846
        raise NotImplementedError(self.iter_changes)
3514.3.1 by John Arbash Meinel
Start working on a special walker that can iterate several trees at once.
847
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
848
    def file_content_matches(
6883.1.3 by Jelmer Vernooij
Add test, make file_id optional.
849
            self, source_path, target_path,
6883.1.2 by Jelmer Vernooij
Fix order.
850
            source_stat=None, target_stat=None):
5906.1.8 by Jelmer Vernooij
Tests.
851
        """Check if two files are the same in the source and target trees.
852
853
        This only checks that the contents of the files are the same,
854
        it does not touch anything else.
855
6883.7.1 by Jelmer Vernooij
Swap order of arguments to Tree.file_content_matches.
856
        :param source_path: Path of the file in the source tree
857
        :param target_path: Path of the file in the target tree
5906.1.8 by Jelmer Vernooij
Tests.
858
        :param source_stat: Optional stat value of the file in the source tree
859
        :param target_stat: Optional stat value of the file in the target tree
860
        :return: Boolean indicating whether the files have the same contents
861
        """
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
862
        with self.lock_read():
863
            source_verifier_kind, source_verifier_data = (
7143.15.15 by Jelmer Vernooij
Merge trunk.
864
                self.source.get_file_verifier(source_path, source_stat))
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
865
            target_verifier_kind, target_verifier_data = (
866
                self.target.get_file_verifier(
7141.7.1 by Jelmer Vernooij
Get rid of file_ids in most of Tree.
867
                    target_path, target_stat))
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
868
            if source_verifier_kind == target_verifier_kind:
869
                return (source_verifier_data == target_verifier_data)
870
            # Fall back to SHA1 for now
871
            if source_verifier_kind != "SHA1":
872
                source_sha1 = self.source.get_file_sha1(
7325.1.1 by Jelmer Vernooij
Get rid of file_id arguments to get_file_sha1.
873
                    source_path, source_stat)
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
874
            else:
875
                source_sha1 = source_verifier_data
876
            if target_verifier_kind != "SHA1":
877
                target_sha1 = self.target.get_file_sha1(
7325.1.1 by Jelmer Vernooij
Get rid of file_id arguments to get_file_sha1.
878
                    target_path, target_stat)
6754.8.16 by Jelmer Vernooij
Get rid of all uses of needs_read_lock
879
            else:
880
                target_sha1 = target_verifier_data
881
            return (source_sha1 == target_sha1)
3514.3.1 by John Arbash Meinel
Start working on a special walker that can iterate several trees at once.
882
7357.1.4 by Jelmer Vernooij
Merge trunk.
883
    def find_target_path(self, path, recurse='none'):
7357.1.1 by Jelmer Vernooij
Move logic for find_previous_path onto InterTree.
884
        """Find target tree path.
885
886
        :param path: Path to search for (exists in source)
887
        :return: path in target, or None if there is no equivalent path.
888
        :raise NoSuchFile: If the path doesn't exist in source
889
        """
7467.4.21 by Jelmer Vernooij
Move find_{source,target}_path as well.
890
        raise NotImplementedError(self.find_target_path)
7357.1.1 by Jelmer Vernooij
Move logic for find_previous_path onto InterTree.
891
7357.1.6 by Jelmer Vernooij
Add find_source_path.
892
    def find_source_path(self, path, recurse='none'):
893
        """Find the source tree path.
894
895
        :param path: Path to search for (exists in target)
896
        :return: path in source, or None if there is no equivalent path.
897
        :raise NoSuchFile: if the path doesn't exist in target
898
        """
7467.4.21 by Jelmer Vernooij
Move find_{source,target}_path as well.
899
        raise NotImplementedError(self.find_source_path)
7357.1.6 by Jelmer Vernooij
Add find_source_path.
900
7357.1.4 by Jelmer Vernooij
Merge trunk.
901
    def find_target_paths(self, paths, recurse='none'):
7357.1.1 by Jelmer Vernooij
Move logic for find_previous_path onto InterTree.
902
        """Find target tree paths.
903
904
        :param paths: Iterable over paths in target to search for
905
        :return: Dictionary mapping from source paths to paths in target , or
906
            None if there is no equivalent path.
907
        """
908
        ret = {}
909
        for path in paths:
7357.1.4 by Jelmer Vernooij
Merge trunk.
910
            ret[path] = self.find_target_path(path, recurse=recurse)
7357.1.1 by Jelmer Vernooij
Move logic for find_previous_path onto InterTree.
911
        return ret
912
7357.1.6 by Jelmer Vernooij
Add find_source_path.
913
    def find_source_paths(self, paths, recurse='none'):
914
        """Find source tree paths.
915
916
        :param paths: Iterable over paths in target to search for
917
        :return: Dictionary mapping from target paths to paths in source, or
918
            None if there is no equivalent path.
919
        """
920
        ret = {}
921
        for path in paths:
922
            ret[path] = self.find_source_path(path, recurse=recurse)
923
        return ret
924
7143.15.2 by Jelmer Vernooij
Run autopep8.
925
7458.1.1 by Jelmer Vernooij
Support importing Git submodules as tree references.
926
def find_previous_paths(from_tree, to_tree, paths, recurse='none'):
6883.5.2 by Jelmer Vernooij
Add find_previous_paths call.
927
    """Find previous tree paths.
928
929
    :param from_tree: From tree
930
    :param to_tree: To tree
7324.2.1 by Jelmer Vernooij
Use find_previous_path in InterTree.iter_changes.
931
    :param paths: Iterable over paths in from_tree to search for
6883.5.2 by Jelmer Vernooij
Add find_previous_paths call.
932
    :return: Dictionary mapping from from_tree paths to paths in to_tree, or
933
        None if there is no equivalent path.
934
    """
7357.1.6 by Jelmer Vernooij
Add find_source_path.
935
    return InterTree.get(to_tree, from_tree).find_source_paths(paths, recurse=recurse)
7357.1.4 by Jelmer Vernooij
Merge trunk.
936
937
938
def find_previous_path(from_tree, to_tree, path, recurse='none'):
6883.5.3 by Jelmer Vernooij
Add find_previous_path.
939
    """Find previous tree path.
940
941
    :param from_tree: From tree
942
    :param to_tree: To tree
7324.2.1 by Jelmer Vernooij
Use find_previous_path in InterTree.iter_changes.
943
    :param path: Path to search for (exists in from_tree)
6883.5.3 by Jelmer Vernooij
Add find_previous_path.
944
    :return: path in to_tree, or None if there is no equivalent path.
7350.5.1 by Jelmer Vernooij
Remove file_id from transform's create_from_tree, get filter tree paths by path.
945
    :raise NoSuchFile: If the path doesn't exist in from_tree
6883.5.3 by Jelmer Vernooij
Add find_previous_path.
946
    """
7357.1.6 by Jelmer Vernooij
Add find_source_path.
947
    return InterTree.get(to_tree, from_tree).find_source_path(
7357.1.4 by Jelmer Vernooij
Merge trunk.
948
        path, recurse=recurse)
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
949
950
7104.3.4 by Jelmer Vernooij
Fix tests.
951
def get_canonical_path(tree, path, normalize):
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
952
    """Find the canonical path of an item, ignoring case.
953
954
    :param tree: Tree to traverse
955
    :param path: Case-insensitive path to look up
7104.3.4 by Jelmer Vernooij
Fix tests.
956
    :param normalize: Function to normalize a filename for comparison
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
957
    :return: The canonical path
958
    """
959
    # go walkin...
960
    cur_path = ''
961
    bit_iter = iter(path.split("/"))
962
    for elt in bit_iter:
7104.3.4 by Jelmer Vernooij
Fix tests.
963
        lelt = normalize(elt)
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
964
        new_path = None
965
        try:
7192.5.2 by Jelmer Vernooij
Fixes.
966
            for child in tree.iter_child_entries(cur_path):
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
967
                try:
968
                    if child.name == elt:
969
                        # if we found an exact match, we can stop now; if
970
                        # we found an approximate match we need to keep
971
                        # searching because there might be an exact match
972
                        # later.
973
                        new_path = osutils.pathjoin(cur_path, child.name)
974
                        break
7104.3.4 by Jelmer Vernooij
Fix tests.
975
                    elif normalize(child.name) == lelt:
7104.3.3 by Jelmer Vernooij
Move get_canonical_paths to WorkingTree.
976
                        new_path = osutils.pathjoin(cur_path, child.name)
977
                except errors.NoSuchId:
978
                    # before a change is committed we can see this error...
979
                    continue
980
        except errors.NotADirectory:
981
            pass
982
        if new_path:
983
            cur_path = new_path
984
        else:
985
            # got to the end of this directory and no entries matched.
986
            # Return what matched so far, plus the rest as specified.
987
            cur_path = osutils.pathjoin(cur_path, elt, *list(bit_iter))
988
            break
989
    return cur_path