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 .decorators import needs_read_lock
32
from .bzr.inventory import Inventory
33
from .bzr.inventorytree import MutableInventoryTree
34
from .osutils import sha_file
35
from .mutabletree import needs_tree_write_lock
36
from .transport.memory import MemoryTransport
39
class MemoryTree(MutableInventoryTree):
40
"""A MemoryTree is a specialisation of MutableTree.
42
It maintains nearly no state outside of read_lock and write_lock
43
transactions. (it keeps a reference to the branch, and its last-revision
47
def __init__(self, branch, revision_id):
48
"""Construct a MemoryTree for branch using revision_id."""
50
self.controldir = branch.controldir
51
self._branch_revision_id = revision_id
53
self._lock_mode = None
55
def get_config_stack(self):
56
return self.branch.get_config_stack()
58
def is_control_filename(self, filename):
59
# Memory tree doesn't have any control filenames
62
@needs_tree_write_lock
63
def _add(self, files, ids, kinds):
64
"""See MutableTree._add."""
65
for f, file_id, kind in zip(files, ids, kinds):
69
self._inventory.add_path(f, kind=kind)
71
self._inventory.add_path(f, kind=kind, file_id=file_id)
74
"""See Tree.basis_tree()."""
75
return self._basis_tree
78
def create_on_branch(branch):
79
"""Create a MemoryTree for branch, using the last-revision of branch."""
80
revision_id = _mod_revision.ensure_null(branch.last_revision())
81
return MemoryTree(branch, revision_id)
83
def _gather_kinds(self, files, kinds):
84
"""See MutableTree._gather_kinds.
86
This implementation does not care about the file kind of
87
missing files, so is a no-op.
90
def get_file(self, file_id, path=None):
91
"""See Tree.get_file."""
93
path = self.id2path(file_id)
94
return self._file_transport.get(path)
96
def get_file_sha1(self, file_id, path=None, stat_value=None):
97
"""See Tree.get_file_sha1()."""
99
path = self.id2path(file_id)
100
stream = self._file_transport.get(path)
101
return sha_file(stream)
103
def get_root_id(self):
104
return self.path2id('')
106
def _comparison_data(self, entry, path):
107
"""See Tree._comparison_data."""
109
return None, False, None
110
return entry.kind, entry.executable, None
112
@needs_tree_write_lock
113
def rename_one(self, from_rel, to_rel):
114
file_id = self.path2id(from_rel)
115
to_dir, to_tail = os.path.split(to_rel)
116
to_parent_id = self.path2id(to_dir)
117
self._file_transport.move(from_rel, to_rel)
118
self._inventory.rename(file_id, to_parent_id, to_tail)
120
def path_content_summary(self, path):
121
"""See Tree.path_content_summary."""
122
id = self.path2id(path)
124
return 'missing', None, None, None
127
bytes = self._file_transport.get_bytes(path)
129
executable = self._inventory[id].executable
130
sha1 = None # no stat cache
131
return (kind, size, executable, sha1)
132
elif kind == 'directory':
133
# memory tree does not support nested trees yet.
134
return kind, None, None, None
135
elif kind == 'symlink':
136
raise NotImplementedError('symlink support')
138
raise NotImplementedError('unknown kind')
140
def _file_size(self, entry, stat_value):
141
"""See Tree._file_size."""
144
return entry.text_size
147
def get_parent_ids(self):
148
"""See Tree.get_parent_ids.
150
This implementation returns the current cached value from
153
return list(self._parent_ids)
155
def has_filename(self, filename):
156
"""See Tree.has_filename()."""
157
return self._file_transport.has(filename)
159
def is_executable(self, file_id, path=None):
160
return self._inventory[file_id].executable
162
def kind(self, file_id):
163
return self._inventory[file_id].kind
165
def mkdir(self, path, file_id=None):
166
"""See MutableTree.mkdir()."""
167
self.add(path, file_id, 'directory')
169
file_id = self.path2id(path)
170
self._file_transport.mkdir(path)
174
def last_revision(self):
175
"""See MutableTree.last_revision."""
176
return self._branch_revision_id
179
"""Lock the memory tree for reading.
181
This triggers population of data from the branch for its revision.
186
self.branch.lock_read()
187
self._lock_mode = "r"
188
self._populate_from_branch()
193
def lock_tree_write(self):
194
"""See MutableTree.lock_tree_write()."""
198
self.branch.lock_read()
199
self._lock_mode = "w"
200
self._populate_from_branch()
201
elif self._lock_mode == "r":
202
raise errors.ReadOnlyError(self)
207
def lock_write(self):
208
"""See MutableTree.lock_write()."""
212
self.branch.lock_write()
213
self._lock_mode = "w"
214
self._populate_from_branch()
215
elif self._lock_mode == "r":
216
raise errors.ReadOnlyError(self)
221
def _populate_from_branch(self):
222
"""Populate the in-tree state from the branch."""
224
if self._branch_revision_id == _mod_revision.NULL_REVISION:
225
self._parent_ids = []
227
self._parent_ids = [self._branch_revision_id]
228
self._inventory = Inventory(None, self._basis_tree.get_revision_id())
229
self._file_transport = MemoryTransport()
230
# TODO copy the revision trees content, or do it lazy, or something.
231
inventory_entries = self._basis_tree.iter_entries_by_dir()
232
for path, entry in inventory_entries:
233
self._inventory.add(entry.copy())
236
if entry.kind == 'directory':
237
self._file_transport.mkdir(path)
238
elif entry.kind == 'file':
239
self._file_transport.put_file(path,
240
self._basis_tree.get_file(entry.file_id))
242
raise NotImplementedError(self._populate_from_branch)
244
def put_file_bytes_non_atomic(self, file_id, bytes):
245
"""See MutableTree.put_file_bytes_non_atomic."""
246
self._file_transport.put_bytes(self.id2path(file_id), bytes)
251
This frees all cached state when the last lock context for the tree is
255
self._basis_tree = None
256
self._parent_ids = []
257
self._inventory = None
262
self._lock_mode = None
266
@needs_tree_write_lock
267
def unversion(self, file_ids):
268
"""Remove the file ids in file_ids from the current versioned set.
270
When a file_id is unversioned, all of its children are automatically
273
:param file_ids: The file ids to stop versioning.
274
:raises: NoSuchId if any fileid is not currently versioned.
276
# XXX: This should be in mutabletree, but the inventory-save action
277
# is not relevant to memory tree. Until that is done in unlock by
278
# working tree, we cannot share the implementation.
279
for file_id in file_ids:
280
if self._inventory.has_id(file_id):
281
self._inventory.remove_recursive_id(file_id)
283
raise errors.NoSuchId(self, file_id)
285
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
286
"""See MutableTree.set_parent_trees()."""
287
for revision_id in revision_ids:
288
_mod_revision.check_not_reserved_id(revision_id)
289
if len(revision_ids) == 0:
290
self._parent_ids = []
291
self._branch_revision_id = _mod_revision.NULL_REVISION
293
self._parent_ids = revision_ids
294
self._branch_revision_id = revision_ids[0]
295
self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
298
def _set_basis(self):
300
self._basis_tree = self.branch.repository.revision_tree(
301
self._branch_revision_id)
302
except errors.NoSuchRevision:
303
if self._allow_leftmost_as_ghost:
304
self._basis_tree = self.branch.repository.revision_tree(
305
_mod_revision.NULL_REVISION)
309
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
310
"""See MutableTree.set_parent_trees()."""
311
if len(parents_list) == 0:
312
self._parent_ids = []
313
self._basis_tree = self.branch.repository.revision_tree(
314
_mod_revision.NULL_REVISION)
316
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
317
# a ghost in the left most parent
318
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
319
self._parent_ids = [parent_id for parent_id, tree in parents_list]
320
if parents_list[0][1] is None or parents_list[0][1] == 'null:':
321
self._basis_tree = self.branch.repository.revision_tree(
322
_mod_revision.NULL_REVISION)
324
self._basis_tree = parents_list[0][1]
325
self._branch_revision_id = parents_list[0][0]