~lifeless/ubuntu/lucid/bzr/2.1.2-sru

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-06-27 15:23:34 UTC
  • mfrom: (1.3.1 upstream) (3.1.78 karmic)
  • Revision ID: james.westby@ubuntu.com-20090627152334-u3smexjpaolh96qd
* New upstream release.
* Bump standards version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
 
69
69
 
70
70
from bzrlib import symbol_versioning
71
71
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
72
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
72
73
from bzrlib.inventory import InventoryEntry, Inventory, ROOT_ID, entry_factory
73
74
import bzrlib.mutabletree
74
75
from bzrlib.mutabletree import needs_tree_write_lock
213
214
            WorkingTree3._comparison_data(self, entry, path)
214
215
        # it looks like a plain directory, but it's really a reference -- see
215
216
        # also kind()
216
 
        if (self._repo_supports_tree_reference and
217
 
            kind == 'directory' and
218
 
            self._directory_is_tree_reference(path)):
 
217
        if (self._repo_supports_tree_reference and kind == 'directory'
 
218
            and entry is not None and entry.kind == 'tree-reference'):
219
219
            kind = 'tree-reference'
220
220
        return kind, executable, stat_value
221
221
 
247
247
            return self._dirstate
248
248
        local_path = self.bzrdir.get_workingtree_transport(None
249
249
            ).local_abspath('dirstate')
250
 
        self._dirstate = dirstate.DirState.on_file(local_path)
 
250
        self._dirstate = dirstate.DirState.on_file(local_path,
 
251
            self._sha1_provider())
251
252
        return self._dirstate
252
253
 
 
254
    def _sha1_provider(self):
 
255
        """A function that returns a SHA1Provider suitable for this tree.
 
256
 
 
257
        :return: None if content filtering is not supported by this tree.
 
258
          Otherwise, a SHA1Provider is returned that sha's the canonical
 
259
          form of files, i.e. after read filters are applied.
 
260
        """
 
261
        if self.supports_content_filtering():
 
262
            return ContentFilterAwareSHA1Provider(self)
 
263
        else:
 
264
            return None
 
265
 
253
266
    def filter_unversioned_files(self, paths):
254
267
        """Filter out paths that are versioned.
255
268
 
338
351
                    parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
339
352
                elif kind == 'tree-reference':
340
353
                    if not self._repo_supports_tree_reference:
341
 
                        raise AssertionError(
342
 
                            "repository of %r "
343
 
                            "doesn't support tree references "
344
 
                            "required by entry %r"
345
 
                            % (self, name))
 
354
                        raise errors.UnsupportedOperation(
 
355
                            self._generate_inventory,
 
356
                            self.branch.repository)
346
357
                    inv_entry.reference_revision = link_or_sha1 or None
347
358
                elif kind != 'symlink':
348
359
                    raise AssertionError("unknown kind %r" % kind)
564
575
    def _kind(self, relpath):
565
576
        abspath = self.abspath(relpath)
566
577
        kind = file_kind(abspath)
567
 
        if (self._repo_supports_tree_reference and
568
 
            kind == 'directory' and
569
 
            self._directory_is_tree_reference(relpath)):
570
 
            kind = 'tree-reference'
 
578
        if (self._repo_supports_tree_reference and kind == 'directory'):
 
579
            entry = self._get_entry(path=relpath)
 
580
            if entry[1] is not None:
 
581
                if entry[1][0][0] == 't':
 
582
                    kind = 'tree-reference'
571
583
        return kind
572
584
 
573
585
    @needs_read_lock
1283
1295
        self.flush()
1284
1296
 
1285
1297
 
 
1298
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
 
1299
 
 
1300
    def __init__(self, tree):
 
1301
        self.tree = tree
 
1302
 
 
1303
    def sha1(self, abspath):
 
1304
        """See dirstate.SHA1Provider.sha1()."""
 
1305
        filters = self.tree._content_filter_stack(
 
1306
            self.tree.relpath(osutils.safe_unicode(abspath)))
 
1307
        return internal_size_sha_file_byname(abspath, filters)[1]
 
1308
 
 
1309
    def stat_and_sha1(self, abspath):
 
1310
        """See dirstate.SHA1Provider.stat_and_sha1()."""
 
1311
        filters = self.tree._content_filter_stack(
 
1312
            self.tree.relpath(osutils.safe_unicode(abspath)))
 
1313
        file_obj = file(abspath, 'rb', 65000)
 
1314
        try:
 
1315
            statvalue = os.fstat(file_obj.fileno())
 
1316
            if filters:
 
1317
                file_obj = filtered_input_file(file_obj, filters)
 
1318
            sha1 = osutils.size_sha_file(file_obj)[1]
 
1319
        finally:
 
1320
            file_obj.close()
 
1321
        return statvalue, sha1
 
1322
 
 
1323
 
1286
1324
class WorkingTree4(DirStateWorkingTree):
1287
1325
    """This is the Format 4 working tree.
