~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree_4.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
166
167
    @needs_tree_write_lock
167
168
    def add_reference(self, sub_tree):
168
169
        # use standard implementation, which calls back to self._add
169
 
        # 
 
170
        #
170
171
        # So we don't store the reference_revision in the working dirstate,
171
 
        # it's just recorded at the moment of commit. 
 
172
        # it's just recorded at the moment of commit.
172
173
        self._add_reference(sub_tree)
173
174
 
174
175
    def break_lock(self):
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
 
287
300
 
288
301
    def _generate_inventory(self):
289
302
        """Create and set self.inventory from the dirstate object.
290
 
        
 
303
 
291
304
        This is relatively expensive: we have to walk the entire dirstate.
292
305
        Ideally we would not, and can deprecate this function.
293
306
        """
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)
363
374
        If either file_id or path is supplied, it is used as the key to lookup.
364
375
        If both are supplied, the fastest lookup is used, and an error is
365
376
        raised if they do not both point at the same row.
366
 
        
 
377
 
367
378
        :param file_id: An optional unicode file_id to be looked up.
368
379
        :param path: An optional unicode path to be looked up.
369
380
        :return: The dirstate row tuple for path/file_id, or (None, None)
425
436
    @needs_read_lock
426
437
    def get_parent_ids(self):
427
438
        """See Tree.get_parent_ids.
428
 
        
 
439
 
429
440
        This implementation requests the ids list from the dirstate file.
430
441
        """
431
442
        return self.current_dirstate().get_parent_ids()
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
953
965
                raise errors.PathsNotVersionedError(paths)
954
966
        # -- remove redundancy in supplied paths to prevent over-scanning --
955
967
        search_paths = osutils.minimum_path_selection(paths)
956
 
        # sketch: 
 
968
        # sketch:
957
969
        # for all search_indexs in each path at or under each element of
958
970
        # search_paths, if the detail is relocated: add the id, and add the
959
971
        # relocated path as one to search if its not searched already. If the
1015
1027
 
1016
1028
    def read_working_inventory(self):
1017
1029
        """Read the working inventory.
1018
 
        
 
1030
 
1019
1031
        This is a meaningless operation for dirstate, but we obey it anyhow.
1020
1032
        """
1021
1033
        return self.inventory
1052
1064
    @needs_tree_write_lock
1053
1065
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
1054
1066
        """Set the parent ids to revision_ids.
1055
 
        
 
1067
 
1056
1068
        See also set_parent_trees. This api will try to retrieve the tree data
1057
1069
        for each element of revision_ids from the trees repository. If you have
1058
1070
        tree data already available, it is more efficient to use
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
        """Return the sha1 of a file given its absolute path."""
 
1305
        filters = self.tree._content_filter_stack(self.tree.relpath(abspath))
 
1306
        return internal_size_sha_file_byname(abspath, filters)[1]
 
1307
 
 
1308
    def stat_and_sha1(self, abspath):
 
1309
        """Return the stat and sha1 of a file given its absolute path."""
 
1310
        filters = self.tree._content_filter_stack(self.tree.relpath(abspath))
 
1311
        file_obj = file(abspath, 'rb', 65000)
 
1312
        try:
 
1313
            statvalue = os.fstat(file_obj.fileno())
 
1314
            if filters:
 
1315
                file_obj = filtered_input_file(file_obj, filters)
 
1316
            sha1 = osutils.size_sha_file(file_obj)[1]
 
1317
        finally:
 
1318
            file_obj.close()
 
1319
        return statvalue, sha1
 
1320
 
 
1321
 
1286
1322
class WorkingTree4(DirStateWorkingTree):
1287
1323
    """This is the Format 4 working tree.
1288
1324
 
1289
1325
    This differs from WorkingTree3 by:
1290
1326
     - Having a consolidated internal dirstate, stored in a
1291
1327
       randomly-accessible sorted file on disk.
1292
 
     - Not having a regular inventory attribute.  One can be synthesized 
 
1328
     - Not having a regular inventory attribute.  One can be synthesized
1293
1329
       on demand but this is expensive and should be avoided.
1294
1330
 
1295
1331
    This is new in bzr 0.15.
1301
1337
 
1302
1338
    This differs from WorkingTree4 by:
1303
1339
     - Supporting content filtering.
 
1340
 
 
1341
    This is new in bzr 1.11.
 
1342
    """
 
1343
 
 
1344
 
 
1345
class WorkingTree6(DirStateWorkingTree):
 
1346
    """This is the Format 6 working tree.
 
1347
 
 
1348
    This differs from WorkingTree5 by:
1304
1349
     - Supporting a current view that may mask the set of files in a tree
1305
1350
       impacted by most user operations.
1306
1351
 
1307
 
    This is new in bzr 1.11.
 
1352
    This is new in bzr 1.14.
1308
1353
    """
1309
1354
 
1310
1355
    def _make_views(self):
1401
1446
 
1402
1447
    def _init_custom_control_files(self, wt):
1403
1448
        """Subclasses with custom control files should override this method.
1404
 
        
 
1449
 
1405
1450
        The working tree and control files are locked for writing when this
1406
1451
        method is called.
1407
 
        
 
1452
 
1408
1453
        :param wt: the WorkingTree object
1409
1454
        """
1410
1455
 
1421
1466
                           _control_files=control_files)
1422
1467
 
1423
1468
    def __get_matchingbzrdir(self):
 
1469
        return self._get_matchingbzrdir()
 
1470
 
 
1471
    def _get_matchingbzrdir(self):
 
1472
        """Overrideable method to get a bzrdir for testing."""
1424
1473
        # please test against something that will let us do tree references
1425
1474
        return bzrdir.format_registry.make_bzrdir(
1426
1475
            'dirstate-with-subtree')
1454
1503
 
1455
1504
 
1456
1505
class WorkingTreeFormat5(DirStateWorkingTreeFormat):
1457
 
    """WorkingTree format supporting views.
 
1506
    """WorkingTree format supporting content filtering.
1458
1507
    """
1459
1508
 
1460
1509
    upgrade_recommended = False
1469
1518
        """See WorkingTreeFormat.get_format_description()."""
1470
1519
        return "Working tree format 5"
1471
1520
 
 
1521
    def supports_content_filtering(self):
 
1522
        return True
 
1523
 
 
1524
 
 
1525
class WorkingTreeFormat6(DirStateWorkingTreeFormat):
 
1526
    """WorkingTree format supporting views.
 
1527
    """
 
1528
 
 
1529
    upgrade_recommended = False
 
1530
 
 
1531
    _tree_class = WorkingTree6
 
1532
 
 
1533
    def get_format_string(self):
 
1534
        """See WorkingTreeFormat.get_format_string()."""
 
1535
        return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
 
1536
 
 
1537
    def get_format_description(self):
 
1538
        """See WorkingTreeFormat.get_format_description()."""
 
1539
        return "Working tree format 6"
 
1540
 
1472
1541
    def _init_custom_control_files(self, wt):
1473
1542
        """Subclasses with custom control files should override this method."""
1474
1543
        wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
1556
1625
        If either file_id or path is supplied, it is used as the key to lookup.
1557
1626
        If both are supplied, the fastest lookup is used, and an error is
1558
1627
        raised if they do not both point at the same row.
1559
 
        
 
1628
 
1560
1629
        :param file_id: An optional unicode file_id to be looked up.
1561
1630
        :param path: An optional unicode path to be looked up.
1562
1631
        :return: The dirstate row tuple for path/file_id, or (None, None)
1677
1746
        return self.inventory[file_id].text_size
1678
1747
 
1679
1748
    def get_file_text(self, file_id, path=None):
1680
 
        return list(self.iter_files_bytes([(file_id, None)]))[0][1]
 
