/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: Jonathan Riddell
  • Date: 2011-05-31 13:52:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5954.
  • Revision ID: jriddell@canonical.com-20110531135218-v6fxs6pad89awidf
Make error message less specific (might not be a local disk issue) and pass through zlib error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 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
31
31
import errno
32
32
import stat
33
33
 
34
 
import bzrlib
35
34
from bzrlib import (
36
35
    bzrdir,
37
36
    cache_utf8,
 
37
    conflicts as _mod_conflicts,
38
38
    debug,
39
39
    dirstate,
40
40
    errors,
 
41
    filters as _mod_filters,
41
42
    generate_ids,
42
43
    osutils,
43
44
    revision as _mod_revision,
46
47
    transform,
47
48
    views,
48
49
    )
49
 
import bzrlib.branch
50
 
import bzrlib.ui
51
50
""")
52
51
 
53
52
from bzrlib.decorators import needs_read_lock, needs_write_lock
54
 
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
55
53
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
56
54
from bzrlib.lock import LogicalLockResult
 
55
from bzrlib.lockable_files import LockableFiles
 
56
from bzrlib.lockdir import LockDir
57
57
from bzrlib.mutabletree import needs_tree_write_lock
58
58
from bzrlib.osutils import (
59
59
    file_kind,
62
62
    realpath,
63
63
    safe_unicode,
64
64
    )
65
 
from bzrlib.trace import mutter
66
65
from bzrlib.transport.local import LocalTransport
67
 
from bzrlib.tree import InterTree
68
 
from bzrlib.tree import Tree
69
 
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
70
 
 
71
 
 
72
 
class DirStateWorkingTree(WorkingTree3):
 
66
from bzrlib.tree import (
 
67
    InterTree,
 
68
    InventoryTree,
 
69
    )
 
70
from bzrlib.workingtree import (
 
71
    InventoryWorkingTree,
 
72
    WorkingTree,
 
73
    WorkingTreeFormat,
 
74
    )
 
75
 
 
76
 
 
77
class DirStateWorkingTree(InventoryWorkingTree):
 
78
 
 
79
    _DEFAULT_WORTH_SAVING_LIMIT = 10
 
80
 
73
81
    def __init__(self, basedir,
74
82
                 branch,
75
83
                 _control_files=None,
85
93
        self._format = _format
86
94
        self.bzrdir = _bzrdir
87
95
        basedir = safe_unicode(basedir)
88
 
        mutter("opening working tree %r", basedir)
 
96
        trace.mutter("opening working tree %r", basedir)
89
97
        self._branch = branch
90
98
        self.basedir = realpath(basedir)
91
99
        # if branch is at our basedir and is a format 6 or less
125
133
            state.add(f, file_id, kind, None, '')
126
134
        self._make_dirty(reset_inventory=True)
127
135
 
 
136
    def _get_check_refs(self):
 
137
        """Return the references needed to perform a check of this tree."""
 
138
        return [('trees', self.last_revision())]
 
139
 
128
140
    def _make_dirty(self, reset_inventory):
129
141
        """Make the tree state dirty.
130
142
 
182
194
 
183
195
    def _comparison_data(self, entry, path):
184
196
        kind, executable, stat_value = \
185
 
            WorkingTree3._comparison_data(self, entry, path)
 
197
            WorkingTree._comparison_data(self, entry, path)
186
198
        # it looks like a plain directory, but it's really a reference -- see
187
199
        # also kind()
188
200
        if (self._repo_supports_tree_reference and kind == 'directory'
194
206
    def commit(self, message=None, revprops=None, *args, **kwargs):
195
207
        # mark the tree as dirty post commit - commit
196
208
        # can change the current versioned list by doing deletes.
197
 
        result = WorkingTree3.commit(self, message, revprops, *args, **kwargs)
 
209
        result = WorkingTree.commit(self, message, revprops, *args, **kwargs)
198
210
        self._make_dirty(reset_inventory=True)
199
211
        return result
200
212
 
219
231
        local_path = self.bzrdir.get_workingtree_transport(None
220
232
            ).local_abspath('dirstate')
221
233
        self._dirstate = dirstate.DirState.on_file(local_path,
222
 
            self._sha1_provider())
 
234
            self._sha1_provider(), self._worth_saving_limit())
223
235
        return self._dirstate
224
236
 
