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
32
class GitInventoryEntry(inventory.InventoryEntry):
34
def __init__(self, inv, parent_id, hexsha, path, name, executable):
36
self.parent_id = parent_id
41
self.revision = self._inventory.revision_id
42
self.executable = executable
43
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
47
if self._object is None:
48
self._object = self._inventory.store[self.hexsha]
52
class GitInventoryFile(GitInventoryEntry):
54
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
55
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
58
self.symlink_target = None
62
return osutils.sha_string(self.object.data)
66
return len(self.object.data)
69
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
70
% (self.__class__.__name__,
78
def kind_character(self):
79
"""See InventoryEntry.kind_character."""
83
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
84
other.executable = self.executable
85
other.text_id = self.text_id
86
other.text_sha1 = self.text_sha1
87
other.text_size = self.text_size
88
other.revision = self.revision
92
class GitInventoryLink(GitInventoryEntry):
94
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
95
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
101
def symlink_target(self):
102
return self.object.data
104
def kind_character(self):
105
"""See InventoryEntry.kind_character."""
109
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
110
other.symlink_target = self.symlink_target
111
other.revision = self.revision
115
class GitInventoryDirectory(GitInventoryEntry):
117
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
118
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
119
self.text_sha1 = None
120
self.text_size = None
121
self.symlink_target = None
122
self.kind = 'directory'
124
def kind_character(self):
125
"""See InventoryEntry.kind_character."""
131
for mode, name, hexsha in self.object.entries():
132
basename = name.decode("utf-8")
133
child_path = osutils.pathjoin(self.path, basename)
134
entry_kind = (mode & 0700000) / 0100000
135
fs_mode = mode & 0777
136
executable = bool(fs_mode & 0111)
138
kind_class = GitInventoryDirectory
139
elif entry_kind == 1:
140
file_kind = (mode & 070000) / 010000
142
kind_class = GitInventoryFile
144
kind_class = GitInventoryLink
146
raise AssertionError(
147
"Unknown file kind, perms=%o." % (mode,))
149
raise AssertionError(
150
"Unknown blob kind, perms=%r." % (mode,))
151
ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
155
other = inventory.InventoryDirectory(self.file_id, self.name,
157
other.revision = self.revision
158
# note that children are *not* copied; they're pulled across when
163
class GitInventory(inventory.Inventory):
165
def __init__(self, tree_id, mapping, store, revision_id):
166
super(GitInventory, self).__init__(revision_id=revision_id)
168
self.mapping = mapping
169
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
171
def _get_ie(self, path):
172
parts = path.split("/")
175
ie = ie.children[name]
178
def has_filename(self, path):
185
def has_id(self, file_id):
187
self.id2path(file_id)
189
except errors.NoSuchId:
192
def id2path(self, file_id):
193
path = self.mapping.parse_file_id(file_id)
195
ie = self._get_ie(path)
196
assert ie.path == path
198
raise errors.NoSuchId(None, file_id)
200
def path2id(self, path):
202
return self._get_ie(path).file_id
206
def __getitem__(self, file_id):
207
if file_id == inventory.ROOT_ID:
209
path = self.mapping.parse_file_id(file_id)
211
return self._get_ie(path)
213
raise errors.NoSuchId(None, file_id)
216
def GitIndexInventory(basis_inventory, mapping, index):
217
inv = inventory.Inventory(root_id=None)
219
def add_parents(path):
220
dirname, _ = osutils.split(path)
221
file_id = inv.path2id(dirname)
226
parent_fid = add_parents(dirname)
227
ie = inv.add_path(dirname, 'directory', mapping.generate_file_id(dirname), parent_fid)
228
if ie.file_id in basis_inventory:
229
ie.revision = basis_inventory[ie.file_id].revision
232
for path, value in index.iteritems():
233
assert isinstance(path, str)
234
assert isinstance(value, tuple) and len(value) == 10
235
(ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
236
old_file_id = basis_inventory.path2id(path)
237
if old_file_id is None:
238
file_id = mapping.generate_file_id(path)
240
file_id = old_file_id
241
if stat.S_ISLNK(mode):
244
assert stat.S_ISREG(mode)
246
ie = inv.add_path(path, kind, file_id, add_parents(path))
247
if old_file_id is not None:
248
ie.revision = basis_inventory[old_file_id].revision