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 GitFileIdMap
45
from .tree import MutableGitIndexTree
48
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
49
"""A Git memory tree."""
51
def __init__(self, branch, store, head):
52
MutableGitIndexTree.__init__(self)
54
self.mapping = self.branch.repository.get_mapping()
58
self._lock_mode = None
59
self._populate_from_branch()
61
def _supports_executable(self):
66
return self.branch.controldir
68
def is_control_filename(self, path):
71
def _gather_kinds(self, files, kinds):
72
"""See MutableTree._gather_kinds.
74
with self.lock_tree_write():
75
for pos, f in enumerate(files):
76
if kinds[pos] is None:
77
kinds[pos] = self.kind(f)
79
def put_file_bytes_non_atomic(self, path, bytes):
80
"""See MutableTree.put_file_bytes_non_atomic."""
81
self._file_transport.put_bytes(path, bytes)
83
def _populate_from_branch(self):
84
"""Populate the in-tree state from the branch."""
85
if self.branch.head is None:
88
self._parent_ids = [self.last_revision()]
89
self._file_transport = MemoryTransport()
90
if self.branch.head is None:
92
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
94
tree_id = self.store[self.branch.head].tree
95
self._basis_fileid_map = self.mapping.get_fileid_map(
96
self.store.__getitem__, tree_id)
97
tree = self.store[tree_id]
98
self._fileid_map = self._basis_fileid_map.copy()
102
(path, tree) = trees.pop()
103
for name, mode, sha in tree.iteritems():
104
subpath = posixpath.join(path, name.decode('utf-8'))
105
if stat.S_ISDIR(mode):
106
self._file_transport.mkdir(subpath)
107
trees.append((subpath, self.store[sha]))
108
elif stat.S_ISREG(mode):
109
self._file_transport.put_bytes(
110
subpath, self.store[sha].data)
111
self._index_add_entry(subpath, 'file')
113
raise NotImplementedError(self._populate_from_branch)
116
"""Lock the memory tree for reading.
118
This triggers population of data from the branch for its revision.
123
self.branch.lock_read()
124
self._lock_mode = "r"
125
self._populate_from_branch()
126
return lock.LogicalLockResult(self.unlock)
127
except BaseException:
131
def lock_tree_write(self):
132
"""See MutableTree.lock_tree_write()."""
136
self.branch.lock_read()
137
self._lock_mode = "w"
138
self._populate_from_branch()
139
elif self._lock_mode == "r":
140
raise errors.ReadOnlyError(self)
141
except BaseException:
144
return lock.LogicalLockResult(self.unlock)
146
def lock_write(self):
147
"""See MutableTree.lock_write()."""
151
self.branch.lock_write()
152
self._lock_mode = "w"
153
self._populate_from_branch()
154
elif self._lock_mode == "r":
155
raise errors.ReadOnlyError(self)
156
return lock.LogicalLockResult(self.unlock)
157
except BaseException:
164
This frees all cached state when the last lock context for the tree is
168
self._parent_ids = []
174
self._lock_mode = None
178
def _lstat(self, path):
179
mem_stat = self._file_transport.stat(path)
180
stat_val = os.stat_result(
181
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
184
def _live_entry(self, path):
185
path = urlutils.quote_from_bytes(path)
186
stat_val = self._lstat(path)
187
if stat.S_ISDIR(stat_val.st_mode):
189
elif stat.S_ISLNK(stat_val.st_mode):
190
blob = Blob.from_string(
191
self._file_transport.readlink(path).encode('utf-8'))
192
elif stat.S_ISREG(stat_val.st_mode):
193
blob = Blob.from_string(self._file_transport.get_bytes(path))
195
raise AssertionError('unknown type %d' % stat_val.st_mode)
196
return index_entry_from_stat(stat_val, blob.id, 0)
198
def get_file_with_stat(self, path):
199
return (self.get_file(path), self._lstat(path))
201
def get_file(self, path):
202
"""See Tree.get_file."""
203
return self._file_transport.get(path)
205
def get_file_sha1(self, path, stat_value=None):
206
"""See Tree.get_file_sha1()."""
207
stream = self._file_transport.get(path)
208
return osutils.sha_file(stream)
210
def get_parent_ids(self):
211
"""See Tree.get_parent_ids.
213
This implementation returns the current cached value from
216
with self.lock_read():
217
return list(self._parent_ids)
219
def last_revision(self):
220
"""See MutableTree.last_revision."""
221
with self.lock_read():
222
if self.branch.head is None:
223
return _mod_revision.NULL_REVISION
224
return self.branch.repository.lookup_foreign_revision_id(
227
def basis_tree(self):
228
"""See Tree.basis_tree()."""
229
return self.branch.repository.revision_tree(self.last_revision())
231
def get_config_stack(self):
232
return self.branch.get_config_stack()
234
def has_filename(self, path):
235
return self._file_transport.has(path)
237
def _set_merges_from_parent_ids(self, rhs_parent_ids):
238
if self.branch.head is None:
239
self._parent_ids = []
241
self._parent_ids = [self.last_revision()]
242
self._parent_ids.extend(rhs_parent_ids)
244
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
245
if len(parent_ids) == 0:
246
self._parent_ids = []
247
self.branch.head = None
249
self._parent_ids = parent_ids
250
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
253
def mkdir(self, path, file_id=None):
254
"""See MutableTree.mkdir()."""
255
self.add(path, None, 'directory')
256
self._file_transport.mkdir(path)
258
def _rename_one(self, from_rel, to_rel):
259
self._file_transport.rename(from_rel, to_rel)
262
stat_value = self._file_transport.stat(p)
263
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
265
def get_symlink_target(self, path):
266
with self.lock_read():
267
return self._file_transport.readlink(path)