1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
18
"""Git Memory Trees."""
20
from __future__ import absolute_import
26
from dulwich.index import (
27
index_entry_from_stat,
29
from dulwich.objects import (
38
revision as _mod_revision,
42
from breezy.transport.memory import MemoryTransport
44
from .mapping import (
48
from .tree import MutableGitIndexTree
51
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
52
"""A Git memory tree."""
54
def __init__(self, branch, store, head):
55
MutableGitIndexTree.__init__(self)
57
self.mapping = self.branch.repository.get_mapping()
61
self._lock_mode = None
62
self._populate_from_branch()
64
def _supports_executable(self):
69
return self.branch.controldir
71
def is_control_filename(self, path):
74
def _gather_kinds(self, files, kinds):
75
"""See MutableTree._gather_kinds.
77
with self.lock_tree_write():
78
for pos, f in enumerate(files):
79
if kinds[pos] is None:
80
kinds[pos] = self.kind(f)
82
def put_file_bytes_non_atomic(self, path, bytes):
83
"""See MutableTree.put_file_bytes_non_atomic."""
84
self._file_transport.put_bytes(path, bytes)
86
def _populate_from_branch(self):
87
"""Populate the in-tree state from the branch."""
88
if self.branch.head is None:
91
self._parent_ids = [self.last_revision()]
92
self._file_transport = MemoryTransport()
93
if self.branch.head is None:
96
tree_id = self.store[self.branch.head].tree
97
tree = self.store[tree_id]
101
(path, tree) = trees.pop()
102
for name, mode, sha in tree.iteritems():
103
subpath = posixpath.join(path, decode_git_path(name))
104
if stat.S_ISDIR(mode):
105
self._file_transport.mkdir(subpath)
106
trees.append((subpath, self.store[sha]))
107
elif stat.S_ISREG(mode):
108
self._file_transport.put_bytes(
109
subpath, self.store[sha].data)
110
self._index_add_entry(subpath, 'file')
112
raise NotImplementedError(self._populate_from_branch)
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()
125
return lock.LogicalLockResult(self.unlock)
126
except BaseException:
130
def lock_tree_write(self):
131
"""See MutableTree.lock_tree_write()."""
135
self.branch.lock_read()
136
self._lock_mode = "w"
137
self._populate_from_branch()
138
elif self._lock_mode == "r":
139
raise errors.ReadOnlyError(self)
140
except BaseException:
143
return lock.LogicalLockResult(self.unlock)
145
def lock_write(self):
146
"""See MutableTree.lock_write()."""
150
self.branch.lock_write()
151
self._lock_mode = "w"
152
self._populate_from_branch()
153
elif self._lock_mode == "r":
154
raise errors.ReadOnlyError(self)
155
return lock.LogicalLockResult(self.unlock)
156
except BaseException:
163
This frees all cached state when the last lock context for the tree is
167
self._parent_ids = []
173
self._lock_mode = None
177
def _lstat(self, path):
178
mem_stat = self._file_transport.stat(path)
179
stat_val = os.stat_result(
180
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
183
def _live_entry(self, path):
184
path = urlutils.quote_from_bytes(path)
185
stat_val = self._lstat(path)
186
if stat.S_ISDIR(stat_val.st_mode):
188
elif stat.S_ISLNK(stat_val.st_mode):
189
blob = Blob.from_string(
190
encode_git_path(self._file_transport.readlink(path)))
191
elif stat.S_ISREG(stat_val.st_mode):
192
blob = Blob.from_string(self._file_transport.get_bytes(path))
194
raise AssertionError('unknown type %d' % stat_val.st_mode)
195
return index_entry_from_stat(stat_val, blob.id, 0)
197
def get_file_with_stat(self, path):
198
return (self.get_file(path), self._lstat(path))
200
def get_file(self, path):
201
"""See Tree.get_file."""
202
return self._file_transport.get(path)
204
def get_file_sha1(self, path, stat_value=None):
205
"""See Tree.get_file_sha1()."""
206
stream = self._file_transport.get(path)
207
return osutils.sha_file(stream)
209
def get_parent_ids(self):
210
"""See Tree.get_parent_ids.
212
This implementation returns the current cached value from
215
with self.lock_read():
216
return list(self._parent_ids)
218
def last_revision(self):
219
"""See MutableTree.last_revision."""
220
with self.lock_read():
221
if self.branch.head is None:
222
return _mod_revision.NULL_REVISION
223
return self.branch.repository.lookup_foreign_revision_id(
226
def basis_tree(self):
227
"""See Tree.basis_tree()."""
228
return self.branch.repository.revision_tree(self.last_revision())
230
def get_config_stack(self):
231
return self.branch.get_config_stack()
233
def has_filename(self, path):
234
return self._file_transport.has(path)
236
def _set_merges_from_parent_ids(self, rhs_parent_ids):
237
if self.branch.head is None:
238
self._parent_ids = []
240
self._parent_ids = [self.last_revision()]
241
self._parent_ids.extend(rhs_parent_ids)
243
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
244
if len(parent_ids) == 0:
245
self._parent_ids = []
246
self.branch.head = None
248
self._parent_ids = parent_ids
249
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
252
def mkdir(self, path, file_id=None):
253
"""See MutableTree.mkdir()."""
254
self.add(path, None, 'directory')
255
self._file_transport.mkdir(path)
257
def _rename_one(self, from_rel, to_rel):
258
self._file_transport.rename(from_rel, to_rel)
261
stat_value = self._file_transport.stat(p)
262
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
264
def get_symlink_target(self, path):
265
with self.lock_read():
266
return self._file_transport.readlink(path)