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(
58
if self._object is None:
59
self._object = self._inventory.store[self.hexsha]
60
assert isinstance(self._object, self._git_class), \
61
"Expected instance of %r, got %r" % \
62
(self._git_class, self._object)
66
class GitInventoryFile(GitInventoryEntry):
70
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
71
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path,
75
self.symlink_target = None
79
return osutils.sha_strings(self.object.chunked)
83
return len(self.object.data)
86
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
87
% (self.__class__.__name__,
95
def kind_character(self):
96
"""See InventoryEntry.kind_character."""
100
other = inventory.InventoryFile(self.file_id, self.name,
102
other.executable = self.executable
103
other.text_id = self.text_id
104
other.text_sha1 = self.text_sha1
105
other.text_size = self.text_size
106
other.revision = self.revision
110
class GitInventoryLink(GitInventoryEntry):
114
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
115
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
116
self.text_sha1 = None
117
self.text_size = None
118
self.kind = 'symlink'
121
def symlink_target(self):
122
return self.object.data
124
def kind_character(self):
125
"""See InventoryEntry.kind_character."""
129
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
130
other.executable = self.executable
131
other.symlink_target = self.symlink_target
132
other.revision = self.revision
136
class GitInventoryTreeReference(GitInventoryEntry):
140
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
141
super(GitInventoryTreeReference, self).__init__(inv, parent_id, hexsha, path, basename, executable)
143
self.reference_revision = inv.mapping.revision_id_foreign_to_bzr(hexsha)
144
self.text_sha1 = None
145
self.text_size = None
146
self.symlink_target = None
147
self.kind = 'tree-reference'
148
self._children = None
150
def kind_character(self):
151
"""See InventoryEntry.kind_character."""
155
class GitInventoryDirectory(GitInventoryEntry):
159
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
160
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
161
self.text_sha1 = None
162
self.text_size = None
163
self.symlink_target = None
164
self.kind = 'directory'
165
self._children = None
167
def kind_character(self):
168
"""See InventoryEntry.kind_character."""
173
if self._children is None:
174
self._retrieve_children()
175
return self._children
177
def _retrieve_children(self):
179
for mode, name, hexsha in self.object.entries():
180
basename = name.decode("utf-8")
181
child_path = osutils.pathjoin(self.path, basename)
182
executable = mode_is_executable(mode)
183
kind_class = {'directory': GitInventoryDirectory,
184
'file': GitInventoryFile,
185
'symlink': GitInventoryLink,
186
'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
187
self._children[basename] = kind_class(self._inventory,
188
self.file_id, hexsha, child_path, basename, executable)
191
other = inventory.InventoryDirectory(self.file_id, self.name,
193
other.revision = self.revision
194
# note that children are *not* copied; they're pulled across when
199
class GitInventory(inventory.Inventory):
201
def __init__(self, tree_id, mapping, store, revision_id):
202
super(GitInventory, self).__init__(revision_id=revision_id)
204
self.mapping = mapping
205
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
207
def _get_ie(self, path):
210
parts = path.split("/")
213
ie = ie.children[name]
216
def has_filename(self, path):
223
def has_id(self, file_id):
225
self.id2path(file_id)
227
except errors.NoSuchId:
230
def id2path(self, file_id):
231
path = self.mapping.parse_file_id(file_id)
233
ie = self._get_ie(path)
234
assert ie.path == path
236
raise errors.NoSuchId(None, file_id)
238
def path2id(self, path):
240
return self._get_ie(path).file_id
244
def __getitem__(self, file_id):
245
if file_id == inventory.ROOT_ID:
247
path = self.mapping.parse_file_id(file_id)
249
return self._get_ie(path)
251
raise errors.NoSuchId(None, file_id)
254
class GitIndexInventory(inventory.Inventory):
255
"""Inventory that retrieves its contents from an index file."""
257
def __init__(self, basis_inventory, mapping, index, store):
258
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
259
self.basis_inv = basis_inventory
260
self.mapping = mapping
262
self._contents_read = False
264
self.root = self.add_path("", 'directory',
265
self.mapping.generate_file_id(""), None)
267
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
268
self._read_contents()
269
return super(GitIndexInventory, self).iter_entries_by_dir(
270
specific_file_ids=specific_file_ids, yield_parents=yield_parents)
272
def has_id(self, file_id):
273
if type(file_id) != str:
276
self.id2path(file_id)
278
except errors.NoSuchId:
281
def has_filename(self, path):
282
if path in self.index:
284
self._read_contents()
285
return super(GitIndexInventory, self).has_filename(path)
287
def id2path(self, file_id):
288
if type(file_id) != str:
290
path = self.mapping.parse_file_id(file_id)
291
if path in self.index:
293
self._read_contents()
294
return super(GitIndexInventory, self).id2path(file_id)
296
def path2id(self, path):
297
if path in self.index:
298
return self.mapping.generate_file_id(path)
299
self._read_contents()
300
return super(GitIndexInventory, self).path2id(path)
302
def __getitem__(self, file_id):
303
self._read_contents()
304
return super(GitIndexInventory, self).__getitem__(file_id)
306
def _read_contents(self):
307
if self._contents_read:
309
self._contents_read = True
310
pb = ui.ui_factory.nested_progress_bar()
312
for i, (path, value) in enumerate(self.index.iteritems()):
313
pb.update("creating working inventory from index",
315
assert isinstance(path, str)
316
assert isinstance(value, tuple) and len(value) == 10
317
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
319
old_ie = self.basis_inv._get_ie(path)
323
file_id = self.mapping.generate_file_id(path)
325
file_id = old_ie.file_id
326
if type(file_id) != str:
328
kind = mode_kind(mode)
329
if old_ie is not None and old_ie.hexsha == sha:
330
# Hasn't changed since basis inv
331
self.add_parents(path)
334
ie = self.add_path(path, kind, file_id,
335
self.add_parents(path))
336
data = self.store[sha].data
337
if kind == "symlink":
338
ie.symlink_target = data
340
ie.text_sha1 = osutils.sha_string(data)
341
ie.text_size = len(data)
346
def add_parents(self, path):
347
dirname, _ = osutils.split(path)
348
file_id = super(GitIndexInventory, self).path2id(dirname)
353
parent_fid = self.add_parents(dirname)
354
ie = self.add_path(dirname, 'directory',
355
self.mapping.generate_file_id(dirname), parent_fid)
356
if ie.file_id in self.basis_inv:
357
ie.revision = self.basis_inv[ie.file_id].revision
359
if type(file_id) != str: