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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
See MemoryTree for more details.
23
from copy import deepcopy
25
from bzrlib import errors, mutabletree
26
from bzrlib.decorators import needs_read_lock, needs_write_lock
27
from bzrlib.osutils import sha_file
28
from bzrlib.transport.memory import MemoryTransport
31
class MemoryTree(mutabletree.MutableTree):
32
"""A MemoryTree is a specialisation of MutableTree.
34
It maintains nearly no state outside of read_lock and write_lock
35
transactions. (it keeps a reference to the branch, and its last-revision
39
def __init__(self, branch, revision_id):
40
"""Construct a MemoryTree for branch using revision_id."""
42
self.bzrdir = branch.bzrdir
43
self._branch_revision_id = revision_id
45
self._lock_mode = None
48
def _add(self, files, ids, kinds):
49
"""See MutableTree._add."""
50
for f, file_id, kind in zip(files, ids, kinds):
54
self._inventory.add_path(f, kind=kind)
56
self._inventory.add_path(f, kind=kind, file_id=file_id)
59
"""See Tree.basis_tree()."""
60
return self._basis_tree
63
def create_on_branch(branch):
64
"""Create a MemoryTree for branch, using the last-revision of branch."""
65
return MemoryTree(branch, branch.last_revision())
67
def _gather_kinds(self, files, kinds):
68
"""See MutableTree._gather_kinds.
70
This implementation does not care about the file kind of
71
missing files, so is a no-op.
74
def get_file(self, file_id):
75
"""See Tree.get_file."""
76
return self._file_transport.get(self.id2path(file_id))
78
def get_file_sha1(self, file_id, path=None):
79
"""See Tree.get_file_sha1()."""
81
path = self.id2path(file_id)
82
stream = self._file_transport.get(path)
83
return sha_file(stream)
86
def get_parent_ids(self):
87
"""See Tree.get_parent_ids.
89
This implementation returns the current cached value from
92
return list(self._parent_ids)
94
def has_filename(self, filename):
95
"""See Tree.has_filename()."""
96
return self._file_transport.has(filename)
98
def is_executable(self, file_id, path=None):
99
return self._inventory[file_id].executable
101
def mkdir(self, path, file_id=None):
102
"""See MutableTree.mkdir()."""
103
self.add(path, file_id, 'directory')
105
file_id = self.path2id(path)
106
self._file_transport.mkdir(path)
110
def last_revision(self):
111
"""See MutableTree.last_revision."""
112
return self._branch_revision_id
115
"""Lock the memory tree for reading.
117
This triggers population of data from the branch for its revision.
122
self.branch.lock_read()
123
self._lock_mode = "r"
124
self._populate_from_branch()
129
def lock_write(self):
130
"""See MutableTree.lock_write()."""
134
self.branch.lock_write()
135
self._lock_mode = "w"
136
self._populate_from_branch()
137
elif self._lock_mode == "r":
138
raise errors.ReadOnlyError(self)
143
def _populate_from_branch(self):
144
"""Populate the in-tree state from the branch."""
145
self._basis_tree = self.branch.repository.revision_tree(
146
self._branch_revision_id)
147
if self._branch_revision_id is None:
148
self._parent_ids = []
150
self._parent_ids = [self._branch_revision_id]
151
self._inventory = deepcopy(self._basis_tree._inventory)
152
self._file_transport = MemoryTransport()
153
# TODO copy the revision trees content, or do it lazy, or something.
154
inventory_entries = self._inventory.iter_entries()
155
inventory_entries.next()
156
for path, entry in inventory_entries:
157
if entry.kind == 'directory':
158
self._file_transport.mkdir(path)
159
elif entry.kind == 'file':
160
self._file_transport.put_file(path,
161
self._basis_tree.get_file(entry.file_id))
163
raise NotImplementedError(self._populate_from_branch)
165
def put_file_bytes_non_atomic(self, file_id, bytes):
166
"""See MutableTree.put_file_bytes_non_atomic."""
167
self._file_transport.put_bytes(self.id2path(file_id), bytes)
172
This frees all cached state when the last lock context for the tree is
176
self._basis_tree = None
177
self._parent_ids = []
178
self._inventory = None
183
self._lock_mode = None
188
def unversion(self, file_ids):
189
"""Remove the file ids in file_ids from the current versioned set.
191
When a file_id is unversioned, all of its children are automatically
194
:param file_ids: The file ids to stop versioning.
195
:raises: NoSuchId if any fileid is not currently versioned.
197
# XXX: This should be in mutabletree, but the inventory-save action
198
# is not relevant to memory tree. Until that is done in unlock by
199
# working tree, we cannot share the implementation.
200
for file_id in file_ids:
201
if self._inventory.has_id(file_id):
202
self._inventory.remove_recursive_id(file_id)
204
raise errors.NoSuchId(self, file_id)
206
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
207
"""See MutableTree.set_parent_trees()."""
208
if len(parents_list) == 0:
209
self._parent_ids = []
210
self._basis_tree = self.branch.repository.revisiontree(None)
212
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
213
# a ghost in the left most parent
214
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
215
self._parent_ids = [parent_id for parent_id, tree in parents_list]
216
if parents_list[0][1] is None:
217
self._basis_tree = self.branch.repository.revisiontree(None)
219
self._basis_tree = parents_list[0][1]
220
self._branch_revision_id = parents_list[0][0]