/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/workingtree_4.py

  • Committer: Martin von Gagern
  • Date: 2010-04-20 08:47:38 UTC
  • mfrom: (5167 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5195.
  • Revision ID: martin.vgagern@gmx.net-20100420084738-ygymnqmdllzrhpfn
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""WorkingTree4 format and implementation.
18
18
 
28
28
 
29
29
from bzrlib.lazy_import import lazy_import
30
30
lazy_import(globals(), """
31
 
from bisect import bisect_left
32
 
import collections
33
 
from copy import deepcopy
34
31
import errno
35
 
import itertools
36
 
import operator
37
32
import stat
38
 
from time import time
39
 
import warnings
40
33
 
41
34
import bzrlib
42
35
from bzrlib import (
43
36
    bzrdir,
44
37
    cache_utf8,
45
 
    conflicts as _mod_conflicts,
46
38
    debug,
47
 
    delta,
48
39
    dirstate,
49
40
    errors,
50
41
    generate_ids,
51
 
    globbing,
52
 
    ignores,
53
 
    merge,
54
42
    osutils,
55
43
    revision as _mod_revision,
56
44
    revisiontree,
57
 
    textui,
58
45
    trace,
59
46
    transform,
60
 
    urlutils,
61
 
    xml5,
62
 
    xml6,
 
47
    views,
63
48
    )
64
49
import bzrlib.branch
65
 
from bzrlib.transport import get_transport
66
50
import bzrlib.ui
67
51
""")
68
52
 
69
 
from bzrlib import symbol_versioning
70
53
from bzrlib.decorators import needs_read_lock, needs_write_lock
71
 
from bzrlib.inventory import InventoryEntry, Inventory, ROOT_ID, entry_factory
72
 
from bzrlib.lockable_files import LockableFiles, TransportLock
73
 
from bzrlib.lockdir import LockDir
 
54
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
 
55
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
74
56
import bzrlib.mutabletree
75
57
from bzrlib.mutabletree import needs_tree_write_lock
76
58
from bzrlib.osutils import (
77
59
    file_kind,
78
60
    isdir,
79
 
    normpath,
80
61
    pathjoin,
81
 
    rand_chars,
82
62
    realpath,
83
63
    safe_unicode,
84
 
    splitpath,
85
64
    )
86
 
from bzrlib.trace import mutter, note
 
65
from bzrlib.trace import mutter
87
66
from bzrlib.transport.local import LocalTransport
88
67
from bzrlib.tree import InterTree
89
 
from bzrlib.progress import DummyProgress, ProgressPhase
90
 
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
91
 
from bzrlib.rio import RioReader, rio_file, Stanza
92
 
from bzrlib.symbol_versioning import (deprecated_passed,
93
 
        deprecated_method,
94
 
        deprecated_function,
95
 
        DEPRECATED_PARAMETER,
96
 
        )
97
68
from bzrlib.tree import Tree
98
69
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
99
70
 
100
71
 
101
 
class WorkingTree4(WorkingTree3):
102
 
    """This is the Format 4 working tree.
103
 
 
104
 
    This differs from WorkingTree3 by:
105
 
     - Having a consolidated internal dirstate, stored in a
106
 
       randomly-accessible sorted file on disk.
107
 
     - Not having a regular inventory attribute.  One can be synthesized 
108
 
       on demand but this is expensive and should be avoided.
109
 
 
110
 
    This is new in bzr 0.15.
111
 
    """
112
 
 
 
72
class DirStateWorkingTree(WorkingTree3):
113
73
    def __init__(self, basedir,
114
74
                 branch,
115
75
                 _control_files=None,
142
102
        self._setup_directory_is_tree_reference()
143
103
        self._detect_case_handling()
144
104
        self._rules_searcher = None
 
105
        self.views = self._make_views()
145
106
        #--- allow tests to select the dirstate iter_changes implementation
146
107
        self._iter_changes = dirstate._process_entry
147
108
 
177
138
    @needs_tree_write_lock
178
139
    def add_reference(self, sub_tree):
179
140
        # use standard implementation, which calls back to self._add
180
 
        # 
 
141
        #
181
142
        # So we don't store the reference_revision in the working dirstate,
182
 
        # it's just recorded at the moment of commit. 
 
143
        # it's just recorded at the moment of commit.
183
144
        self._add_reference(sub_tree)
184
145
 
185
146
    def break_lock(self):
224
185
            WorkingTree3._comparison_data(self, entry, path)
225
186
        # it looks like a plain directory, but it's really a reference -- see
226
187
        # also kind()
227
 
        if (self._repo_supports_tree_reference and
228
 
            kind == 'directory' and
229
 
            self._directory_is_tree_reference(path)):
 
188
        if (self._repo_supports_tree_reference and kind == 'directory'
 
189
            and entry is not None and entry.kind == 'tree-reference'):
230
190
            kind = 'tree-reference'
231
191
        return kind, executable, stat_value
232
192
 
258
218
            return self._dirstate
259
219
        local_path = self.bzrdir.get_workingtree_transport(None
260
220
            ).local_abspath('dirstate')
261
 
        self._dirstate = dirstate.DirState.on_file(local_path)
 
221
        self._dirstate = dirstate.DirState.on_file(local_path,
 
222
            self._sha1_provider())
262
223
        return self._dirstate
263
224
 
 
225
    def _sha1_provider(self):
 
226
        """A function that returns a SHA1Provider suitable for this tree.
 
227
 
 
228
        :return: None if content filtering is not supported by this tree.
 
229
          Otherwise, a SHA1Provider is returned that sha's the canonical
 
230
          form of files, i.e. after read filters are applied.
 
231
        """
 
232
        if self.supports_content_filtering():
 
233
            return ContentFilterAwareSHA1Provider(self)
 
234
        else:
 
235
            return None
 
236
 
264
237
    def filter_unversioned_files(self, paths):
265
238
        """Filter out paths that are versioned.
266
239
 
298
271
 
299
272
    def _generate_inventory(self):
300
273
        """Create and set self.inventory from the dirstate object.
301
 
        
 
274
 
302
275
        This is relatively expensive: we have to walk the entire dirstate.
303
276
        Ideally we would not, and can deprecate this function.
304
277
        """
349
322
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
350
323
                elif kind == 'tree-reference':
351
324
                    if not self._repo_supports_tree_reference:
352
 
                        raise AssertionError(
353
 
                            "repository of %r "
354
 
                            "doesn't support tree references "
355
 
                            "required by entry %r"
356
 
                            % (self, name))
 
325
                        raise errors.UnsupportedOperation(
 
326
                            self._generate_inventory,
 
327
                            self.branch.repository)
357
328
                    inv_entry.reference_revision = link_or_sha1 or None
358
329
                elif kind != 'symlink':
359
330
                    raise AssertionError("unknown kind %r" % kind)
374
345
        If either file_id or path is supplied, it is used as the key to lookup.
375
346
        If both are supplied, the fastest lookup is used, and an error is
376
347
        raised if they do not both point at the same row.
377
 
        
 
348
 
378
349
        :param file_id: An optional unicode file_id to be looked up.
379
350
        :param path: An optional unicode path to be looked up.
380
351
        :return: The dirstate row tuple for path/file_id, or (None, None)
436
407
    @needs_read_lock
437
408
    def get_parent_ids(self):
438
409
        """See Tree.get_parent_ids.
439
 
        
 
410
 
440
411
        This implementation requests the ids list from the dirstate file.
441
412
        """
442
413
        return self.current_dirstate().get_parent_ids()
464
435
        return osutils.lexists(pathjoin(
465
436
                    self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
466
437
 
 
438
    def has_or_had_id(self, file_id):
 
439
        state = self.current_dirstate()
 
440
        row, parents = self._get_entry(file_id=file_id)
 
441
        return row is not None
 
442
 
467
443
    @needs_read_lock
468
444
    def id2path(self, file_id):
469
445
        "Convert a file-id to a path."
575
551
    def _kind(self, relpath):
576
552
        abspath = self.abspath(relpath)
577
553
        kind = file_kind(abspath)
578
 
        if (self._repo_supports_tree_reference and
579
 
            kind == 'directory' and
580
 
            self._directory_is_tree_reference(relpath)):
581
 
            kind = 'tree-reference'
 
554
        if (self._repo_supports_tree_reference and kind == 'directory'):
 
555
            entry = self._get_entry(path=relpath)
 
556
            if entry[1] is not None:
 
557
                if entry[1][0][0] == 't':
 
558
                    kind = 'tree-reference'
582
559
        return kind
583
560
 
584
561
    @needs_read_lock
715
692
            from_entry = self._get_entry(path=from_rel)
716
693
            if from_entry == (None, None):
717
694
                raise errors.BzrMoveFailedError(from_rel,to_dir,
718
 
                    errors.NotVersionedError(path=str(from_rel)))
 
695
                    errors.NotVersionedError(path=from_rel))
719
696
 
720
697
            from_id = from_entry[0][2]
721
698
            to_rel = pathjoin(to_dir, from_tail)
896
873
        for tree in trees:
897
874
            if not (isinstance(tree, DirStateRevisionTree) and tree._revision_id in
898
875
                parents):
899
 
                return super(WorkingTree4, self).paths2ids(paths, trees, require_versioned)
 
876
                return super(DirStateWorkingTree, self).paths2ids(paths,
 
877
                    trees, require_versioned)
900
878
        search_indexes = [0] + [1 + parents.index(tree._revision_id) for tree in trees]
901
879
        # -- make all paths utf8 --
902
880
        paths_utf8 = set()
963
941
                raise errors.PathsNotVersionedError(paths)
964
942
        # -- remove redundancy in supplied paths to prevent over-scanning --
965
943
        search_paths = osutils.minimum_path_selection(paths)
966
 
        # sketch: 
 
944
        # sketch:
967
945
        # for all search_indexs in each path at or under each element of
968
946
        # search_paths, if the detail is relocated: add the id, and add the
969
947
        # relocated path as one to search if its not searched already. If the
1025
1003
 
1026
1004
    def read_working_inventory(self):
1027
1005
        """Read the working inventory.
1028
 
        
 
1006
 
1029
1007
        This is a meaningless operation for dirstate, but we obey it anyhow.
1030
1008
        """
1031
1009
        return self.inventory
1049
1027
    def set_last_revision(self, new_revision):
1050
1028
        """Change the last revision in the working tree."""
1051
1029
        parents = self.get_parent_ids()
1052
 
        if new_revision in (NULL_REVISION, None):
 
1030
        if new_revision in (_mod_revision.NULL_REVISION, None):
1053
1031
            if len(parents) >= 2:
1054
1032
                raise AssertionError(
1055
1033
                    "setting the last parent to none with a pending merge is "
1062
1040
    @needs_tree_write_lock
1063
1041
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
1064
1042
        """Set the parent ids to revision_ids.
1065
 
        
 
1043
 
1066
1044
        See also set_parent_trees. This api will try to retrieve the tree data
1067
1045
        for each element of revision_ids from the trees repository. If you have
1068
1046
        tree data already available, it is more efficient to use
1222
1200
                # just forget the whole block.
1223
1201
                entry_index = 0
1224
1202
                while entry_index < len(block[1]):
1225
 
                    # Mark this file id as having been removed
1226
1203
                    entry = block[1][entry_index]
1227
 
                    ids_to_unversion.discard(entry[0][2])
1228
 
                    if (entry[1][0][0] in 'ar' # don't remove absent or renamed
1229
 
                                               # entries
1230
 
                        or not state._make_absent(entry)):
 
1204
                    if entry[1][0][0] in 'ar':
 
1205
                        # don't remove absent or renamed entries
1231
1206
                        entry_index += 1
 
1207
                    else:
 
1208
                        # Mark this file id as having been removed
 
1209
                        ids_to_unversion.discard(entry[0][2])
 
1210
                        if not state._make_absent(entry):
 
1211
                            # The block has not shrunk.
 
1212
                            entry_index += 1
1232
1213
                # go to the next block. (At the moment we dont delete empty
1233
1214
                # dirblocks)
1234
1215
                block_index += 1
1286
1267
        if self._dirty:
1287
1268
            raise AssertionError("attempting to write an inventory when the "
1288
1269
                "dirstate is dirty will lose pending changes")
1289
 
        self.current_dirstate().set_state_from_inventory(inv)
1290
 
        self._make_dirty(reset_inventory=False)
1291
 
        if self._inventory is not None:
 
1270
        had_inventory = self._inventory is not None
 
1271
        # Setting self._inventory = None forces the dirstate to regenerate the
 
1272
        # working inventory. We do this because self.inventory may be inv, or
 
1273
        # may have been modified, and either case would prevent a clean delta
 
1274
        # being created.
 
1275
        self._inventory = None
 
1276
        # generate a delta,
 
1277
        delta = inv._make_delta(self.inventory)
 
1278
        # and apply it.
 
1279
        self.apply_inventory_delta(delta)
 
1280
        if had_inventory:
1292
1281
            self._inventory = inv
1293
1282
        self.flush()
1294
1283
 
1295
1284
 
1296
 
class WorkingTreeFormat4(WorkingTreeFormat3):
1297
 
    """The first consolidated dirstate working tree format.
1298
 
 
1299
 
    This format:
1300
 
        - exists within a metadir controlling .bzr
1301
 
        - includes an explicit version marker for the workingtree control
1302
 
          files, separate from the BzrDir format
1303
 
        - modifies the hash cache format
1304
 
        - is new in bzr 0.15
1305
 
        - uses a LockDir to guard access to it.
1306
 
    """
1307
 
 
1308
 
    upgrade_recommended = False
1309
 
 
1310
 
    _tree_class = WorkingTree4
1311
 
 
1312
 
    def get_format_string(self):
1313
 
        """See WorkingTreeFormat.get_format_string()."""
1314
 
        return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
1315
 
 
1316
 
    def get_format_description(self):
1317
 
        """See WorkingTreeFormat.get_format_description()."""
1318
 
        return "Working tree format 4"
 
1285
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
 
1286
 
 
1287
    def __init__(self, tree):
 
1288
        self.tree = tree
 
1289
 
 
1290
    def sha1(self, abspath):
 
1291
        """See dirstate.SHA1Provider.sha1()."""
 
1292
        filters = self.tree._content_filter_stack(
 
1293
            self.tree.relpath(osutils.safe_unicode(abspath)))
 
1294
        return internal_size_sha_file_byname(abspath, filters)[1]
 
1295
 
 
1296
    def stat_and_sha1(self, abspath):
 
1297
        """See dirstate.SHA1Provider.stat_and_sha1()."""
 
1298
        filters = self.tree._content_filter_stack(
 
1299
            self.tree.relpath(osutils.safe_unicode(abspath)))
 
1300
        file_obj = file(abspath, 'rb', 65000)
 
1301
        try:
 
1302
            statvalue = os.fstat(file_obj.fileno())
 
1303
            if filters:
 
1304
                file_obj = filtered_input_file(file_obj, filters)
 
1305
            sha1 = osutils.size_sha_file(file_obj)[1]
 
1306
        finally:
 
1307
            file_obj.close()
 
1308
        return statvalue, sha1
 
1309
 
 
1310
 
 
1311
class ContentFilteringDirStateWorkingTree(DirStateWorkingTree):
 
1312
    """Dirstate working tree that supports content filtering.
 
1313
 
 
1314
    The dirstate holds the hash and size of the canonical form of the file, 
 
1315
    and most methods must return that.
 
1316
    """
 
1317
 
 
1318
    def _file_content_summary(self, path, stat_result):
 
1319
        # This is to support the somewhat obsolete path_content_summary method
 
1320
        # with content filtering: see
 
1321
        # <https://bugs.edge.launchpad.net/bzr/+bug/415508>.
 
1322
        #
 
1323
        # If the dirstate cache is up to date and knows the hash and size,
 
1324
        # return that.
 
1325
        # Otherwise if there are no content filters, return the on-disk size
 
1326
        # and leave the hash blank.
 
1327
        # Otherwise, read and filter the on-disk file and use its size and
 
1328
        # hash.
 
1329
        #
 
1330
        # The dirstate doesn't store the size of the canonical form so we
 
1331
        # can't trust it for content-filtered trees.  We just return None.
 
1332
        dirstate_sha1 = self._dirstate.sha1_from_stat(path, stat_result)
 
1333
        executable = self._is_executable_from_path_and_stat(path, stat_result)
 
1334
        return ('file', None, executable, dirstate_sha1)
 
1335
 
 
1336
 
 
1337
class WorkingTree4(DirStateWorkingTree):
 
1338
    """This is the Format 4 working tree.
 
1339
 
 
1340
    This differs from WorkingTree3 by:
 
1341
     - Having a consolidated internal dirstate, stored in a
 
1342
       randomly-accessible sorted file on disk.
 
1343
     - Not having a regular inventory attribute.  One can be synthesized
 
1344
       on demand but this is expensive and should be avoided.
 
1345
 
 
1346
    This is new in bzr 0.15.
 
1347
    """
 
1348
 
 
1349
 
 
1350
class WorkingTree5(ContentFilteringDirStateWorkingTree):
 
1351
    """This is the Format 5 working tree.
 
1352
 
 
1353
    This differs from WorkingTree4 by:
 
1354
     - Supporting content filtering.
 
1355
 
 
1356
    This is new in bzr 1.11.
 
1357
    """
 
1358
 
 
1359
 
 
1360
class WorkingTree6(ContentFilteringDirStateWorkingTree):
 
1361
    """This is the Format 6 working tree.
 
1362
 
 
1363
    This differs from WorkingTree5 by:
 
1364
     - Supporting a current view that may mask the set of files in a tree
 
1365
       impacted by most user operations.
 
1366
 
 
1367
    This is new in bzr 1.14.
 
1368
    """
 
1369
 
 
1370
    def _make_views(self):
 
1371
        return views.PathBasedViews(self)
 
1372
 
 
1373
 
 
1374
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
1319
1375
 
1320
1376
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1321
1377
                   accelerator_tree=None, hardlink=False):
1361
1417
        wt.lock_tree_write()
1362
1418
        try:
1363
1419
            self._init_custom_control_files(wt)
1364
 
            if revision_id in (None, NULL_REVISION):
 
1420
            if revision_id in (None, _mod_revision.NULL_REVISION):
1365
1421
                if branch.repository.supports_rich_root():
1366
1422
                    wt._set_root_id(generate_ids.gen_root_id())
1367
1423
                else:
1378
1434
                    pass
1379
1435
            if basis is None:
1380
1436
                basis = branch.repository.revision_tree(revision_id)
1381
 
            if revision_id == NULL_REVISION:
 
1437
            if revision_id == _mod_revision.NULL_REVISION:
1382
1438
                parents_list = []
1383
1439
            else:
1384
1440
                parents_list = [(revision_id, basis)]
1392
1448
                if basis_root_id is not None:
1393
1449
                    wt._set_root_id(basis_root_id)
1394
1450
                    wt.flush()
 
1451
                if wt.supports_content_filtering():
 
1452
                    # The original tree may not have the same content filters
 
1453
                    # applied so we can't safely build the inventory delta from
 
1454
                    # the source tree.
 
1455
                    delta_from_tree = False
 
1456
                else:
 
1457
                    delta_from_tree = True
1395
1458
                # delta_from_tree is safe even for DirStateRevisionTrees,
1396
1459
                # because wt4.apply_inventory_delta does not mutate the input
1397
1460
                # inventory entries.
1398
1461
                transform.build_tree(basis, wt, accelerator_tree,
1399
 
                                     hardlink=hardlink, delta_from_tree=True)
 
1462
                                     hardlink=hardlink,
 
1463
                                     delta_from_tree=delta_from_tree)
