1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Git inventory."""
from bzrlib import (
errors,
inventory,
osutils,
urlutils,
)
class GitInventoryEntry(inventory.InventoryEntry):
def __init__(self, inv, parent_id, hexsha, path, name, executable):
self.name = name
self.parent_id = parent_id
self._inventory = inv
self._object = None
self.hexsha = hexsha
self.path = path
self.revision = self._inventory.revision_id
self.executable = executable
self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
@property
def object(self):
if self._object is None:
self._object = self._inventory.store[self.hexsha]
return self._object
class GitInventoryFile(GitInventoryEntry):
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
self.kind = 'file'
self.symlink_target = None
@property
def text_sha1(self):
return osutils.sha_string(self.object.data)
@property
def text_size(self):
return len(self.object.data)
def kind_character(self):
"""See InventoryEntry.kind_character."""
return ''
class GitInventoryLink(GitInventoryEntry):
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
self.text_sha1 = None
self.text_size = None
self.kind = 'symlink'
@property
def symlink_target(self):
return self.object.data
def kind_character(self):
"""See InventoryEntry.kind_character."""
return ''
class GitInventoryDirectory(GitInventoryEntry):
def __init__(self, inv, parent_id, hexsha, path, basename, executable):
super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
self.text_sha1 = None
self.text_size = None
self.symlink_target = None
self.kind = 'directory'
def kind_character(self):
"""See InventoryEntry.kind_character."""
return '/'
@property
def children(self):
ret = {}
for mode, name, hexsha in self.object.entries():
basename = name.decode("utf-8")
child_path = osutils.pathjoin(self.path, basename)
entry_kind = (mode & 0700000) / 0100000
fs_mode = mode & 0777
executable = bool(fs_mode & 0111)
if entry_kind == 0:
kind_class = GitInventoryDirectory
elif entry_kind == 1:
file_kind = (mode & 070000) / 010000
if file_kind == 0:
kind_class = GitInventoryFile
elif file_kind == 2:
kind_class = GitInventoryLink
else:
raise AssertionError(
"Unknown file kind, perms=%o." % (mode,))
else:
raise AssertionError(
"Unknown blob kind, perms=%r." % (mode,))
ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
return ret
class GitInventory(inventory.Inventory):
def __init__(self, tree_id, mapping, store, revision_id):
super(GitInventory, self).__init__(revision_id)
self.store = store
self.mapping = mapping
self.root = GitInventoryDirectory(self, None, tree_id, "", "", False)
def _get_ie(self, path):
parts = path.split("/")
ie = self.root
for name in parts:
ie = ie.children[name]
return ie
def has_filename(self, path):
try:
self._get_ie(path)
return True
except KeyError:
return False
def has_id(self, file_id):
try:
self.id2path(file_id)
return True
except errors.NoSuchId:
return False
def id2path(self, file_id):
path = self.mapping.parse_file_id(file_id)
try:
ie = self._get_ie(path)
assert ie.path == path
except KeyError:
raise errors.NoSuchId(None, file_id)
def path2id(self, path):
try:
return self._get_ie(path).file_id
except KeyError:
return None
def __getitem__(self, file_id):
if file_id == inventory.ROOT_ID:
return self.root
path = self.mapping.parse_file_id(file_id)
return self._get_ie(path)
|