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()
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, file_id=None):
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:
89
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
91
tree_id = self.store[self.branch.head].tree
92
self._basis_fileid_map = self.mapping.get_fileid_map(
93
self.store.__getitem__, tree_id)
94
tree = self.store[tree_id]
95
self._fileid_map = self._basis_fileid_map.copy()
99
(path, tree) = trees.pop()
100
for name, mode, sha in tree.iteritems():
101
subpath = posixpath.join(path, name.decode('utf-8'))
102
if stat.S_ISDIR(mode):
103
self._file_transport.mkdir(subpath)
104
trees.append((subpath, self.store[sha]))
105
elif stat.S_ISREG(mode):
106
self._file_transport.put_bytes(
107
subpath, self.store[sha].data)
108
self._index_add_entry(subpath, 'file')
110
raise NotImplementedError(self._populate_from_branch)
113
"""Lock the memory tree for reading.
115
This triggers population of data from the branch for its revision.
120
self.branch.lock_read()
121
self._lock_mode = "r"
122
self._populate_from_branch()
123
return lock.LogicalLockResult(self.unlock)
124
except BaseException:
128
def lock_tree_write(self):
129
"""See MutableTree.lock_tree_write()."""
133
self.branch.lock_read()
134
self._lock_mode = "w"
135
self._populate_from_branch()
136
elif self._lock_mode == "r":
137
raise errors.ReadOnlyError(self)
138
except BaseException:
141
return lock.LogicalLockResult(self.unlock)
143
def lock_write(self):
144
"""See MutableTree.lock_write()."""
148
self.branch.lock_write()
149
self._lock_mode = "w"
150
self._populate_from_branch()
151
elif self._lock_mode == "r":
152
raise errors.ReadOnlyError(self)
153
return lock.LogicalLockResult(self.unlock)
154
except BaseException:
161
This frees all cached state when the last lock context for the tree is
165
self._parent_ids = []
171
self._lock_mode = None
175
def _lstat(self, path):
176
mem_stat = self._file_transport.stat(path)
177
stat_val = os.stat_result(
178
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
181
def _live_entry(self, path):
182
path = urlutils.quote_from_bytes(path)
183
stat_val = self._lstat(path)
184
if stat.S_ISDIR(stat_val.st_mode):
186
elif stat.S_ISLNK(stat_val.st_mode):
187
blob = Blob.from_string(
188
self._file_transport.readlink(path).encode('utf-8'))
189
elif stat.S_ISREG(stat_val.st_mode):
190
blob = Blob.from_string(self._file_transport.get_bytes(path))
192
raise AssertionError('unknown type %d' % stat_val.st_mode)
193
return index_entry_from_stat(stat_val, blob.id, 0)
195
def get_file_with_stat(self, path, file_id=None):
196
return (self.get_file(path, file_id), self._lstat(path))
198
def get_file(self, path, file_id=None):
199
"""See Tree.get_file."""
200
return self._file_transport.get(path)
202
def get_file_sha1(self, path, file_id=None, stat_value=None):
203
"""See Tree.get_file_sha1()."""
204
stream = self._file_transport.get(path)
205
return osutils.sha_file(stream)
207
def get_parent_ids(self):
208
"""See Tree.get_parent_ids.
210
This implementation returns the current cached value from
213
with self.lock_read():
214
return list(self._parent_ids)
216
def last_revision(self):
217
"""See MutableTree.last_revision."""
218
with self.lock_read():
219
if self.branch.head is None:
220
return _mod_revision.NULL_REVISION
221
return self.branch.repository.lookup_foreign_revision_id(
224
def basis_tree(self):
225
"""See Tree.basis_tree()."""
226
return self.branch.repository.revision_tree(self.last_revision())
228
def get_config_stack(self):
229
return self.branch.get_config_stack()
231
def has_filename(self, path):
232
return self._file_transport.has(path)
234
def _set_merges_from_parent_ids(self, rhs_parent_ids):
235
if self.branch.head is None:
236
self._parent_ids = []
238
self._parent_ids = [self.last_revision()]
239
self._parent_ids.extend(rhs_parent_ids)
241
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
242
if len(parent_ids) == 0:
243
self._parent_ids = []
244
self.branch.head = None
246
self._parent_ids = parent_ids
247
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
250
def mkdir(self, path, file_id=None):
251
"""See MutableTree.mkdir()."""
252
self.add(path, None, 'directory')
253
self._file_transport.mkdir(path)
255
def _rename_one(self, from_rel, to_rel):
256
self._file_transport.rename(from_rel, to_rel)
259
stat_value = self._file_transport.stat(p)
260
return osutils.file_kind_from_stat_mode(stat_value.st_mode)