1400
1464
            finally:
1401
1465
                basis.unlock()
1402
1466
        finally:
1406
1470
 
1407
1471
    def _init_custom_control_files(self, wt):
1408
1472
        """Subclasses with custom control files should override this method.
1409
 
        
 
1473
 
1410
1474
        The working tree and control files are locked for writing when this
1411
1475
        method is called.
1412
 
        
 
1476
 
1413
1477
        :param wt: the WorkingTree object
1414
1478
        """
1415
1479
 
1426
1490
                           _control_files=control_files)
1427
1491
 
1428
1492
    def __get_matchingbzrdir(self):
 
1493
        return self._get_matchingbzrdir()
 
1494
 
 
1495
    def _get_matchingbzrdir(self):
 
1496
        """Overrideable method to get a bzrdir for testing."""
1429
1497
        # please test against something that will let us do tree references
1430
1498
        return bzrdir.format_registry.make_bzrdir(
1431
1499
            'dirstate-with-subtree')
1433
1501
    _matchingbzrdir = property(__get_matchingbzrdir)
1434
1502
 
1435
1503
 
 
1504
class WorkingTreeFormat4(DirStateWorkingTreeFormat):
 
1505
    """The first consolidated dirstate working tree format.
 
1506
 
 
1507
    This format:
 
1508
        - exists within a metadir controlling .bzr
 
1509
        - includes an explicit version marker for the workingtree control
 
1510
          files, separate from the BzrDir format
 
1511
        - modifies the hash cache format
 
1512
        - is new in bzr 0.15
 
1513
        - uses a LockDir to guard access to it.
 
1514
    """
 
