65
54
# TODO: add a 'validate_utf8' for things like revision_id and file_id
66
55
# and a validator for parent-ids
67
_schema = {b'format': (None, int, _is_format_10),
68
b'committer': ('committer', bytes, cache_utf8.decode),
69
b'timezone': ('timezone', int, None),
70
b'timestamp': ('timestamp', bytes, float),
71
b'revision-id': ('revision_id', bytes, None),
72
b'parent-ids': ('parent_ids', list, None),
73
b'inventory-sha1': ('inventory_sha1', bytes, None),
74
b'message': ('message', bytes, cache_utf8.decode),
75
b'properties': ('properties', dict, _validate_properties),
56
_schema = {'format': (None, int, _is_format_10),
57
'committer': ('committer', str, cache_utf8.decode),
58
'timezone': ('timezone', int, None),
59
'timestamp': ('timestamp', str, float),
60
'revision-id': ('revision_id', str, None),
61
'parent-ids': ('parent_ids', list, None),
62
'inventory-sha1': ('inventory_sha1', str, None),
63
'message': ('message', str, cache_utf8.decode),
64
'properties': ('properties', dict, _validate_properties),
78
67
def write_revision_to_string(self, rev):
79
68
encode_utf8 = cache_utf8._utf8_encode
81
70
# This lets us control the ordering, so that we are able to create
85
(b"committer", encode_utf8(rev.committer)[0]),
74
("committer", encode_utf8(rev.committer)[0]),
87
76
if rev.timezone is not None:
88
ret.append((b"timezone", rev.timezone))
77
ret.append(("timezone", rev.timezone))
89
78
# For bzr revisions, the most common property is just 'branch-nick'
90
79
# which changes infrequently.
92
for key, value in rev.properties.items():
93
revprops[encode_utf8(key)[0]] = encode_utf8(value)[0]
94
ret.append((b'properties', revprops))
81
for key, value in rev.properties.iteritems():
82
revprops[key] = encode_utf8(value)[0]
83
ret.append(('properties', revprops))
96
(b"timestamp", b"%.3f" % rev.timestamp),
97
(b"revision-id", rev.revision_id),
98
(b"parent-ids", rev.parent_ids),
99
(b"inventory-sha1", rev.inventory_sha1),
100
(b"message", encode_utf8(rev.message)[0]),
85
("timestamp", "%.3f" % rev.timestamp),
86
("revision-id", rev.revision_id),
87
("parent-ids", rev.parent_ids),
88
("inventory-sha1", rev.inventory_sha1),
89
("message", encode_utf8(rev.message)[0]),
102
91
return bencode.bencode(ret)
104
def write_revision_to_lines(self, rev):
105
return self.write_revision_to_string(rev).splitlines(True)
93
def write_revision(self, rev, f):
94
f.write(self.write_revision_to_string(rev))
107
96
def read_revision_from_string(self, text):
108
97
# TODO: consider writing a Revision decoder, rather than using the
142
131
return self.read_revision_from_string(f.read())
145
class CHKSerializer(serializer.Serializer):
134
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml7.Serializer_v7):
135
"""A CHKInventory based serializer that supports tree references"""
137
supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
139
revision_format_num = None
140
support_altered_by_hack = False
142
def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
144
if not kind in self.supported_kinds:
145
raise AssertionError('unsupported entry kind %s' % kind)
146
if kind == 'tree-reference':
147
file_id = elt.attrib['file_id']
148
name = elt.attrib['name']
149
parent_id = elt.attrib['parent_id']
150
revision = elt.get('revision')
151
reference_revision = elt.get('reference_revision')
152
return inventory.TreeReference(file_id, name, parent_id, revision,
155
return xml7.Serializer_v7._unpack_entry(self, elt,
156
entry_cache=entry_cache, return_from_cache=return_from_cache)
158
def __init__(self, node_size, search_key_name):
159
self.maximum_size = node_size
160
self.search_key_name = search_key_name
163
class CHKSerializer(xml6.Serializer_v6):
146
164
"""A CHKInventory based serializer with 'plain' behaviour."""
149
167
revision_format_num = None
150
168
support_altered_by_hack = False
151
supported_kinds = {'file', 'directory', 'symlink', 'tree-reference'}
153
170
def __init__(self, node_size, search_key_name):
154
171
self.maximum_size = node_size
155
172
self.search_key_name = search_key_name
157
def _unpack_inventory(self, elt, revision_id=None, entry_cache=None,
158
return_from_cache=False):
159
"""Construct from XML Element"""
160
inv = xml_serializer.unpack_inventory_flat(elt, self.format_num,
161
xml_serializer.unpack_inventory_entry, entry_cache,
165
def read_inventory_from_lines(self, xml_lines, revision_id=None,
166
entry_cache=None, return_from_cache=False):
167
"""Read xml_string into an inventory object.
169
:param xml_string: The xml to read.
170
:param revision_id: If not-None, the expected revision id of the
172
:param entry_cache: An optional cache of InventoryEntry objects. If
173
supplied we will look up entries via (file_id, revision_id) which
174
should map to a valid InventoryEntry (File/Directory/etc) object.
175
:param return_from_cache: Return entries directly from the cache,
176
rather than copying them first. This is only safe if the caller
177
promises not to mutate the returned inventory entries, but it can
178
make some operations significantly faster.
181
return self._unpack_inventory(
182
xml_serializer.fromstringlist(xml_lines), revision_id,
183
entry_cache=entry_cache,
184
return_from_cache=return_from_cache)
185
except xml_serializer.ParseError as e:
186
raise errors.UnexpectedInventoryFormat(e)
188
def read_inventory(self, f, revision_id=None):
189
"""Read an inventory from a file-like object."""
192
return self._unpack_inventory(self._read_element(f),
196
except xml_serializer.ParseError as e:
197
raise errors.UnexpectedInventoryFormat(e)
199
def write_inventory_to_lines(self, inv):
200
"""Return a list of lines with the encoded inventory."""
201
return self.write_inventory(inv, None)
203
def write_inventory_to_chunks(self, inv):
204
"""Return a list of lines with the encoded inventory."""
205
return self.write_inventory(inv, None)
207
def write_inventory(self, inv, f, working=False):
208
"""Write inventory to a file.
210
:param inv: the inventory to write.
211
:param f: the file to write. (May be None if the lines are the desired
213
:param working: If True skip history data - text_sha1, text_size,
214
reference_revision, symlink_target.
215
:return: The inventory as a list of lines.
218
append = output.append
219
if inv.revision_id is not None:
222
xml_serializer.encode_and_escape(inv.revision_id), b'"'])
225
append(b'<inventory format="%s"%s>\n' % (
226
self.format_num, revid))
227
append(b'<directory file_id="%s" name="%s" revision="%s" />\n' % (
228
xml_serializer.encode_and_escape(inv.root.file_id),
229
xml_serializer.encode_and_escape(inv.root.name),
230
xml_serializer.encode_and_escape(inv.root.revision)))
231
xml_serializer.serialize_inventory_flat(inv,
233
root_id=None, supported_kinds=self.supported_kinds,
240
chk_serializer_255_bigpage = CHKSerializer(65536, b'hash-255-way')
175
chk_serializer_255_bigpage = CHKSerializer(65536, 'hash-255-way')
243
178
class CHKBEncodeSerializer(BEncodeRevisionSerializer1, CHKSerializer):
244
179
"""A CHKInventory and BEncode based serializer with 'plain' behaviour."""
249
chk_bencode_serializer = CHKBEncodeSerializer(65536, b'hash-255-way')
184
chk_bencode_serializer = CHKBEncodeSerializer(65536, 'hash-255-way')