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 .bzr.inventory import Inventory
32
from .bzr.inventorytree import MutableInventoryTree
33
from .osutils import sha_file
34
from .transport.memory import MemoryTransport
37
class MemoryTree(MutableInventoryTree):
38
"""A MemoryTree is a specialisation of MutableTree.
40
It maintains nearly no state outside of read_lock and write_lock
41
transactions. (it keeps a reference to the branch, and its last-revision
45
def __init__(self, branch, revision_id):
46
"""Construct a MemoryTree for branch using revision_id."""
48
self.controldir = branch.controldir
49
self._branch_revision_id = revision_id
51
self._lock_mode = None
53
def get_config_stack(self):
54
return self.branch.get_config_stack()
56
def is_control_filename(self, filename):
57
# Memory tree doesn't have any control filenames
60
def _add(self, files, ids, kinds):
61
"""See MutableTree._add."""
62
with self.lock_tree_write():
63
for f, file_id, kind in zip(files, ids, kinds):
67
self._inventory.add_path(f, kind=kind)
69
self._inventory.add_path(f, kind=kind, file_id=file_id)
72
"""See Tree.basis_tree()."""
73
return self._basis_tree
76
def create_on_branch(branch):
77
"""Create a MemoryTree for branch, using the last-revision of branch."""
78
revision_id = _mod_revision.ensure_null(branch.last_revision())
79
return MemoryTree(branch, revision_id)
81
def _gather_kinds(self, files, kinds):
82
"""See MutableTree._gather_kinds.
84
This implementation does not care about the file kind of
85
missing files, so is a no-op.
88
def get_file(self, file_id, path=None):
89
"""See Tree.get_file."""
91
path = self.id2path(file_id)
92
return self._file_transport.get(path)
94
def get_file_sha1(self, file_id, path=None, stat_value=None):
95
"""See Tree.get_file_sha1()."""
97
path = self.id2path(file_id)
98
stream = self._file_transport.get(path)
99
return sha_file(stream)
101
def get_root_id(self):
102
return self.path2id('')
104
def _comparison_data(self, entry, path):
105
"""See Tree._comparison_data."""
107
return None, False, None
108
return entry.kind, entry.executable, None
110
def rename_one(self, from_rel, to_rel):
111
with self.lock_tree_write():
112
file_id = self.path2id(from_rel)
113
to_dir, to_tail = os.path.split(to_rel)
114
to_parent_id = self.path2id(to_dir)
115
self._file_transport.move(from_rel, to_rel)
116
self._inventory.rename(file_id, to_parent_id, to_tail)
118
def path_content_summary(self, path):
119
"""See Tree.path_content_summary."""
120
id = self.path2id(path)
122
return 'missing', None, None, None
125
bytes = self._file_transport.get_bytes(path)
127
executable = self._inventory[id].executable
128
sha1 = None # no stat cache
129
return (kind, size, executable, sha1)
130
elif kind == 'directory':
131
# memory tree does not support nested trees yet.
132
return kind, None, None, None
133
elif kind == 'symlink':
134
raise NotImplementedError('symlink support')
136
raise NotImplementedError('unknown kind')
138
def _file_size(self, entry, stat_value):
139
"""See Tree._file_size."""
142
return entry.text_size
144
def get_parent_ids(self):
145
"""See Tree.get_parent_ids.
147
This implementation returns the current cached value from
150
with self.lock_read():
151
return list(self._parent_ids)
153
def has_filename(self, filename):
154
"""See Tree.has_filename()."""
155
return self._file_transport.has(filename)
157
def is_executable(self, file_id, path=None):
158
return self._inventory[file_id].executable
160
def kind(self, file_id):
161
return self._inventory[file_id].kind
163
def mkdir(self, path, file_id=None):
164
"""See MutableTree.mkdir()."""
165
self.add(path, file_id, 'directory')
167
file_id = self.path2id(path)
168
self._file_transport.mkdir(path)
171
def last_revision(self):
172
"""See MutableTree.last_revision."""
173
with self.lock_read():
174
return self._branch_revision_id
177
"""Lock the memory tree for reading.
179
This triggers population of data from the branch for its revision.
184
self.branch.lock_read()
185
self._lock_mode = "r"
186
self._populate_from_branch()
187
return lock.LogicalLockResult(self.unlock)
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)
205
return lock.LogicalLockResult(self.unlock)
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)
217
return lock.LogicalLockResult(self.unlock)
222
def _populate_from_branch(self):
223
"""Populate the in-tree state from the branch."""
225
if self._branch_revision_id == _mod_revision.NULL_REVISION:
226
self._parent_ids = []
228
self._parent_ids = [self._branch_revision_id]
229
self._inventory = Inventory(None, self._basis_tree.get_revision_id())
230
self._file_transport = MemoryTransport()
231
# TODO copy the revision trees content, or do it lazy, or something.
232
inventory_entries = self._basis_tree.iter_entries_by_dir()
233
for path, entry in inventory_entries:
234
self._inventory.add(entry.copy())
237
if entry.kind == 'directory':
238
self._file_transport.mkdir(path)
239
elif entry.kind == 'file':
240
self._file_transport.put_file(path,
241
self._basis_tree.get_file(entry.file_id))
243
raise NotImplementedError(self._populate_from_branch)
245
def put_file_bytes_non_atomic(self, file_id, bytes):
246
"""See MutableTree.put_file_bytes_non_atomic."""
247
self._file_transport.put_bytes(self.id2path(file_id), bytes)
252
This frees all cached state when the last lock context for the tree is
256
self._basis_tree = None
257
self._parent_ids = []
258
self._inventory = None
263
self._lock_mode = None
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
with self.lock_tree_write():
277
# XXX: This should be in mutabletree, but the inventory-save action
278
# is not relevant to memory tree. Until that is done in unlock by
279
# working tree, we cannot share the implementation.
280
for file_id in file_ids:
281
if self._inventory.has_id(file_id):
282
self._inventory.remove_recursive_id(file_id)
284
raise errors.NoSuchId(self, file_id)
286
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
287
"""See MutableTree.set_parent_trees()."""
288
for revision_id in revision_ids:
289
_mod_revision.check_not_reserved_id(revision_id)
290
if len(revision_ids) == 0:
291
self._parent_ids = []
292
self._branch_revision_id = _mod_revision.NULL_REVISION
294
self._parent_ids = revision_ids
295
self._branch_revision_id = revision_ids[0]
296
self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
299
def _set_basis(self):
301
self._basis_tree = self.branch.repository.revision_tree(
302
self._branch_revision_id)
303
except errors.NoSuchRevision:
304
if self._allow_leftmost_as_ghost:
305
self._basis_tree = self.branch.repository.revision_tree(
306
_mod_revision.NULL_REVISION)
310
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
311
"""See MutableTree.set_parent_trees()."""
312
if len(parents_list) == 0:
313
self._parent_ids = []
314
self._basis_tree = self.branch.repository.revision_tree(
315
_mod_revision.NULL_REVISION)
317
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
318
# a ghost in the left most parent
319
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
320
self._parent_ids = [parent_id for parent_id, tree in parents_list]
321
if parents_list[0][1] is None or parents_list[0][1] == 'null:':
322
self._basis_tree = self.branch.repository.revision_tree(
323
_mod_revision.NULL_REVISION)
325
self._basis_tree = parents_list[0][1]
326
self._branch_revision_id = parents_list[0][0]