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
30
revision as _mod_revision,
32
from .bzr.inventory import Inventory
33
from .bzr.inventorytree import MutableInventoryTree
34
from .osutils import sha_file
35
from .transport.memory import MemoryTransport
38
class MemoryTree(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.controldir = branch.controldir
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
def _add(self, files, ids, kinds):
62
"""See MutableTree._add."""
63
with self.lock_tree_write():
64
for f, file_id, kind in zip(files, ids, kinds):
66
st_mode = self._file_transport.stat(f).st_mode
67
if stat.S_ISREG(st_mode):
69
elif stat.S_ISLNK(st_mode):
71
elif stat.S_ISDIR(st_mode):
74
raise AssertionError('Unknown file kind')
76
self._inventory.add_path(f, kind=kind)
78
self._inventory.add_path(f, kind=kind, file_id=file_id)
81
"""See Tree.basis_tree()."""
82
return self._basis_tree
85
def create_on_branch(branch):
86
"""Create a MemoryTree for branch, using the last-revision of branch."""
87
revision_id = _mod_revision.ensure_null(branch.last_revision())
88
return MemoryTree(branch, revision_id)
90
def _gather_kinds(self, files, kinds):
91
"""See MutableTree._gather_kinds.
93
This implementation does not care about the file kind of
94
missing files, so is a no-op.
97
def get_file(self, path):
98
"""See Tree.get_file."""
99
return self._file_transport.get(path)
101
def get_file_sha1(self, path, stat_value=None):
102
"""See Tree.get_file_sha1()."""
103
stream = self._file_transport.get(path)
104
return sha_file(stream)
106
def get_root_id(self):
107
return self.path2id('')
109
def _comparison_data(self, entry, path):
110
"""See Tree._comparison_data."""
112
return None, False, None
113
return entry.kind, entry.executable, None
115
def rename_one(self, from_rel, to_rel):
116
with self.lock_tree_write():
117
file_id = self.path2id(from_rel)
118
to_dir, to_tail = os.path.split(to_rel)
119
to_parent_id = self.path2id(to_dir)
120
self._file_transport.move(from_rel, to_rel)
121
self._inventory.rename(file_id, to_parent_id, to_tail)
123
def path_content_summary(self, path):
124
"""See Tree.path_content_summary."""
125
id = self.path2id(path)
127
return 'missing', None, None, None
128
kind = self.kind(path, id)
130
bytes = self._file_transport.get_bytes(path)
132
executable = self._inventory[id].executable
133
sha1 = None # no stat cache
134
return (kind, size, executable, sha1)
135
elif kind == 'directory':
136
# memory tree does not support nested trees yet.
137
return kind, None, None, None
138
elif kind == 'symlink':
139
return kind, None, None, self._inventory[id].symlink_target
141
raise NotImplementedError('unknown kind')
143
def get_parent_ids(self):
144
"""See Tree.get_parent_ids.
146
This implementation returns the current cached value from
149
with self.lock_read():
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, path):
157
return self._inventory.get_entry_by_path(path).executable
159
def kind(self, path):
160
return self._inventory.get_entry_by_path(path).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)
170
def last_revision(self):
171
"""See MutableTree.last_revision."""
172
with self.lock_read():
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()
186
return lock.LogicalLockResult(self.unlock)
187
except BaseException:
191
def lock_tree_write(self):
192
"""See MutableTree.lock_tree_write()."""
196
self.branch.lock_read()
197
self._lock_mode = "w"
198
self._populate_from_branch()
199
elif self._lock_mode == "r":
200
raise errors.ReadOnlyError(self)
201
except BaseException:
204
return lock.LogicalLockResult(self.unlock)
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)
216
return lock.LogicalLockResult(self.unlock)
217
except BaseException:
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 == 'symlink':
239
self._file_transport.symlink(entry.symlink_target, path)
240
elif entry.kind == 'file':
241
self._file_transport.put_file(
242
path, self._basis_tree.get_file(path))
244
raise NotImplementedError(self._populate_from_branch)
246
def put_file_bytes_non_atomic(self, path, bytes):
247
"""See MutableTree.put_file_bytes_non_atomic."""
248
self._file_transport.put_bytes(path, bytes)
253
This frees all cached state when the last lock context for the tree is
257
self._basis_tree = None
258
self._parent_ids = []
259
self._inventory = None
264
self._lock_mode = None
268
def unversion(self, paths):
269
"""Remove the paths from the current versioned set.
271
When a file_id is unversioned, all of its children are automatically
274
:param paths: The paths to stop versioning.
275
:raises: NoSuchId if any fileid is not currently versioned.
277
with self.lock_tree_write():
278
# XXX: This should be in mutabletree, but the inventory-save action
279
# is not relevant to memory tree. Until that is done in unlock by
280
# working tree, we cannot share the implementation.
283
file_id = self.path2id(path)
285
raise errors.NoSuchFile(path)
286
file_ids.add(file_id)
287
for file_id in file_ids:
288
if self._inventory.has_id(file_id):
289
self._inventory.remove_recursive_id(file_id)
291
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
292
"""See MutableTree.set_parent_trees()."""
293
for revision_id in revision_ids:
294
_mod_revision.check_not_reserved_id(revision_id)
295
if len(revision_ids) == 0:
296
self._parent_ids = []
297
self._branch_revision_id = _mod_revision.NULL_REVISION
299
self._parent_ids = revision_ids
300
self._branch_revision_id = revision_ids[0]
301
self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
304
def _set_basis(self):
306
self._basis_tree = self.branch.repository.revision_tree(
307
self._branch_revision_id)
308
except errors.NoSuchRevision:
309
if self._allow_leftmost_as_ghost:
310
self._basis_tree = self.branch.repository.revision_tree(
311
_mod_revision.NULL_REVISION)
315
def get_symlink_target(self, path):
316
with self.lock_read():
317
return self._file_transport.readlink(path)
319
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
320
"""See MutableTree.set_parent_trees()."""
321
if len(parents_list) == 0:
322
self._parent_ids = []
323
self._basis_tree = self.branch.repository.revision_tree(
324
_mod_revision.NULL_REVISION)
326
if parents_list[0][1] is None and not allow_leftmost_as_ghost:
327
# a ghost in the left most parent
328
raise errors.GhostRevisionUnusableHere(parents_list[0][0])
329
self._parent_ids = [parent_id for parent_id, tree in parents_list]
330
if parents_list[0][1] is None or parents_list[0][1] == b'null:':
331
self._basis_tree = self.branch.repository.revision_tree(
332
_mod_revision.NULL_REVISION)
334
self._basis_tree = parents_list[0][1]
335
self._branch_revision_id = parents_list[0][0]