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."""
24
from dulwich.index import (
25
index_entry_from_stat,
27
from dulwich.objects import (
36
revision as _mod_revision,
40
from breezy.transport.memory import MemoryTransport
42
from .mapping import (
46
from .tree import MutableGitIndexTree
49
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
50
"""A Git memory tree."""
52
def __init__(self, branch, store, head):
53
MutableGitIndexTree.__init__(self)
55
self.mapping = self.branch.repository.get_mapping()
59
self._lock_mode = None
60
self._populate_from_branch()
62
def _supports_executable(self):
67
return self.branch.controldir
69
def is_control_filename(self, path):
72
def _gather_kinds(self, files, kinds):
73
"""See MutableTree._gather_kinds.
75
with self.lock_tree_write():
76
for pos, f in enumerate(files):
77
if kinds[pos] is None:
78
kinds[pos] = self.kind(f)
80
def put_file_bytes_non_atomic(self, path, bytes):
81
"""See MutableTree.put_file_bytes_non_atomic."""
82
self._file_transport.put_bytes(path, bytes)
84
def _populate_from_branch(self):
85
"""Populate the in-tree state from the branch."""
86
if self.branch.head is None:
89
self._parent_ids = [self.last_revision()]
90
self._file_transport = MemoryTransport()
91
if self.branch.head is None:
94
tree_id = self.store[self.branch.head].tree
95
tree = self.store[tree_id]
99
(path, tree) = trees.pop()
100
for name, mode, sha in tree.iteritems():
101
subpath = posixpath.join(path, decode_git_path(name))
102
if stat.S_ISDIR(mode):
103
self._file_transport.mkdir(subpath)
104
trees.append((subpath, self.store[sha]))
105
elif stat.S_ISREG(mode):
106
self._file_transport.put_bytes(
107
subpath, self.store[sha].data)
108
self._index_add_entry(subpath, 'file')
110
raise NotImplementedError(self._populate_from_branch)
113
"""Lock the memory tree for reading.
115
This triggers population of data from the branch for its revision.
120
self.branch.lock_read()
121
self._lock_mode = "r"
122
self._populate_from_branch()
123
return lock.LogicalLockResult(self.unlock)
124
except BaseException:
128
def lock_tree_write(self):
129
"""See MutableTree.lock_tree_write()."""
133
self.branch.lock_read()
134
self._lock_mode = "w"
135
self._populate_from_branch()
136
elif self._lock_mode == "r":
137
raise errors.ReadOnlyError(self)
138
except BaseException:
141
return lock.LogicalLockResult(self.unlock)
143
def lock_write(self):
144
"""See MutableTree.lock_write()."""
148
self.branch.lock_write()
149
self._lock_mode = "w"
150
self._populate_from_branch()
151
elif self._lock_mode == "r":
152
raise errors.ReadOnlyError(self)
153
return lock.LogicalLockResult(self.unlock)
154
except BaseException:
161
This frees all cached state when the last lock context for the tree is
165
self._parent_ids = []
171
self._lock_mode = None
175
def _lstat(self, path):
176
mem_stat = self._file_transport.stat(path)
177
stat_val = os.stat_result(
178
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
181
def _live_entry(self, path):
182
path = urlutils.quote_from_bytes(path)
183
stat_val = self._lstat(path)
184
if stat.S_ISDIR(stat_val.st_mode):
186
elif stat.S_ISLNK(stat_val.st_mode):
187
blob = Blob.from_string(
188
encode_git_path(self._file_transport.readlink(path)))
189
elif stat.S_ISREG(stat_val.st_mode):
190
blob = Blob.from_string(self._file_transport.get_bytes(path))
192
raise AssertionError('unknown type %d' % stat_val.st_mode)
193
return index_entry_from_stat(stat_val, blob.id, 0)
195
def get_file_with_stat(self, path):
196
return (self.get_file(path), self._lstat(path))
198
def get_file(self, path):
199
"""See Tree.get_file."""
200
return self._file_transport.get(path)
202
def get_file_sha1(self, path, stat_value=None):
203
"""See Tree.get_file_sha1()."""
204
stream = self._file_transport.get(path)
205
return osutils.sha_file(stream)
207
def get_parent_ids(self):
208
"""See Tree.get_parent_ids.
210
This implementation returns the current cached value from
213
with self.lock_read():
214
return list(self._parent_ids)
216
def last_revision(self):
217
"""See MutableTree.last_revision."""
218
with self.lock_read():
219
if self.branch.head is None:
220
return _mod_revision.NULL_REVISION
221
return self.branch.repository.lookup_foreign_revision_id(
224
def basis_tree(self):
225
"""See Tree.basis_tree()."""
226
return self.branch.repository.revision_tree(self.last_revision())
228
def get_config_stack(self):
229
return self.branch.get_config_stack()
231
def has_filename(self, path):
232
return self._file_transport.has(path)
234
def _set_merges_from_parent_ids(self, rhs_parent_ids):
235
if self.branch.head is None:
236
self._parent_ids = []
238
self._parent_ids = [self.last_revision()]
239
self._parent_ids.extend(rhs_parent_ids)
241
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
242
if len(parent_ids) == 0:
243
self._parent_ids = []
244
self.branch.head = None
246
self._parent_ids = parent_ids
247
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
250
def mkdir(self, path, file_id=None):
251
"""See MutableTree.mkdir()."""
252
self.add(path, None, 'directory')
253
self._file_transport.mkdir(path)
255
def _rename_one(self, from_rel, to_rel):
256
self._file_transport.rename(from_rel, to_rel)
259
stat_value = self._file_transport.stat(p)
260
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
262
def get_symlink_target(self, path):
263
with self.lock_read():
264
return self._file_transport.readlink(path)