/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tree classes, representing directory at point in time.
18
18
"""
19
19
 
20
 
from .lazy_import import lazy_import
21
 
lazy_import(globals(), """
22
 
from breezy.i18n import gettext
23
 
""")
 
20
import os
 
21
from collections import deque
 
22
from cStringIO import StringIO
24
23
 
25
 
from . import (
26
 
    errors,
27
 
    lock,
 
24
import bzrlib
 
25
from bzrlib import (
 
26
    delta,
28
27
    osutils,
29
 
    revision as _mod_revision,
30
 
    trace,
 
28
    symbol_versioning,
31
29
    )
32
 
from .inter import InterObject
33
 
 
34
 
 
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
 
 
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
 
 
53
 
class TreeEntry(object):
54
 
    """An entry that implements the minimum interface used by commands.
55
 
    """
56
 
 
57
 
    __slots__ = []
58
 
 
59
 
    def __eq__(self, other):
60
 
        # yes, this is ugly, TODO: best practice __eq__ style.
61
 
        return (isinstance(other, TreeEntry)
62
 
                and other.__class__ == self.__class__)
63
 
 
64
 
    kind = None
65
 
 
66
 
    def kind_character(self):
67
 
        return "???"
68
 
 
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
 
 
77
 
 
78
 
class TreeDirectory(TreeEntry):
79
 
    """See TreeEntry. This is a directory in a working tree."""
80
 
 
81
 
    __slots__ = []
82
 
 
83
 
    kind = 'directory'
84
 
 
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
 
 
92
 
    __slots__ = []
93
 
 
94
 
    kind = 'file'
95
 
 
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
 
 
103
 
    __slots__ = []
104
 
 
105
 
    kind = 'symlink'
106
 
 
107
 
    def kind_character(self):
108
 
        return ''
109
 
 
110
 
 
111
 
class TreeReference(TreeEntry):
112
 
    """See TreeEntry. This is a reference to a nested tree in a working tree."""
113
 
 
114
 
    __slots__ = []
115
 
 
116
 
    kind = 'tree-reference'
117
 
 
118
 
    def kind_character(self):
119
 
        return '+'
120
 
 
121
 
 
122
 
class TreeChange(object):
123
 
    """Describes the changes between the same item in two different trees."""
124
 
 
125
 
    __slots__ = ['file_id', 'path', 'changed_content', 'versioned', 'parent_id',
126
 
                 'name', 'kind', 'executable', 'copied']
127
 
 
128
 
    def __init__(self, file_id, path, changed_content, versioned, parent_id,
129
 
                 name, kind, executable, copied=False):
130
 
        self.file_id = file_id
131
 
        self.path = path
132
 
        self.changed_content = changed_content
133
 
        self.versioned = versioned
134
 
        self.parent_id = parent_id
135
 
        self.name = name
136
 
        self.kind = kind
137
 
        self.executable = executable
138
 
        self.copied = copied
139
 
 
140
 
    def __repr__(self):
141
 
        return "%s%r" % (self.__class__.__name__, self._as_tuple())
142
 
 
143
 
    def __len__(self):
144
 
        return len(self.__slots__)
145
 
 
146
 
    def _as_tuple(self):
147
 
        return (self.file_id, self.path, self.changed_content, self.versioned,
148
 
                self.parent_id, self.name, self.kind, self.executable, self.copied)
149
 
 
150
 
    def __eq__(self, other):
151
 
        if isinstance(other, TreeChange):
152
 
            return self._as_tuple() == other._as_tuple()
153
 
        if isinstance(other, tuple):
154
 
            return self._as_tuple() == other
155
 
        return False
156
 
 
157
 
    def __lt__(self, other):
158
 
        return self._as_tuple() < other._as_tuple()
159
 
 
160
 
    def meta_modified(self):
161
 
        if self.versioned == (True, True):
162
 
            return (self.executable[0] != self.executable[1])
163
 
        return False
164
 
 
165
 
    def is_reparented(self):
166
 
        return self.parent_id[0] != self.parent_id[1]
167
 
 
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),
173
 
            (self.executable[0], None),
174
 
            copied=False)
 
30
from bzrlib.decorators import needs_read_lock
 
31
from bzrlib.errors import BzrError, BzrCheckError
 
32
from bzrlib import errors
 
33
from bzrlib.inventory import Inventory, InventoryFile
 
34
from bzrlib.inter import InterObject
 
35
from bzrlib.osutils import fingerprint_file
 
36
import bzrlib.revision
 
37
from bzrlib.trace import mutter, note
175
38
 
176
39
 
177
40
class Tree(object):
178
41
    """Abstract file tree.
179
42
 
180
43
    There are several subclasses:
181
 
 
 
44
    
182
45
    * `WorkingTree` exists as files on disk editable by the user.
183
46
 
184
47
    * `RevisionTree` is a tree as recorded at some point in the past.
185
48
 
 
49
    Trees contain an `Inventory` object, and also know how to retrieve
 
50
    file texts mentioned in the inventory, either from a working
 
51
    directory or from a store.
 
52
 
 
53
    It is possible for trees to contain files that are not described
 
54
    in their inventory or vice versa; for this use `filenames()`.
 
55
 
186
56
    Trees can be compared, etc, regardless of whether they are working
187
57
    trees or versioned trees.
188
58
    """
189
 
 
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
 
 
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
 
 
206
 
    def supports_symlinks(self):
207
 
        """Does this tree support symbolic links?
208
 
        """
209
 
        return osutils.has_symlinks()
210
 
 
 
59
    
211
60
    def changes_from(self, other, want_unchanged=False, specific_files=None,
212
 
                     extra_trees=None, require_versioned=False, include_root=False,
213
 
                     want_unversioned=False):
 
61
        extra_trees=None, require_versioned=False, include_root=False,
 
62
        want_unversioned=False):
214
63
        """Return a TreeDelta of the changes from other to this tree.
215
64
 
216
65
        :param other: A tree to compare with.