1515
 
 
1516
    upgrade_recommended = False
 
1517
 
 
1518
    _tree_class = WorkingTree4
 
1519
 
 
1520
    def get_format_string(self):
 
1521
        """See WorkingTreeFormat.get_format_string()."""
 
1522
        return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
 
1523
 
 
1524
    def get_format_description(self):
 
1525
        """See WorkingTreeFormat.get_format_description()."""
 
1526
        return "Working tree format 4"
 
1527
 
 
1528
 
 
1529
class WorkingTreeFormat5(DirStateWorkingTreeFormat):
 
1530
    """WorkingTree format supporting content filtering.
 
1531
    """
 
1532
 
 
1533
    upgrade_recommended = False
 
1534
 
 
1535
    _tree_class = WorkingTree5
 
1536
 
 
1537
    def get_format_string(self):
 
1538
        """See WorkingTreeFormat.get_format_string()."""
 
1539
        return "Bazaar Working Tree Format 5 (bzr 1.11)\n"
 
1540
 
 
1541
    def get_format_description(self):
 
1542
        """See WorkingTreeFormat.get_format_description()."""
 
1543
        return "Working tree format 5"
 
1544
 
 
1545
    def supports_content_filtering(self):
 
1546
        return True
 
1547
 
 
1548
 
 
1549
class WorkingTreeFormat6(DirStateWorkingTreeFormat):
 
