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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# 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."""
import stat
from bzrlib import (
errors,
inventory,
osutils,
ui,
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.text_id = None
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 __repr__(self):
return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
% (self.__class__.__name__,
self.file_id,
self.name,
self.parent_id,
self.text_sha1,
self.text_size,
self.revision))
def kind_character(self):
"""See InventoryEntry.kind_character."""
return ''
def copy(self):
other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
other.executable = self.executable
other.text_id = self.text_id
other.text_sha1 = self.text_sha1
other.text_size = self.text_size
other.revision = self.revision
return other
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 ''
def copy(self):
other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
other.symlink_target = self.symlink_target
other.revision = self.revision
return other
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'
self._children = None
def kind_character(self):
"""See InventoryEntry.kind_character."""
return '/'
@property
def children(self):
if self._children is None:
self._retrieve_children()
return self._children
def _retrieve_children(self):
self._children = {}
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,))
self._children[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
def copy(self):
other = inventory.InventoryDirectory(self.file_id, self.name,
self.parent_id)
other.revision = self.revision
# note that children are *not* copied; they're pulled across when
# others are added
return other
class GitInventory(inventory.Inventory):
def __init__(self, tree_id, mapping, store, revision_id):
super(GitInventory, self).__init__(revision_id=revision_id)
self.store = store
self.mapping = mapping
self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", 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)
try:
return self._get_ie(path)
except KeyError:
raise errors.NoSuchId(None, file_id)
class GitIndexInventory(inventory.Inventory):
"""Inventory that retrieves its contents from an index file."""
def __init__(self, basis_inventory, mapping, index):
super(GitIndexInventory, self).__init__(revision_id=None, root_id=basis_inventory.root.file_id)
self.basis_inv = basis_inventory
self.mapping = mapping
self.index = index
pb = ui.ui_factory.nested_progress_bar()
try:
for i, (path, value) in enumerate(self.index.iteritems()):
pb.update("creating working inventory from index",
i, len(self.index))
assert isinstance(path, str)
assert isinstance(value, tuple) and len(value) == 10
(ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
try:
old_ie = self.basis_inv._get_ie(path)
except KeyError:
old_ie = None
if old_ie is None:
file_id = self.mapping.generate_file_id(path)
else:
file_id = old_ie.file_id
if stat.S_ISLNK(mode):
kind = 'symlink'
else:
assert stat.S_ISREG(mode)
kind = 'file'
if old_ie is not None and old_ie.hexsha == sha:
# Hasn't changed since basis inv
self.add_parents(path)
self.add(old_ie)
else:
ie = self.add_path(path, kind, file_id, self.add_parents(path))
ie.revision = None
finally:
pb.finished()
def add_parents(self, path):
dirname, _ = osutils.split(path)
file_id = self.path2id(dirname)
if file_id is None:
if dirname == "":
parent_fid = None
else:
parent_fid = self.add_parents(dirname)
ie = self.add_path(dirname, 'directory',
self.mapping.generate_file_id(dirname), parent_fid)
if ie.file_id in self.basis_inv:
ie.revision = self.basis_inv[ie.file_id].revision
file_id = ie.file_id
return file_id
|