221
70
        :param want_unchanged: An optional boolean requesting the inclusion of
222
71
            unchanged entries in the result.
223
72
        :param extra_trees: An optional list of additional trees to use when
224
 
            mapping the contents of specific_files (paths) to their identities.
 
73
            mapping the contents of specific_files (paths) to file_ids.
225
74
        :param require_versioned: An optional boolean (defaults to False). When
226
75
            supplied and True all the 'specific_files' must be versioned, or
227
76
            a PathsNotVersionedError will be thrown.
228
77
        :param want_unversioned: Scan for unversioned paths.
229
78
 
230
 
        The comparison will be performed by an InterTree object looked up on
 
79
        The comparison will be performed by an InterTree object looked up on 
231
80
        self and other.
232
81
        """
233
82
        # Martin observes that Tree.changes_from returns a TreeDelta and this
242
91
            want_unversioned=want_unversioned,
243
92
            )
244
93
 
245
 
    def iter_changes(self, from_tree, include_unchanged=False,
 
94
    def _iter_changes(self, from_tree, include_unchanged=False,
246
95
                     specific_files=None, pb=None, extra_trees=None,
247
96
                     require_versioned=True, want_unversioned=False):
248
 
        """See InterTree.iter_changes"""
249
97
        intertree = InterTree.get(from_tree, self)
250
 
        return intertree.iter_changes(include_unchanged, specific_files, pb,
251
 
                                      extra_trees, require_versioned,
252
 
                                      want_unversioned=want_unversioned)
253
 
 
 
98
        return intertree._iter_changes(include_unchanged, specific_files, pb,
 
99
            extra_trees, require_versioned, want_unversioned=want_unversioned)
 
100
    
254
101
    def conflicts(self):
255
102
        """Get a list of the conflicts in the tree.
256
103
 
257
 
        Each conflict is an instance of breezy.conflicts.Conflict.
 
104
        Each conflict is an instance of bzrlib.conflicts.Conflict.
258
105
        """
259
 
        from . import conflicts as _mod_conflicts
260
 
        return _mod_conflicts.ConflictList()
 
106
        return []
261
107
 
262
108
    def extras(self):
263
109
        """For trees that can have unversioned files, return all such paths."""
264
110
        return []
265
111
 
266
112
    def get_parent_ids(self):
267
 
        """Get the parent ids for this tree.
 
113
        """Get the parent ids for this tree. 
268
114
 
269
115
        :return: a list of parent ids. [] is returned to indicate
270
116
        a tree with no parents.
271
117
        :raises: BzrError if the parents are not known.
272
118
        """
273
119
        raise NotImplementedError(self.get_parent_ids)
274
 
 
 
120
    
275
121
    def has_filename(self, filename):
276
122
        """True if the tree has given filename."""
277
 
        raise NotImplementedError(self.has_filename)
 
123
        raise NotImplementedError()
 
124
 
 
125
    def has_id(self, file_id):
 
126
        file_id = osutils.safe_file_id(file_id)
 
127
        return self.inventory.has_id(file_id)
 
128
 
 
129
    __contains__ = has_id
 
130
 
 
131
    def has_or_had_id(self, file_id):
 
132
        file_id = osutils.safe_file_id(file_id)
 
133
        if file_id == self.inventory.root.file_id:
 
134
            return True
 
135
        return self.inventory.has_id(file_id)
278
136
 
279
137
    def is_ignored(self, filename):
280
138
        """Check whether the filename is ignored by this tree.
284
142
        """
285
143
        return False
286
144
 
287
 
    def all_file_ids(self):
288
 
        """Iterate through all file ids, including ids for missing files."""
289
 
        raise NotImplementedError(self.all_file_ids)
290
 
 
291
 
    def all_versioned_paths(self):
292
 
        """Iterate through all paths, including paths for missing files."""
293
 
        raise NotImplementedError(self.all_versioned_paths)
294
 
 
295
 
    def id2path(self, file_id, recurse='down'):
 
145
    def __iter__(self):
 
146
        return iter(self.inventory)
 
147
 
 
148
    def id2path(self, file_id):
296
149
        """Return the path for a file id.
297
150
 
298
151
        :raises NoSuchId:
299
152
        """
300
 
        raise NotImplementedError(self.id2path)
301
 
 
302
 
    def iter_entries_by_dir(self, specific_files=None, recurse_nested=False):
 
153
        file_id = osutils.safe_file_id(file_id)
 
154
        return self.inventory.id2path(file_id)
 
155
 
 
156
    def is_control_filename(self, filename):
 
157
        """True if filename is the name of a control file in this tree.
 
158
        
 
159
        :param filename: A filename within the tree. This is a relative path
 
160
        from the root of this tree.
 
161
 
 
162
        This is true IF and ONLY IF the filename is part of the meta data
 
163
        that bzr controls in this tree. I.E. a random .bzr directory placed
 
164
        on disk will not be a control file for this tree.
 
165
        """
 
166
        return self.bzrdir.is_control_filename(filename)
 
167
 
 
168
    @needs_read_lock
 
169
    def iter_entries_by_dir(self, specific_file_ids=None):
303
170
        """Walk the tree in 'by_dir' order.
304
171
 
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
 
 
323
 
        The yield order (ignoring root) would be::
324
 
 
325
 
          a, f, a/b, a/d, a/b/c, a/d/e, f/g
326
 
 
327
 
        If recurse_nested is enabled then nested trees are included as if
328
 
        they were a part of the tree. If is disabled then TreeReference
329
 
        objects (without any children) are yielded.
330
 
        """
331
 
        raise NotImplementedError(self.iter_entries_by_dir)
332
 
 
333
 
    def iter_child_entries(self, path):
334
 
        """Iterate over the children of a directory or tree reference.
335
 
 
336
 
        :param path: Path of the directory
337
 
        :raise NoSuchFile: When the path does not exist
338
 
        :return: Iterator over entries in the directory
339
 
        """
340
 
        raise NotImplementedError(self.iter_child_entries)
