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,
41
from breezy.transport.memory import MemoryTransport
43
from .mapping import GitFileIdMap
44
from .tree import MutableGitIndexTree
46
class GitMemoryTree(MutableGitIndexTree,_mod_tree.Tree):
47
"""A Git memory tree."""
49
def __init__(self, branch, store, head):
50
MutableGitIndexTree.__init__(self)
52
self.mapping = self.branch.repository.get_mapping()
56
self._lock_mode = None
57
self._populate_from_branch()
61
return self.branch.controldir
63
def is_control_filename(self, path):
66
def _gather_kinds(self, files, kinds):
67
"""See MutableTree._gather_kinds.
69
with self.lock_tree_write():
70
for pos, f in enumerate(files):
71
if kinds[pos] is None:
72
kinds[pos] = self.kind(f)
74
def put_file_bytes_non_atomic(self, path, bytes, file_id=None):
75
"""See MutableTree.put_file_bytes_non_atomic."""
76
self._file_transport.put_bytes(path, bytes)
78
def _populate_from_branch(self):
79
"""Populate the in-tree state from the branch."""
80
if self.branch.head is None:
83
self._parent_ids = [self.last_revision()]
84
self._file_transport = MemoryTransport()
85
if self.branch.head is None:
87
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
89
tree_id = self.store[self.branch.head].tree
90
self._basis_fileid_map = self.mapping.get_fileid_map(
91
self.store.__getitem__, tree_id)
92
tree = self.store[tree_id]
93
self._fileid_map = self._basis_fileid_map.copy()
97
(path, tree) = trees.pop()
98
for name, mode, sha in tree.iteritems():
99
subpath = posixpath.join(path, name)
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(subpath, self.store[sha].data)
105
self._index_add_entry(subpath, 'file')
107
raise NotImplementedError(self._populate_from_branch)
110
"""Lock the memory tree for reading.
112
This triggers population of data from the branch for its revision.
117
self.branch.lock_read()
118
self._lock_mode = "r"
119
self._populate_from_branch()
120
return lock.LogicalLockResult(self.unlock)
125
def lock_tree_write(self):
126
"""See MutableTree.lock_tree_write()."""
130
self.branch.lock_read()
131
self._lock_mode = "w"
132
self._populate_from_branch()
133
elif self._lock_mode == "r":
134
raise errors.ReadOnlyError(self)
138
return lock.LogicalLockResult(self.unlock)
140
def lock_write(self):
141
"""See MutableTree.lock_write()."""
145
self.branch.lock_write()
146
self._lock_mode = "w"
147
self._populate_from_branch()
148
elif self._lock_mode == "r":
149
raise errors.ReadOnlyError(self)
150
return lock.LogicalLockResult(self.unlock)
158
This frees all cached state when the last lock context for the tree is
162
self._parent_ids = []
168
self._lock_mode = None
172
def _lstat(self, path):
173
mem_stat = self._file_transport.stat(path)
174
stat_val = os.stat_result(
175
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
178
def _live_entry(self, 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(self._file_transport.readlink(path))
184
elif stat.S_ISREG(stat_val.st_mode):
185
blob = Blob.from_string(self._file_transport.get_bytes(path))
187
raise AssertionError('unknown type %d' % stat_val.st_mode)
188
return index_entry_from_stat(stat_val, blob.id, 0)
190
def get_file_with_stat(self, path, file_id=None):
191
return (self.get_file(path, file_id), self._lstat(path))
193
def get_file(self, path, file_id=None):
194
"""See Tree.get_file."""
195
return self._file_transport.get(path)
197
def get_file_sha1(self, path, file_id=None, stat_value=None):
198
"""See Tree.get_file_sha1()."""
199
stream = self._file_transport.get(path)
200
return osutils.sha_file(stream)
202
def get_parent_ids(self):
203
"""See Tree.get_parent_ids.
205
This implementation returns the current cached value from
208
with self.lock_read():
209
return list(self._parent_ids)
211
def last_revision(self):
212
"""See MutableTree.last_revision."""
213
with self.lock_read():
214
if self.branch.head is None:
215
return _mod_revision.NULL_REVISION
216
return self.branch.repository.lookup_foreign_revision_id(self.branch.head)
218
def basis_tree(self):
219
"""See Tree.basis_tree()."""
220
return self.branch.repository.revision_tree(self.last_revision())
222
def get_config_stack(self):
223
return self.branch.get_config_stack()
225
def has_filename(self, path):
226
return self._file_transport.has(path)
228
def _set_merges_from_parent_ids(self, rhs_parent_ids):
229
if self.branch.head is None:
230
self._parent_ids = []
232
self._parent_ids = [self.last_revision()]
233
self._parent_ids.extend(rhs_parent_ids)
235
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
236
if len(parent_ids) == 0:
237
self._parent_ids = []
238
self.branch.head = None
240
self._parent_ids = parent_ids
241
self.branch.head = self.branch.repository.lookup_bzr_revision_id(parent_ids[0])[0]
243
def mkdir(self, path, file_id=None):
244
"""See MutableTree.mkdir()."""
245
self.add(path, None, 'directory')
246
self._file_transport.mkdir(path)
248
def _rename_one(self, from_rel, to_rel):
249
self._file_transport.rename(from_rel, to_rel)
252
stat_value = self._file_transport.stat(p)
253
return osutils.file_kind_from_stat_mode(stat_value.st_mode)