1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
See MemoryTree for more details.
22
from __future__ import absolute_import
29
revision as _mod_revision,
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):
39
"""A MemoryTree is a specialisation of MutableTree.
41
It maintains nearly no state outside of read_lock and write_lock
42
transactions. (it keeps a reference to the branch, and its last-revision
46
def __init__(self, branch, revision_id):
47
"""Construct a MemoryTree for branch using revision_id."""
49
self.bzrdir = branch.bzrdir
50
self._branch_revision_id = revision_id
52
self._lock_mode = None
54
def get_config_stack(self):
55
return self.branch.get_config_stack()
57
def is_control_filename(self, filename):
58
# Memory tree doesn't have any control filenames
61
@needs_tree_write_lock
62
def _add(self, files, ids, kinds):
63
"""See MutableTree._add."""
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)
73
"""See Tree.basis_tree()."""
74
return self._basis_tree
77
def create_on_branch(branch):
78
"""Create a MemoryTree for branch, using the last-revision of branch."""
79
revision_id = _mod_revision.ensure_null(branch.last_revision())
80
return MemoryTree(branch, revision_id)
82
def _gather_kinds(self, files, kinds):
83
"""See MutableTree._gather_kinds.
85
This implementation does not care about the file kind of
86
missing files, so is a no-op.
89
def get_file(self, file_id, path=None):
90
"""See Tree.get_file."""
92
path = self.id2path(file_id)
93
return self._file_transport.get(path)
95
def get_file_sha1(self, file_id, path=None, stat_value=None):
96
"""See Tree.get_file_sha1()."""
98
path = self.id2path(file_id)
99
stream = self._file_transport.get(path)
100
return sha_file(stream)
102
def get_root_id(self):
103
return self.path2id('')
105
def _comparison_data(self, entry, path):
106
"""See Tree._comparison_data."""
108
return None, False, None
109
return entry.kind, entry.executable, None
111
@needs_tree_write_lock
112
def rename_one(self, from_rel, to_rel):
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)
119
def path_content_summary(self, path):
120
"""See Tree.path_content_summary."""
121
id = self.path2id(path)
123
return 'missing', None, None, None
126
bytes = self._file_transport.get_bytes(path)
128
executable = self._inventory[id].executable
129
sha1 = None # no stat cache
130
return (kind, size, executable, sha1)
131
elif kind == 'directory':
132
# memory tree does not support nested trees yet.
133
return kind, None, None, None
134
elif kind == 'symlink':
135
raise NotImplementedError('symlink support')
137
raise NotImplementedError('unknown kind')
139
def _file_size(self, entry, stat_value):
140
"""See Tree._file_size."""
143
return entry.text_size
146
def get_parent_ids(self):
147
"""See Tree.get_parent_ids.
149
This implementation returns the current cached value from
152
return list(self._parent_ids)
154
def has_filename(self, filename):
155
"""See Tree.has_filename()."""
156
return self._file_transport.has(filename)
158
def is_executable(self, file_id, path=None):
159
return self._inventory[file_id].executable
161
def kind(self, file_id):
162
return self._inventory[file_id].kind
164
def mkdir(self, path, file_id=None):
165
"""See MutableTree.mkdir()."""
166
self.add(path, file_id, 'directory')
168
file_id = self.path2id(path)
169
self._file_transport.mkdir(path)
173
def last_revision(self):
174
"""See MutableTree.last_revision."""
175
return self._branch_revision_id
178
"""Lock the memory tree for reading.
180
This triggers population of data from the branch for its revision.
185
self.branch.lock_read()
186
self._lock_mode = "r"
187
self._populate_from_branch()
192
def lock_tree_write(self):
193
"""See MutableTree.lock_tree_write()."""
197
self.branch.lock_read()
198
self._lock_mode = "w"
199
self._populate_from_branch()
200
elif self._lock_mode == "r":
201
raise errors.ReadOnlyError(self)
206
def lock_write(self):
207
"""See MutableTree.lock_write()."""
211
self.branch.lock_write()
212
self._lock_mode = "w"
213
self._populate_from_branch()
214
elif self._lock_mode == "r":
215
raise errors.ReadOnlyError(self)
220
def _populate_from_branch(self):
221
"""Populate the in-tree state from the branch."""
223
if self._branch_revision_id == _mod_revision.NULL_REVISION:
224
self._parent_ids = []
226
self._parent_ids = [self._branch_revision_id]
227
self._inventory = Inventory(None, self._basis_tree.get_revision_id())
228
self._file_transport = MemoryTransport()
229
# TODO copy the revision trees content, or do it lazy, or something.
230
inventory_entries = self._basis_tree.iter_entries_by_dir()
231
for path, entry in inventory_entries:
232
self._inventory.add(entry.copy())
235
if entry.kind == 'directory':
236
self._file_transport.mkdir(path)
237
elif entry.kind == 'file':
238
self._file_transport.put_file(path,
239
self._basis_tree.get_file(entry.file_id))
241
raise NotImplementedError(self._populate_from_branch)
243
def put_file_bytes_non_atomic(self, file_id, bytes):
244
"""See MutableTree.put_file_bytes_non_atomic."""
245
self._file_transport.put_bytes(self.id2path(file_id), bytes)
250
This frees all cached state when the last lock context for the tree is
254
self._basis_tree = None
255
self._parent_ids = []
256
self._inventory = None
261
self._lock_mode = None
265
@needs_tree_write_lock
266
def unversion(self, file_ids):
267
"""Remove the file ids in file_ids from the current versioned set.
269
When a file_id is unversioned, all of its children are automatically
272
:param file_ids: The file ids to stop versioning.
273
:raises: NoSuchId if any fileid is not currently versioned.
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)
284
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
285
"""See MutableTree.set_parent_trees()."""
286
for revision_id in revision_ids:
287
_mod_revision.check_not_reserved_id(revision_id)
288
if len(revision_ids) == 0:
289
self._parent_ids = []
290
self._branch_revision_id = _mod_revision.NULL_REVISION
292
self._parent_ids = revision_ids
293
self._branch_revision_id = revision_ids[0]
294
self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
297
def _set_basis(self):
299
self._basis_tree = self.branch.repository.revision_tree(
300
self._branch_revision_id)
301
except errors.NoSuchRevision:
302
if self._allow_leftmost_as_ghost:
303
self._basis_tree = self.branch.repository.revision_tree(
304
_mod_revision.NULL_REVISION)
308
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
309
"""See MutableTree.set_parent_trees()."""
310
if len(parents_list) == 0:
311
self._parent_ids = []
312
self._basis_tree = self.branch.repository.revision_tree(
313
_mod_revision.NULL_REVISION)
315
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
316
# a ghost in the left most parent
317
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
318
self._parent_ids = [parent_id for parent_id, tree in parents_list]
319
if parents_list[0][1] is None or parents_list[0][1] == 'null:':
320
self._basis_tree = self.branch.repository.revision_tree(
321
_mod_revision.NULL_REVISION)
323
self._basis_tree = parents_list[0][1]
324
self._branch_revision_id = parents_list[0][0]