38
38
revision as _mod_revision,
42
41
from breezy.transport.memory import MemoryTransport
43
from .mapping import GitFileIdMap
44
44
from .tree import MutableGitIndexTree
47
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
46
class GitMemoryTree(MutableGitIndexTree,_mod_tree.Tree):
48
47
"""A Git memory tree."""
50
49
def __init__(self, branch, store, head):
75
71
if kinds[pos] is None:
76
72
kinds[pos] = self.kind(f)
78
def put_file_bytes_non_atomic(self, path, bytes):
74
def put_file_bytes_non_atomic(self, path, bytes, file_id=None):
79
75
"""See MutableTree.put_file_bytes_non_atomic."""
80
76
self._file_transport.put_bytes(path, bytes)
88
84
self._file_transport = MemoryTransport()
89
85
if self.branch.head is None:
87
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
92
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)
93
92
tree = self.store[tree_id]
93
self._fileid_map = self._basis_fileid_map.copy()
95
95
trees = [("", tree)]
97
97
(path, tree) = trees.pop()
98
98
for name, mode, sha in tree.iteritems():
99
subpath = posixpath.join(path, name.decode('utf-8'))
99
subpath = posixpath.join(path, name)
100
100
if stat.S_ISDIR(mode):
101
101
self._file_transport.mkdir(subpath)
102
102
trees.append((subpath, self.store[sha]))
103
103
elif stat.S_ISREG(mode):
104
self._file_transport.put_bytes(
105
subpath, self.store[sha].data)
104
self._file_transport.put_bytes(subpath, self.store[sha].data)
106
105
self._index_add_entry(subpath, 'file')
108
107
raise NotImplementedError(self._populate_from_branch)
179
178
def _live_entry(self, path):
180
path = urlutils.quote_from_bytes(path)
181
179
stat_val = self._lstat(path)
182
180
if stat.S_ISDIR(stat_val.st_mode):
184
182
elif stat.S_ISLNK(stat_val.st_mode):
185
blob = Blob.from_string(
186
self._file_transport.readlink(path).encode('utf-8'))
183
blob = Blob.from_string(self._file_transport.readlink(path))
187
184
elif stat.S_ISREG(stat_val.st_mode):
188
185
blob = Blob.from_string(self._file_transport.get_bytes(path))
190
187
raise AssertionError('unknown type %d' % stat_val.st_mode)
191
188
return index_entry_from_stat(stat_val, blob.id, 0)
193
def get_file_with_stat(self, path):
194
return (self.get_file(path), self._lstat(path))
190
def get_file_with_stat(self, path, file_id=None):
191
return (self.get_file(path, file_id), self._lstat(path))
196
def get_file(self, path):
193
def get_file(self, path, file_id=None):
197
194
"""See Tree.get_file."""
198
195
return self._file_transport.get(path)
200
def get_file_sha1(self, path, stat_value=None):
197
def get_file_sha1(self, path, file_id=None, stat_value=None):
201
198
"""See Tree.get_file_sha1()."""
202
199
stream = self._file_transport.get(path)
203
200
return osutils.sha_file(stream)
216
213
with self.lock_read():
217
214
if self.branch.head is None:
218
215
return _mod_revision.NULL_REVISION
219
return self.branch.repository.lookup_foreign_revision_id(
216
return self.branch.repository.lookup_foreign_revision_id(self.branch.head)
222
218
def basis_tree(self):
223
219
"""See Tree.basis_tree()."""
242
238
self.branch.head = None
244
240
self._parent_ids = parent_ids
245
self.branch.head = self.branch.repository.lookup_bzr_revision_id(
241
self.branch.head = self.branch.repository.lookup_bzr_revision_id(parent_ids[0])[0]
248
243
def mkdir(self, path, file_id=None):
249
244
"""See MutableTree.mkdir()."""
256
251
def kind(self, p):
257
252
stat_value = self._file_transport.stat(p)
258
253
return osutils.file_kind_from_stat_mode(stat_value.st_mode)
260
def get_symlink_target(self, path):
261
with self.lock_read():
262
return self._file_transport.readlink(path)