19
19
See MemoryTree for more details.
22
from __future__ import absolute_import
28
29
revision as _mod_revision,
30
from .bzr.inventory import Inventory
31
from .bzr.inventorytree import MutableInventoryTree
32
from .osutils import sha_file
33
from .transport.memory import MemoryTransport
36
class MemoryTree(MutableInventoryTree):
31
from bzrlib.decorators import needs_read_lock
32
from bzrlib.inventory import Inventory
33
from bzrlib.osutils import sha_file
34
from bzrlib.mutabletree import needs_tree_write_lock
35
from bzrlib.transport.memory import MemoryTransport
38
class MemoryTree(mutabletree.MutableInventoryTree):
37
39
"""A MemoryTree is a specialisation of MutableTree.
39
41
It maintains nearly no state outside of read_lock and write_lock
62
58
# Memory tree doesn't have any control filenames
61
@needs_tree_write_lock
65
62
def _add(self, files, ids, kinds):
66
63
"""See MutableTree._add."""
67
with self.lock_tree_write():
68
for f, file_id, kind in zip(files, ids, kinds):
70
st_mode = self._file_transport.stat(f).st_mode
71
if stat.S_ISREG(st_mode):
73
elif stat.S_ISLNK(st_mode):
75
elif stat.S_ISDIR(st_mode):
78
raise AssertionError('Unknown file kind')
80
self._inventory.add_path(f, kind=kind)
82
self._inventory.add_path(f, kind=kind, file_id=file_id)
64
for f, file_id, kind in zip(files, ids, kinds):
68
self._inventory.add_path(f, kind=kind)
70
self._inventory.add_path(f, kind=kind, file_id=file_id)
84
72
def basis_tree(self):
85
73
"""See Tree.basis_tree()."""
98
86
missing files, so is a no-op.
101
def get_file(self, path):
89
def get_file(self, file_id, path=None):
102
90
"""See Tree.get_file."""
92
path = self.id2path(file_id)
103
93
return self._file_transport.get(path)
105
def get_file_sha1(self, path, stat_value=None):
95
def get_file_sha1(self, file_id, path=None, stat_value=None):
106
96
"""See Tree.get_file_sha1()."""
98
path = self.id2path(file_id)
107
99
stream = self._file_transport.get(path)
108
100
return sha_file(stream)
102
def get_root_id(self):
103
return self.path2id('')
110
105
def _comparison_data(self, entry, path):
111
106
"""See Tree._comparison_data."""
112
107
if entry is None:
113
108
return None, False, None
114
109
return entry.kind, entry.executable, None
111
@needs_tree_write_lock
116
112
def rename_one(self, from_rel, to_rel):
117
with self.lock_tree_write():
118
file_id = self.path2id(from_rel)
119
to_dir, to_tail = os.path.split(to_rel)
120
to_parent_id = self.path2id(to_dir)
121
self._file_transport.move(from_rel, to_rel)
122
self._inventory.rename(file_id, to_parent_id, to_tail)
113
file_id = self.path2id(from_rel)
114
to_dir, to_tail = os.path.split(to_rel)
115
to_parent_id = self.path2id(to_dir)
116
self._file_transport.move(from_rel, to_rel)
117
self._inventory.rename(file_id, to_parent_id, to_tail)
124
119
def path_content_summary(self, path):
125
120
"""See Tree.path_content_summary."""
126
121
id = self.path2id(path)
128
123
return 'missing', None, None, None
129
kind = self.kind(path, id)
130
125
if kind == 'file':
131
126
bytes = self._file_transport.get_bytes(path)
132
127
size = len(bytes)
133
128
executable = self._inventory[id].executable
134
sha1 = None # no stat cache
129
sha1 = None # no stat cache
135
130
return (kind, size, executable, sha1)
136
131
elif kind == 'directory':
137
132
# memory tree does not support nested trees yet.
138
133
return kind, None, None, None
139
134
elif kind == 'symlink':
140
return kind, None, None, self._inventory[id].symlink_target
135
raise NotImplementedError('symlink support')
142
137
raise NotImplementedError('unknown kind')
139
def _file_size(self, entry, stat_value):
140
"""See Tree._file_size."""
143
return entry.text_size
144
146
def get_parent_ids(self):
145
147
"""See Tree.get_parent_ids.
147
149
This implementation returns the current cached value from
148
150
self._parent_ids.
150
with self.lock_read():
151
return list(self._parent_ids)
152
return list(self._parent_ids)
153
154
def has_filename(self, filename):
154
155
"""See Tree.has_filename()."""
155
156
return self._file_transport.has(filename)
157
def is_executable(self, path):
158
return self._inventory.get_entry_by_path(path).executable
158
def is_executable(self, file_id, path=None):
159
return self._inventory[file_id].executable
160
def kind(self, path):
161
return self._inventory.get_entry_by_path(path).kind
161
def kind(self, file_id):
162
return self._inventory[file_id].kind
163
164
def mkdir(self, path, file_id=None):
164
165
"""See MutableTree.mkdir()."""
237
235
if entry.kind == 'directory':
238
236
self._file_transport.mkdir(path)
239
elif entry.kind == 'symlink':
240
self._file_transport.symlink(entry.symlink_target, path)
241
237
elif entry.kind == 'file':
242
self._file_transport.put_file(
243
path, self._basis_tree.get_file(path))
238
self._file_transport.put_file(path,
239
self._basis_tree.get_file(entry.file_id))
245
241
raise NotImplementedError(self._populate_from_branch)
247
def put_file_bytes_non_atomic(self, path, bytes):
243
def put_file_bytes_non_atomic(self, file_id, bytes):
248
244
"""See MutableTree.put_file_bytes_non_atomic."""
249
self._file_transport.put_bytes(path, bytes)
245
self._file_transport.put_bytes(self.id2path(file_id), bytes)
251
247
def unlock(self):
252
248
"""Release a lock.
269
def unversion(self, paths):
270
"""Remove the paths from the current versioned set.
265
@needs_tree_write_lock
266
def unversion(self, file_ids):
267
"""Remove the file ids in file_ids from the current versioned set.
272
269
When a file_id is unversioned, all of its children are automatically
275
:param paths: The paths to stop versioning.
272
:param file_ids: The file ids to stop versioning.
276
273
:raises: NoSuchId if any fileid is not currently versioned.
278
with self.lock_tree_write():
279
# XXX: This should be in mutabletree, but the inventory-save action
280
# is not relevant to memory tree. Until that is done in unlock by
281
# working tree, we cannot share the implementation.
284
file_id = self.path2id(path)
286
raise errors.NoSuchFile(path)
287
file_ids.add(file_id)
288
for file_id in file_ids:
289
if self._inventory.has_id(file_id):
290
self._inventory.remove_recursive_id(file_id)
275
# XXX: This should be in mutabletree, but the inventory-save action
276
# is not relevant to memory tree. Until that is done in unlock by
277
# working tree, we cannot share the implementation.
278
for file_id in file_ids:
279
if self._inventory.has_id(file_id):
280
self._inventory.remove_recursive_id(file_id)
282
raise errors.NoSuchId(self, file_id)
292
284
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
293
285
"""See MutableTree.set_parent_trees()."""
316
def get_symlink_target(self, path):
317
with self.lock_read():
318
return self._file_transport.readlink(path)
320
308
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
321
309
"""See MutableTree.set_parent_trees()."""
322
310
if len(parents_list) == 0:
323
311
self._parent_ids = []
324
312
self._basis_tree = self.branch.repository.revision_tree(
325
_mod_revision.NULL_REVISION)
313
_mod_revision.NULL_REVISION)
327
315
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
328
316
# a ghost in the left most parent
329
317
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
330
318
self._parent_ids = [parent_id for parent_id, tree in parents_list]
331
if parents_list[0][1] is None or parents_list[0][1] == b'null:':
319
if parents_list[0][1] is None or parents_list[0][1] == 'null:':
332
320
self._basis_tree = self.branch.repository.revision_tree(
333
_mod_revision.NULL_REVISION)
321
_mod_revision.NULL_REVISION)
335
323
self._basis_tree = parents_list[0][1]
336
324
self._branch_revision_id = parents_list[0][0]