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
29
class GitInventoryEntry(inventory.InventoryEntry):
31
def __init__(self, inv, parent_id, hexsha, path, name, executable):
33
self.parent_id = parent_id
38
self.revision = self._inventory.revision_id
39
self.executable = executable
40
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
44
if self._object is None:
45
self._object = self._inventory.store[self.hexsha]
49
class GitInventoryFile(GitInventoryEntry):
51
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
52
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
55
self.symlink_target = None
59
return osutils.sha_string(self.object.data)
63
return len(self.object.data)
66
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
67
% (self.__class__.__name__,
75
def kind_character(self):
76
"""See InventoryEntry.kind_character."""
80
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
81
other.executable = self.executable
82
other.text_id = self.text_id
83
other.text_sha1 = self.text_sha1
84
other.text_size = self.text_size
85
other.revision = self.revision
89
class GitInventoryLink(GitInventoryEntry):
91
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
92
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
98
def symlink_target(self):
99
return self.object.data
101
def kind_character(self):
102
"""See InventoryEntry.kind_character."""
106
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
107
other.symlink_target = self.symlink_target
108
other.revision = self.revision
112
class GitInventoryDirectory(GitInventoryEntry):
114
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
115
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
116
self.text_sha1 = None
117
self.text_size = None
118
self.symlink_target = None
119
self.kind = 'directory'
121
def kind_character(self):
122
"""See InventoryEntry.kind_character."""
128
for mode, name, hexsha in self.object.entries():
129
basename = name.decode("utf-8")
130
child_path = osutils.pathjoin(self.path, basename)
131
entry_kind = (mode & 0700000) / 0100000
132
fs_mode = mode & 0777
133
executable = bool(fs_mode & 0111)
135
kind_class = GitInventoryDirectory
136
elif entry_kind == 1:
137
file_kind = (mode & 070000) / 010000
139
kind_class = GitInventoryFile
141
kind_class = GitInventoryLink
143
raise AssertionError(
144
"Unknown file kind, perms=%o." % (mode,))
146
raise AssertionError(
147
"Unknown blob kind, perms=%r." % (mode,))
148
ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
152
other = inventory.InventoryDirectory(self.file_id, self.name,
154
other.revision = self.revision
155
# note that children are *not* copied; they're pulled across when
160
class GitInventory(inventory.Inventory):
162
def __init__(self, tree_id, mapping, store, revision_id):
163
super(GitInventory, self).__init__(revision_id=revision_id)
165
self.mapping = mapping
166
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
168
def _get_ie(self, path):
169
parts = path.split("/")
172
ie = ie.children[name]
175
def has_filename(self, path):
182
def has_id(self, file_id):
184
self.id2path(file_id)
186
except errors.NoSuchId:
189
def id2path(self, file_id):
190
path = self.mapping.parse_file_id(file_id)
192
ie = self._get_ie(path)
193
assert ie.path == path
195
raise errors.NoSuchId(None, file_id)
197
def path2id(self, path):
199
return self._get_ie(path).file_id
203
def __getitem__(self, file_id):
204
if file_id == inventory.ROOT_ID:
206
path = self.mapping.parse_file_id(file_id)
208
return self._get_ie(path)
210
raise errors.NoSuchId(None, file_id)