1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from dulwich.objects import (
34
from bzrlib.plugins.git.mapping import (
40
class GitInventoryEntry(inventory.InventoryEntry):
44
def __init__(self, inv, parent_id, hexsha, path, name, executable):
46
self.parent_id = parent_id
51
self.revision = self._inventory.revision_id
52
self.executable = executable
53
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
57
if self._object is None:
58
self._object = self._inventory.store[self.hexsha]
59
assert isinstance(self._object, self._git_class), \
60
"Expected instance of %r, got %r" % \
61
(self._git_class, self._object)
65
class GitInventoryFile(GitInventoryEntry):
69
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
70
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
73
self.symlink_target = None
77
return osutils.sha_string(self.object.data)
81
return len(self.object.data)
84
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
85
% (self.__class__.__name__,
93
def kind_character(self):
94
"""See InventoryEntry.kind_character."""
98
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
99
other.executable = self.executable
100
other.text_id = self.text_id
101
other.text_sha1 = self.text_sha1
102
other.text_size = self.text_size
103
other.revision = self.revision
107
class GitInventoryLink(GitInventoryEntry):
111
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
112
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
113
self.text_sha1 = None
114
self.text_size = None
115
self.kind = 'symlink'
118
def symlink_target(self):
119
return self.object.data
121
def kind_character(self):
122
"""See InventoryEntry.kind_character."""
126
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
127
other.symlink_target = self.symlink_target
128
other.revision = self.revision
132
class GitInventoryTreeReference(GitInventoryEntry):
136
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
137
super(GitInventoryTreeReference, self).__init__(inv, parent_id, hexsha, path, basename, executable)
139
self.reference_revision = inv.mapping.revision_id_foreign_to_bzr(hexsha)
140
self.text_sha1 = None
141
self.text_size = None
142
self.symlink_target = None
143
self.kind = 'tree-reference'
144
self._children = None
146
def kind_character(self):
147
"""See InventoryEntry.kind_character."""
151
class GitInventoryDirectory(GitInventoryEntry):
155
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
156
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
157
self.text_sha1 = None
158
self.text_size = None
159
self.symlink_target = None
160
self.kind = 'directory'
161
self._children = None
163
def kind_character(self):
164
"""See InventoryEntry.kind_character."""
169
if self._children is None:
170
self._retrieve_children()
171
return self._children
173
def _retrieve_children(self):
175
for mode, name, hexsha in self.object.entries():
176
basename = name.decode("utf-8")
177
child_path = osutils.pathjoin(self.path, basename)
178
executable = mode_is_executable(mode)
179
kind_class = {'directory': GitInventoryDirectory,
180
'file': GitInventoryFile,
181
'symlink': GitInventoryLink,
182
'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
183
self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
186
other = inventory.InventoryDirectory(self.file_id, self.name,
188
other.revision = self.revision
189
# note that children are *not* copied; they're pulled across when
194
class GitInventory(inventory.Inventory):
196
def __init__(self, tree_id, mapping, store, revision_id):
197
super(GitInventory, self).__init__(revision_id=revision_id)
199
self.mapping = mapping
200
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
202
def _get_ie(self, path):
205
parts = path.split("/")
208
ie = ie.children[name]
211
def has_filename(self, path):
218
def has_id(self, file_id):
220
self.id2path(file_id)
222
except errors.NoSuchId:
225
def id2path(self, file_id):
226
path = self.mapping.parse_file_id(file_id)
228
ie = self._get_ie(path)
229
assert ie.path == path
231
raise errors.NoSuchId(None, file_id)
233
def path2id(self, path):
235
return self._get_ie(path).file_id
239
def __getitem__(self, file_id):
240
if file_id == inventory.ROOT_ID:
242
path = self.mapping.parse_file_id(file_id)
244
return self._get_ie(path)
246
raise errors.NoSuchId(None, file_id)
249
class GitIndexInventory(inventory.Inventory):
250
"""Inventory that retrieves its contents from an index file."""
252
def __init__(self, basis_inventory, mapping, index, store):
253
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
254
self.basis_inv = basis_inventory
255
self.mapping = mapping
257
self._contents_read = False
258
self.root = self.add_path("", 'directory',
259
self.mapping.generate_file_id(""), None)
261
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
262
self._read_contents()
263
return super(GitIndexInventory, self).iter_entries_by_dir(specific_file_ids=specific_file_ids, yield_parents=yield_parents)
265
def has_id(self, file_id):
267
self.id2path(file_id)
269
except errors.NoSuchId:
272
def has_filename(self, path):
273
if path in self.index:
275
self._read_contents()
276
return super(GitIndexInventory, self).has_filename(path)
278
def id2path(self, file_id):
279
path = self.mapping.parse_file_id(file_id)
280
if path in self.index:
282
self._read_contents()
283
return super(GitIndexInventory, self).id2path(file_id)
285
def path2id(self, path):
286
if path in self.index:
287
return self.mapping.generate_file_id(path)
288
self._read_contents()
289
return super(GitIndexInventory, self).path2id(path)
291
def __getitem__(self, file_id):
292
self._read_contents()
293
return super(GitIndexInventory, self).__getitem__(file_id)
295
def _read_contents(self):
296
if self._contents_read:
298
self._contents_read = True
299
pb = ui.ui_factory.nested_progress_bar()
301
for i, (path, value) in enumerate(self.index.iteritems()):
302
pb.update("creating working inventory from index",
304
assert isinstance(path, str)
305
assert isinstance(value, tuple) and len(value) == 10
306
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
308
old_ie = self.basis_inv._get_ie(path)
312
file_id = self.mapping.generate_file_id(path)
314
file_id = old_ie.file_id
315
kind = mode_kind(mode)
316
if old_ie is not None and old_ie.hexsha == sha:
317
# Hasn't changed since basis inv
318
self.add_parents(path)
321
ie = self.add_path(path, kind, file_id, self.add_parents(path))
322
data = store[sha].data
323
if kind == "symlink":
324
ie.symlink_target = data
326
ie.text_sha1 = osutils.sha_string(data)
327
ie.text_size = len(data)
332
def add_parents(self, path):
333
dirname, _ = osutils.split(path)
334
file_id = super(GitIndexInventory, self).path2id(dirname)
339
parent_fid = self.add_parents(dirname)
340
ie = self.add_path(dirname, 'directory',
341
self.mapping.generate_file_id(dirname), parent_fid)
342
if ie.file_id in self.basis_inv:
343
ie.revision = self.basis_inv[ie.file_id].revision