341
 
 
342
 
    def list_files(self, include_root=False, from_dir=None, recursive=True,
343
 
                   recurse_nested=False):
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
349
 
        :param recurse_nested: enter nested trees
350
 
        :return: iterator over tuples of
351
 
            (path, versioned, kind, inventory entry)
352
 
        """
353
 
        raise NotImplementedError(self.list_files)
 
172
        This will yield each entry in the tree as a (path, entry) tuple. The
 
173
        order that they are yielded is: the contents of a directory are 
 
174
        preceeded by the parent of a directory, and all the contents of a 
 
175
        directory are grouped together.
 
176
        """
 
177
        return self.inventory.iter_entries_by_dir(
 
178
            specific_file_ids=specific_file_ids)
354
179
 
355
180
    def iter_references(self):
356
 
        if self.supports_tree_reference():
357
 
            for path, entry in self.iter_entries_by_dir():
358
 
                if entry.kind == 'tree-reference':
359
 
                    yield path
360
 
 
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
 
 
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
 
 
383
 
    def kind(self, path):
 
181
        for path, entry in self.iter_entries_by_dir():
 
182
            if entry.kind == 'tree-reference':
 
183
                yield path, entry.file_id
 
184
 
 
185
    def kind(self, file_id):
384
186
        raise NotImplementedError("Tree subclass %s must implement kind"
385
 
                                  % self.__class__.__name__)
386
 
 
387
 
    def stored_kind(self, path):
388
 
        """File kind stored for this path.
389
 
 
390
 
        May not match kind on disk for working trees.  Always available
391
 
        for versioned files, even when the file itself is missing.
392
 
        """
393
 
        return self.kind(path)
394
 
 
395
 
    def path_content_summary(self, path):
396
 
        """Get a summary of the information about path.
397
 
 
398
 
        All the attributes returned are for the canonical form, not the
399
 
        convenient form (if content filters are in use.)
400
 
 
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()).
404
 
            size is present if kind is file and the size of the
405
 
                canonical form can be cheaply determined, None otherwise.
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
 
 
413
 
    def get_reference_revision(self, path, branch=None):
 
187
            % self.__class__.__name__)
 
188
 
 
189
    def get_reference_revision(self, file_id, path=None):
414
190
        raise NotImplementedError("Tree subclass %s must implement "
415
191
                                  "get_reference_revision"
416
 
                                  % self.__class__.__name__)
 
192
            % self.__class__.__name__)
417
193
 
418
194
    def _comparison_data(self, entry, path):
419
195
        """Return a tuple of kind, executable, stat_value for a file.
426
202
        """
427
203
        raise NotImplementedError(self._comparison_data)
428
204
 
429
 
    def get_file(self, path):
430
 
        """Return a file object for the file path in the tree.
431
 
        """
 
205
    def _file_size(self, entry, stat_value):
 
206
        raise NotImplementedError(self._file_size)
 
207
 
 
208
    def _get_inventory(self):
 
209
        return self._inventory
 
210
    
 
211
    def get_file(self, file_id):
 
212
        """Return a file object for the file file_id in the tree."""
432
213
        raise NotImplementedError(self.get_file)
433
214
 
434
 
    def get_file_with_stat(self, path):
435
 
        """Get a file handle and stat object for path.
436
 
 
437
 
        The default implementation returns (self.get_file, None) for backwards
438
 
        compatibility.
439
 
 
440
 
        :param path: The path of the file.
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
 
        """
445
 
        return (self.get_file(path), None)
446
 
 
447
 
    def get_file_text(self, path):
448
 
        """Return the byte content of a file.
449
 
 
450
 
        :param path: The path of the file.
451
 
 
452
 
        :returns: A single byte string for the whole file.
453
 
        """
454
 
        with self.get_file(path) as my_file:
455
 
            return my_file.read()
456
 
 
457
 
    def get_file_lines(self, path):
458
 
        """Return the content of a file, as lines.
459
 
 
460
 
        :param path: The path of the file.
461
 
        """
462
 
        return osutils.split_lines(self.get_file_text(path))
463
 
 
464
 
    def get_file_verifier(self, path, stat_value=None):
465
 
        """Return a verifier for a file.
466
 
 
467
 
        The default implementation returns a sha1.
468
 
 
 
215
    def get_file_mtime(self, file_id, path=None):
 
216
        """Return the modification time for a file.
 
217
 
 
218
        :param file_id: The handle for this file.
469
219
        :param path: The path that this file can be found at.
470
220
            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
 
        """
474
 
        return ("SHA1", self.get_file_sha1(path, stat_value=stat_value))
475
 
 
476
 
    def get_file_sha1(self, path, stat_value=None):
477
 
        """Return the SHA1 file for a file.
478
 
 
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
 
 
483
 
        :param path: The path that this file can be found at.
484
 
        :param stat_value: Optional stat value for the object
485
 
        """
486
 
        raise NotImplementedError(self.get_file_sha1)
487
 
 
488
 
    def get_file_mtime(self, path):
489
 
        """Return the modification time for a file.
490
 
 
491
 
        :param path: The path that this file can be found at.
492
221
        """
493
222
        raise NotImplementedError(self.get_file_mtime)
494
223
 
495
 
    def get_file_size(self, path):
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
 
 
503
 
    def is_executable(self, path):
504
 
        """Check if a file is executable.
505
 
 
506
 
        :param path: The path that this file can be found at.
507
 
        """
508
 
        raise NotImplementedError(self.is_executable)
509
 
 
510
 
    def iter_files_bytes(self, desired_files):
511
 
        """Iterate through file contents.
512
 
 
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
 
 
526
 
        :param desired_files: a list of (path, identifier) pairs
527
 
        """
528
 
        for path, identifier in desired_files:
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.)
533
 
            cur_file = (self.get_file_text(path),)
534
 
            yield identifier, cur_file
535
 
 
536
 
    def get_symlink_target(self, path):
537
 
        """Get the target for a given path.
538
 
 
539
 
        It is assumed that the caller already knows that path is referencing
 
224
    def get_file_by_path(self, path):
 
225
        return self.get_file(self._inventory.path2id(path))
 
226
 
 
227
    def get_symlink_target(self, file_id):
 
228
        """Get the target for a given file_id.
 
229
 
 
230
        It is assumed that the caller already knows that file_id is referencing
540
231
        a symlink.
541
 
        :param path: The path of the file.
 
232
        :param file_id: Handle for the symlink entry.
542
233
        :return: The path the symlink points to.
543
234
        """
544
235
        raise NotImplementedError(self.get_symlink_target)
545
236
 
546
 
    def annotate_iter(self, path,
547
 
                      default_revision=_mod_revision.CURRENT_REVISION):
548
 
        """Return an iterator of revision_id, line tuples.
 
237
    def annotate_iter(self, file_id):
 
238
        """Return an iterator of revision_id, line tuples
549
239
 
550
240
        For working trees (and mutable trees in general), the special
551
241
        revision_id 'current:' will be used for lines that are new in this
552
242
        tree, e.g. uncommitted changes.
553
 
        :param path: The file to produce an annotated version from
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.
 
243
        :param file_id: The file to produce an annotated version from
557
244
        """
558
245
        raise NotImplementedError(self.annotate_iter)
559
246
 
 
247
    inventory = property(_get_inventory,
 
248
                         doc="Inventory of this Tree")
 
249
 
 
250
    def _check_retrieved(self, ie, f):
 
251
        if not __debug__:
 
252
            return  
 
253
        fp = fingerprint_file(f)
 
254
        f.seek(0)
 
255
        
 
256
        if ie.text_size is not None:
 
257
            if ie.text_size != fp['size']:
 
258
                raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
 
259
                        ["inventory expects %d bytes" % ie.text_size,
 
260
                         "file is actually %d bytes" % fp['size'],
 
261
                         "store is probably damaged/corrupt"])
 
262
 
 
263
        if ie.text_sha1 != fp['sha1']:
 
264
            raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
 
265
                    ["inventory expects %s" % ie.text_sha1,
 
266
                     "file is actually %s" % fp['sha1'],
 
267
                     "store is probably damaged/corrupt"])
 
268
 
 
269
    @needs_read_lock
560
270
    def path2id(self, path):
561
271
        """Return the id for path in this tree."""
562
 
        raise NotImplementedError(self.path2id)
563
 
 
564
 
    def is_versioned(self, path):
565
 
        """Check whether path is versioned.
566
 
 
567
 
        :param path: Path to check
568
 
        :return: boolean
569
 
        """
570
 
        return self.path2id(path) is not None
571
 
 
572
 
    def find_related_paths_across_trees(self, paths, trees=[],
573
 
                                        require_versioned=True):
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
 
        """
588
 
        raise NotImplementedError(self.find_related_paths_across_trees)
 
272
        return self._inventory.path2id(path)
 
273
 
 
274
    def paths2ids(self, paths, trees=[], require_versioned=True):
 
275
        """Return all the ids that can be reached by walking from paths.
 
276
        
 
277
        Each path is looked up in each this tree and any extras provided in
 
278
        trees, and this is repeated recursively: the children in an extra tree
 
279
        of a directory that has been renamed under a provided path in this tree
 
280
        are all returned, even if none exist until a provided path in this
 
281
        tree, and vice versa.
 
282
 
 
283
        :param paths: An iterable of paths to start converting to ids from.
 
284
            Alternatively, if paths is None, no ids should be calculated and None
 
285
            will be returned. This is offered to make calling the api unconditional
 
286
            for code that *might* take a list of files.
 
287
        :param trees: Additional trees to consider.
 
288
        :param require_versioned: If False, do not raise NotVersionedError if
 
289
            an element of paths is not versioned in this tree and all of trees.
 
290
        """
 
291
        return find_ids_across_trees(paths, [self] + list(trees), require_versioned)
 
292
 
 
293
    def print_file(self, file_id):
 
294
        """Print file with id `file_id` to stdout."""
 
295
        file_id = osutils.safe_file_id(file_id)
 
296
        import sys
 
297
        sys.stdout.write(self.get_file_text(file_id))
589
298
 
590
299
    def lock_read(self):
591
 
        """Lock this tree for multiple read only operations.
592
 
 
593
 
        :return: A breezy.lock.LogicalLockResult.
594
 
        """
595
 
        return lock.LogicalLockResult(self.unlock)
 
300
        pass
596
301
 
597
302
    def revision_tree(self, revision_id):
598
303
        """Obtain a revision tree for the revision revision_id.
599
304
 
600
305
        The intention of this method is to allow access to possibly cached
601
306
        tree data. Implementors of this method should raise NoSuchRevision if
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
 
307
        the tree is not locally available, even if they could obtain the 
 
308
        tree via a repository or some other means. Callers are responsible 
604
309
        for finding the ultimate source for a revision tree.
605
310
 
606
311
        :param revision_id: The revision_id of the requested tree.
611
316
 
612
317
    def unknowns(self):
613
318
        """What files are present in this tree and unknown.
614
 
 
 
319
        
615
320
        :return: an iterator over the unknown files.
616
321
        """
617
322
        return iter([])
625
330
        :return: set of paths.
626
331
        """
627
332
        # NB: we specifically *don't* call self.has_filename, because for
628
 
        # WorkingTrees that can indicate files that exist on disk but that
 
333
        # WorkingTrees that can indicate files that exist on disk but that 
629
334
        # are not versioned.
630
 
        return set(p for p in paths if not self.is_versioned(p))
 
335
        pred = self.inventory.has_filename
 
336
        return set((p for p in paths if not pred(p)))
631
337
 
632
338
    def walkdirs(self, prefix=""):
633
339
        """Walk the contents of this tree from path down.
635
341
        This yields all the data about the contents of a directory at a time.
636
342
        After each directory has been yielded, if the caller has mutated the
637
343
        list to exclude some directories, they are then not descended into.
638
 
 
 
344
        
639
345
        The data yielded is of the form:
640
346
        ((directory-relpath, directory-path-from-root, directory-fileid),
641
 
        [(relpath, basename, kind, lstat, path_from_tree_root, file_id,
 
347
        [(relpath, basename, kind, lstat, path_from_tree_root, file_id, 
642
348
          versioned_kind), ...]),
643
349
         - directory-relpath is the containing dirs relpath from prefix
644
350
         - directory-path-from-root is the containing dirs path from /
650
356
           versioned_kind.
651
357
         - lstat is the stat data *if* the file was statted.
652
358
         - path_from_tree_root is the path from the root of the tree.
653
 
         - file_id is the file_id if the entry is versioned.
654
 
         - versioned_kind is the kind of the file as last recorded in the
 
359
         - file_id is the file_id is the entry is versioned.
 
360
         - versioned_kind is the kind of the file as last recorded in the 
655
361
           versioning system. If 'unknown' the file is not versioned.
656
362
        One of 'kind' and 'versioned_kind' must not be 'unknown'.
657
363
 
662
368
        """
663
369
        raise NotImplementedError(self.walkdirs)
664
370
 
665
 
    def supports_content_filtering(self):
 
371
 
 
372
class EmptyTree(Tree):
 
373
 
 
374
    def __init__(self):
 
375
        self._inventory = Inventory(root_id=None)
 
376
        symbol_versioning.warn('EmptyTree is deprecated as of bzr 0.9 please'
 
377
                               ' use repository.revision_tree instead.',
 
378
                               DeprecationWarning, stacklevel=2)
 
379
 
 
380
    def get_parent_ids(self):
 
381
        return []
 
382
 
 
383
    def get_symlink_target(self, file_id):
 
384
        return None
 
385
 
 
386
    def has_filename(self, filename):
666
387
        return False
667
388
 
668
 
    def _content_filter_stack(self, path=None):
669
 
        """The stack of content filters for a path if filtering is supported.
670
 
 
671
 
        Readers will be applied in first-to-last order.
672
 
        Writers will be applied in last-to-first order.
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
678
 
        """
679
 
        from . import debug, filters
680
 
        filter_pref_names = filters._get_registered_names()
681
 
        if len(filter_pref_names) == 0:
682
 
            return []
683
 
        prefs = next(self.iter_search_rules([path], filter_pref_names))
684
 
        stk = filters._get_filter_stack_for(prefs)
685
 
        if 'filters' in debug.debug_flags:
686
 
            trace.note(
687
 
                gettext("*** {0} content-filter: {1} => {2!r}").format(path, prefs, stk))
688
 
        return stk
689
 
 
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():
699
 
            return self._content_filter_stack
700
 
        else:
701
 
            return None
702
 
 
703
 
    def iter_search_rules(self, path_names, pref_names=None,
704
 
                          _default_searcher=None):
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
 
        """
714
 
        from . import rules
715
 
        if _default_searcher is None:
716
 
            _default_searcher = rules._per_user_searcher
717
 
        searcher = self._get_rules_searcher(_default_searcher)
718
 
        if searcher is not None:
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)
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
 
 
731
 
    def archive(self, format, name, root='', subdir=None,
732
 
                force_mtime=None):
733
 
        """Create an archive of this tree.
734
 
 
735
 
        :param format: Format name (e.g. 'tar')
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
 
        """
741
 
        from .archive import create_archive
742
 
        with self.lock_read():
743
 
            return create_archive(format, self, name, root,
744
 
                                  subdir, force_mtime=force_mtime)
745
 
 
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
 
 
751
 
    def preview_transform(self, pb=None):
752
 
        """Obtain a transform object."""
753
 
        raise NotImplementedError(self.preview_transform)
 
389
    def kind(self, file_id):
 
390
        file_id = osutils.safe_file_id(file_id)
 
391
        assert self._inventory[file_id].kind == "directory"
 
392
        return "directory"
 
393
 
 
394
    def list_files(self, include_root=False):
 
395
        return iter([])
 
396
    
 
397
    def __contains__(self, file_id):
 
398
        file_id = osutils.safe_file_id(file_id)
 
399
        return (file_id in self._inventory)
 
400
 
 
401
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
402
        return None
 
403
 
 
404
 
 
405
######################################################################
 
406
# diff
 
407
 
 
408
# TODO: Merge these two functions into a single one that can operate
 
409
# on either a whole tree or a set of files.
 
410
 
 
411
# TODO: Return the diff in order by filename, not by category or in
 
412
# random order.  Can probably be done by lock-stepping through the
 
413
# filenames from both trees.
 
414
 
 
415
 
 
416
def file_status(filename, old_tree, new_tree):
 
417
    """Return single-letter status, old and new names for a file.
 
418
 
 
419
    The complexity here is in deciding how to represent renames;
 
420
    many complex cases are possible.
 
421
    """
 
422
    old_inv = old_tree.inventory
 
423
    new_inv = new_tree.inventory
 
424
    new_id = new_inv.path2id(filename)
 
425
    old_id = old_inv.path2id(filename)
 
426
 
 
427
    if not new_id and not old_id:
 
428
        # easy: doesn't exist in either; not versioned at all
 
429
        if new_tree.is_ignored(filename):
 
430
            return 'I', None, None
 
431
        else:
 
432
            return '?', None, None
 
433
    elif new_id:
 
434
        # There is now a file of this name, great.
 
435
        pass
 
436
    else:
 
437
        # There is no longer a file of this name, but we can describe
 
438
        # what happened to the file that used to have
 
439
        # this name.  There are two possibilities: either it was
 
440
        # deleted entirely, or renamed.
 
441
        assert old_id
 
442
        if new_inv.has_id(old_id):
 
443
            return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id)
 
444
        else:
 
445
            return 'D', old_inv.id2path(old_id), None
 
