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)
54
self.symlink_target = None
58
return osutils.sha_string(self.object.data)
62
return len(self.object.data)
64
def kind_character(self):
65
"""See InventoryEntry.kind_character."""
69
class GitInventoryLink(GitInventoryEntry):
71
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
72
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
78
def symlink_target(self):
79
return self.object.data
81
def kind_character(self):
82
"""See InventoryEntry.kind_character."""
86
class GitInventoryDirectory(GitInventoryEntry):
88
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
89
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
92
self.symlink_target = None
93
self.kind = 'directory'
95
def kind_character(self):
96
"""See InventoryEntry.kind_character."""
102
for mode, name, hexsha in self.object.entries():
103
basename = name.decode("utf-8")
104
child_path = osutils.pathjoin(self.path, basename)
105
entry_kind = (mode & 0700000) / 0100000
106
fs_mode = mode & 0777
107
executable = bool(fs_mode & 0111)
109
kind_class = GitInventoryDirectory
110
elif entry_kind == 1:
111
file_kind = (mode & 070000) / 010000
113
kind_class = GitInventoryFile
115
kind_class = GitInventoryLink
117
raise AssertionError(
118
"Unknown file kind, perms=%o." % (mode,))
120
raise AssertionError(
121
"Unknown blob kind, perms=%r." % (mode,))
122
ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
126
class GitInventory(inventory.Inventory):
128
def __init__(self, tree_id, mapping, store, revision_id):
129
super(GitInventory, self).__init__(revision_id)
131
self.mapping = mapping
132
self.root = GitInventoryDirectory(self, None, tree_id, "", "", False)
134
def _get_ie(self, path):
135
parts = path.split("/")
138
ie = ie.children[name]
141
def has_filename(self, path):
148
def has_id(self, file_id):
150
self.id2path(file_id)
152
except errors.NoSuchId:
155
def id2path(self, file_id):
156
path = self.mapping.parse_file_id(file_id)
158
ie = self._get_ie(path)
159
assert ie.path == path
161
raise errors.NoSuchId(None, file_id)
163
def path2id(self, path):
165
return self._get_ie(path).file_id
169
def __getitem__(self, file_id):
170
if file_id == inventory.ROOT_ID:
172
path = self.mapping.parse_file_id(file_id)
173
return self._get_ie(path)