52
52
from bzrlib.symbol_versioning import deprecated_in, deprecated_method
53
53
from bzrlib.trace import mutter
54
from bzrlib.static_tuple import StaticTuple
57
56
class InventoryEntry(object):
959
958
descend(self.root, u'')
962
def path2id(self, relpath):
961
def path2id(self, name):
963
962
"""Walk down through directories to return entry of last component.
965
:param relpath: may be either a list of path components, or a single
966
string, in which case it is automatically split.
964
names may be either a list of path components, or a single
965
string, in which case it is automatically split.
968
967
This returns the entry of the last component in the path,
969
968
which may be either a file or a directory.
971
970
Returns None IFF the path is not found.
973
if isinstance(relpath, basestring):
974
names = osutils.splitpath(relpath)
972
if isinstance(name, basestring):
973
name = osutils.splitpath(name)
975
# mutter("lookup path %r" % name)
979
978
parent = self.root
1600
1599
interesting.add(None) # this will auto-filter it in the loop
1601
1600
remaining_parents.discard(None)
1602
1601
while remaining_parents:
1602
if None in remaining_parents:
1603
import pdb; pdb.set_trace()
1603
1604
next_parents = set()
1604
1605
for entry in self._getitems(remaining_parents):
1605
1606
next_parents.add(entry.parent_id)
1614
1615
while directories_to_expand:
1615
1616
# Expand directories by looking in the
1616
1617
# parent_id_basename_to_file_id map
1617
keys = [StaticTuple(f,).intern() for f in directories_to_expand]
1618
keys = [(f,) for f in directories_to_expand]
1618
1619
directories_to_expand = set()
1619
1620
items = self.parent_id_basename_to_file_id.iteritems(keys)
1620
1621
next_file_ids = set([item[1] for item in items])
1639
1640
parent_to_children) = self._expand_fileids_to_parents_and_children(
1640
1641
specific_fileids)
1641
# There is some overlap here, but we assume that all interesting items
1642
# are in the _fileid_to_entry_cache because we had to read them to
1643
# determine if they were a dir we wanted to recurse, or just a file
1644
# This should give us all the entries we'll want to add, so start
1646
other = Inventory(self.root_id)
1642
entries = self.iter_entries()
1643
other = Inventory(entries.next()[1].file_id)
1647
1644
other.root.revision = self.root.revision
1648
1645
other.revision_id = self.revision_id
1649
if not interesting or not parent_to_children:
1650
# empty filter, or filtering entrys that don't exist
1651
# (if even 1 existed, then we would have populated
1652
# parent_to_children with at least the tree root.)
1654
cache = self._fileid_to_entry_cache
1656
remaining_children = collections.deque(parent_to_children[self.root_id])
1658
import pdb; pdb.set_trace()
1660
while remaining_children:
1661
file_id = remaining_children.popleft()
1663
if ie.kind == 'directory':
1664
ie = ie.copy() # We create a copy to depopulate the .children attribute
1665
# TODO: depending on the uses of 'other' we should probably alwyas
1666
# '.copy()' to prevent someone from mutating other and
1667
# invaliding our internal cache
1669
if file_id in parent_to_children:
1670
remaining_children.extend(parent_to_children[file_id])
1646
for path, entry in entries:
1647
file_id = entry.file_id
1648
if file_id in interesting:
1649
other.add(entry.copy())
1677
1656
# to filter out empty names because of non rich-root...
1678
1657
sections = bytes.split('\n')
1679
1658
kind, file_id = sections[0].split(': ')
1680
return (sections[2], intern(file_id), intern(sections[3]))
1659
return (sections[2], file_id, sections[3])
1682
1661
def _bytes_to_entry(self, bytes):
1683
1662
"""Deserialise a serialised entry."""
1705
1684
result.reference_revision = sections[4]
1707
1686
raise ValueError("Not a serialised entry %r" % bytes)
1708
result.file_id = intern(result.file_id)
1709
result.revision = intern(sections[3])
1687
result.revision = sections[3]
1710
1688
if result.parent_id == '':
1711
1689
result.parent_id = None
1712
1690
self._fileid_to_entry_cache[result.file_id] = result
1811
1789
deletes.add(file_id)
1813
new_key = StaticTuple(file_id,)
1791
new_key = (file_id,)
1814
1792
new_value = result._entry_to_bytes(entry)
1815
1793
# Update caches. It's worth doing this whether
1816
1794
# we're propagating the old caches or not.
1819
1797
if old_path is None:
1822
old_key = StaticTuple(file_id,)
1800
old_key = (file_id,)
1823
1801
if self.id2path(file_id) != old_path:
1824
1802
raise errors.InconsistentDelta(old_path, file_id,
1825
1803
"Entry was at wrong other path %r." %
1826
1804
self.id2path(file_id))
1827
1805
altered.add(file_id)
1828
id_to_entry_delta.append(StaticTuple(old_key, new_key, new_value))
1806
id_to_entry_delta.append((old_key, new_key, new_value))
1829
1807
if result.parent_id_basename_to_file_id is not None:
1830
1808
# parent_id, basename changes
1831
1809
if old_path is None:
1918
1896
raise errors.BzrError('Duplicate key in inventory: %r\n%r'
1919
1897
% (key, bytes))
1920
1898
info[key] = value
1921
revision_id = intern(info['revision_id'])
1922
root_id = intern(info['root_id'])
1923
search_key_name = intern(info.get('search_key_name', 'plain'))
1924
parent_id_basename_to_file_id = intern(info.get(
1925
'parent_id_basename_to_file_id', None))
1926
if not parent_id_basename_to_file_id.startswith('sha1:'):
1927
raise ValueError('parent_id_basename_to_file_id should be a sha1'
1928
' key not %r' % (parent_id_basename_to_file_id,))
1899
revision_id = info['revision_id']
1900
root_id = info['root_id']
1901
search_key_name = info.get('search_key_name', 'plain')
1902
parent_id_basename_to_file_id = info.get(
1903
'parent_id_basename_to_file_id', None)
1929
1904
id_to_entry = info['id_to_entry']
1930
if not id_to_entry.startswith('sha1:'):
1931
raise ValueError('id_to_entry should be a sha1'
1932
' key not %r' % (id_to_entry,))
1934
1906
result = CHKInventory(search_key_name)
1935
1907
result.revision_id = revision_id
1938
1910
result._search_key_name)
1939
1911
if parent_id_basename_to_file_id is not None:
1940
1912
result.parent_id_basename_to_file_id = chk_map.CHKMap(
1941
chk_store, StaticTuple(parent_id_basename_to_file_id,),
1913
chk_store, (parent_id_basename_to_file_id,),
1942
1914
search_key_func=search_key_func)
1944
1916
result.parent_id_basename_to_file_id = None
1946
result.id_to_entry = chk_map.CHKMap(chk_store,
1947
StaticTuple(id_to_entry,),
1918
result.id_to_entry = chk_map.CHKMap(chk_store, (id_to_entry,),
1948
1919
search_key_func=search_key_func)
1949
1920
if (result.revision_id,) != expected_revision_id:
1950
1921
raise ValueError("Mismatched revision id and expected: %r, %r" %
1972
1943
id_to_entry_dict = {}
1973
1944
parent_id_basename_dict = {}
1974
1945
for path, entry in inventory.iter_entries():
1975
key = StaticTuple(entry.file_id,).intern()
1976
id_to_entry_dict[key] = entry_to_bytes(entry)
1946
id_to_entry_dict[(entry.file_id,)] = entry_to_bytes(entry)
1977
1947
p_id_key = parent_id_basename_key(entry)
1978
1948
parent_id_basename_dict[p_id_key] = entry.file_id
2002
1972
parent_id = entry.parent_id
2005
return StaticTuple(parent_id, entry.name.encode('utf8')).intern()
1975
return parent_id, entry.name.encode('utf8')
2007
1977
def __getitem__(self, file_id):
2008
1978
"""map a single file_id -> InventoryEntry."""
2015
1985
return self._bytes_to_entry(
2016
self.id_to_entry.iteritems([StaticTuple(file_id,)]).next()[1])
1986
self.id_to_entry.iteritems([(file_id,)]).next()[1])
2017
1987
except StopIteration:
2018
1988
# really we're passing an inventory, not a tree...
2019
1989
raise errors.NoSuchId(self, file_id)
2032
2002
remaining.append(file_id)
2034
2004
result.append(entry)
2035
file_keys = [StaticTuple(f,).intern() for f in remaining]
2005
file_keys = [(f,) for f in remaining]
2036
2006
for file_key, value in self.id_to_entry.iteritems(file_keys):
2037
2007
entry = self._bytes_to_entry(value)
2038
2008
result.append(entry)
2043
2013
# Perhaps have an explicit 'contains' method on CHKMap ?
2044
2014
if self._fileid_to_entry_cache.get(file_id, None) is not None:
2047
self.id_to_entry.iteritems([StaticTuple(file_id,)]))) == 1
2016
return len(list(self.id_to_entry.iteritems([(file_id,)]))) == 1
2049
2018
def is_root(self, file_id):
2050
2019
return file_id == self.root_id
2179
2148
delta.append((old_path, new_path, file_id, entry))
2182
def path2id(self, relpath):
2151
def path2id(self, name):
2183
2152
"""See CommonInventory.path2id()."""
2184
2153
# TODO: perhaps support negative hits?
2185
result = self._path_to_fileid_cache.get(relpath, None)
2154
result = self._path_to_fileid_cache.get(name, None)
2186
2155
if result is not None:
2188
if isinstance(relpath, basestring):
2189
names = osutils.splitpath(relpath)
2157
if isinstance(name, basestring):
2158
names = osutils.splitpath(name)
2192
2161
current_id = self.root_id
2193
2162
if current_id is None:
2195
2164
parent_id_index = self.parent_id_basename_to_file_id
2197
2165
for basename in names:
2198
if cur_path is None:
2201
cur_path = cur_path + '/' + basename
2166
# TODO: Cache each path we figure out in this function.
2202
2167
basename_utf8 = basename.encode('utf8')
2203
file_id = self._path_to_fileid_cache.get(cur_path, None)
2168
key_filter = [(current_id, basename_utf8)]
2170
for (parent_id, name_utf8), file_id in parent_id_index.iteritems(
2171
key_filter=key_filter):
2172
if parent_id != current_id or name_utf8 != basename_utf8:
2173
raise errors.BzrError("corrupt inventory lookup! "
2174
"%r %r %r %r" % (parent_id, current_id, name_utf8,
2204
2176
if file_id is None:
2205
key_filter = [StaticTuple(current_id, basename_utf8)]
2206
items = parent_id_index.iteritems(key_filter)
2207
for (parent_id, name_utf8), file_id in items:
2208
if parent_id != current_id or name_utf8 != basename_utf8:
2209
raise errors.BzrError("corrupt inventory lookup! "
2210
"%r %r %r %r" % (parent_id, current_id, name_utf8,
2215
self._path_to_fileid_cache[cur_path] = file_id
2216
2178
current_id = file_id
2179
self._path_to_fileid_cache[name] = current_id
2217
2180
return current_id
2219
2182
def to_lines(self):
2224
2187
lines.append('search_key_name: %s\n' % (self._search_key_name,))
2225
2188
lines.append("root_id: %s\n" % self.root_id)
2226
2189
lines.append('parent_id_basename_to_file_id: %s\n' %
2227
(self.parent_id_basename_to_file_id.key()[0],))
2190
self.parent_id_basename_to_file_id.key())
2228
2191
lines.append("revision_id: %s\n" % self.revision_id)
2229
lines.append("id_to_entry: %s\n" % (self.id_to_entry.key()[0],))
2192
lines.append("id_to_entry: %s\n" % self.id_to_entry.key())
2231
2194
lines.append("revision_id: %s\n" % self.revision_id)
2232
2195
lines.append("root_id: %s\n" % self.root_id)
2233
2196
if self.parent_id_basename_to_file_id is not None:
2234
2197
lines.append('parent_id_basename_to_file_id: %s\n' %
2235
(self.parent_id_basename_to_file_id.key()[0],))
2236
lines.append("id_to_entry: %s\n" % (self.id_to_entry.key()[0],))
2198
self.parent_id_basename_to_file_id.key())
2199
lines.append("id_to_entry: %s\n" % self.id_to_entry.key())
2280
2243
parent_id_index = self._chk_inventory.parent_id_basename_to_file_id
2281
2244
child_keys = set()
2282
2245
for (parent_id, name_utf8), file_id in parent_id_index.iteritems(
2283
key_filter=[StaticTuple(self.file_id,)]):
2284
child_keys.add(StaticTuple(file_id,))
2246
key_filter=[(self.file_id,)]):
2247
child_keys.add((file_id,))
2286
2249
for file_id_key in child_keys:
2287
2250
entry = self._chk_inventory._fileid_to_entry_cache.get(
2321
2284
factory = entry_factory[kind]
2322
2285
except KeyError:
2323
raise errors.BadFileKindError(name, kind)
2286
raise BzrError("unknown kind %r" % kind)
2324
2287
return factory(file_id, name, parent_id)