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)
65
def kind_character(self):
66
"""See InventoryEntry.kind_character."""
70
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
71
other.executable = self.executable
72
other.text_id = self.text_id
73
other.text_sha1 = self.text_sha1
74
other.text_size = self.text_size
75
other.revision = self.revision
79
class GitInventoryLink(GitInventoryEntry):
81
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
82
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
88
def symlink_target(self):
89
return self.object.data
91
def kind_character(self):
92
"""See InventoryEntry.kind_character."""
96
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
97
other.symlink_target = self.symlink_target
98
other.revision = self.revision
102
class GitInventoryDirectory(GitInventoryEntry):
104
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
105
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
106
self.text_sha1 = None
107
self.text_size = None
108
self.symlink_target = None
109
self.kind = 'directory'
111
def kind_character(self):
112
"""See InventoryEntry.kind_character."""
118
for mode, name, hexsha in self.object.entries():
119
basename = name.decode("utf-8")
120
child_path = osutils.pathjoin(self.path, basename)
121
entry_kind = (mode & 0700000) / 0100000
122
fs_mode = mode & 0777
123
executable = bool(fs_mode & 0111)
125
kind_class = GitInventoryDirectory
126
elif entry_kind == 1:
127
file_kind = (mode & 070000) / 010000
129
kind_class = GitInventoryFile
131
kind_class = GitInventoryLink
133
raise AssertionError(
134
"Unknown file kind, perms=%o." % (mode,))
136
raise AssertionError(
137
"Unknown blob kind, perms=%r." % (mode,))
138
ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
142
other = inventory.InventoryDirectory(self.file_id, self.name,
144
other.revision = self.revision
145
# note that children are *not* copied; they're pulled across when
150
class GitInventory(inventory.Inventory):
152
def __init__(self, tree_id, mapping, store, revision_id):
153
super(GitInventory, self).__init__(revision_id=revision_id)
155
self.mapping = mapping
156
self.root = GitInventoryDirectory(self, None, tree_id, "", "", False)
158
def _get_ie(self, path):
159
parts = path.split("/")
162
ie = ie.children[name]
165
def has_filename(self, path):
172
def has_id(self, file_id):
174
self.id2path(file_id)
176
except errors.NoSuchId:
179
def id2path(self, file_id):
180
path = self.mapping.parse_file_id(file_id)
182
ie = self._get_ie(path)
183
assert ie.path == path
185
raise errors.NoSuchId(None, file_id)
187
def path2id(self, path):
189
return self._get_ie(path).file_id
193
def __getitem__(self, file_id):
194
if file_id == inventory.ROOT_ID:
196
path = self.mapping.parse_file_id(file_id)
198
return self._get_ie(path)
200
raise errors.NoSuchId(None, file_id)