446
 
 
447
    # if the file_id is new in this revision, it is added
 
448
    if new_id and not old_inv.has_id(new_id):
 
449
        return 'A'
 
450
 
 
451
    # if there used to be a file of this name, but that ID has now
 
452
    # disappeared, it is deleted
 
453
    if old_id and not new_inv.has_id(old_id):
 
454
        return 'D'
 
455
 
 
456
    return 'wtf?'
 
457
 
 
458
    
 
459
 
 
460
def find_renames(old_inv, new_inv):
 
461
    for file_id in old_inv:
 
462
        if file_id not in new_inv:
 
463
            continue
 
464
        old_name = old_inv.id2path(file_id)
 
465
        new_name = new_inv.id2path(file_id)
 
466
        if old_name != new_name:
 
467
            yield (old_name, new_name)
 
468
            
 
469
 
 
470
def find_ids_across_trees(filenames, trees, require_versioned=True):
 
471
    """Find the ids corresponding to specified filenames.
 
472
    
 
473
    All matches in all trees will be used, and all children of matched
 
474
    directories will be used.
 
475
 
 
476
    :param filenames: The filenames to find file_ids for (if None, returns
 
477
        None)
 
478
    :param trees: The trees to find file_ids within
 
479
    :param require_versioned: if true, all specified filenames must occur in
 
480
    at least one tree.
 
481
    :return: a set of file ids for the specified filenames and their children.
 
482
    """
 
483
    if not filenames:
 
484
        return None
 
485
    specified_path_ids = _find_ids_across_trees(filenames, trees,
 
486
        require_versioned)
 
487
    return _find_children_across_trees(specified_path_ids, trees)
 
488
 
 
489
 
 
490
def _find_ids_across_trees(filenames, trees, require_versioned):
 
491
    """Find the ids corresponding to specified filenames.
 
492
    
 
493
    All matches in all trees will be used, but subdirectories are not scanned.
 
494
 
 
495
    :param filenames: The filenames to find file_ids for
 
496
    :param trees: The trees to find file_ids within
 
497
    :param require_versioned: if true, all specified filenames must occur in
 
498
        at least one tree.
 
499
    :return: a set of (path, file ids) for the specified filenames
 
500
    """
 
501
    not_versioned = []
 
502
    interesting_ids = set()
 
503
    for tree_path in filenames:
 
504
        not_found = True
 
505
        for tree in trees:
 
506
            file_id = tree.path2id(tree_path)
 
507
            if file_id is not None:
 
508
                interesting_ids.add(file_id)
 
509
                not_found = False
 
510
        if not_found:
 
511
            not_versioned.append(tree_path)
 
512
    if len(not_versioned) > 0 and require_versioned:
 
513
        raise errors.PathsNotVersionedError(not_versioned)
 
514
    return interesting_ids
 
515
 
 
516
 
 
517
def _find_children_across_trees(specified_ids, trees):
 
518
    """Return a set including specified ids and their children
 
519
    
 
520
    All matches in all trees will be used.
 
521
 
 
522
    :param trees: The trees to find file_ids within
 
523
    :return: a set containing all specified ids and their children 
 
524
    """
 
525
    interesting_ids = set(specified_ids)
 
526
    pending = interesting_ids
 
527
    # now handle children of interesting ids
 
528
    # we loop so that we handle all children of each id in both trees
 
529
    while len(pending) > 0:
 
530
        new_pending = set()
 
531
        for file_id in pending:
 
532
            for tree in trees:
 
533
                if not tree.has_id(file_id):
 
534
                    continue
 
535
                entry = tree.inventory[file_id]
 
536
                for child in getattr(entry, 'children', {}).itervalues():
 
537
                    if child.file_id not in interesting_ids:
 
538
                        new_pending.add(child.file_id)
 
539
        interesting_ids.update(new_pending)
 
540
        pending = new_pending
 
541
    return interesting_ids
754
542
 
755
543
 
756
544
class InterTree(InterObject):
759
547
    Its instances have methods like 'compare' and contain references to the
760
548
    source and target trees these operations are to be carried out on.
761
549
 
762
 
    Clients of breezy should not need to use InterTree directly, rather they
 
550
    clients of bzrlib should not need to use InterTree directly, rather they
763
551
    should use the convenience methods on Tree such as 'Tree.compare()' which
764
552
    will pass through to InterTree as appropriate.
765
553
    """
766
554
 
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
 
 
773
555
    _optimisers = []
774
556
 
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
 
 
 
557
    @needs_read_lock
781
558
    def compare(self, want_unchanged=False, specific_files=None,
782
 
                extra_trees=None, require_versioned=False, include_root=False,
783
 
                want_unversioned=False):
 
559
        extra_trees=None, require_versioned=False, include_root=False,
 
560
        want_unversioned=False):
784
561
        """Return the changes from source to target.
785
562
 
786
563
        :return: A TreeDelta.
797
574
            a PathsNotVersionedError will be thrown.
798
575
        :param want_unversioned: Scan for unversioned paths.
799
576
        """
800
 
        from . import delta
 
577
        # NB: show_status depends on being able to pass in non-versioned files
 
578
        # and report them as unknown
801
579
        trees = (self.source,)
802
580
        if extra_trees is not None:
803
581
            trees = trees + tuple(extra_trees)
804
 
        with self.lock_read():
805
 
            return delta._compare_trees(self.source, self.target, want_unchanged,
806
 
                                        specific_files, include_root, extra_trees=extra_trees,
807
 
                                        require_versioned=require_versioned,
808
 
                                        want_unversioned=want_unversioned)
 
582
        # target is usually the newer tree:
 
583
        specific_file_ids = self.target.paths2ids(specific_files, trees,
 
584
            require_versioned=require_versioned)
 
585
        if specific_files and not specific_file_ids:
 
586
            # All files are unversioned, so just return an empty delta
 
587
            # _compare_trees would think we want a complete delta
 
588
            result = delta.TreeDelta()
 
589
            fake_entry = InventoryFile('unused', 'unused', 'unused')
 
590
            result.unversioned = [(path, None,
 
591
                self.target._comparison_data(fake_entry, path)[0]) for path in
 
592
                specific_files]
 
593
            return result
 
594
        return delta._compare_trees(self.source, self.target, want_unchanged,
 
595
            specific_files, include_root, extra_trees=extra_trees,
 
596
            want_unversioned=want_unversioned)
809
597
 
810
 
    def iter_changes(self, include_unchanged=False,
811
 
                     specific_files=None, pb=None, extra_trees=[],
812
 
                     require_versioned=True, want_unversioned=False):
 
598
    def _iter_changes(self, include_unchanged=False,
 
599
                      specific_files=None, pb=None, extra_trees=[],
 
600
                      require_versioned=True, want_unversioned=False):
813
601
        """Generate an iterator of changes between trees.
814
602
 
815
603
        A tuple is returned:
833
621
        :param require_versioned: Raise errors.PathsNotVersionedError if a
834
622
            path in the specific_files list is not versioned in one of
835
623
            source, target or extra_trees.
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.
842
624
        :param want_unversioned: Should unversioned files be returned in the
843
625
            output. An unversioned file is defined as one with (False, False)
844
626
            for the versioned pair.
845
627
        """
846
 
        raise NotImplementedError(self.iter_changes)
847
 
 
848
 
    def file_content_matches(
849
 
            self, source_path, target_path,
850
 
            source_stat=None, target_stat=None):
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
 
 
856
 
        :param source_path: Path of the file in the source tree
857
 
        :param target_path: Path of the file in the target tree
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
 
        """
862
 
        with self.lock_read():
863
 
            source_verifier_kind, source_verifier_data = (
864
 
                self.source.get_file_verifier(source_path, source_stat))
865
 
            target_verifier_kind, target_verifier_data = (
866
 
                self.target.get_file_verifier(
867
 
                    target_path, target_stat))
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(
873
 
                    source_path, source_stat)
874
 
            else:
875
 
                source_sha1 = source_verifier_data
876
 
            if target_verifier_kind != "SHA1":
877
 
                target_sha1 = self.target.get_file_sha1(
878
 
                    target_path, target_stat)
879
 
            else:
880
 
                target_sha1 = target_verifier_data
881
 
            return (source_sha1 == target_sha1)
882
 
 
883
 
    def find_target_path(self, path, recurse='none'):
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
 
        """
890
 
        raise NotImplementedError(self.find_target_path)
891
 
 
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
 
        """
899
 
        raise NotImplementedError(self.find_source_path)
900
 
 
901
 
    def find_target_paths(self, paths, recurse='none'):
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:
910
 
            ret[path] = self.find_target_path(path, recurse=recurse)
911
 
        return ret
912
 
 
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
 
 
925
 
 
926
 
def find_previous_paths(from_tree, to_tree, paths, recurse='none'):
927
 
    """Find previous tree paths.
928
 
 
929
 
    :param from_tree: From tree
930
 
    :param to_tree: To tree
931
 
    :param paths: Iterable over paths in from_tree to search for
932
 
    :return: Dictionary mapping from from_tree paths to paths in to_tree, or
933
 
        None if there is no equivalent path.
934
 
    """
935
 
    return InterTree.get(to_tree, from_tree).find_source_paths(paths, recurse=recurse)
936
 
 
937
 
 
938
 
def find_previous_path(from_tree, to_tree, path, recurse='none'):
939
 
    """Find previous tree path.
940
 
 
941
 
    :param from_tree: From tree
942
 
    :param to_tree: To tree
943
 
    :param path: Path to search for (exists in from_tree)
944
 
    :return: path in to_tree, or None if there is no equivalent path.
945
 
    :raise NoSuchFile: If the path doesn't exist in from_tree
946
 
    """
947
 
    return InterTree.get(to_tree, from_tree).find_source_path(
948
 
        path, recurse=recurse)
949
 
 
950
 
 
951
 
def get_canonical_path(tree, path, normalize):
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
956
 
    :param normalize: Function to normalize a filename for comparison
957
 
    :return: The canonical path
958
 
    """
959
 
    # go walkin...
960
 
    cur_path = ''
961
 
    bit_iter = iter(path.split("/"))
962
 
    for elt in bit_iter:
963
 
        lelt = normalize(elt)
964
 
        new_path = None
965
 
        try:
966
 
            for child in tree.iter_child_entries(cur_path):
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
975
 
                    elif normalize(child.name) == lelt:
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
 
628
        result = []
 
629
        lookup_trees = [self.source]
 
630
        if extra_trees:
 
631
             lookup_trees.extend(extra_trees)
 
632
        specific_file_ids = self.target.paths2ids(specific_files,
 
633
            lookup_trees, require_versioned=require_versioned)
 
634
        if want_unversioned:
 
635
            all_unversioned = sorted([(p.split('/'), p) for p in self.target.extras()
 
636
                if not specific_files or
 
637
                    osutils.is_inside_any(specific_files, p)])
 
638
            all_unversioned = deque(all_unversioned)
984
639
        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
 
640
            all_unversioned = deque()
 
641
        to_paths = {}
 
642
        from_entries_by_dir = list(self.source.inventory.iter_entries_by_dir(
 
643
            specific_file_ids=specific_file_ids))
 
644
        from_data = dict((e.file_id, (p, e)) for p, e in from_entries_by_dir)
 
645
        to_entries_by_dir = list(self.target.inventory.iter_entries_by_dir(
 
646
            specific_file_ids=specific_file_ids))
 
647
        num_entries = len(from_entries_by_dir) + len(to_entries_by_dir)
 
648
        entry_count = 0
 
649
        # the unversioned path lookup only occurs on real trees - where there 
 
650
        # can be extras. So the fake_entry is solely used to look up
 
651
        # executable it values when execute is not supported.
 
652
        fake_entry = InventoryFile('unused', 'unused', 'unused')
 
653
        for to_path, to_entry in to_entries_by_dir:
 
654
            while all_unversioned and all_unversioned[0][0] < to_path.split('/'):
 
655
                unversioned_path = all_unversioned.popleft()
 
656
                to_kind, to_executable, to_stat = \
 
657
                    self.target._comparison_data(fake_entry, unversioned_path[1])
 
658
                yield (None, (None, unversioned_path[1]), True, (False, False),
 
659
                    (None, None),
 
660
                    (None, unversioned_path[0][-1]),
 
661
                    (None, to_kind),
 
662
                    (None, to_executable))
 
663
            file_id = to_entry.file_id
 
664
            to_paths[file_id] = to_path
 
665
            entry_count += 1
 
666
            changed_content = False
 
667
            from_path, from_entry = from_data.get(file_id, (None, None))
 
668
            from_versioned = (from_entry is not None)
 
669
            if from_entry is not None:
 
670
                from_versioned = True
 
671
                from_name = from_entry.name
 
672
                from_parent = from_entry.parent_id
 
673
                from_kind, from_executable, from_stat = \
 
674
                    self.source._comparison_data(from_entry, from_path)
 
675
                entry_count += 1
 
676
            else:
 
677
                from_versioned = False
 
678
                from_kind = None
 
679
                from_parent = None
 
680
                from_name = None
 
681
                from_executable = None
 
682
            versioned = (from_versioned, True)
 
683
            to_kind, to_executable, to_stat = \
 
684
                self.target._comparison_data(to_entry, to_path)
 
685
            kind = (from_kind, to_kind)
 
686
            if kind[0] != kind[1]:
 
687
                changed_content = True
 
688
            elif from_kind == 'file':
 
689
                from_size = self.source._file_size(from_entry, from_stat)
 
690
                to_size = self.target._file_size(to_entry, to_stat)
 
691
                if from_size != to_size:
 
692
                    changed_content = True
 
693
                elif (self.source.get_file_sha1(file_id, from_path, from_stat) !=
 
694
                    self.target.get_file_sha1(file_id, to_path, to_stat)):
 
695
                    changed_content = True
 
696
            elif from_kind == 'symlink':
 
697
                if (self.source.get_symlink_target(file_id) !=
 
698
                    self.target.get_symlink_target(file_id)):
 
699
                    changed_content = True
 
700
                elif from_kind == 'tree-reference':
 
701
                    if (self.source.get_reference_revision(file_id, from_path)
 
702
                        != self.target.get_reference_revision(file_id, to_path)):
 
703
                        changed_content = True 
 
704
            parent = (from_parent, to_entry.parent_id)
 
705
            name = (from_name, to_entry.name)
 
706
            executable = (from_executable, to_executable)
 
707
            if pb is not None:
 
708
                pb.update('comparing files', entry_count, num_entries)
 
709
            if (changed_content is not False or versioned[0] != versioned[1]
 
710
                or parent[0] != parent[1] or name[0] != name[1] or 
 
711
                executable[0] != executable[1] or include_unchanged):
 
712
                yield (file_id, (from_path, to_path), changed_content,
 
713
                    versioned, parent, name, kind, executable)
 
714
 
 
715
        while all_unversioned:
 
716
            # yield any trailing unversioned paths
 
717
            unversioned_path = all_unversioned.popleft()
 
718
            to_kind, to_executable, to_stat = \
 
719
                self.target._comparison_data(fake_entry, unversioned_path[1])
 
720
            yield (None, (None, unversioned_path[1]), True, (False, False),
 
721
                (None, None),
 
722
                (None, unversioned_path[0][-1]),
 
723
                (None, to_kind),
 
724
                (None, to_executable))
 
725
 
 
726
        def get_to_path(to_entry):
 
727
            if to_entry.parent_id is None:
 
728
                to_path = '' # the root
 
729
            else:
 
730
                if to_entry.parent_id not in to_paths:
 
731
                    # recurse up
 
732
                    return get_to_path(self.target.inventory[to_entry.parent_id])
 
733
                to_path = osutils.pathjoin(to_paths[to_entry.parent_id],
 
734
                                           to_entry.name)
 
735
            to_paths[to_entry.file_id] = to_path
 
736
            return to_path
 
737
 
 
738
        for path, from_entry in from_entries_by_dir:
 
739
            file_id = from_entry.file_id
 
740
            if file_id in to_paths:
 
741
                # already returned
 
742
                continue
 
743
            if not file_id in self.target.inventory:
 
744
                # common case - paths we have not emitted are not present in
 
745
                # target.
 
746
                to_path = None
 
747
            else:
 
748
                to_path = get_to_path(self.target.inventory[file_id])
 
749
            entry_count += 1
 
750
            if pb is not None:
 
751
                pb.update('comparing files', entry_count, num_entries)
 
752
            versioned = (True, False)
 
753
            parent = (from_entry.parent_id, None)
 
754
            name = (from_entry.name, None)
 
755
            from_kind, from_executable, stat_value = \
 
756
                self.source._comparison_data(from_entry, path)
 
757
            kind = (from_kind, None)
 
758
            executable = (from_executable, None)
 
759
            changed_content = True
 
760
            # the parent's path is necessarily known at this point.
 
761
            yield(file_id, (path, to_path), changed_content, versioned, parent,
 
762
                  name, kind, executable)
 
763
 
 
764
 
 
765
# This was deprecated before 0.12, but did not have an official warning
 
766
@symbol_versioning.deprecated_function(symbol_versioning.zero_twelve)
 
767
def RevisionTree(*args, **kwargs):
 
768
    """RevisionTree has moved to bzrlib.revisiontree.RevisionTree()
 
769
 
 
770
    Accessing it as bzrlib.tree.RevisionTree has been deprecated as of
 
771
    bzr 0.12.
 
772
    """
 
773
    from bzrlib.revisiontree import RevisionTree as _RevisionTree
 
774
    return _RevisionTree(*args, **kwargs)
 
775
 
 
776