1550
    """WorkingTree format supporting views.
 
1551
    """
 
1552
 
 
1553
    upgrade_recommended = False
 
1554
 
 
1555
    _tree_class = WorkingTree6
 
1556
 
 
1557
    def get_format_string(self):
 
1558
        """See WorkingTreeFormat.get_format_string()."""
 
1559
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
 
1560
 
 
1561
    def get_format_description(self):
 
1562
        """See WorkingTreeFormat.get_format_description()."""
 
1563
        return "Working tree format 6"
 
1564
 
 
1565
    def _init_custom_control_files(self, wt):
 
1566
        """Subclasses with custom control files should override this method."""
 
1567
        wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
 
1568
 
 
1569
    def supports_content_filtering(self):
 
1570
        return True
 
1571
 
 
1572
    def supports_views(self):
 
1573
        return True
 
1574
 
 
1575
 
1436
1576
class DirStateRevisionTree(Tree):
1437
 
    """A revision tree pulling the inventory from a dirstate."""
 
1577
    """A revision tree pulling the inventory from a dirstate.
 
1578
    
 
1579
    Note that this is one of the historical (ie revision) trees cached in the
 
1580
    dirstate for easy access, not the workingtree.
 
1581
    """
1438
1582
 
1439
1583
    def __init__(self, dirstate, revision_id, repository):
