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
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()
62
return self.branch.controldir
64
def is_control_filename(self, path):
67
def _gather_kinds(self, files, kinds):
68
"""See MutableTree._gather_kinds.
70
with self.lock_tree_write():
71
for pos, f in enumerate(files):
72
if kinds[pos] is None:
73
kinds[pos] = self.kind(f)
75
def put_file_bytes_non_atomic(self, path, bytes, file_id=None):
76
"""See MutableTree.put_file_bytes_non_atomic."""
77
self._file_transport.put_bytes(path, bytes)
79
def _populate_from_branch(self):
80
"""Populate the in-tree state from the branch."""
81
if self.branch.head is None:
84
self._parent_ids = [self.last_revision()]
85
self._file_transport = MemoryTransport()
86
if self.branch.head is None:
88
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
90
tree_id = self.store[self.branch.head].tree
91
self._basis_fileid_map = self.mapping.get_fileid_map(
92
self.store.__getitem__, tree_id)
93
tree = self.store[tree_id]
94
self._fileid_map = self._basis_fileid_map.copy()
98
(path, tree) = trees.pop()
99
for name, mode, sha in tree.iteritems():
100
subpath = posixpath.join(path, name.decode('utf-8'))
101
if stat.S_ISDIR(mode):
102
self._file_transport.mkdir(subpath)
103
trees.append((subpath, self.store[sha]))
104
elif stat.S_ISREG(mode):
105
self._file_transport.put_bytes(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)
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)
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)
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(self._file_transport.readlink(path).encode('utf-8'))
186
elif stat.S_ISREG(stat_val.st_mode):
187
blob = Blob.from_string(self._file_transport.get_bytes(path))
189
raise AssertionError('unknown type %d' % stat_val.st_mode)
190
return index_entry_from_stat(stat_val, blob.id, 0)
192
def get_file_with_stat(self, path, file_id=None):
193
return (self.get_file(path, file_id), self._lstat(path))
195
def get_file(self, path, file_id=None):
196
"""See Tree.get_file."""
197
return self._file_transport.get(path)
199
def get_file_sha1(self, path, file_id=None, stat_value=None):
200
"""See Tree.get_file_sha1()."""
201
stream = self._file_transport.get(path)
202
return osutils.sha_file(stream)
204
def get_parent_ids(self):
205
"""See Tree.get_parent_ids.
207
This implementation returns the current cached value from
210
with self.lock_read():
211
return list(self._parent_ids)
213
def last_revision(self):
214
"""See MutableTree.last_revision."""
215
with self.lock_read():
216
if self.branch.head is None:
217
return _mod_revision.NULL_REVISION
218
return self.branch.repository.lookup_foreign_revision_id(self.branch.head)
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(parent_ids[0])[0]
245
def mkdir(self, path, file_id=None):
246
"""See MutableTree.mkdir()."""
247
self.add(path, None, 'directory')
248
self._file_transport.mkdir(path)
250
def _rename_one(self, from_rel, to_rel):
251
self._file_transport.rename(from_rel, to_rel)
254
stat_value = self._file_transport.stat(p)
255
return osutils.file_kind_from_stat_mode(stat_value.st_mode)