125
129
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
130
other.executable = self.executable
126
131
other.symlink_target = self.symlink_target
127
132
other.revision = self.revision
136
class GitInventoryTreeReference(GitInventoryEntry):
140
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
141
super(GitInventoryTreeReference, self).__init__(inv, parent_id, hexsha, path, basename, executable)
143
self.reference_revision = inv.mapping.revision_id_foreign_to_bzr(hexsha)
144
self.text_sha1 = None
145
self.text_size = None
146
self.symlink_target = None
147
self.kind = 'tree-reference'
148
self._children = None
150
def kind_character(self):
151
"""See InventoryEntry.kind_character."""
131
155
class GitInventoryDirectory(GitInventoryEntry):
133
157
_git_class = Tree
155
179
for mode, name, hexsha in self.object.entries():
156
180
basename = name.decode("utf-8")
157
181
child_path = osutils.pathjoin(self.path, basename)
158
entry_kind = (mode & 0700000) / 0100000
159
fs_mode = mode & 0777
160
executable = bool(fs_mode & 0111)
162
kind_class = GitInventoryDirectory
163
elif entry_kind == 1:
164
file_kind = (mode & 070000) / 010000
166
kind_class = GitInventoryFile
168
kind_class = GitInventoryLink
170
raise AssertionError(
171
"Unknown file kind, perms=%o." % (mode,))
173
raise AssertionError(
174
"Unknown blob kind, perms=%r." % (mode,))
175
self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
182
if self._inventory.mapping.is_control_file(child_path):
184
executable = mode_is_executable(mode)
185
kind_class = {'directory': GitInventoryDirectory,
186
'file': GitInventoryFile,
187
'symlink': GitInventoryLink,
188
'tree-reference': GitInventoryTreeReference}[mode_kind(mode)]
189
self._children[basename] = kind_class(self._inventory,
190
self.file_id, hexsha, child_path, basename, executable)
178
other = inventory.InventoryDirectory(self.file_id, self.name,
193
other = inventory.InventoryDirectory(self.file_id, self.name,
180
195
other.revision = self.revision
181
196
# note that children are *not* copied; they're pulled across when
186
201
class GitInventory(inventory.Inventory):
188
def __init__(self, tree_id, mapping, store, revision_id):
204
return "<%s for %r in %r>" % (self.__class__.__name__,
205
self.root.hexsha, self.store)
207
def __init__(self, tree_id, mapping, fileid_map, store, revision_id):
189
208
super(GitInventory, self).__init__(revision_id=revision_id)
190
209
self.store = store
210
self.fileid_map = fileid_map
191
211
self.mapping = mapping
192
212
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
194
214
def _get_ie(self, path):
195
parts = path.split("/")
215
if path == "" or path == []:
217
if isinstance(path, basestring):
218
parts = path.split("/")
197
222
for name in parts:
198
ie = ie.children[name]
223
ie = ie.children[name]
201
226
def has_filename(self, path):
239
264
class GitIndexInventory(inventory.Inventory):
240
265
"""Inventory that retrieves its contents from an index file."""
242
def __init__(self, basis_inventory, mapping, index):
243
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
268
return "<%s for %r>" % (self.__class__.__name__, self.index)
270
def __init__(self, basis_inventory, fileid_map, index, store):
271
if basis_inventory is None:
274
root_id = basis_inventory.root.file_id
275
super(GitIndexInventory, self).__init__(revision_id=None, root_id=root_id)
244
276
self.basis_inv = basis_inventory
245
self.mapping = mapping
277
self.fileid_map = fileid_map
246
278
self.index = index
279
self._contents_read = False
281
self.root = self.add_path("", 'directory',
282
self.fileid_map.lookup_file_id(""), None)
284
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
285
self._read_contents()
286
return super(GitIndexInventory, self).iter_entries_by_dir(
287
specific_file_ids=specific_file_ids, yield_parents=yield_parents)
289
def has_id(self, file_id):
290
if type(file_id) != str:
293
self.id2path(file_id)
295
except errors.NoSuchId:
298
def has_filename(self, path):
299
if path in self.index:
301
self._read_contents()
302
return super(GitIndexInventory, self).has_filename(path)
304
def id2path(self, file_id):
305
if type(file_id) != str:
307
path = self.fileid_map.lookup_path(file_id)
308
if path in self.index:
310
self._read_contents()
311
return super(GitIndexInventory, self).id2path(file_id)
313
def path2id(self, path):
314
if type(path) in (list, tuple):
315
path = "/".join(path)
316
if path in self.index:
317
file_id = self.fileid_map.lookup_file_id(path)
319
self._read_contents()
320
file_id = super(GitIndexInventory, self).path2id(path)
321
if file_id is not None and type(file_id) is not str:
325
def __getitem__(self, file_id):
326
self._read_contents()
327
return super(GitIndexInventory, self).__getitem__(file_id)
329
def _read_contents(self):
330
if self._contents_read:
332
self._contents_read = True
248
333
pb = ui.ui_factory.nested_progress_bar()
250
335
for i, (path, value) in enumerate(self.index.iteritems()):
251
pb.update("creating working inventory from index",
336
pb.update("creating working inventory from index",
252
337
i, len(self.index))
253
338
assert isinstance(path, str)
254
339
assert isinstance(value, tuple) and len(value) == 10
255
(ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
257
old_ie = self.basis_inv._get_ie(path)
340
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
341
if self.basis_inv is not None:
343
old_ie = self.basis_inv._get_ie(path)
260
348
if old_ie is None:
261
file_id = self.mapping.generate_file_id(path)
349
file_id = self.fileid_map.lookup_file_id(path)
263
351
file_id = old_ie.file_id
264
if stat.S_ISLNK(mode):
267
assert stat.S_ISREG(mode)
352
if type(file_id) != str:
354
kind = mode_kind(mode)
269
355
if old_ie is not None and old_ie.hexsha == sha:
270
356
# Hasn't changed since basis inv
271
357
self.add_parents(path)
274
ie = self.add_path(path, kind, file_id, self.add_parents(path))
360
ie = self.add_path(path, kind, file_id,
361
self.add_parents(path))
362
data = self.store[sha].data
363
if kind == "symlink":
364
ie.symlink_target = data
366
ie.text_sha1 = osutils.sha_string(data)
367
ie.text_size = len(data)
275
368
ie.revision = None
279
372
def add_parents(self, path):
280
373
dirname, _ = osutils.split(path)
281
file_id = self.path2id(dirname)
374
file_id = super(GitIndexInventory, self).path2id(dirname)
282
375
if file_id is None:
283
376
if dirname == "":
284
377
parent_fid = None
286
379
parent_fid = self.add_parents(dirname)
287
ie = self.add_path(dirname, 'directory',
288
self.mapping.generate_file_id(dirname), parent_fid)
289
if ie.file_id in self.basis_inv:
380
ie = self.add_path(dirname, 'directory',
381
self.fileid_map.lookup_file_id(dirname), parent_fid)
382
if self.basis_inv is not None and ie.file_id in self.basis_inv:
290
383
ie.revision = self.basis_inv[ie.file_id].revision
291
384
file_id = ie.file_id
385
if type(file_id) != str: