14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from ...bzr.xml_serializer import (
17
from bzrlib.xml_serializer import (
21
22
escape_invalid_chars,
23
from ...bzr.inventory import ROOT_ID, Inventory
24
from ...bzr import inventory
25
from ...revision import Revision
26
from ...errors import BzrError
24
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
25
import bzrlib.inventory as inventory
26
from bzrlib.revision import Revision
27
from bzrlib.errors import BzrError
29
30
class _Serializer_v4(XMLSerializer):
64
def _unpack_inventory(self, elt, revision_id=None, entry_cache=None,
65
return_from_cache=False):
66
def _unpack_inventory(self, elt, revision_id=None, entry_cache=None):
66
67
"""Construct from XML Element
68
69
:param revision_id: Ignored parameter used by xml5.
70
root_id = elt.get('file_id')
71
root_id = (root_id.encode('ascii') if root_id else ROOT_ID)
71
root_id = elt.get('file_id') or ROOT_ID
72
72
inv = Inventory(root_id)
74
ie = self._unpack_entry(e, entry_cache=entry_cache,
75
return_from_cache=return_from_cache)
74
ie = self._unpack_entry(e, entry_cache=entry_cache)
76
75
if ie.parent_id == ROOT_ID:
77
76
ie.parent_id = root_id
81
def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
82
# original format inventories don't have a parent_id for
83
# nodes in the root directory, but it's cleaner to use one
81
def _unpack_entry(self, elt, entry_cache=None):
82
## original format inventories don't have a parent_id for
83
## nodes in the root directory, but it's cleaner to use one
85
85
parent_id = elt.get('parent_id')
86
parent_id = (parent_id.encode('ascii') if parent_id else ROOT_ID)
88
file_id = elt.get('file_id').encode('ascii')
89
89
kind = elt.get('kind')
90
90
if kind == 'directory':
91
ie = inventory.InventoryDirectory(file_id,
91
ie = inventory.InventoryDirectory(elt.get('file_id'),
94
94
elif kind == 'file':
95
ie = inventory.InventoryFile(file_id,
95
ie = inventory.InventoryFile(elt.get('file_id'),
98
98
ie.text_id = elt.get('text_id')
99
if ie.text_id is not None:
100
ie.text_id = ie.text_id.encode('utf-8')
101
99
ie.text_sha1 = elt.get('text_sha1')
102
if ie.text_sha1 is not None:
103
ie.text_sha1 = ie.text_sha1.encode('ascii')
104
100
v = elt.get('text_size')
105
101
ie.text_size = v and int(v)
106
102
elif kind == 'symlink':
107
ie = inventory.InventoryLink(file_id,
103
ie = inventory.InventoryLink(elt.get('file_id'),
110
106
ie.symlink_target = elt.get('symlink_target')
118
115
def _pack_revision(self, rev):
119
116
"""Revision object -> xml tree"""
120
117
root = Element('revision',
121
committer=rev.committer,
122
timestamp='%.9f' % rev.timestamp,
123
revision_id=rev.revision_id,
124
inventory_id=rev.inventory_id,
125
inventory_sha1=rev.inventory_sha1,
118
committer = rev.committer,
119
timestamp = '%.9f' % rev.timestamp,
120
revision_id = rev.revision_id,
121
inventory_id = rev.inventory_id,
122
inventory_sha1 = rev.inventory_sha1,
128
125
root.set('timezone', str(rev.timezone))
143
140
p.set('revision_sha1', rev.parent_sha1s[i])
146
def write_revision_to_string(self, rev):
147
return tostring(self._pack_revision(rev)) + b'\n'
149
def _write_element(self, elt, f):
150
ElementTree(elt).write(f, 'utf-8')
153
144
def _unpack_revision(self, elt):
154
145
"""XML Element -> Revision object"""
157
148
if elt.tag not in ('revision', 'changeset'):
158
149
raise BzrError("unexpected tag in revision file: %r" % elt)
160
rev = Revision(committer=elt.get('committer'),
161
timestamp=float(elt.get('timestamp')),
162
revision_id=elt.get('revision_id'),
163
inventory_id=elt.get('inventory_id'),
164
inventory_sha1=elt.get('inventory_sha1')
151
rev = Revision(committer = elt.get('committer'),
152
timestamp = float(elt.get('timestamp')),
153
revision_id = elt.get('revision_id'),
154
inventory_id = elt.get('inventory_id'),
155
inventory_sha1 = elt.get('inventory_sha1')
167
158
precursor = elt.get('precursor')