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):
100
98
"""Create a new testament for rev using inventory."""
101
self.revision_id = rev.revision_id
99
self.revision_id = str(rev.revision_id)
102
100
self.committer = rev.committer
103
101
self.timezone = rev.timezone or 0
104
102
self.timestamp = rev.timestamp
106
104
self.parent_ids = rev.parent_ids[:]
107
105
self.inventory = inventory
108
106
self.revprops = copy(rev.properties)
109
if contains_whitespace(self.revision_id):
110
raise ValueError(self.revision_id)
111
if contains_linebreaks(self.committer):
112
raise ValueError(self.committer)
107
assert not contains_whitespace(self.revision_id)
108
assert not contains_linebreaks(self.committer)
114
110
def as_text_lines(self):
115
111
"""Yield text form as a sequence of lines.
127
124
# inventory length contains the root, which is not shown here
129
126
for parent_id in sorted(self.parent_ids):
130
if contains_whitespace(parent_id):
131
raise ValueError(parent_id)
127
assert not contains_whitespace(parent_id)
132
128
a(' %s\n' % parent_id)
134
130
for l in self.message.splitlines():
136
132
a('inventory:\n')
137
for path, ie in self._get_entries():
133
for path, ie in self.inventory.iter_entries():
138
134
a(self._entry_to_line(path, ie))
139
135
r.extend(self._revprops_to_lines())
140
return [line.encode('utf-8') for line in r]
142
def _get_entries(self):
143
entries = self.inventory.iter_entries()
138
assert isinstance(l, basestring), \
139
'%r of type %s is not a plain string' % (l, type(l))
147
142
def _escape_path(self, path):
148
if contains_linebreaks(path):
149
raise ValueError(path)
150
return unicode(path.replace('\\', '/').replace(' ', '\ '))
143
assert not contains_linebreaks(path)
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"""
154
if contains_whitespace(ie.file_id):
155
raise ValueError(ie.file_id)
148
l = ' ' + str(ie.kind)
149
l += ' ' + self._escape_path(path)
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
161
raise AssertionError()
162
content = ie.text_sha1
155
l += ' ' + ie.text_sha1
164
156
elif ie.kind == 'symlink':
165
if not ie.symlink_target:
166
raise AssertionError()
167
content = self._escape_path(ie.symlink_target)
170
l = u' %s %s %s%s%s\n' % (ie.kind, self._escape_path(path),
171
ie.file_id.decode('utf8'),
172
content_spacer, content)
157
assert ie.symlink_target
158
l += ' ' + self._escape_path(ie.symlink_target)
175
162
def as_text(self):
178
165
def as_short_text(self):
179
166
"""Return short digest-based testament."""
180
return (self.short_header +
168
map(s.update, self.as_text_lines())
169
return ('bazaar-ng testament short form 1\n'
181
170
'revision-id: %s\n'
183
% (self.revision_id, self.as_sha1()))
172
% (self.revision_id, s.hexdigest()))
185
174
def _revprops_to_lines(self):
186
175
"""Pack up revision properties."""
189
178
r = ['properties:\n']
190
179
for name, value in sorted(self.revprops.items()):
191
if contains_whitespace(name):
192
raise ValueError(name)
180
assert isinstance(name, str)
181
assert not contains_whitespace(name)
193
182
r.append(' %s:\n' % name)
194
183
for line in value.splitlines():
195
r.append(u' %s\n' % line)
184
if not isinstance(line, str):
185
line = line.encode('utf-8')
186
r.append(' %s\n' % line)
200
map(s.update, self.as_text_lines())
204
class StrictTestament(Testament):
205
"""This testament format is for use as a checksum in bundle format 0.8"""
207
long_header = 'bazaar-ng testament version 2.1\n'
208
short_header = 'bazaar-ng testament short form 2.1\n'
209
def _entry_to_line(self, path, ie):
210
l = Testament._entry_to_line(self, path, ie)[:-1]
211
l += ' ' + ie.revision
212
l += {True: ' yes\n', False: ' no\n'}[ie.executable]
216
class StrictTestament3(StrictTestament):
217
"""This testament format is for use as a checksum in bundle format 0.9+
219
It differs from StrictTestament by including data about the tree root.
222
long_header = 'bazaar testament version 3 strict\n'
223
short_header = 'bazaar testament short form 3 strict\n'
224
def _get_entries(self):
225
return self.inventory.iter_entries()
227
def _escape_path(self, path):
228
if contains_linebreaks(path):
229
raise ValueError(path)
232
return unicode(path.replace('\\', '/').replace(' ', '\ '))