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.objects import (
34
revision as _mod_revision,
37
from breezy.transport.memory import MemoryTransport
39
from .mapping import GitFileIdMap
40
from .tree import MutableGitIndexTree
42
class GitMemoryTree(MutableGitIndexTree,_mod_tree.Tree):
43
"""A Git memory tree."""
45
def __init__(self, branch, store, head):
46
MutableGitIndexTree.__init__(self)
48
self.mapping = self.branch.repository.get_mapping()
52
self._lock_mode = None
53
self._populate_from_branch()
57
return self.branch.controldir
59
def is_control_filename(self, path):
62
def _gather_kinds(self, files, kinds):
63
"""See MutableTree._gather_kinds.
65
with self.lock_tree_write():
66
for pos, f in enumerate(files):
67
if kinds[pos] is None:
68
kinds[pos] = self.kind(f)
70
def put_file_bytes_non_atomic(self, path, bytes, file_id=None):
71
"""See MutableTree.put_file_bytes_non_atomic."""
72
self._file_transport.put_bytes(path, bytes)
74
def _populate_from_branch(self):
75
"""Populate the in-tree state from the branch."""
76
if self.branch.head is None:
79
self._parent_ids = [self.last_revision()]
80
self._file_transport = MemoryTransport()
81
if self.branch.head is None:
83
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
85
tree_id = self.store[self.branch.head].tree
86
self._basis_fileid_map = self.mapping.get_fileid_map(
87
self.store.__getitem__, tree_id)
88
tree = self.store[tree_id]
89
self._fileid_map = self._basis_fileid_map.copy()
93
(path, tree) = trees.pop()
94
for name, mode, sha in tree.iteritems():
95
subpath = posixpath.join(path, name)
96
if stat.S_ISDIR(mode):
97
self._file_transport.mkdir(subpath)
98
trees.append((subpath, self.store[sha]))
99
elif stat.S_ISREG(mode):
100
self._file_transport.put_bytes(subpath, self.store[sha].data)
101
self._index_add_entry(subpath, 'file')
103
raise NotImplementedError(self._populate_from_branch)
106
"""Lock the memory tree for reading.
108
This triggers population of data from the branch for its revision.
113
self.branch.lock_read()
114
self._lock_mode = "r"
115
self._populate_from_branch()
116
return lock.LogicalLockResult(self.unlock)
121
def lock_tree_write(self):
122
"""See MutableTree.lock_tree_write()."""
126
self.branch.lock_read()
127
self._lock_mode = "w"
128
self._populate_from_branch()
129
elif self._lock_mode == "r":
130
raise errors.ReadOnlyError(self)
134
return lock.LogicalLockResult(self.unlock)
136
def lock_write(self):
137
"""See MutableTree.lock_write()."""
141
self.branch.lock_write()
142
self._lock_mode = "w"
143
self._populate_from_branch()
144
elif self._lock_mode == "r":
145
raise errors.ReadOnlyError(self)
146
return lock.LogicalLockResult(self.unlock)
154
This frees all cached state when the last lock context for the tree is
158
self._parent_ids = []
164
self._lock_mode = None
168
def _lstat(self, path):
169
mem_stat = self._file_transport.stat(path)
170
stat_val = os.stat_result(
171
(mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
174
def get_file_with_stat(self, path, file_id=None):
175
return (self.get_file(path, file_id), self._lstat(path))
177
def get_file(self, path, file_id=None):
178
"""See Tree.get_file."""
179
return self._file_transport.get(path)
181
def get_file_sha1(self, path, file_id=None, stat_value=None):
182
"""See Tree.get_file_sha1()."""
183
stream = self._file_transport.get(path)
184
return osutils.sha_file(stream)
186
def get_parent_ids(self):
187
"""See Tree.get_parent_ids.
189
This implementation returns the current cached value from
192
with self.lock_read():
193
return list(self._parent_ids)
195
def last_revision(self):
196
"""See MutableTree.last_revision."""
197
with self.lock_read():
198
if self.branch.head is None:
199
return _mod_revision.NULL_REVISION
200
return self.branch.repository.lookup_foreign_revision_id(self.branch.head)
202
def basis_tree(self):
203
"""See Tree.basis_tree()."""
204
return self.branch.repository.revision_tree(self.last_revision())
206
def get_config_stack(self):
207
return self.branch.get_config_stack()
209
def has_filename(self, path):
210
return self._file_transport.has(path)
212
def _set_merges_from_parent_ids(self, rhs_parent_ids):
213
if self.branch.head is None:
214
self._parent_ids = []
216
self._parent_ids = [self.last_revision()]
217
self._parent_ids.extend(rhs_parent_ids)
219
def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
220
if len(parent_ids) == 0:
221
self._parent_ids = []
222
self.branch.head = None
224
self._parent_ids = parent_ids
225
self.branch.head = self.branch.repository.lookup_bzr_revision_id(parent_ids[0])[0]
227
def mkdir(self, path, file_id=None):
228
"""See MutableTree.mkdir()."""
229
self.add(path, None, 'directory')
230
self._file_transport.mkdir(path)
232
def _rename_one(self, from_rel, to_rel):
233
self._file_transport.rename(from_rel, to_rel)
236
stat_value = self._file_transport.stat(p)
237
return osutils.file_kind_from_stat_mode(stat_value.st_mode)