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 .tree import MutableGitIndexTree
45
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
46
"""A Git memory tree."""
48
def __init__(self, branch, store, head):
49
MutableGitIndexTree.__init__(self)
51
self.mapping = self.branch.repository.get_mapping()
55
self._lock_mode = None
56
self._populate_from_branch()
58
def _supports_executable(self):
63
return self.branch.controldir
65
def is_control_filename(self, path):
68
def _gather_kinds(self, files, kinds):
69
"""See MutableTree._gather_kinds.
71
with self.lock_tree_write():
72
for pos, f in enumerate(files):
73
if kinds[pos] is None:
74
kinds[pos] = self.kind(f)
76
def put_file_bytes_non_atomic(self, path, bytes):
77
"""See MutableTree.put_file_bytes_non_atomic."""
78
self._file_transport.put_bytes(path, bytes)
80
def _populate_from_branch(self):
81
"""Populate the in-tree state from the branch."""
82
if self.branch.head is None:
85
self._parent_ids = [self.last_revision()]
86
self._file_transport = MemoryTransport()
87
if self.branch.head is None:
90
tree_id = self.store[self.branch.head].tree
91
tree = self.store[tree_id]
95
(path, tree) = trees.pop()
96
for name, mode, sha in tree.iteritems():
97
subpath = posixpath.join(path, name.decode('utf-8'))
98
if stat.S_ISDIR(mode):
99
self._file_transport.mkdir(subpath)
100
trees.append((subpath, self.store[sha]))
101
elif stat.S_ISREG(mode):
102
self._file_transport.put_bytes(
103
subpath, self.store[sha].data)
104
self._index_add_entry(subpath, 'file')
106
raise NotImplementedError(self._populate_from_branch)
109
"""Lock the memory tree for reading.
111
This triggers population of data from the branch for its revision.
116
self.branch.lock_read()
117
self._lock_mode = "r"
118
self._populate_from_branch()
119
return lock.LogicalLockResult(self.unlock)
120
except BaseException:
124
def lock_tree_write(self):
125
"""See MutableTree.lock_tree_write()."""
129
self.branch.lock_read()
130
self._lock_mode = "w"
131
self._populate_from_branch()
132
elif self._lock_mode == "r":
133
raise errors.ReadOnlyError(self)
134
except BaseException:
137
return lock.LogicalLockResult(self.unlock)
139
def lock_write(self):
140
"""See MutableTree.lock_write()."""
144
self.branch.lock_write()
145
self._lock_mode = "w"
146
self._populate_from_branch()
147
elif self._lock_mode == "r":
148
raise errors.ReadOnlyError(self)
149
return lock.LogicalLockResult(self.unlock)
150
except BaseException:
157
This frees all cached state when the last lock context for the tree is
161
self._parent_ids = []
167
self._lock_mode = None
171
def _lstat(self, path):
172
mem_stat = self._file_transport.stat(path)
173
stat_val = os.stat_result(
174
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
177
def _live_entry(self, path):
178
path = urlutils.quote_from_bytes(path)
179
stat_val = self._lstat(path)
180
if stat.S_ISDIR(stat_val.st_mode):
182
elif stat.S_ISLNK(stat_val.st_mode):
183
blob = Blob.from_string(
184
self._file_transport.readlink(path).encode('utf-8'))
185
elif stat.S_ISREG(stat_val.st_mode):
186
blob = Blob.from_string(self._file_transport.get_bytes(path))
188
raise AssertionError('unknown type %d' % stat_val.st_mode)
189
return index_entry_from_stat(stat_val, blob.id, 0)
191
def get_file_with_stat(self, path):
192
return (self.get_file(path), self._lstat(path))
194
def get_file(self, path):
195
"""See Tree.get_file."""
196
return self._file_transport.get(path)
198
def get_file_sha1(self, path, stat_value=None):
199
"""See Tree.get_file_sha1()."""
200
stream = self._file_transport.get(path)
201
return osutils.sha_file(stream)
203
def get_parent_ids(self):
204
"""See Tree.get_parent_ids.
206
This implementation returns the current cached value from
209
with self.lock_read():
210
return list(self._parent_ids)
212
def last_revision(self):
213
"""See MutableTree.last_revision."""
214
with self.lock_read():
215
if self.branch.head is None:
216
return _mod_revision.NULL_REVISION
217
return self.branch.repository.lookup_foreign_revision_id(
220
def basis_tree(self):
221
"""See Tree.basis_tree()."""
222
return self.branch.repository.revision_tree(self.last_revision())
224
def get_config_stack(self):
225
return self.branch.get_config_stack()
227
def has_filename(self, path):
228
return self._file_transport.has(path)
230
def _set_merges_from_parent_ids(self, rhs_parent_ids):
231
if self.branch.head is None:
232
self._parent_ids = []
234
self._parent_ids = [self.last_revision()]
235
self._parent_ids.extend(rhs_parent_ids)
237
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
238
if len(parent_ids) == 0:
239
self._parent_ids = []
240
self.branch.head = None
242
self._parent_ids = parent_ids
243
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
246
def mkdir(self, path, file_id=None):
247
"""See MutableTree.mkdir()."""
248
self.add(path, None, 'directory')
249
self._file_transport.mkdir(path)
251
def _rename_one(self, from_rel, to_rel):
252
self._file_transport.rename(from_rel, to_rel)
255
stat_value = self._file_transport.stat(p)
256
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
258
def get_symlink_target(self, path):
259
with self.lock_read():
260
return self._file_transport.readlink(path)