1440
1584
        self._dirstate = dirstate
1509
1653
        If either file_id or path is supplied, it is used as the key to lookup.
1510
1654
        If both are supplied, the fastest lookup is used, and an error is
1511
1655
        raised if they do not both point at the same row.
1512
 
        
 
1656
 
1513
1657
        :param file_id: An optional unicode file_id to be looked up.
1514
1658
        :param path: An optional unicode path to be looked up.
1515
1659
        :return: The dirstate row tuple for path/file_id, or (None, None)
1612
1756
            return None
1613
1757
        parent_index = self._get_parent_index()
1614
1758
        last_changed_revision = entry[1][parent_index][4]
1615
 
        return self._repository.get_revision(last_changed_revision).timestamp
 
1759
        try:
 
1760
            rev = self._repository.get_revision(last_changed_revision)
 
1761
        except errors.NoSuchRevision:
 
1762
            raise errors.FileTimestampUnavailable(self.id2path(file_id))
 
1763
        return rev.timestamp
1616
1764
 
1617
1765
    def get_file_sha1(self, file_id, path=None, stat_value=None):
1618
1766
        entry = self._get_entry(file_id=file_id, path=path)
1625
1773
    def get_file(self, file_id, path=None):
1626
1774
        return StringIO(self.get_file_text(file_id))
