1
# Copyright (C) 2005, 2006, 2009 Canonical Ltd
1
# (C) 2005 Canonical Development 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
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Serializer factory for reading and writing bundles.
22
from bzrlib import errors
26
23
from bzrlib.bundle.serializer import (BundleSerializer,
29
28
from bzrlib.bundle.serializer import binary_diff
30
29
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
31
30
from bzrlib.diff import internal_diff
32
31
from bzrlib.osutils import pathjoin
32
from bzrlib.progress import DummyProgress
33
33
from bzrlib.revision import NULL_REVISION
34
from bzrlib.rio import RioWriter, read_stanzas
34
36
from bzrlib.testament import StrictTestament
35
from bzrlib.timestamp import (
39
37
from bzrlib.textfile import text_file
40
38
from bzrlib.trace import mutter
57
55
self.properties = properties
59
def add_utf8_property(self, name, value):
60
"""Add a property whose value is currently utf8 to the action."""
61
self.properties.append((name, value.decode('utf8')))
63
57
def add_property(self, name, value):
64
58
"""Add a property to the action"""
65
59
self.properties.append((name, value))
101
95
return BundleReader(f).info
103
def check_compatible(self):
104
if self.source.supports_rich_root():
105
raise errors.IncompatibleBundleFormat('0.8', repr(self.source))
107
97
def write(self, source, revision_ids, forced_bases, f):
108
98
"""Write the bundless to the supplied files.
116
106
self.revision_ids = revision_ids
117
107
self.forced_bases = forced_bases
119
self.check_compatible()
120
109
source.lock_read()
122
111
self._write_main_header()
123
pb = ui.ui_factory.nested_progress_bar()
125
114
self._write_revisions(pb)
131
def write_bundle(self, repository, target, base, fileobj):
132
return self._write_bundle(repository, target, base, fileobj)
134
121
def _write_main_header(self):
135
122
"""Write the header for the changes"""
137
f.write(_get_bundle_header('0.8'))
124
f.write(BUNDLE_HEADER)
140
def _write(self, key, value, indent=1, trailing_space_when_empty=False):
141
"""Write out meta information, with proper indenting, etc.
143
:param trailing_space_when_empty: To work around a bug in earlier
144
bundle readers, when writing an empty property, we use "prop: \n"
145
rather than writing "prop:\n".
146
If this parameter is True, and value is the empty string, we will
147
write an extra space.
150
raise ValueError('indentation must be greater than 0')
128
def _write(self, key, value, indent=1):
129
"""Write out meta information, with proper indenting, etc"""
130
assert indent > 0, 'indentation must be greater than 0'
152
132
f.write('#' + (' ' * indent))
153
133
f.write(key.encode('utf-8'))
155
if trailing_space_when_empty and value == '':
159
elif isinstance(value, str):
163
elif isinstance(value, unicode):
136
elif isinstance(value, basestring):
165
138
f.write(value.encode('utf-8'))
169
142
for entry in value:
170
143
f.write('#' + (' ' * (indent+2)))
171
if isinstance(entry, str):
174
f.write(entry.encode('utf-8'))
144
f.write(entry.encode('utf-8'))
177
147
def _write_revisions(self, pb):
181
151
last_rev_id = None
182
152
last_rev_tree = None
184
i_max = len(self.revision_ids)
154
i_max = len(self.revision_ids)
185
155
for i, rev_id in enumerate(self.revision_ids):
186
pb.update("Generating revision data", i, i_max)
156
pb.update("Generating revsion data", i, i_max)
187
157
rev = self.source.get_revision(rev_id)
188
158
if rev_id == last_rev_id:
189
159
rev_tree = last_rev_tree
191
rev_tree = self.source.revision_tree(rev_id)
161
base_tree = self.source.revision_tree(rev_id)
162
rev_tree = self.source.revision_tree(rev_id)
192
163
if rev_id in self.forced_bases:
193
164
explicit_base = True
194
165
base_id = self.forced_bases[rev_id]
207
178
base_tree = self.source.revision_tree(base_id)
208
179
force_binary = (i != 0)
209
self._write_revision(rev, rev_tree, base_id, base_tree,
180
self._write_revision(rev, rev_tree, base_id, base_tree,
210
181
explicit_base, force_binary)
212
183
last_rev_id = base_id
213
184
last_rev_tree = base_tree
215
def _testament_sha1(self, revision_id):
216
return StrictTestament.from_revision(self.source,
217
revision_id).as_sha1()
219
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
186
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
220
187
explicit_base, force_binary):
221
188
"""Write out the information for a revision."""
222
189
def w(key, value):
230
197
self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
232
199
w('revision id', rev.revision_id)
233
w('sha1', self._testament_sha1(rev.revision_id))
200
w('sha1', StrictTestament.from_revision(self.source,
201
rev.revision_id).as_sha1())
234
202
w('inventory sha1', rev.inventory_sha1)
235
203
if rev.parent_ids:
236
204
w('parent ids', rev.parent_ids)
238
206
w('base id', base_rev)
239
207
if rev.properties:
240
208
self._write('properties', None, indent=1)
241
for name, value in sorted(rev.properties.items()):
242
self._write(name, value, indent=3,
243
trailing_space_when_empty=True)
209
for name, value in rev.properties.items():
210
self._write(name, value, indent=3)
245
212
# Add an extra blank space at the end
246
213
self.to_file.write('\n')
254
221
self.to_file.write(' // '.join(p_texts).encode('utf-8'))
255
222
self.to_file.write('\n')
257
def _write_delta(self, new_tree, old_tree, default_revision_id,
224
def _write_delta(self, new_tree, old_tree, default_revision_id,
259
226
"""Write out the changes between the trees."""
260
227
DEVNULL = '/dev/null'
277
244
old_lines = tree_lines(old_tree, require_text=True)
278
245
new_lines = tree_lines(new_tree, require_text=True)
279
246
action.write(self.to_file)
280
internal_diff(old_path, old_lines, new_path, new_lines,
247
internal_diff(old_path, old_lines, new_path, new_lines,
282
249
except errors.BinaryFile:
283
250
old_lines = tree_lines(old_tree, require_text=False)
284
251
new_lines = tree_lines(new_tree, require_text=False)
285
252
action.add_property('encoding', 'base64')
286
253
action.write(self.to_file)
287
binary_diff(old_path, old_lines, new_path, new_lines,
254
binary_diff(old_path, old_lines, new_path, new_lines,
290
257
def finish_action(action, file_id, kind, meta_modified, text_modified,
291
258
old_path, new_path):
292
259
entry = new_tree.inventory[file_id]
293
260
if entry.revision != default_revision_id:
294
action.add_utf8_property('last-changed', entry.revision)
261
action.add_property('last-changed', entry.revision)
295
262
if meta_modified:
296
263
action.add_bool_property('executable', entry.executable)
297
264
if text_modified and kind == "symlink":
302
269
action.write(self.to_file)
304
delta = new_tree.changes_from(old_tree, want_unchanged=True,
271
delta = new_tree.changes_from(old_tree, want_unchanged=True)
306
272
for path, file_id, kind in delta.removed:
307
273
action = Action('removed', [kind, path]).write(self.to_file)
309
275
for path, file_id, kind in delta.added:
310
276
action = Action('added', [kind, path], [('file-id', file_id)])
311
meta_modified = (kind=='file' and
277
meta_modified = (kind=='file' and
312
278
new_tree.is_executable(file_id))
313
279
finish_action(action, file_id, kind, meta_modified, True,
333
299
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
334
300
if new_rev != old_rev:
335
action = Action('modified', [ie.kind,
301
action = Action('modified', [ie.kind,
336
302
new_tree.id2path(ie.file_id)])
337
action.add_utf8_property('last-changed', ie.revision)
303
action.add_property('last-changed', ie.revision)
338
304
action.write(self.to_file)
350
316
object.__init__(self)
351
317
self.from_file = iter(from_file)
352
318
self._next_line = None
354
self.info = self._get_info()
320
self.info = BundleInfo()
355
321
# We put the actual inventory ids in the footer, so that the patch
356
322
# is easier to read for humans.
357
323
# Unfortunately, that means we need to read everything before we
451
414
revision_info = self.info.revisions[-1]
452
if key in revision_info.__dict__:
415
if hasattr(revision_info, key):
453
416
if getattr(revision_info, key) is None:
454
if key in ('file_id', 'revision_id', 'base_id'):
455
value = value.encode('utf8')
456
elif key in ('parent_ids'):
457
value = [v.encode('utf8') for v in value]
458
417
setattr(revision_info, key, value)
460
419
raise errors.MalformedHeader('Duplicated Key: %s' % key)
462
421
# What do we do with a key we don't recognize
463
422
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
465
424
def _read_many(self, indent):
466
425
"""If a line ends with no entry, that means that it should be
467
426
followed with multiple lines of values.
504
463
elif line.startswith('... '):
505
464
action += line[len('... '):-1].decode('utf-8')
507
if (self._next_line is not None and
466
if (self._next_line is not None and
508
467
self._next_line.startswith('===')):
509
468
return action, lines, True
510
469
elif self._next_line is None or self._next_line.startswith('#'):
524
483
action, lines, do_continue = self._read_one_patch()
525
484
if action is not None:
526
485
revision_actions.append((action, lines))
527
if self.info.revisions[-1].tree_actions is not None:
528
raise AssertionError()
486
assert self.info.revisions[-1].tree_actions is None
529
487
self.info.revisions[-1].tree_actions = revision_actions
531
489
def _read_footer(self):
542
500
# Consume the trailing \n and stop processing
543
501
self._next().next()
546
class BundleInfo08(BundleInfo):
548
def _update_tree(self, bundle_tree, revision_id):
549
bundle_tree.note_last_changed('', revision_id)
550
BundleInfo._update_tree(self, bundle_tree, revision_id)
552
def _testament_sha1_from_revision(self, repository, revision_id):
553
testament = StrictTestament.from_revision(repository, revision_id)
554
return testament.as_sha1()
556
def _testament_sha1(self, revision, inventory):
557
return StrictTestament(revision, inventory).as_sha1()