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
24
from dulwich.objects import (
39
class GitInventoryEntry(inventory.InventoryEntry):
43
def __init__(self, inv, parent_id, hexsha, path, name, executable):
45
self.parent_id = parent_id
50
self.revision = self._inventory.revision_id
51
self.executable = executable
52
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
56
if self._object is None:
57
self._object = self._inventory.store[self.hexsha]
58
assert isinstance(self._object, self._git_class), \
59
"Expected instance of %r, got %r" % \
60
(self._git_class, self._object)
64
class GitInventoryFile(GitInventoryEntry):
68
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
69
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
72
self.symlink_target = None
76
return osutils.sha_string(self.object.data)
80
return len(self.object.data)
83
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
84
% (self.__class__.__name__,
92
def kind_character(self):
93
"""See InventoryEntry.kind_character."""
97
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
98
other.executable = self.executable
99
other.text_id = self.text_id
100
other.text_sha1 = self.text_sha1
101
other.text_size = self.text_size
102
other.revision = self.revision
106
class GitInventoryLink(GitInventoryEntry):
110
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
111
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
112
self.text_sha1 = None
113
self.text_size = None
114
self.kind = 'symlink'
117
def symlink_target(self):
118
return self.object.data
120
def kind_character(self):
121
"""See InventoryEntry.kind_character."""
125
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
126
other.symlink_target = self.symlink_target
127
other.revision = self.revision
131
class GitInventoryDirectory(GitInventoryEntry):
135
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
136
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
137
self.text_sha1 = None
138
self.text_size = None
139
self.symlink_target = None
140
self.kind = 'directory'
141
self._children = None
143
def kind_character(self):
144
"""See InventoryEntry.kind_character."""
149
if self._children is None:
150
self._retrieve_children()
151
return self._children
153
def _retrieve_children(self):
155
for mode, name, hexsha in self.object.entries():
156
basename = name.decode("utf-8")
157
child_path = osutils.pathjoin(self.path, basename)
158
entry_kind = (mode & 0700000) / 0100000
159
fs_mode = mode & 0777
160
executable = bool(fs_mode & 0111)
162
kind_class = GitInventoryDirectory
163
elif entry_kind == 1:
164
file_kind = (mode & 070000) / 010000
166
kind_class = GitInventoryFile
168
kind_class = GitInventoryLink
170
raise AssertionError(
171
"Unknown file kind, perms=%o." % (mode,))
173
raise AssertionError(
174
"Unknown blob kind, perms=%r." % (mode,))
175
self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
178
other = inventory.InventoryDirectory(self.file_id, self.name,
180
other.revision = self.revision
181
# note that children are *not* copied; they're pulled across when
186
class GitInventory(inventory.Inventory):
188
def __init__(self, tree_id, mapping, store, revision_id):
189
super(GitInventory, self).__init__(revision_id=revision_id)
191
self.mapping = mapping
192
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
194
def _get_ie(self, path):
195
parts = path.split("/")
198
ie = ie.children[name]
201
def has_filename(self, path):
208
def has_id(self, file_id):
210
self.id2path(file_id)
212
except errors.NoSuchId:
215
def id2path(self, file_id):
216
path = self.mapping.parse_file_id(file_id)
218
ie = self._get_ie(path)
219
assert ie.path == path
221
raise errors.NoSuchId(None, file_id)
223
def path2id(self, path):
225
return self._get_ie(path).file_id
229
def __getitem__(self, file_id):
230
if file_id == inventory.ROOT_ID:
232
path = self.mapping.parse_file_id(file_id)
234
return self._get_ie(path)
236
raise errors.NoSuchId(None, file_id)
239
class GitIndexInventory(inventory.Inventory):
240
"""Inventory that retrieves its contents from an index file."""
242
def __init__(self, basis_inventory, mapping, index):
243
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
244
self.basis_inv = basis_inventory
245
self.mapping = mapping
248
pb = ui.ui_factory.nested_progress_bar()
250
for i, (path, value) in enumerate(self.index.iteritems()):
251
pb.update("creating working inventory from index",
253
assert isinstance(path, str)
254
assert isinstance(value, tuple) and len(value) == 10
255
(ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
257
old_ie = self.basis_inv._get_ie(path)
261
file_id = self.mapping.generate_file_id(path)
263
file_id = old_ie.file_id
264
if stat.S_ISLNK(mode):
267
assert stat.S_ISREG(mode)
269
if old_ie is not None and old_ie.hexsha == sha:
270
# Hasn't changed since basis inv
271
self.add_parents(path)
274
ie = self.add_path(path, kind, file_id, self.add_parents(path))
279
def add_parents(self, path):
280
dirname, _ = osutils.split(path)
281
file_id = self.path2id(dirname)
286
parent_fid = self.add_parents(dirname)
287
ie = self.add_path(dirname, 'directory',
288
self.mapping.generate_file_id(dirname), parent_fid)
289
if ie.file_id in self.basis_inv:
290
ie.revision = self.basis_inv[ie.file_id].revision