1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
70
70
# revisions can be serialized.
72
72
from copy import copy
73
from cStringIO import StringIO
73
75
from sha import sha
75
77
from bzrlib.osutils import contains_whitespace, contains_linebreaks
78
79
class Testament(object):
79
80
"""Reduced summary of a revision.
83
84
- produced from a revision
85
86
- loaded from a stream
86
87
- compared to a revision
89
long_header = 'bazaar-ng testament version 1\n'
90
short_header = 'bazaar-ng testament short form 1\n'
93
def from_revision(cls, repository, revision_id):
91
def from_revision(cls, branch, revision_id):
94
92
"""Produce a new testament from a historical revision"""
95
rev = repository.get_revision(revision_id)
96
inventory = repository.get_inventory(revision_id)
93
rev = branch.get_revision(revision_id)
94
inventory = branch.get_inventory(revision_id)
97
95
return cls(rev, inventory)
99
97
def __init__(self, rev, inventory):
131
130
for l in self.message.splitlines():
133
132
a('inventory:\n')
134
for path, ie in self._get_entries():
133
for path, ie in self.inventory.iter_entries():
135
134
a(self._entry_to_line(path, ie))
136
135
r.extend(self._revprops_to_lines())
139
138
assert isinstance(l, basestring), \
140
139
'%r of type %s is not a plain string' % (l, type(l))
141
return [line.encode('utf-8') for line in r]
143
def _get_entries(self):
144
entries = self.inventory.iter_entries()
148
142
def _escape_path(self, path):
149
143
assert not contains_linebreaks(path)
150
return unicode(path.replace('\\', '/').replace(' ', '\ '))
144
return unicode(path.replace('\\', '/').replace(' ', '\ ')).encode('utf-8')
152
146
def _entry_to_line(self, path, ie):
153
147
"""Turn an inventory entry into a testament line"""
148
l = ' ' + str(ie.kind)
149
l += ' ' + self._escape_path(path)
154
150
assert not contains_whitespace(ie.file_id)
151
l += ' ' + unicode(ie.file_id).encode('utf-8')
158
152
if ie.kind == 'file':
159
153
# TODO: avoid switching on kind
160
154
assert ie.text_sha1
161
content = ie.text_sha1
155
l += ' ' + ie.text_sha1
163
156
elif ie.kind == 'symlink':
164
157
assert ie.symlink_target
165
content = self._escape_path(ie.symlink_target)
168
l = u' %s %s %s%s%s\n' % (ie.kind, self._escape_path(path),
170
content_spacer, content)
158
l += ' ' + self._escape_path(ie.symlink_target)
173
162
def as_text(self):
176
165
def as_short_text(self):
177
166
"""Return short digest-based testament."""
178
return (self.short_header +
168
map(s.update, self.as_text_lines())
169
return ('bazaar-ng testament short form 1\n'
179
170
'revision-id: %s\n'
181
% (self.revision_id, self.as_sha1()))
172
% (self.revision_id, s.hexdigest()))
183
174
def _revprops_to_lines(self):
184
175
"""Pack up revision properties."""
190
181
assert not contains_whitespace(name)
191
182
r.append(' %s:\n' % name)
192
183
for line in value.splitlines():
193
r.append(u' %s\n' % line)
184
if not isinstance(line, str):
185
line = line.encode('utf-8')
186
r.append(' %s\n' % line)
198
map(s.update, self.as_text_lines())
202
class StrictTestament(Testament):
203
"""This testament format is for use as a checksum in bundle format 0.8"""
205
long_header = 'bazaar-ng testament version 2.1\n'
206
short_header = 'bazaar-ng testament short form 2.1\n'
207
def _entry_to_line(self, path, ie):
208
l = Testament._entry_to_line(self, path, ie)[:-1]
209
l += ' ' + ie.revision
210
l += {True: ' yes\n', False: ' no\n'}[ie.executable]
214
class StrictTestament3(StrictTestament):
215
"""This testament format is for use as a checksum in bundle format 0.9+
217
It differs from StrictTestament by including data about the tree root.
220
long_header = 'bazaar testament version 3 strict\n'
221
short_header = 'bazaar testament short form 3 strict\n'
222
def _get_entries(self):
223
return self.inventory.iter_entries()
225
def _escape_path(self, path):
226
assert not contains_linebreaks(path)
229
return unicode(path.replace('\\', '/').replace(' ', '\ '))