21
21
from bzrlib import (
28
class GitInventory(inventory.Inventory):
30
def __init__(self, tree_id, mapping, store, revision_id):
31
super(GitInventory, self).__init__(revision_id)
33
self.mapping = mapping
34
self.root.revision = revision_id
35
self._build_inventory(tree_id, self.root, "")
37
def _build_inventory(self, tree_id, ie, path):
38
assert isinstance(path, str)
39
tree = self.store[tree_id]
40
for mode, name, hexsha in tree.entries():
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():
41
103
basename = name.decode("utf-8")
45
child_path = urlutils.join(path, name)
46
file_id = self.mapping.generate_file_id(child_path)
104
child_path = osutils.pathjoin(self.path, basename)
47
105
entry_kind = (mode & 0700000) / 0100000
106
fs_mode = mode & 0777
107
executable = bool(fs_mode & 0111)
48
108
if entry_kind == 0:
49
child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
109
kind_class = GitInventoryDirectory
50
110
elif entry_kind == 1:
51
111
file_kind = (mode & 070000) / 010000
52
b = self.store[hexsha]
53
112
if file_kind == 0:
54
child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
55
child_ie.text_sha1 = osutils.sha_string(b.data)
113
kind_class = GitInventoryFile
56
114
elif file_kind == 2:
57
child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
58
child_ie.symlink_target = b.data
59
child_ie.text_sha1 = osutils.sha_string("")
115
kind_class = GitInventoryLink
61
117
raise AssertionError(
62
118
"Unknown file kind, perms=%o." % (mode,))
63
child_ie.text_id = b.id
64
child_ie.text_size = len(b.data)
66
120
raise AssertionError(
67
121
"Unknown blob kind, perms=%r." % (mode,))
69
child_ie.executable = bool(fs_mode & 0111)
70
# TODO: This should be set to the revision id in which
71
# child_ie was last changed instead.
72
child_ie.revision = self.root.revision
75
self._build_inventory(hexsha, child_ie, child_path)
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)