1627
1775
 
1628
 
    def get_file_lines(self, file_id):
1629
 
        return osutils.split_lines(self.get_file_text(file_id))
1630
 
 
1631
1776
    def get_file_size(self, file_id):
1632
1777
        """See Tree.get_file_size"""
1633
1778
        return self.inventory[file_id].text_size
1634
1779
 
1635
 
    def get_file_text(self, file_id):
1636
 
        return list(self.iter_files_bytes([(file_id, None)]))[0][1]
 
1780
    def get_file_text(self, file_id, path=None):
 
1781
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
1782
        return ''.join(content)
1637
1783
 
1638
1784
    def get_reference_revision(self, file_id, path=None):
1639
1785
        return self.inventory[file_id].reference_revision
1658
1804
        if entry[1][parent_index][0] != 'l':
1659
1805
            return None
1660
1806
        else:
1661
 
            # At present, none of the tree implementations supports non-ascii
1662
 
            # symlink targets. So we will just assume that the dirstate path is
1663
 
            # correct.
1664
 
            return entry[1][parent_index][1]
 
1807
            target = entry[1][parent_index][1]
 
1808
            target = target.decode('utf8')
 
1809
            return target
1665
1810
 
1666
1811
    def get_revision_id(self):
1667
1812
        """Return the revision id for this tree."""
1688
1833
        entry = self._get_entry(file_id=file_id)[1]
1689
1834
        if entry is None:
1690
1835
            raise errors.NoSuchId(tree=self, file_id=file_id)
1691
 
        return dirstate.DirState._minikind_to_kind[entry[1][0]]
 
1836
        parent_index = self._get_parent_index()
 
1837
        return dirstate.DirState._minikind_to_kind[entry[parent_index][0]]
1692
1838
 
1693
1839
    def stored_kind(self, file_id):
1694
1840
        """See Tree.stored_kind"""
1714
1860
            return None
1715
1861
        return ie.executable
1716
1862
 
1717
 
    def list_files(self, include_root=False):
 
1863
    def list_files(self, include_root=False, from_dir=None, recursive=True):
1718
1864
        # We use a standard implementation, because DirStateRevisionTree is
1719
1865
        # dealing with one of the parents of the current state
1720
1866
        inv = self._get_inventory()
1721
 
        entries = inv.iter_entries()
1722
 
        if self.inventory.root is not None and not include_root:
 
1867
        if from_dir is None:
 
1868
            from_dir_id = None
 
1869
        else:
 
1870
            from_dir_id = inv.path2id(from_dir)
 
1871
            if from_dir_id is None:
 
1872
                # Directory not versioned
 
1873
                return
 
1874
        entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
 
