1
# This program is free software; you can redistribute it and/or modify
2
# it under the terms of the GNU General Public License as published by
3
# the Free Software Foundation; either version 2 of the License, or
4
# (at your option) any later version.
6
# This program is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
# GNU General Public License for more details.
11
# You should have received a copy of the GNU General Public License
12
# along with this program; if not, write to the Free Software
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
17
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
18
import bzrlib.inventory as inventory
19
from bzrlib.revision import Revision
20
from bzrlib.errors import BzrError
27
class _Serializer_v4(Serializer):
28
"""Version 0.0.4 serializer
30
You should use the serialzer_v4 singleton."""
34
def _pack_inventory(self, inv):
35
"""Convert to XML Element"""
36
e = Element('inventory')
38
if inv.root.file_id not in (None, ROOT_ID):
39
e.set('file_id', inv.root.file_id)
40
for path, ie in inv.iter_entries():
41
e.append(self._pack_entry(ie))
45
def _pack_entry(self, ie):
46
"""Convert InventoryEntry to XML element"""
48
e.set('name', ie.name)
49
e.set('file_id', ie.file_id)
50
e.set('kind', ie.kind)
52
if ie.text_size != None:
53
e.set('text_size', '%d' % ie.text_size)
55
for f in ['text_id', 'text_sha1', 'symlink_target']:
60
# to be conservative, we don't externalize the root pointers
61
# for now, leaving them as null in the xml form. in a future
62
# version it will be implied by nested elements.
63
if ie.parent_id != ROOT_ID:
64
assert isinstance(ie.parent_id, basestring)
65
e.set('parent_id', ie.parent_id)
72
def _unpack_inventory(self, elt):
73
"""Construct from XML Element
75
assert elt.tag == 'inventory'
76
root_id = elt.get('file_id') or ROOT_ID
77
inv = Inventory(root_id)
79
ie = self._unpack_entry(e)
80
if ie.parent_id == ROOT_ID:
81
ie.parent_id = root_id
86
def _unpack_entry(self, elt):
87
assert elt.tag == 'entry'
89
## original format inventories don't have a parent_id for
90
## nodes in the root directory, but it's cleaner to use one
92
parent_id = elt.get('parent_id')
96
kind = elt.get('kind')
97
if kind == 'directory':
98
ie = inventory.InventoryDirectory(elt.get('file_id'),
102
ie = inventory.InventoryFile(elt.get('file_id'),
105
ie.text_id = elt.get('text_id')
106
ie.text_sha1 = elt.get('text_sha1')
107
v = elt.get('text_size')
108
ie.text_size = v and int(v)
109
elif kind == 'symlink':
110
ie = inventory.InventoryLink(elt.get('file_id'),
113
ie.symlink_target = elt.get('symlink_target')
115
raise BzrError("unknown kind %r" % kind)
117
## mutter("read inventoryentry: %r", elt.attrib)
122
def _pack_revision(self, rev):
123
"""Revision object -> xml tree"""
124
root = Element('revision',
125
committer = rev.committer,
126
timestamp = '%.9f' % rev.timestamp,
127
revision_id = rev.revision_id,
128
inventory_id = rev.inventory_id,
129
inventory_sha1 = rev.inventory_sha1,
132
root.set('timezone', str(rev.timezone))
135
msg = SubElement(root, 'message')
136
msg.text = rev.message
140
pelts = SubElement(root, 'parents')
141
pelts.tail = pelts.text = '\n'
142
for i, parent_id in enumerate(rev.parents):
143
p = SubElement(pelts, 'revision_ref')
146
p.set('revision_id', parent_id)
147
if i < len(rev.parent_sha1s):
148
p.set('revision_sha1', rev.parent_sha1s[i])
152
def _unpack_revision(self, elt):
153
"""XML Element -> Revision object"""
155
# <changeset> is deprecated...
156
if elt.tag not in ('revision', 'changeset'):
157
raise BzrError("unexpected tag in revision file: %r" % elt)
159
rev = Revision(committer = elt.get('committer'),
160
timestamp = float(elt.get('timestamp')),
161
revision_id = elt.get('revision_id'),
162
inventory_id = elt.get('inventory_id'),
163
inventory_sha1 = elt.get('inventory_sha1')
166
precursor = elt.get('precursor')
167
precursor_sha1 = elt.get('precursor_sha1')
169
pelts = elt.find('parents')
173
assert p.tag == 'revision_ref', \
174
"bad parent node tag %r" % p.tag
175
rev.parent_ids.append(p.get('revision_id'))
176
rev.parent_sha1s.append(p.get('revision_sha1'))
179
prec_parent = rev.parent_ids[0]
180
assert prec_parent == precursor
182
# revisions written prior to 0.0.5 have a single precursor
183
# give as an attribute
184
rev.parent_ids.append(precursor)
185
rev.parent_sha1s.append(precursor_sha1)
187
v = elt.get('timezone')
188
rev.timezone = v and int(v)
190
rev.message = elt.findtext('message') # text of <message>
196
"""singleton instance"""
197
serializer_v4 = _Serializer_v4()