225
237
    def _sha1_provider(self):
234
246
        else:
235
247
            return None
236
248
 
 
249
    def _worth_saving_limit(self):
 
250
        """How many hash changes are ok before we must save the dirstate.
 
251
 
 
252
        :return: an integer. -1 means never save.
 
253
        """
 
254
        config = self.branch.get_config()
 
255
        val = config.get_user_option('bzr.workingtree.worth_saving_limit')
 
256
        if val is None:
 
257
            val = self._DEFAULT_WORTH_SAVING_LIMIT
 
258
        else:
 
259
            try:
 
260
                val = int(val)
 
261
            except ValueError, e:
 
262
                trace.warning('Invalid config value for'
 
263
                              ' "bzr.workingtree.worth_saving_limit"'
 
264
                              ' value %r is not an integer.'
 
265
                              % (val,))
 
266
                val = self._DEFAULT_WORTH_SAVING_LIMIT
 
267
        return val
 
268
 
237
269
    def filter_unversioned_files(self, paths):
238
270
        """Filter out paths that are versioned.
239
271
 
369
401
        state = self.current_dirstate()
370
402
        if stat_value is None:
371
403
            try:
372
 
                stat_value = os.lstat(file_abspath)
 
404
                stat_value = osutils.lstat(file_abspath)
373
405
            except OSError, e:
374
406
                if e.errno == errno.ENOENT:
375
407
                    return None
478
510
            self._must_be_locked()
479
511
            if not path:
480
512
                path = self.id2path(file_id)
481
 
            mode = os.lstat(self.abspath(path)).st_mode
 
513
            mode = osutils.lstat(self.abspath(path)).st_mode
482
514
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
483
515
 
484
516
    def all_file_ids(self):
850
882
                rollback_rename()
851
883
                raise
852
884
            result.append((from_rel, to_rel))
853
 
            state._dirblock_state = dirstate.DirState.IN_MEMORY_MODIFIED
 
885
            state._mark_modified()
854
886
            self._make_dirty(reset_inventory=False)
855
887
 
856
888
        return result
1113
1145
                        _mod_revision.NULL_REVISION)))
1114
1146
                ghosts.append(rev_id)
1115
1147
            accepted_revisions.add(rev_id)
1116
 
        dirstate.set_parent_trees(real_trees, ghosts=ghosts)
 
1148
        updated = False
 
1149
        if (len(real_trees) == 1
 
1150
            and not ghosts
 
1151
            and self.branch.repository._format.fast_deltas
 
1152
            and isinstance(real_trees[0][1],
 
1153
                revisiontree.InventoryRevisionTree)
 
1154
            and self.get_parent_ids()):
 
1155
            rev_id, rev_tree = real_trees[0]
 
1156
            basis_id = self.get_parent_ids()[0]
 
1157
            # There are times when basis_tree won't be in
 
1158
            # self.branch.repository, (switch, for example)
 
1159
            try:
 
1160
                basis_tree = self.branch.repository.revision_tree(basis_id)
 
1161
            except errors.NoSuchRevision:
 
1162
                # Fall back to the set_parent_trees(), since we can't use
 
1163
                # _make_delta if we can't get the RevisionTree
 
1164
                pass
 
1165
            else:
 
1166
                delta = rev_tree.inventory._make_delta(basis_tree.inventory)
 
1167
                dirstate.update_basis_by_delta(delta, rev_id)
 
1168
                updated = True
 
1169
        if not updated:
 
1170
            dirstate.set_parent_trees(real_trees, ghosts=ghosts)
1117
1171
        self._make_dirty(reset_inventory=False)
1118
1172
 
1119
1173
    def _set_root_id(self, file_id):
1139
1193
 
1140
1194
    def unlock(self):
1141
1195
        """Unlock in format 4 trees needs to write the entire dirstate."""
1142
 
        # do non-implementation specific cleanup
1143
 
        self._cleanup()
1144
 
 
1145
1196
        if self._control_files._lock_count == 1:
 
1197
            # do non-implementation specific cleanup
 
1198
            self._cleanup()
 
1199
 
1146
1200
            # eventually we should do signature checking during read locks for
1147
1201
            # dirstate updates.
1148
1202
            if self._control_files._lock_mode == 'w':
1247
1301
        # have to change the legacy inventory too.
1248
1302
        if self._inventory is not None:
1249
1303
            for file_id in file_ids:
1250
 
                self._inventory.remove_recursive_id(file_id)
 
1304
                if self._inventory.has_id(file_id):
 
1305
                    self._inventory.remove_recursive_id(file_id)
1251
1306
 
1252
1307
    @needs_tree_write_lock
1253
1308
    def rename_one(self, from_rel, to_rel, after=False):
1254
1309
        """See WorkingTree.rename_one"""
1255
1310
        self.flush()
1256
 
        WorkingTree.rename_one(self, from_rel, to_rel, after)
 
1311
        super(DirStateWorkingTree, self).rename_one(from_rel, to_rel, after)
1257
1312
 
1258
1313
    @needs_tree_write_lock
1259
1314
    def apply_inventory_delta(self, changes):
1292
1347
            self._inventory = inv
1293
1348
        self.flush()
1294
1349
 
 
1350
    @needs_tree_write_lock
 
1351
    def reset_state(self, revision_ids=None):
 
1352
        """Reset the state of the working tree.
 
