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
17
from __future__ import absolute_import
21
See MemoryTree for more details.
30
revision as _mod_revision,
32
from bzrlib.decorators import needs_read_lock
33
from bzrlib.inventory import Inventory
34
from bzrlib.osutils import sha_file
35
from bzrlib.mutabletree import needs_tree_write_lock
36
from bzrlib.transport.memory import MemoryTransport
39
class MemoryTree(mutabletree.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.bzrdir = branch.bzrdir
51
self._branch_revision_id = revision_id
53
self._lock_mode = None
55
def is_control_filename(self, filename):
56
# Memory tree doesn't have any control filenames
59
@needs_tree_write_lock
60
def _add(self, files, ids, kinds):
61
"""See MutableTree._add."""
62
for f, file_id, kind in zip(files, ids, kinds):
66
self._inventory.add_path(f, kind=kind)
68
self._inventory.add_path(f, kind=kind, file_id=file_id)
71
"""See Tree.basis_tree()."""
72
return self._basis_tree
75
def create_on_branch(branch):
76
"""Create a MemoryTree for branch, using the last-revision of branch."""
77
revision_id = _mod_revision.ensure_null(branch.last_revision())
78
return MemoryTree(branch, revision_id)
80
def _gather_kinds(self, files, kinds):
81
"""See MutableTree._gather_kinds.
83
This implementation does not care about the file kind of
84
missing files, so is a no-op.
87
def get_file(self, file_id, path=None):
88
"""See Tree.get_file."""
90
path = self.id2path(file_id)
91
return self._file_transport.get(path)
93
def get_file_sha1(self, file_id, path=None, stat_value=None):
94
"""See Tree.get_file_sha1()."""
96
path = self.id2path(file_id)
97
stream = self._file_transport.get(path)
98
return sha_file(stream)
100
def get_root_id(self):
101
return self.path2id('')
103
def _comparison_data(self, entry, path):
104
"""See Tree._comparison_data."""
106
return None, False, None
107
return entry.kind, entry.executable, None
109
@needs_tree_write_lock
110
def rename_one(self, from_rel, to_rel):
111
file_id = self.path2id(from_rel)
112
to_dir, to_tail = os.path.split(to_rel)
113
to_parent_id = self.path2id(to_dir)
114
self._file_transport.move(from_rel, to_rel)
115
self._inventory.rename(file_id, to_parent_id, to_tail)
117
def path_content_summary(self, path):
118
"""See Tree.path_content_summary."""
119
id = self.path2id(path)
121
return 'missing', None, None, None
124
bytes = self._file_transport.get_bytes(path)
126
executable = self._inventory[id].executable
127
sha1 = None # no stat cache
128
return (kind, size, executable, sha1)
129
elif kind == 'directory':
130
# memory tree does not support nested trees yet.
131
return kind, None, None, None
132
elif kind == 'symlink':
133
raise NotImplementedError('symlink support')
135
raise NotImplementedError('unknown kind')
137
def _file_size(self, entry, stat_value):
138
"""See Tree._file_size."""
141
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
return list(self._parent_ids)
152
def has_filename(self, filename):
153
"""See Tree.has_filename()."""
154
return self._file_transport.has(filename)
156
def is_executable(self, file_id, path=None):
157
return self._inventory[file_id].executable
159
def kind(self, file_id):
160
return self._inventory[file_id].kind
162
def mkdir(self, path, file_id=None):
163
"""See MutableTree.mkdir()."""
164
self.add(path, file_id, 'directory')
166
file_id = self.path2id(path)
167
self._file_transport.mkdir(path)
171
def last_revision(self):
172
"""See MutableTree.last_revision."""
173
return self._branch_revision_id
176
"""Lock the memory tree for reading.
178
This triggers population of data from the branch for its revision.
183
self.branch.lock_read()
184
self._lock_mode = "r"
185
self._populate_from_branch()
190
def lock_tree_write(self):
191
"""See MutableTree.lock_tree_write()."""
195
self.branch.lock_read()
196
self._lock_mode = "w"
197
self._populate_from_branch()
198
elif self._lock_mode == "r":
199
raise errors.ReadOnlyError(self)
204
def lock_write(self):
205
"""See MutableTree.lock_write()."""
209
self.branch.lock_write()
210
self._lock_mode = "w"
211
self._populate_from_branch()
212
elif self._lock_mode == "r":
213
raise errors.ReadOnlyError(self)
218
def _populate_from_branch(self):
219
"""Populate the in-tree state from the branch."""
221
if self._branch_revision_id == _mod_revision.NULL_REVISION:
222
self._parent_ids = []
224
self._parent_ids = [self._branch_revision_id]
225
self._inventory = Inventory(None, self._basis_tree.get_revision_id())
226
self._file_transport = MemoryTransport()
227
# TODO copy the revision trees content, or do it lazy, or something.
228
inventory_entries = self._basis_tree.iter_entries_by_dir()
229
for path, entry in inventory_entries:
230
self._inventory.add(entry.copy())
233
if entry.kind == 'directory':
234
self._file_transport.mkdir(path)
235
elif entry.kind == 'file':
236
self._file_transport.put_file(path,
237
self._basis_tree.get_file(entry.file_id))
239
raise NotImplementedError(self._populate_from_branch)
241
def put_file_bytes_non_atomic(self, file_id, bytes):
242
"""See MutableTree.put_file_bytes_non_atomic."""
243
self._file_transport.put_bytes(self.id2path(file_id), bytes)
248
This frees all cached state when the last lock context for the tree is
252
self._basis_tree = None
253
self._parent_ids = []
254
self._inventory = None
259
self._lock_mode = None
263
@needs_tree_write_lock
264
def unversion(self, file_ids):
265
"""Remove the file ids in file_ids from the current versioned set.
267
When a file_id is unversioned, all of its children are automatically
270
:param file_ids: The file ids to stop versioning.
271
:raises: NoSuchId if any fileid is not currently versioned.
273
# XXX: This should be in mutabletree, but the inventory-save action
274
# is not relevant to memory tree. Until that is done in unlock by
275
# working tree, we cannot share the implementation.
276
for file_id in file_ids:
277
if self._inventory.has_id(file_id):
278
self._inventory.remove_recursive_id(file_id)
280
raise errors.NoSuchId(self, file_id)
282
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
283
"""See MutableTree.set_parent_trees()."""
284
for revision_id in revision_ids:
285
_mod_revision.check_not_reserved_id(revision_id)
286
if len(revision_ids) == 0:
287
self._parent_ids = []
288
self._branch_revision_id = _mod_revision.NULL_REVISION
290
self._parent_ids = revision_ids
291
self._branch_revision_id = revision_ids[0]
292
self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
295
def _set_basis(self):
297
self._basis_tree = self.branch.repository.revision_tree(
298
self._branch_revision_id)
299
except errors.NoSuchRevision:
300
if self._allow_leftmost_as_ghost:
301
self._basis_tree = self.branch.repository.revision_tree(
302
_mod_revision.NULL_REVISION)
306
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
307
"""See MutableTree.set_parent_trees()."""
308
if len(parents_list) == 0:
309
self._parent_ids = []
310
self._basis_tree = self.branch.repository.revision_tree(
311
_mod_revision.NULL_REVISION)
313
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
314
# a ghost in the left most parent
315
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
316
self._parent_ids = [parent_id for parent_id, tree in parents_list]
317
if parents_list[0][1] is None or parents_list[0][1] == 'null:':
318
self._basis_tree = self.branch.repository.revision_tree(
319
_mod_revision.NULL_REVISION)
321
self._basis_tree = parents_list[0][1]
322
self._branch_revision_id = parents_list[0][0]