1288
1326
 
1301
1339
 
1302
1340
    This differs from WorkingTree4 by:
1303
1341
     - Supporting content filtering.
 
1342
 
 
1343
    This is new in bzr 1.11.
 
1344
    """
 
1345
 
 
1346
 
 
1347
class WorkingTree6(DirStateWorkingTree):
 
1348
    """This is the Format 6 working tree.
 
1349
 
 
1350
    This differs from WorkingTree5 by:
1304
1351
     - Supporting a current view that may mask the set of files in a tree
1305
1352
       impacted by most user operations.
1306
1353
 
1307
 
    This is new in bzr 1.11.
 
1354
    This is new in bzr 1.14.
1308
1355
    """
1309
1356
 
1310
1357
    def _make_views(self):
1387
1434
                if basis_root_id is not None:
1388
1435
                    wt._set_root_id(basis_root_id)
1389
1436
                    wt.flush()
 
1437
                # If content filtering is supported, do not use the accelerator
 
1438
                # tree - the cost of transforming the content both ways and
 
1439
                # checking for changed content can outweight the gains it gives.
 
1440
                # Note: do NOT move this logic up higher - using the basis from
 
1441
                # the accelerator tree is still desirable because that can save
 
1442
                # a minute or more of processing on large trees!
 
1443
                # The original tree may not have the same content filters
 
1444
                # applied so we can't safely build the inventory delta from
 
1445
                # the source tree.
 
1446
                if wt.supports_content_filtering():
 
1447
                    accelerator_tree = None
 
1448
                    delta_from_tree = False
 
1449
                else:
 
1450
                    delta_from_tree = True
1390
1451
                # delta_from_tree is safe even for DirStateRevisionTrees,
1391
1452
                # because wt4.apply_inventory_delta does not mutate the input
1392
1453
                # inventory entries.
1393
1454
                transform.build_tree(basis, wt, accelerator_tree,
1394
 
                                     hardlink=hardlink, delta_from_tree=True)
 
1455
                                     hardlink=hardlink,
 
1456
                                     delta_from_tree=delta_from_tree)
1395
1457
            finally:
1396
1458
                basis.unlock()
1397
1459
        finally:
1421
1483
                           _control_files=control_files)
1422
1484
 
1423
1485
    def __get_matchingbzrdir(self):
 
1486
        return self._get_matchingbzrdir()
 
1487
 
 
1488
    def _get_matchingbzrdir(self):
 
1489
        """Overrideable method to get a bzrdir for testing."""
1424
1490
        # please test against something that will let us do tree references
1425
1491
        return bzrdir.format_registry.make_bzrdir(
1426
1492
            'dirstate-with-subtree')
1454
1520
 
1455
1521
 
1456
1522
class WorkingTreeFormat5(DirStateWorkingTreeFormat):
1457
 
    """WorkingTree format supporting views.
 
1523
    """WorkingTree format supporting content filtering.
1458
1524
    """
1459
1525
 
1460
1526
    upgrade_recommended = False
1469
1535
        """See WorkingTreeFormat.get_format_description()."""
1470
1536
        return "Working tree format 5"
1471
1537
 
 
1538
    def supports_content_filtering(self):
 
1539
        return True
 
1540
 
 
1541
 
 
1542
class WorkingTreeFormat6(DirStateWorkingTreeFormat):
 
1543
    """WorkingTree format supporting views.
 
1544
    """
 
1545
 
 
1546
    upgrade_recommended = False
 
1547
 
 
1548
    _tree_class = WorkingTree6
 
1549
 
 
1550
    def get_format_string(self):
 
1551
        """See WorkingTreeFormat.get_format_string()."""
 
1552
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
 
1553
 
 
1554
    def get_format_description(self):
 
1555
        """See WorkingTreeFormat.get_format_description()."""
 
1556
        return "Working tree format 6"
 
1557
 
1472
1558
    def _init_custom_control_files(self, wt):
1473
1559
        """Subclasses with custom control files should override this method."""
1474
1560
        wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
1677
1763
        return self.inventory[file_id].text_size
1678
1764
 
1679
1765
    def get_file_text(self, file_id, path=None):
1680
 
        return list(self.iter_files_bytes([(file_id, None)]))[0][1]
 
1766
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
1767
        return ''.join(content)
1681
1768
 
1682
1769
    def get_reference_revision(self, file_id, path=None):
1683
1770
        return self.inventory[file_id].reference_revision
1702
1789
        if entry[1][parent_index][0] != 'l':
1703
1790
            return None
1704
1791
        else:
1705
 
            # At present, none of the tree implementations supports non-ascii
1706
 
            # symlink targets. So we will just assume that the dirstate path is
1707
 
            # correct.
1708
 
            return entry[1][parent_index][1]
 
1792
            target = entry[1][parent_index][1]
 
1793
            target = target.decode('utf8')
 
1794
            return target
1709
1795
 
1710
1796
    def get_revision_id(self):
1711
1797
        """Return the revision id for this tree."""
1950
2036
        else:
1951
2037
            specific_files = set([''])
1952
2038
        # -- specific_files is now a utf8 path set --
1953
 
        search_specific_files = set()
 
2039
 
1954
2040
        # -- get the state object and prepare it.
1955
2041
        state = self.target.current_dirstate()
1956
2042
        state._read_dirblocks_if_needed()
1980
2066
            if not all_versioned:
1981
2067
                raise errors.PathsNotVersionedError(specific_files)
1982
2068
        # -- remove redundancy in supplied specific_files to prevent over-scanning --
1983
 
        for path in specific_files:
1984
 
            other_specific_files = specific_files.difference(set([path]))
1985
 
            if not osutils.is_inside_any(other_specific_files, path):
1986
 
                # this is a top level path, we must check it.
1987
 
                search_specific_files.add(path)
 
2069
        search_specific_files = osutils.minimum_path_selection(specific_files)
1988
2070
 
1989
2071
        use_filesystem_for_exec = (sys.platform != 'win32')
1990
2072
        iter_changes = self.target._iter_changes(include_unchanged,
2069
2151
        # tree during upgrade.
2070
2152
        tree._control_files.lock_write()
2071
2153
        try:
 
2154
            self.update_format(tree)
 
2155
        finally:
 
2156
            tree._control_files.unlock()
 
2157
 
 
2158
    def update_format(self, tree):
 
2159
        """Change the format marker."""
 
2160
        tree._transport.put_bytes('format',
 
2161
            self.target_format.get_format_string(),
 
2162
            mode=tree.bzrdir._get_file_mode())
 
2163
 
 
2164
 
 
2165
class Converter4or5to6(object):
 
2166
    """Perform an in-place upgrade of format 4 or 5 to format 6 trees."""
 
2167
 
 
2168
    def __init__(self):
 
2169
        self.target_format = WorkingTreeFormat6()
 
2170
 
 
2171
    def convert(self, tree):
 
2172
        # lock the control files not the tree, so that we don't get tree
 
2173
        # on-unlock behaviours, and so that no-one else diddles with the
 
2174
        # tree during upgrade.
 
2175
        tree._control_files.lock_write()
 
2176
        try:
2072
2177
            self.init_custom_control_files(tree)
2073
2178
            self.update_format(tree)
2074
2179
        finally: