1
# Copyright (C) 2007-2011 Canonical Ltd
1
# Copyright (C) 2007-2012 Canonical Ltd
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
54
58
from bzrlib.lock import LogicalLockResult
55
59
from bzrlib.lockable_files import LockableFiles
56
60
from bzrlib.lockdir import LockDir
57
from bzrlib.mutabletree import needs_tree_write_lock
61
from bzrlib.mutabletree import (
63
needs_tree_write_lock,
58
65
from bzrlib.osutils import (
70
81
from bzrlib.workingtree import (
71
82
InventoryWorkingTree,
84
WorkingTreeFormatMetaDir,
77
88
class DirStateWorkingTree(InventoryWorkingTree):
79
_DEFAULT_WORTH_SAVING_LIMIT = 10
81
90
def __init__(self, basedir,
83
92
_control_files=None,
252
261
:return: an integer. -1 means never save.
254
config = self.branch.get_config()
255
val = config.get_user_option('bzr.workingtree.worth_saving_limit')
257
val = self._DEFAULT_WORTH_SAVING_LIMIT
261
except ValueError, e:
262
trace.warning('Invalid config value for'
263
' "bzr.workingtree.worth_saving_limit"'
264
' value %r is not an integer.'
266
val = self._DEFAULT_WORTH_SAVING_LIMIT
263
conf = self.get_config_stack()
264
return conf.get('bzr.workingtree.worth_saving_limit')
269
266
def filter_unversioned_files(self, paths):
270
267
"""Filter out paths that are versioned.
433
430
self._generate_inventory()
434
431
return self._inventory
433
@deprecated_method(deprecated_in((2, 5, 0)))
434
def _get_inventory(self):
435
return self.root_inventory
436
437
inventory = property(_get_inventory,
437
438
doc="Inventory of this Tree")
440
root_inventory = property(_get_root_inventory,
441
"Root inventory of this tree")
440
444
def get_parent_ids(self):
441
445
"""See Tree.get_parent_ids.
488
492
return False # Missing entries are not executable
489
493
return entry[1][0][3] # Executable?
491
if not osutils.supports_executable():
492
def is_executable(self, file_id, path=None):
493
"""Test if a file is executable or not.
495
def is_executable(self, file_id, path=None):
496
"""Test if a file is executable or not.
495
Note: The caller is expected to take a read-lock before calling this.
498
Note: The caller is expected to take a read-lock before calling this.
500
if not self._supports_executable():
497
501
entry = self._get_entry(file_id=file_id, path=path)
498
502
if entry == (None, None):
500
504
return entry[1][0][3]
502
_is_executable_from_path_and_stat = \
503
_is_executable_from_path_and_stat_from_basis
505
def is_executable(self, file_id, path=None):
506
"""Test if a file is executable or not.
508
Note: The caller is expected to take a read-lock before calling this.
510
506
self._must_be_locked()
512
508
path = self.id2path(file_id)
899
895
def path2id(self, path):
900
896
"""Return the id for path in this tree."""
897
if isinstance(path, list):
900
path = osutils.pathjoin(*path)
901
901
path = path.strip('/')
902
902
entry = self._get_entry(path=path)
903
903
if entry == (None, None):
981
981
all_versioned = False
983
983
if not all_versioned:
984
raise errors.PathsNotVersionedError(paths)
984
raise errors.PathsNotVersionedError(
985
[p.decode('utf-8') for p in paths])
985
986
# -- remove redundancy in supplied paths to prevent over-scanning --
986
987
search_paths = osutils.minimum_path_selection(paths)
1036
1037
found_dir_names = set(dir_name_id[:2] for dir_name_id in found)
1037
1038
for dir_name in split_paths:
1038
1039
if dir_name not in found_dir_names:
1039
raise errors.PathsNotVersionedError(paths)
1040
raise errors.PathsNotVersionedError(
1041
[p.decode('utf-8') for p in paths])
1041
1043
for dir_name_id, trees_info in found.iteritems():
1042
1044
for index in search_indexes:
1163
1165
# _make_delta if we can't get the RevisionTree
1166
delta = rev_tree.inventory._make_delta(basis_tree.inventory)
1168
delta = rev_tree.root_inventory._make_delta(
1169
basis_tree.root_inventory)
1167
1170
dirstate.update_basis_by_delta(delta, rev_id)
1169
1172
if not updated:
1366
1369
base_tree = trees[0][1]
1367
1370
state = self.current_dirstate()
1368
1371
# We don't support ghosts yet
1369
state.set_state_from_scratch(base_tree.inventory, trees, [])
1372
state.set_state_from_scratch(base_tree.root_inventory, trees, [])
1372
1375
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1494
1497
control_files = self._open_control_files(a_bzrdir)
1495
1498
control_files.create_lock()
1496
1499
control_files.lock_write()
1497
transport.put_bytes('format', self.get_format_string(),
1500
transport.put_bytes('format', self.as_string(),
1498
1501
mode=a_bzrdir._get_file_mode())
1499
1502
if from_branch is not None:
1500
1503
branch = from_branch
1560
1563
transform.build_tree(basis, wt, accelerator_tree,
1561
1564
hardlink=hardlink,
1562
1565
delta_from_tree=delta_from_tree)
1566
for hook in MutableTree.hooks['post_build_tree']:
1608
1613
def _get_matchingbzrdir(self):
1609
1614
"""Overrideable method to get a bzrdir for testing."""
1610
1615
# please test against something that will let us do tree references
1611
return bzrdir.format_registry.make_bzrdir(
1612
'dirstate-with-subtree')
1616
return controldir.format_registry.make_bzrdir(
1617
'development-subtree')
1614
1619
_matchingbzrdir = property(__get_matchingbzrdir)
1621
1626
- exists within a metadir controlling .bzr
1622
1627
- includes an explicit version marker for the workingtree control
1623
files, separate from the BzrDir format
1628
files, separate from the ControlDir format
1624
1629
- modifies the hash cache format
1625
1630
- is new in bzr 0.15
1626
1631
- uses a LockDir to guard access to it.
1631
1636
_tree_class = WorkingTree4
1633
def get_format_string(self):
1639
def get_format_string(cls):
1634
1640
"""See WorkingTreeFormat.get_format_string()."""
1635
1641
return "Bazaar Working Tree Format 4 (bzr 0.15)\n"
1648
1654
_tree_class = WorkingTree5
1650
def get_format_string(self):
1657
def get_format_string(cls):
1651
1658
"""See WorkingTreeFormat.get_format_string()."""
1652
1659
return "Bazaar Working Tree Format 5 (bzr 1.11)\n"
1668
1675
_tree_class = WorkingTree6
1670
def get_format_string(self):
1678
def get_format_string(cls):
1671
1679
"""See WorkingTreeFormat.get_format_string()."""
1672
1680
return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
1685
1693
def supports_views(self):
1696
def _get_matchingbzrdir(self):
1697
"""Overrideable method to get a bzrdir for testing."""
1698
# We use 'development-subtree' instead of '2a', because we have a
1699
# few tests that want to test tree references
1700
return bzrdir.format_registry.make_bzrdir('development-subtree')
1689
1703
class DirStateRevisionTree(InventoryTree):
1690
1704
"""A revision tree pulling the inventory from a dirstate.
1773
1787
if path is not None:
1774
1788
path = path.encode('utf8')
1775
1789
parent_index = self._get_parent_index()
1776
return self._dirstate._get_entry(parent_index, fileid_utf8=file_id, path_utf8=path)
1790
return self._dirstate._get_entry(parent_index, fileid_utf8=file_id,
1778
1793
def _generate_inventory(self):
1779
1794
"""Create and set self.inventory from the dirstate object.
1861
1876
# Make sure the file exists
1862
1877
entry = self._get_entry(file_id, path=path)
1863
1878
if entry == (None, None): # do we raise?
1879
raise errors.NoSuchId(self, file_id)
1865
1880
parent_index = self._get_parent_index()
1866
1881
last_changed_revision = entry[1][parent_index][4]
1881
1896
@needs_read_lock
1882
1897
def get_file_revision(self, file_id):
1883
return self.inventory[file_id].revision
1898
inv, inv_file_id = self._unpack_file_id(file_id)
1899
return inv[inv_file_id].revision
1885
1901
def get_file(self, file_id, path=None):
1886
1902
return StringIO(self.get_file_text(file_id))
1888
1904
def get_file_size(self, file_id):
1889
1905
"""See Tree.get_file_size"""
1890
return self.inventory[file_id].text_size
1906
inv, inv_file_id = self._unpack_file_id(file_id)
1907
return inv[inv_file_id].text_size
1892
1909
def get_file_text(self, file_id, path=None):
1893
_, content = list(self.iter_files_bytes([(file_id, None)]))[0]
1894
return ''.join(content)
1911
for _, content_iter in self.iter_files_bytes([(file_id, None)]):
1912
if content is not None:
1913
raise AssertionError('iter_files_bytes returned'
1914
' too many entries')
1915
# For each entry returned by iter_files_bytes, we must consume the
1916
# content_iter before we step the files iterator.
1917
content = ''.join(content_iter)
1919
raise AssertionError('iter_files_bytes did not return'
1920
' the requested data')
1896
1923
def get_reference_revision(self, file_id, path=None):
1897
return self.inventory[file_id].reference_revision
1924
inv, inv_file_id = self._unpack_file_id(file_id)
1925
return inv[inv_file_id].reference_revision
1899
1927
def iter_files_bytes(self, desired_files):
1900
1928
"""See Tree.iter_files_bytes.
1924
1952
"""Return the revision id for this tree."""
1925
1953
return self._revision_id
1927
def _get_inventory(self):
1955
def _get_root_inventory(self):
1928
1956
if self._inventory is not None:
1929
1957
return self._inventory
1930
1958
self._must_be_locked()
1931
1959
self._generate_inventory()
1932
1960
return self._inventory
1962
root_inventory = property(_get_root_inventory,
1963
doc="Inventory of this Tree")
1965
@deprecated_method(deprecated_in((2, 5, 0)))
1966
def _get_inventory(self):
1967
return self.root_inventory
1934
1969
inventory = property(_get_inventory,
1935
1970
doc="Inventory of this Tree")
1955
1990
def path_content_summary(self, path):
1956
1991
"""See Tree.path_content_summary."""
1957
id = self.inventory.path2id(path)
1992
inv, inv_file_id = self._path2inv_file_id(path)
1993
if inv_file_id is None:
1959
1994
return ('missing', None, None, None)
1960
entry = self._inventory[id]
1995
entry = inv[inv_file_id]
1961
1996
kind = entry.kind
1962
1997
if kind == 'file':
1963
1998
return (kind, entry.text_size, entry.executable, entry.text_sha1)
1967
2002
return (kind, None, None, None)
1969
2004
def is_executable(self, file_id, path=None):
1970
ie = self.inventory[file_id]
2005
inv, inv_file_id = self._unpack_file_id(file_id)
2006
ie = inv[inv_file_id]
1971
2007
if ie.kind != "file":
1973
2009
return ie.executable
1978
2014
def list_files(self, include_root=False, from_dir=None, recursive=True):
1979
2015
# We use a standard implementation, because DirStateRevisionTree is
1980
2016
# dealing with one of the parents of the current state
1981
inv = self._get_inventory()
1982
2017
if from_dir is None:
2018
inv = self.root_inventory
1983
2019
from_dir_id = None
1985
from_dir_id = inv.path2id(from_dir)
2021
inv, from_dir_id = self._path2inv_file_id(from_dir)
1986
2022
if from_dir_id is None:
1987
2023
# Directory not versioned
2025
# FIXME: Support nested trees
1989
2026
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
1990
2027
if inv.root is not None and not include_root and from_dir is None:
2013
2050
def path2id(self, path):
2014
2051
"""Return the id for path in this tree."""
2015
2052
# lookup by path: faster than splitting and walking the ivnentory.
2053
if isinstance(path, list):
2056
path = osutils.pathjoin(*path)
2016
2057
entry = self._get_entry(path=path)
2017
2058
if entry == (None, None):
2041
2082
# So for now, we just build up the parent inventory, and extract
2042
2083
# it the same way RevisionTree does.
2043
2084
_directory = 'directory'
2044
inv = self._get_inventory()
2085
inv = self._get_root_inventory()
2045
2086
top_id = inv.path2id(prefix)
2046
2087
if top_id is None:
2186
2227
path_entries = state._entries_for_path(path)
2187
2228
if not path_entries:
2188
2229
# this specified path is not present at all: error
2189
not_versioned.append(path)
2230
not_versioned.append(path.decode('utf-8'))
2191
2232
found_versioned = False
2192
2233
# for each id at this path
2200
2241
if not found_versioned:
2201
2242
# none of the indexes was not 'absent' at all ids for this
2203
not_versioned.append(path)
2244
not_versioned.append(path.decode('utf-8'))
2204
2245
if len(not_versioned) > 0:
2205
2246
raise errors.PathsNotVersionedError(not_versioned)
2206
2247
# -- remove redundancy in supplied specific_files to prevent over-scanning --
2273
2314
def update_format(self, tree):
2274
2315
"""Change the format marker."""
2275
2316
tree._transport.put_bytes('format',
2276
self.target_format.get_format_string(),
2317
self.target_format.as_string(),
2277
2318
mode=tree.bzrdir._get_file_mode())
2296
2337
def update_format(self, tree):
2297
2338
"""Change the format marker."""
2298
2339
tree._transport.put_bytes('format',
2299
self.target_format.get_format_string(),
2340
self.target_format.as_string(),
2300
2341
mode=tree.bzrdir._get_file_mode())
2325
2366
def update_format(self, tree):
2326
2367
"""Change the format marker."""
2327
2368
tree._transport.put_bytes('format',
2328
self.target_format.get_format_string(),
2369
self.target_format.as_string(),
2329
2370
mode=tree.bzrdir._get_file_mode())