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 .tree import MutableGitIndexTree
47
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
48
"""A Git memory tree."""
50
def __init__(self, branch, store, head):
51
MutableGitIndexTree.__init__(self)
53
self.mapping = self.branch.repository.get_mapping()
57
self._lock_mode = None
58
self._populate_from_branch()
60
def _supports_executable(self):
65
return self.branch.controldir
67
def is_control_filename(self, path):
70
def _gather_kinds(self, files, kinds):
71
"""See MutableTree._gather_kinds.
73
with self.lock_tree_write():
74
for pos, f in enumerate(files):
75
if kinds[pos] is None:
76
kinds[pos] = self.kind(f)
78
def put_file_bytes_non_atomic(self, path, bytes):
79
"""See MutableTree.put_file_bytes_non_atomic."""
80
self._file_transport.put_bytes(path, bytes)
82
def _populate_from_branch(self):
83
"""Populate the in-tree state from the branch."""
84
if self.branch.head is None:
87
self._parent_ids = [self.last_revision()]
88
self._file_transport = MemoryTransport()
89
if self.branch.head is None:
92
tree_id = self.store[self.branch.head].tree
93
tree = self.store[tree_id]
97
(path, tree) = trees.pop()
98
for name, mode, sha in tree.iteritems():
99
subpath = posixpath.join(path, name.decode('utf-8'))
100
if stat.S_ISDIR(mode):
101
self._file_transport.mkdir(subpath)
102
trees.append((subpath, self.store[sha]))
103
elif stat.S_ISREG(mode):
104
self._file_transport.put_bytes(
105
subpath, self.store[sha].data)
106
self._index_add_entry(subpath, 'file')
108
raise NotImplementedError(self._populate_from_branch)
111
"""Lock the memory tree for reading.
113
This triggers population of data from the branch for its revision.
118
self.branch.lock_read()
119
self._lock_mode = "r"
120
self._populate_from_branch()
121
return lock.LogicalLockResult(self.unlock)
122
except BaseException:
126
def lock_tree_write(self):
127
"""See MutableTree.lock_tree_write()."""
131
self.branch.lock_read()
132
self._lock_mode = "w"
133
self._populate_from_branch()
134
elif self._lock_mode == "r":
135
raise errors.ReadOnlyError(self)
136
except BaseException:
139
return lock.LogicalLockResult(self.unlock)
141
def lock_write(self):
142
"""See MutableTree.lock_write()."""
146
self.branch.lock_write()
147
self._lock_mode = "w"
148
self._populate_from_branch()
149
elif self._lock_mode == "r":
150
raise errors.ReadOnlyError(self)
151
return lock.LogicalLockResult(self.unlock)
152
except BaseException:
159
This frees all cached state when the last lock context for the tree is
163
self._parent_ids = []
169
self._lock_mode = None
173
def _lstat(self, path):
174
mem_stat = self._file_transport.stat(path)
175
stat_val = os.stat_result(
176
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
179
def _live_entry(self, path):
180
path = urlutils.quote_from_bytes(path)
181
stat_val = self._lstat(path)
182
if stat.S_ISDIR(stat_val.st_mode):
184
elif stat.S_ISLNK(stat_val.st_mode):
185
blob = Blob.from_string(
186
self._file_transport.readlink(path).encode('utf-8'))
187
elif stat.S_ISREG(stat_val.st_mode):
188
blob = Blob.from_string(self._file_transport.get_bytes(path))
190
raise AssertionError('unknown type %d' % stat_val.st_mode)
191
return index_entry_from_stat(stat_val, blob.id, 0)
193
def get_file_with_stat(self, path):
194
return (self.get_file(path), self._lstat(path))
196
def get_file(self, path):
197
"""See Tree.get_file."""
198
return self._file_transport.get(path)
200
def get_file_sha1(self, path, stat_value=None):
201
"""See Tree.get_file_sha1()."""
202
stream = self._file_transport.get(path)
203
return osutils.sha_file(stream)
205
def get_parent_ids(self):
206
"""See Tree.get_parent_ids.
208
This implementation returns the current cached value from
211
with self.lock_read():
212
return list(self._parent_ids)
214
def last_revision(self):
215
"""See MutableTree.last_revision."""
216
with self.lock_read():
217
if self.branch.head is None:
218
return _mod_revision.NULL_REVISION
219
return self.branch.repository.lookup_foreign_revision_id(
222
def basis_tree(self):
223
"""See Tree.basis_tree()."""
224
return self.branch.repository.revision_tree(self.last_revision())
226
def get_config_stack(self):
227
return self.branch.get_config_stack()
229
def has_filename(self, path):
230
return self._file_transport.has(path)
232
def _set_merges_from_parent_ids(self, rhs_parent_ids):
233
if self.branch.head is None:
234
self._parent_ids = []
236
self._parent_ids = [self.last_revision()]
237
self._parent_ids.extend(rhs_parent_ids)
239
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
240
if len(parent_ids) == 0:
241
self._parent_ids = []
242
self.branch.head = None
244
self._parent_ids = parent_ids
245
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
248
def mkdir(self, path, file_id=None):
249
"""See MutableTree.mkdir()."""
250
self.add(path, None, 'directory')
251
self._file_transport.mkdir(path)
253
def _rename_one(self, from_rel, to_rel):
254
self._file_transport.rename(from_rel, to_rel)
257
stat_value = self._file_transport.stat(p)
258
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
260
def get_symlink_target(self, path):
261
with self.lock_read():
262
return self._file_transport.readlink(path)