1353
 
 
1354
        This does a hard-reset to a last-known-good state. This is a way to
 
1355
        fix if something got corrupted (like the .bzr/checkout/dirstate file)
 
1356
        """
 
1357
        if revision_ids is None:
 
1358
            revision_ids = self.get_parent_ids()
 
1359
        if not revision_ids:
 
1360
            base_tree = self.branch.repository.revision_tree(
 
1361
                _mod_revision.NULL_REVISION)
 
1362
            trees = []
 
1363
        else:
 
1364
            trees = zip(revision_ids,
 
1365
                        self.branch.repository.revision_trees(revision_ids))
 
1366
            base_tree = trees[0][1]
 
1367
        state = self.current_dirstate()
 
1368
        # We don't support ghosts yet
 
1369
        state.set_state_from_scratch(base_tree.inventory, trees, [])
 
1370
 
1295
1371
 
1296
1372
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1297
1373
 
1302
1378
        """See dirstate.SHA1Provider.sha1()."""
1303
1379
        filters = self.tree._content_filter_stack(
1304
1380
            self.tree.relpath(osutils.safe_unicode(abspath)))
1305
 
        return internal_size_sha_file_byname(abspath, filters)[1]
 
1381
        return _mod_filters.internal_size_sha_file_byname(abspath, filters)[1]
1306
1382
 
1307
1383
    def stat_and_sha1(self, abspath):
1308
1384
        """See dirstate.SHA1Provider.stat_and_sha1()."""
1312
1388
        try:
1313
1389
            statvalue = os.fstat(file_obj.fileno())
1314
1390
            if filters:
1315
 
                file_obj = filtered_input_file(file_obj, filters)
 
1391
                file_obj = _mod_filters.filtered_input_file(file_obj, filters)
1316
1392
            sha1 = osutils.size_sha_file(file_obj)[1]
1317
1393
        finally:
1318
1394
            file_obj.close()
1329
1405
    def _file_content_summary(self, path, stat_result):
1330
1406
        # This is to support the somewhat obsolete path_content_summary method
1331
1407
        # with content filtering: see
1332
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/415508>.
 
1408
        # <https://bugs.launchpad.net/bzr/+bug/415508>.
1333
1409
        #
1334
1410
        # If the dirstate cache is up to date and knows the hash and size,
1335
1411
        # return that.
1348
1424
class WorkingTree4(DirStateWorkingTree):
1349
1425
    """This is the Format 4 working tree.
1350
1426
 
1351
 
    This differs from WorkingTree3 by:
 
1427
    This differs from WorkingTree by:
1352
1428
     - Having a consolidated internal dirstate, stored in a
1353
1429
       randomly-accessible sorted file on disk.
1354
1430
     - Not having a regular inventory attribute.  One can be synthesized
1382
1458
        return views.PathBasedViews(self)
1383
1459
 
1384
1460
 
1385
 
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
 
1461
class DirStateWorkingTreeFormat(WorkingTreeFormat):
 
1462
 
 
1463
    missing_parent_conflicts = True
 
1464
 
 
1465
    _lock_class = LockDir
 
1466
    _lock_file_name = 'lock'
 
1467
 
 
1468
    def _open_control_files(self, a_bzrdir):
 
1469
        transport = a_bzrdir.get_workingtree_transport(None)
 
1470
        return LockableFiles(transport, self._lock_file_name,
 
1471
                             self._lock_class)
1386
1472
 
1387
1473
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1388
1474
                   accelerator_tree=None, hardlink=False):
1389
1475
        """See WorkingTreeFormat.initialize().
1390
1476
 
1391
1477
        :param revision_id: allows creating a working tree at a different
1392
 
        revision than the branch is at.
 
1478
            revision than the branch is at.
1393
1479
        :param accelerator_tree: A tree which can be used for retrieving file
1394
1480
            contents more quickly than the revision tree, i.e. a workingtree.
1395
1481
            The revision tree will be used for cases where accelerator_tree's
1488
1574
        :param wt: the WorkingTree object
1489
1575
        """
1490
1576
 
 
1577
    def open(self, a_bzrdir, _found=False):
 
1578
        """Return the WorkingTree object for a_bzrdir
 
1579
 
 
1580
        _found is a private parameter, do not use it. It is used to indicate
 
1581
               if format probing has already been done.
 
1582
        """
 
1583
        if not _found:
 
1584
            # we are being called directly and must probe.
 
1585
            raise NotImplementedError
 
1586
        if not isinstance(a_bzrdir.transport, LocalTransport):
 
1587
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
 
1588
        wt = self._open(a_bzrdir, self._open_control_files(a_bzrdir))
 
1589
        return wt
 
1590
 
1491
1591
    def _open(self, a_bzrdir, control_files):
1492
1592
        """Open the tree itself.
1493
1593
 
1584
1684
        return True
1585
1685
 
1586
1686
 
1587
 
class DirStateRevisionTree(Tree):
 
1687
class DirStateRevisionTree(InventoryTree):
1588
1688
    """A revision tree pulling the inventory from a dirstate.
1589
1689
    
1590
1690
    Note that this is one of the historical (ie revision) trees cached in the
1609
1709
    def annotate_iter(self, file_id,
1610
1710
                      default_revision=_mod_revision.CURRENT_REVISION):
1611
1711
        """See Tree.annotate_iter"""
1612
 
        text_key = (file_id, self.inventory[file_id].revision)
 
1712
        text_key = (file_id, self.get_file_revision(file_id))
1613
1713
        annotations = self._repository.texts.annotate(text_key)
1614
1714
        return [(key[-1], line) for (key, line) in annotations]
1615
1715
 
1737
1837
                elif kind == 'directory':
1738
1838
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
1739
1839
                elif kind == 'symlink':
1740
 
                    inv_entry.executable = False
1741
 
                    inv_entry.text_size = None
1742
1840
                    inv_entry.symlink_target = utf8_decode(fingerprint)[0]
1743
1841
                elif kind == 'tree-reference':
1744
1842
                    inv_entry.reference_revision = fingerprint or None
1781
1879
            return parent_details[1]
1782
1880
        return None
1783
1881
 
 
1882
    @needs_read_lock
 
1883
    def get_file_revision(self, file_id):
 
1884
        return self.inventory[file_id].revision
 
1885
 
1784
1886
    def get_file(self, file_id, path=None):
1785
1887
        return StringIO(self.get_file_text(file_id))
1786
1888
 
1809
1911
                                       identifier))
1810
1912
        return self._repository.iter_files_bytes(repo_desired_files)
1811
1913
 
1812
 
    def get_symlink_target(self, file_id):
 
1914
    def get_symlink_target(self, file_id, path=None):
1813
1915
        entry = self._get_entry(file_id=file_id)
1814
1916
        parent_index = self._get_parent_index()
1815
1917
        if entry[1][parent_index][0] != 'l':
1868
1970
    def is_executable(self, file_id, path=None):
1869
1971
        ie = self.inventory[file_id]
1870
1972
        if ie.kind != "file":
1871
 
            return None
 
1973
            return False
1872
1974
        return ie.executable
1873
1975
 
1874
1976
    def is_locked(self):
1987
2089
    def make_source_parent_tree(source, target):
1988
2090
        """Change the source tree into a parent of the target."""
1989
2091
        revid = source.commit('record tree')
1990
 
        target.branch.repository.fetch(source.branch.repository, revid)
 
2092
        target.branch.fetch(source.branch, revid)
1991
2093
        target.set_parent_ids([revid])
1992
2094
        return target.basis_tree(), target
1993
2095