1749
        _, content = list(self.iter_files_bytes([(file_id, None)]))[0]
 
1750
        return ''.join(content)
1681
1751
 
1682
1752
    def get_reference_revision(self, file_id, path=None):
1683
1753
        return self.inventory[file_id].reference_revision
1702
1772
        if entry[1][parent_index][0] != 'l':
1703
1773
            return None
1704
1774
        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]
 
1775
            target = entry[1][parent_index][1]
 
1776
            target = target.decode('utf8')
 
1777
            return target
1709
1778
 
1710
1779
    def get_revision_id(self):
1711
1780
        """Return the revision id for this tree."""
1808
1877
 
1809
1878
    def walkdirs(self, prefix=""):
1810
1879
        # TODO: jam 20070215 This is the lazy way by using the RevisionTree
1811
 
        # implementation based on an inventory.  
 
1880
        # implementation based on an inventory.
1812
1881
        # This should be cleaned up to use the much faster Dirstate code
1813
1882
        # So for now, we just build up the parent inventory, and extract
1814
1883
        # it the same way RevisionTree does.
1843
1912
 
1844
1913
class InterDirStateTree(InterTree):
1845
1914
    """Fast path optimiser for changes_from with dirstate trees.
1846
 
    
1847
 
    This is used only when both trees are in the dirstate working file, and 
1848
 
    the source is any parent within the dirstate, and the destination is 
 
1915
 
 
1916
    This is used only when both trees are in the dirstate working file, and
 
1917
    the source is any parent within the dirstate, and the destination is
1849
1918
    the current working tree of the same dirstate.
1850
1919
    """
1851
1920
    # this could be generalized to allow comparisons between any trees in the
2004
2073
        # the source revid must be in the target dirstate
2005
2074
        if not (source._revision_id == NULL_REVISION or
2006
2075
            source._revision_id in target.get_parent_ids()):
2007
 
            # TODO: what about ghosts? it may well need to 
 
2076
            # TODO: what about ghosts? it may well need to
2008
2077
            # check for them explicitly.
2009
2078
            return False
2010
2079
        return True
2020
2089
 
2021
2090
    def convert(self, tree):
2022
2091
        # lock the control files not the tree, so that we dont get tree
2023
 
        # on-unlock behaviours, and so that noone else diddles with the 
 
2092
        # on-unlock behaviours, and so that noone else diddles with the
2024
2093
        # tree during upgrade.
2025
2094
        tree._control_files.lock_write()
2026
2095
        try:
2065
2134
 
2066
2135
    def convert(self, tree):
2067
2136
        # lock the control files not the tree, so that we don't get tree
2068
 
        # on-unlock behaviours, and so that no-one else diddles with the 
 
2137
        # on-unlock behaviours, and so that no-one else diddles with the
 
2138
        # tree during upgrade.
 
2139
        tree._control_files.lock_write()
 
2140
        try:
 
2141
            self.update_format(tree)
 
2142
        finally:
 
2143
            tree._control_files.unlock()
 
2144
 
 
2145
    def update_format(self, tree):
 
2146
        """Change the format marker."""
 
2147
        tree._transport.put_bytes('format',
 
2148
            self.target_format.get_format_string(),
 
2149
            mode=tree.bzrdir._get_file_mode())
 
2150
 
 
2151
 
 
2152
class Converter4or5to6(object):
 
2153
    """Perform an in-place upgrade of format 4 or 5 to format 6 trees."""
 
2154
 
 
2155
    def __init__(self):
 
2156
        self.target_format = WorkingTreeFormat6()
 
2157
 
 
2158
    def convert(self, tree):
 
2159
        # lock the control files not the tree, so that we don't get tree
 
2160
        # on-unlock behaviours, and so that no-one else diddles with the
2069
2161
        # tree during upgrade.
2070
2162
        tree._control_files.lock_write()
2071
2163
        try: