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
33
class GitInventoryEntry(inventory.InventoryEntry):
35
def __init__(self, inv, parent_id, hexsha, path, name, executable):
37
self.parent_id = parent_id
42
self.revision = self._inventory.revision_id
43
self.executable = executable
44
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
48
if self._object is None:
49
self._object = self._inventory.store[self.hexsha]
53
class GitInventoryFile(GitInventoryEntry):
55
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
56
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
59
self.symlink_target = None
63
return osutils.sha_string(self.object.data)
67
return len(self.object.data)
70
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
71
% (self.__class__.__name__,
79
def kind_character(self):
80
"""See InventoryEntry.kind_character."""
84
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
85
other.executable = self.executable
86
other.text_id = self.text_id
87
other.text_sha1 = self.text_sha1
88
other.text_size = self.text_size
89
other.revision = self.revision
93
class GitInventoryLink(GitInventoryEntry):
95
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
96
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
102
def symlink_target(self):
103
return self.object.data
105
def kind_character(self):
106
"""See InventoryEntry.kind_character."""
110
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
111
other.symlink_target = self.symlink_target
112
other.revision = self.revision
116
class GitInventoryDirectory(GitInventoryEntry):
118
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
119
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
120
self.text_sha1 = None
121
self.text_size = None
122
self.symlink_target = None
123
self.kind = 'directory'
124
self._children = None
126
def kind_character(self):
127
"""See InventoryEntry.kind_character."""
132
if self._children is None:
133
self._retrieve_children()
134
return self._children
136
def _retrieve_children(self):
138
for mode, name, hexsha in self.object.entries():
139
basename = name.decode("utf-8")
140
child_path = osutils.pathjoin(self.path, basename)
141
entry_kind = (mode & 0700000) / 0100000
142
fs_mode = mode & 0777
143
executable = bool(fs_mode & 0111)
145
kind_class = GitInventoryDirectory
146
elif entry_kind == 1:
147
file_kind = (mode & 070000) / 010000
149
kind_class = GitInventoryFile
151
kind_class = GitInventoryLink
153
raise AssertionError(
154
"Unknown file kind, perms=%o." % (mode,))
156
raise AssertionError(
157
"Unknown blob kind, perms=%r." % (mode,))
158
self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
161
other = inventory.InventoryDirectory(self.file_id, self.name,
163
other.revision = self.revision
164
# note that children are *not* copied; they're pulled across when
169
class GitInventory(inventory.Inventory):
171
def __init__(self, tree_id, mapping, store, revision_id):
172
super(GitInventory, self).__init__(revision_id=revision_id)
174
self.mapping = mapping
175
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
177
def _get_ie(self, path):
178
parts = path.split("/")
181
ie = ie.children[name]
184
def has_filename(self, path):
191
def has_id(self, file_id):
193
self.id2path(file_id)
195
except errors.NoSuchId:
198
def id2path(self, file_id):
199
path = self.mapping.parse_file_id(file_id)
201
ie = self._get_ie(path)
202
assert ie.path == path
204
raise errors.NoSuchId(None, file_id)
206
def path2id(self, path):
208
return self._get_ie(path).file_id
212
def __getitem__(self, file_id):
213
if file_id == inventory.ROOT_ID:
215
path = self.mapping.parse_file_id(file_id)
217
return self._get_ie(path)
219
raise errors.NoSuchId(None, file_id)
222
class GitIndexInventory(inventory.Inventory):
223
"""Inventory that retrieves its contents from an index file."""
225
def __init__(self, basis_inventory, mapping, index):
226
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
227
self.basis_inv = basis_inventory
228
self.mapping = mapping
231
pb = ui.ui_factory.nested_progress_bar()
233
for i, (path, value) in enumerate(self.index.iteritems()):
234
pb.update("creating working inventory from index",
236
assert isinstance(path, str)
237
assert isinstance(value, tuple) and len(value) == 10
238
(ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
240
old_ie = self.basis_inv._get_ie(path)
244
file_id = self.mapping.generate_file_id(path)
246
file_id = old_ie.file_id
247
if stat.S_ISLNK(mode):
250
assert stat.S_ISREG(mode)
252
if old_ie is not None and old_ie.hexsha == sha:
253
# Hasn't changed since basis inv
254
self.add_parents(path)
257
ie = self.add_path(path, kind, file_id, self.add_parents(path))
262
def add_parents(self, path):
263
dirname, _ = osutils.split(path)
264
file_id = self.path2id(dirname)
269
parent_fid = self.add_parents(dirname)
270
ie = self.add_path(dirname, 'directory',
271
self.mapping.generate_file_id(dirname), parent_fid)
272
if ie.file_id in self.basis_inv:
273
ie.revision = self.basis_inv[ie.file_id].revision