1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
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
17
17
"""WorkingTree4 format and implementation.
29
29
from bzrlib.lazy_import import lazy_import
30
30
lazy_import(globals(), """
31
from bisect import bisect_left
33
from copy import deepcopy
42
35
from bzrlib import (
45
conflicts as _mod_conflicts,
55
43
revision as _mod_revision,
64
49
import bzrlib.branch
65
from bzrlib.transport import get_transport
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
54
from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
55
from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
72
56
import bzrlib.mutabletree
73
57
from bzrlib.mutabletree import needs_tree_write_lock
74
58
from bzrlib.osutils import (
84
from bzrlib.trace import mutter, note
65
from bzrlib.trace import mutter
85
66
from bzrlib.transport.local import LocalTransport
86
67
from bzrlib.tree import InterTree
87
from bzrlib.progress import DummyProgress, ProgressPhase
88
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
89
from bzrlib.rio import RioReader, rio_file, Stanza
90
from bzrlib.symbol_versioning import (deprecated_passed,
95
68
from bzrlib.tree import Tree
96
69
from bzrlib.workingtree import WorkingTree, WorkingTree3, WorkingTreeFormat3
129
102
self._setup_directory_is_tree_reference()
130
103
self._detect_case_handling()
131
104
self._rules_searcher = None
105
self.views = self._make_views()
132
106
#--- allow tests to select the dirstate iter_changes implementation
133
107
self._iter_changes = dirstate._process_entry
164
138
@needs_tree_write_lock
165
139
def add_reference(self, sub_tree):
166
140
# use standard implementation, which calls back to self._add
168
142
# So we don't store the reference_revision in the working dirstate,
169
# it's just recorded at the moment of commit.
143
# it's just recorded at the moment of commit.
170
144
self._add_reference(sub_tree)
172
146
def break_lock(self):
211
185
WorkingTree3._comparison_data(self, entry, path)
212
186
# it looks like a plain directory, but it's really a reference -- see
214
if (self._repo_supports_tree_reference and
215
kind == 'directory' and
216
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'):
217
190
kind = 'tree-reference'
218
191
return kind, executable, stat_value
245
218
return self._dirstate
246
219
local_path = self.bzrdir.get_workingtree_transport(None
247
220
).local_abspath('dirstate')
248
self._dirstate = dirstate.DirState.on_file(local_path)
221
self._dirstate = dirstate.DirState.on_file(local_path,
222
self._sha1_provider())
249
223
return self._dirstate
225
def _sha1_provider(self):
226
"""A function that returns a SHA1Provider suitable for this tree.
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.
232
if self.supports_content_filtering():
233
return ContentFilterAwareSHA1Provider(self)
251
237
def filter_unversioned_files(self, paths):
252
238
"""Filter out paths that are versioned.
286
272
def _generate_inventory(self):
287
273
"""Create and set self.inventory from the dirstate object.
289
275
This is relatively expensive: we have to walk the entire dirstate.
290
276
Ideally we would not, and can deprecate this function.
336
322
parent_ies[(dirname + '/' + name).strip('/')] = inv_entry
337
323
elif kind == 'tree-reference':
338
324
if not self._repo_supports_tree_reference:
339
raise AssertionError(
341
"doesn't support tree references "
342
"required by entry %r"
325
raise errors.UnsupportedOperation(
326
self._generate_inventory,
327
self.branch.repository)
344
328
inv_entry.reference_revision = link_or_sha1 or None
345
329
elif kind != 'symlink':
346
330
raise AssertionError("unknown kind %r" % kind)
361
345
If either file_id or path is supplied, it is used as the key to lookup.
362
346
If both are supplied, the fastest lookup is used, and an error is
363
347
raised if they do not both point at the same row.
365
349
:param file_id: An optional unicode file_id to be looked up.
366
350
:param path: An optional unicode path to be looked up.
367
351
:return: The dirstate row tuple for path/file_id, or (None, None)
451
435
return osutils.lexists(pathjoin(
452
436
self.basedir, row[0].decode('utf8'), row[1].decode('utf8')))
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
455
444
def id2path(self, file_id):
456
445
"Convert a file-id to a path."
562
551
def _kind(self, relpath):
563
552
abspath = self.abspath(relpath)
564
553
kind = file_kind(abspath)
565
if (self._repo_supports_tree_reference and
566
kind == 'directory' and
567
self._directory_is_tree_reference(relpath)):
568
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'
702
692
from_entry = self._get_entry(path=from_rel)
703
693
if from_entry == (None, None):
704
694
raise errors.BzrMoveFailedError(from_rel,to_dir,
705
errors.NotVersionedError(path=str(from_rel)))
695
errors.NotVersionedError(path=from_rel))
707
697
from_id = from_entry[0][2]
708
698
to_rel = pathjoin(to_dir, from_tail)
951
941
raise errors.PathsNotVersionedError(paths)
952
942
# -- remove redundancy in supplied paths to prevent over-scanning --
953
943
search_paths = osutils.minimum_path_selection(paths)
955
945
# for all search_indexs in each path at or under each element of
956
946
# search_paths, if the detail is relocated: add the id, and add the
957
947
# relocated path as one to search if its not searched already. If the
1037
1027
def set_last_revision(self, new_revision):
1038
1028
"""Change the last revision in the working tree."""
1039
1029
parents = self.get_parent_ids()
1040
if new_revision in (NULL_REVISION, None):
1030
if new_revision in (_mod_revision.NULL_REVISION, None):
1041
1031
if len(parents) >= 2:
1042
1032
raise AssertionError(
1043
1033
"setting the last parent to none with a pending merge is "
1050
1040
@needs_tree_write_lock
1051
1041
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
1052
1042
"""Set the parent ids to revision_ids.
1054
1044
See also set_parent_trees. This api will try to retrieve the tree data
1055
1045
for each element of revision_ids from the trees repository. If you have
1056
1046
tree data already available, it is more efficient to use
1210
1200
# just forget the whole block.
1211
1201
entry_index = 0
1212
1202
while entry_index < len(block[1]):
1213
# Mark this file id as having been removed
1214
1203
entry = block[1][entry_index]
1215
ids_to_unversion.discard(entry[0][2])
1216
if (entry[1][0][0] in 'ar' # don't remove absent or renamed
1218
or not state._make_absent(entry)):
1204
if entry[1][0][0] in 'ar':
1205
# don't remove absent or renamed entries
1219
1206
entry_index += 1
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.
1220
1213
# go to the next block. (At the moment we dont delete empty
1222
1215
block_index += 1
1277
class ContentFilterAwareSHA1Provider(dirstate.SHA1Provider):
1279
def __init__(self, tree):
1282
def sha1(self, abspath):
1283
"""See dirstate.SHA1Provider.sha1()."""
1284
filters = self.tree._content_filter_stack(
1285
self.tree.relpath(osutils.safe_unicode(abspath)))
1286
return internal_size_sha_file_byname(abspath, filters)[1]
1288
def stat_and_sha1(self, abspath):
1289
"""See dirstate.SHA1Provider.stat_and_sha1()."""
1290
filters = self.tree._content_filter_stack(
1291
self.tree.relpath(osutils.safe_unicode(abspath)))
1292
file_obj = file(abspath, 'rb', 65000)
1294
statvalue = os.fstat(file_obj.fileno())
1296
file_obj = filtered_input_file(file_obj, filters)
1297
sha1 = osutils.size_sha_file(file_obj)[1]
1300
return statvalue, sha1
1284
1303
class WorkingTree4(DirStateWorkingTree):
1285
1304
"""This is the Format 4 working tree.
1287
1306
This differs from WorkingTree3 by:
1288
1307
- Having a consolidated internal dirstate, stored in a
1289
1308
randomly-accessible sorted file on disk.
1290
- Not having a regular inventory attribute. One can be synthesized
1309
- Not having a regular inventory attribute. One can be synthesized
1291
1310
on demand but this is expensive and should be avoided.
1293
1312
This is new in bzr 0.15.
1300
1319
This differs from WorkingTree4 by:
1301
1320
- Supporting content filtering.
1322
This is new in bzr 1.11.
1326
class WorkingTree6(DirStateWorkingTree):
1327
"""This is the Format 6 working tree.
1329
This differs from WorkingTree5 by:
1302
1330
- Supporting a current view that may mask the set of files in a tree
1303
1331
impacted by most user operations.
1305
This is new in bzr 1.11.
1333
This is new in bzr 1.14.
1336
def _make_views(self):
1337
return views.PathBasedViews(self)
1309
1340
class DirStateWorkingTreeFormat(WorkingTreeFormat3):
1310
1341
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
1351
1382
wt.lock_tree_write()
1353
1384
self._init_custom_control_files(wt)
1354
if revision_id in (None, NULL_REVISION):
1385
if revision_id in (None, _mod_revision.NULL_REVISION):
1355
1386
if branch.repository.supports_rich_root():
1356
1387
wt._set_root_id(generate_ids.gen_root_id())
1369
1400
if basis is None:
1370
1401
basis = branch.repository.revision_tree(revision_id)
1371
if revision_id == NULL_REVISION:
1402
if revision_id == _mod_revision.NULL_REVISION:
1372
1403
parents_list = []
1374
1405
parents_list = [(revision_id, basis)]
1382
1413
if basis_root_id is not None:
1383
1414
wt._set_root_id(basis_root_id)
1416
# If content filtering is supported, do not use the accelerator
1417
# tree - the cost of transforming the content both ways and
1418
# checking for changed content can outweight the gains it gives.
1419
# Note: do NOT move this logic up higher - using the basis from
1420
# the accelerator tree is still desirable because that can save
1421
# a minute or more of processing on large trees!
1422
# The original tree may not have the same content filters
1423
# applied so we can't safely build the inventory delta from
1425
if wt.supports_content_filtering():
1426
accelerator_tree = None
1427
delta_from_tree = False
1429
delta_from_tree = True
1385
1430
# delta_from_tree is safe even for DirStateRevisionTrees,
1386
1431
# because wt4.apply_inventory_delta does not mutate the input
1387
1432
# inventory entries.
1388
1433
transform.build_tree(basis, wt, accelerator_tree,
1389
hardlink=hardlink, delta_from_tree=True)
1435
delta_from_tree=delta_from_tree)
1397
1443
def _init_custom_control_files(self, wt):
1398
1444
"""Subclasses with custom control files should override this method.
1400
1446
The working tree and control files are locked for writing when this
1401
1447
method is called.
1403
1449
:param wt: the WorkingTree object
1416
1462
_control_files=control_files)
1418
1464
def __get_matchingbzrdir(self):
1465
return self._get_matchingbzrdir()
1467
def _get_matchingbzrdir(self):
1468
"""Overrideable method to get a bzrdir for testing."""
1419
1469
# please test against something that will let us do tree references
1420
1470
return bzrdir.format_registry.make_bzrdir(
1421
1471
'dirstate-with-subtree')
1464
1514
"""See WorkingTreeFormat.get_format_description()."""
1465
1515
return "Working tree format 5"
1517
def supports_content_filtering(self):
1521
class WorkingTreeFormat6(DirStateWorkingTreeFormat):
1522
"""WorkingTree format supporting views.
1525
upgrade_recommended = False
1527
_tree_class = WorkingTree6
1529
def get_format_string(self):
1530
"""See WorkingTreeFormat.get_format_string()."""
1531
return "Bazaar Working Tree Format 6 (bzr 1.14)\n"
1533
def get_format_description(self):
1534
"""See WorkingTreeFormat.get_format_description()."""
1535
return "Working tree format 6"
1467
1537
def _init_custom_control_files(self, wt):
1468
1538
"""Subclasses with custom control files should override this method."""
1469
1539
wt._transport.put_bytes('views', '', mode=wt.bzrdir._get_file_mode())
1551
1621
If either file_id or path is supplied, it is used as the key to lookup.
1552
1622
If both are supplied, the fastest lookup is used, and an error is
1553
1623
raised if they do not both point at the same row.
1555
1625
:param file_id: An optional unicode file_id to be looked up.
1556
1626
:param path: An optional unicode path to be looked up.
1557
1627
:return: The dirstate row tuple for path/file_id, or (None, None)
1672
1742
return self.inventory[file_id].text_size
1674
1744
def get_file_text(self, file_id, path=None):
1675
return list(self.iter_files_bytes([(file_id, None)]))[0][1]
1745
_, content = list(self.iter_files_bytes([(file_id, None)]))[0]
1746
return ''.join(content)
1677
1748
def get_reference_revision(self, file_id, path=None):
1678
1749
return self.inventory[file_id].reference_revision
1697
1768
if entry[1][parent_index][0] != 'l':
1700
# At present, none of the tree implementations supports non-ascii
1701
# symlink targets. So we will just assume that the dirstate path is
1703
return entry[1][parent_index][1]
1771
target = entry[1][parent_index][1]
1772
target = target.decode('utf8')
1705
1775
def get_revision_id(self):
1706
1776
"""Return the revision id for this tree."""
1754
1824
return ie.executable
1756
def list_files(self, include_root=False):
1826
def list_files(self, include_root=False, from_dir=None, recursive=True):
1757
1827
# We use a standard implementation, because DirStateRevisionTree is
1758
1828
# dealing with one of the parents of the current state
1759
1829
inv = self._get_inventory()
1760
entries = inv.iter_entries()
1761
if self.inventory.root is not None and not include_root:
1830
if from_dir is None:
1833
from_dir_id = inv.path2id(from_dir)
1834
if from_dir_id is None:
1835
# Directory not versioned
1837
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
1838
if inv.root is not None and not include_root and from_dir is None:
1763
1840
for path, entry in entries:
1764
1841
yield path, 'V', entry.kind, entry.file_id, entry
1804
1881
def walkdirs(self, prefix=""):
1805
1882
# TODO: jam 20070215 This is the lazy way by using the RevisionTree
1806
# implementation based on an inventory.
1883
# implementation based on an inventory.
1807
1884
# This should be cleaned up to use the much faster Dirstate code
1808
1885
# So for now, we just build up the parent inventory, and extract
1809
1886
# it the same way RevisionTree does.
1839
1916
class InterDirStateTree(InterTree):
1840
1917
"""Fast path optimiser for changes_from with dirstate trees.
1842
This is used only when both trees are in the dirstate working file, and
1843
the source is any parent within the dirstate, and the destination is
1919
This is used only when both trees are in the dirstate working file, and
1920
the source is any parent within the dirstate, and the destination is
1844
1921
the current working tree of the same dirstate.
1846
1923
# this could be generalized to allow comparisons between any trees in the
1872
1949
if not CompiledDirstateHelpersFeature.available():
1873
1950
from bzrlib.tests import UnavailableFeature
1874
1951
raise UnavailableFeature(CompiledDirstateHelpersFeature)
1875
from bzrlib._dirstate_helpers_c import ProcessEntryC
1952
from bzrlib._dirstate_helpers_pyx import ProcessEntryC
1876
1953
result = klass.make_source_parent_tree(source, target)
1877
1954
result[1]._iter_changes = ProcessEntryC
1918
1995
require_versioned, want_unversioned=want_unversioned)
1919
1996
parent_ids = self.target.get_parent_ids()
1920
1997
if not (self.source._revision_id in parent_ids
1921
or self.source._revision_id == NULL_REVISION):
1998
or self.source._revision_id == _mod_revision.NULL_REVISION):
1922
1999
raise AssertionError(
1923
2000
"revision {%s} is not stored in {%s}, but %s "
1924
2001
"can only be used for trees stored in the dirstate"
1925
2002
% (self.source._revision_id, self.target, self.iter_changes))
1926
2003
target_index = 0
1927
if self.source._revision_id == NULL_REVISION:
2004
if self.source._revision_id == _mod_revision.NULL_REVISION:
1928
2005
source_index = None
1929
2006
indices = (target_index,)
1946
2023
specific_files = set([''])
1947
2024
# -- specific_files is now a utf8 path set --
1948
search_specific_files = set()
1949
2026
# -- get the state object and prepare it.
1950
2027
state = self.target.current_dirstate()
1951
2028
state._read_dirblocks_if_needed()
1952
2029
if require_versioned:
1953
2030
# -- check all supplied paths are versioned in a search tree. --
1954
all_versioned = True
1955
2032
for path in specific_files:
1956
2033
path_entries = state._entries_for_path(path)
1957
2034
if not path_entries:
1958
2035
# this specified path is not present at all: error
1959
all_versioned = False
2036
not_versioned.append(path)
1961
2038
found_versioned = False
1962
2039
# for each id at this path
1963
2040
for entry in path_entries:
1970
2047
if not found_versioned:
1971
2048
# none of the indexes was not 'absent' at all ids for this
1973
all_versioned = False
1975
if not all_versioned:
1976
raise errors.PathsNotVersionedError(specific_files)
2050
not_versioned.append(path)
2051
if len(not_versioned) > 0:
2052
raise errors.PathsNotVersionedError(not_versioned)
1977
2053
# -- remove redundancy in supplied specific_files to prevent over-scanning --
1978
for path in specific_files:
1979
other_specific_files = specific_files.difference(set([path]))
1980
if not osutils.is_inside_any(other_specific_files, path):
1981
# this is a top level path, we must check it.
1982
search_specific_files.add(path)
2054
search_specific_files = osutils.minimum_path_selection(specific_files)
1984
2056
use_filesystem_for_exec = (sys.platform != 'win32')
1985
2057
iter_changes = self.target._iter_changes(include_unchanged,
1997
2069
(revisiontree.RevisionTree, DirStateRevisionTree)):
1999
2071
# the source revid must be in the target dirstate
2000
if not (source._revision_id == NULL_REVISION or
2072
if not (source._revision_id == _mod_revision.NULL_REVISION or
2001
2073
source._revision_id in target.get_parent_ids()):
2002
# TODO: what about ghosts? it may well need to
2074
# TODO: what about ghosts? it may well need to
2003
2075
# check for them explicitly.
2016
2088
def convert(self, tree):
2017
2089
# lock the control files not the tree, so that we dont get tree
2018
# on-unlock behaviours, and so that noone else diddles with the
2090
# on-unlock behaviours, and so that noone else diddles with the
2019
2091
# tree during upgrade.
2020
2092
tree._control_files.lock_write()
2061
2133
def convert(self, tree):
2062
2134
# lock the control files not the tree, so that we don't get tree
2063
# on-unlock behaviours, and so that no-one else diddles with the
2135
# on-unlock behaviours, and so that no-one else diddles with the
2136
# tree during upgrade.
2137
tree._control_files.lock_write()
2139
self.update_format(tree)
2141
tree._control_files.unlock()
2143
def update_format(self, tree):
2144
"""Change the format marker."""
2145
tree._transport.put_bytes('format',
2146
self.target_format.get_format_string(),
2147
mode=tree.bzrdir._get_file_mode())
2150
class Converter4or5to6(object):
2151
"""Perform an in-place upgrade of format 4 or 5 to format 6 trees."""
2154
self.target_format = WorkingTreeFormat6()
2156
def convert(self, tree):
2157
# lock the control files not the tree, so that we don't get tree
2158
# on-unlock behaviours, and so that no-one else diddles with the
2064
2159
# tree during upgrade.
2065
2160
tree._control_files.lock_write()