1875
        if inv.root is not None and not include_root and from_dir is None:
1723
1876
            entries.next()
1724
1877
        for path, entry in entries:
1725
1878
            yield path, 'V', entry.kind, entry.file_id, entry
1764
1917
 
1765
1918
    def walkdirs(self, prefix=""):
1766
1919
        # TODO: jam 20070215 This is the lazy way by using the RevisionTree
1767
 
        # implementation based on an inventory.  
 
1920
        # implementation based on an inventory.
1768
1921
        # This should be cleaned up to use the much faster Dirstate code
1769
1922
        # So for now, we just build up the parent inventory, and extract
1770
1923
        # it the same way RevisionTree does.
1799
1952
 
1800
1953
class InterDirStateTree(InterTree):
1801
1954
    """Fast path optimiser for changes_from with dirstate trees.
1802
 
    
1803
 
    This is used only when both trees are in the dirstate working file, and 
1804
 
    the source is any parent within the dirstate, and the destination is 
 
1955
 
 
1956
    This is used only when both trees are in the dirstate working file, and
 
1957
    the source is any parent within the dirstate, and the destination is
1805
1958
    the current working tree of the same dirstate.
1806
1959
    """
1807
1960
    # this could be generalized to allow comparisons between any trees in the
1827
1980
        return result
1828
1981
 
1829
1982
    @classmethod
1830
 
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source, target):
 
1983
    def make_source_parent_tree_compiled_dirstate(klass, test_case, source,
 
1984
                                                  target):
1831
1985
        from bzrlib.tests.test__dirstate_helpers import \
1832
 
            CompiledDirstateHelpersFeature
1833
 
        if not CompiledDirstateHelpersFeature.available():
1834
 
            from bzrlib.tests import UnavailableFeature
1835
 
            raise UnavailableFeature(CompiledDirstateHelpersFeature)
1836
 
        from bzrlib._dirstate_helpers_c import ProcessEntryC
 
1986
            compiled_dirstate_helpers_feature
 
1987
        test_case.requireFeature(compiled_dirstate_helpers_feature)
 
1988
        from bzrlib._dirstate_helpers_pyx import ProcessEntryC
1837
1989
        result = klass.make_source_parent_tree(source, target)
1838
1990
        result[1]._iter_changes = ProcessEntryC
1839
1991
        return result
1869
2021
            output. An unversioned file is defined as one with (False, False)
1870
2022
            for the versioned pair.
1871
2023
        """
1872
 
        # NB: show_status depends on being able to pass in non-versioned files
1873
 
        # and report them as unknown
1874
2024
        # TODO: handle extra trees in the dirstate.
1875
2025
        if (extra_trees or specific_files == []):
1876
2026
            # we can't fast-path these cases (yet)
1879
2029
                require_versioned, want_unversioned=want_unversioned)
1880
2030
        parent_ids = self.target.get_parent_ids()
1881
2031
        if not (self.source._revision_id in parent_ids
1882
 
                or self.source._revision_id == NULL_REVISION):
 
2032
                or self.source._revision_id == _mod_revision.NULL_REVISION):
1883
2033
            raise AssertionError(
1884
2034
                "revision {%s} is not stored in {%s}, but %s "
1885
2035
                "can only be used for trees stored in the dirstate"
1886
2036
                % (self.source._revision_id, self.target, self.iter_changes))
1887
2037
        target_index = 0
1888
 
        if self.source._revision_id == NULL_REVISION:
 
2038
        if self.source._revision_id == _mod_revision.NULL_REVISION:
1889
2039
            source_index = None
1890
2040
            indices = (target_index,)
1891
2041
        else:
1906
2056
        else:
1907
2057
            specific_files = set([''])
1908
2058
        # -- specific_files is now a utf8 path set --
1909
 
        search_specific_files = set()
 
2059
 
1910
2060
        # -- get the state object and prepare it.
1911
2061
        state = self.target.current_dirstate()
1912
2062
        state._read_dirblocks_if_needed()
1913
2063
        if require_versioned:
1914
2064
            # -- check all supplied paths are versioned in a search tree. --
1915
 
            all_versioned = True
 
2065
            not_versioned = []
1916
2066
            for path in specific_files:
1917
2067
                path_entries = state._entries_for_path(path)
1918
2068
                if not path_entries:
1919
2069
                    # this specified path is not present at all: error
1920
 
                    all_versioned = False
1921
 
                    break
 
2070
                    not_versioned.append(path)
 
2071
                    continue
1922
2072
                found_versioned = False
1923
2073
                # for each id at this path
1924
2074
                for entry in path_entries:
1931
2081
                if not found_versioned:
1932
2082
                    # none of the indexes was not 'absent' at all ids for this
1933
2083
                    # path.
1934
 
                    all_versioned = False
1935
 
                    break
1936
 
            if not all_versioned:
1937
 
                raise errors.PathsNotVersionedError(specific_files)
 
2084
                    not_versioned.append(path)
 
2085
            if len(not_versioned) > 0:
 
2086
                raise errors.PathsNotVersionedError(not_versioned)
1938
2087
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
1939
 
        for path in specific_files:
1940
 
            other_specific_files = specific_files.difference(set([path]))
1941
 
            if not osutils.is_inside_any(other_specific_files, path):
1942
 
                # this is a top level path, we must check it.
1943
 
                search_specific_files.add(path)
 
2088
        search_specific_files = osutils.minimum_path_selection(specific_files)
1944
2089
 
1945
2090
        use_filesystem_for_exec = (sys.platform != 'win32')
1946
2091
        iter_changes = self.target._iter_changes(include_unchanged,
1951
2096
    @staticmethod
1952
2097
    def is_compatible(source, target):
1953
2098
        # the target must be a dirstate working tree
1954
 
        if not isinstance(target, WorkingTree4):
 
2099
        if not isinstance(target, DirStateWorkingTree):
1955
2100
            return False
1956
 
        # the source must be a revtreee or dirstate rev tree.
 
2101
        # the source must be a revtree or dirstate rev tree.
1957
2102
        if not isinstance(source,
1958
2103
            (revisiontree.RevisionTree, DirStateRevisionTree)):
1959
2104
            return False
1960
2105
        # the source revid must be in the target dirstate
1961
 
        if not (source._revision_id == NULL_REVISION or
 
2106
        if not (source._revision_id == _mod_revision.NULL_REVISION or
1962
2107
            source._revision_id in target.get_parent_ids()):
1963
 
            # TODO: what about ghosts? it may well need to 
 
2108
            # TODO: what about ghosts? it may well need to
1964
2109
            # check for them explicitly.
1965
2110
            return False
1966
2111
        return True
1976
2121
 
1977
2122
    def convert(self, tree):
1978
2123
        # lock the control files not the tree, so that we dont get tree
1979
 
        # on-unlock behaviours, and so that noone else diddles with the 
 
2124
        # on-unlock behaviours, and so that noone else diddles with the
1980
2125
        # tree during upgrade.
1981
2126
        tree._control_files.lock_write()
1982
2127
        try:
2011
2156
        tree._transport.put_bytes('format',
2012
2157
            self.target_format.get_format_string(),
2013
2158
            mode=tree.bzrdir._get_file_mode())
 
2159
 
 
2160
 
 
2161
class Converter4to5(object):
 
2162
    """Perform an in-place upgrade of format 4 to format 5 trees."""
 
2163
 
 
2164
    def __init__(self):
 
2165
        self.target_format = WorkingTreeFormat5()
 
2166
 
 
2167
    def convert(self, tree):
 
2168
        # lock the control files not the tree, so that we don't get tree
 
2169
        # on-unlock behaviours, and so that no-one else diddles with the
 
2170
        # tree during upgrade.
 
2171
        tree._control_files.lock_write()
 
2172
        try:
 
2173
            self.update_format(tree)
 
2174
        finally:
 
2175
            tree._control_files.unlock()
 
2176
 
 
2177
    def update_format(self, tree):
 
2178
        """Change the format marker."""
 
2179
        tree._transport.put_bytes('format',
 
2180
            self.target_format.get_format_string(),
 
2181
            mode=tree.bzrdir._get_file_mode())
 
2182
 
 
2183
 
 
2184
class Converter4or5to6(object):
 
2185
    """Perform an in-place upgrade of format 4 or 5 to format 6 trees."""
 
2186
 
 
2187
    def __init__(self):
 
2188
        self.target_format = WorkingTreeFormat6()
 
2189
 
 
2190
    def convert(self, tree):
 
2191
        # lock the control files not the tree, so that we don't get tree
 
2192
        # on-unlock behaviours, and so that no-one else diddles with the
 
2193
        # tree during upgrade.
 
2194
        tree._control_files.lock_write()
 
2195
        try:
 
2196
            self.init_custom_control_files(tree)
 
2197
            self.update_format(tree)
 
2198
        finally:
 
2199
            tree._control_files.unlock()
 
2200
 
 
2201
    def init_custom_control_files(self, tree):
 
2202
        """Initialize custom control files."""
 
2203
        tree._transport.put_bytes('views', '',
 
2204
            mode=tree.bzrdir._get_file_mode())
 
2205
 
 
2206
    def update_format(self, tree):
 
2207
        """Change the format marker."""
 
2208
        tree._transport.put_bytes('format',
 
2209
            self.target_format.get_format_string(),
 
2210
            mode=tree.bzrdir._get_file_mode())