1
# Copyright (C) 2005, 2006, 2009 Canonical Ltd
1
# Copyright (C) 2005, 2006 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
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
120
118
source.lock_read()
122
120
self._write_main_header()
123
pb = ui.ui_factory.nested_progress_bar()
125
123
self._write_revisions(pb)
131
def write_bundle(self, repository, target, base, fileobj):
132
return self._write_bundle(repository, target, base, fileobj)
134
130
def _write_main_header(self):
135
131
"""Write the header for the changes"""
137
f.write(_get_bundle_header('0.8'))
133
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')
137
def _write(self, key, value, indent=1):
138
"""Write out meta information, with proper indenting, etc"""
139
assert indent > 0, 'indentation must be greater than 0'
152
141
f.write('#' + (' ' * indent))
153
142
f.write(key.encode('utf-8'))
155
if trailing_space_when_empty and value == '':
159
145
elif isinstance(value, str):
184
170
i_max = len(self.revision_ids)
185
171
for i, rev_id in enumerate(self.revision_ids):
186
pb.update("Generating revision data", i, i_max)
172
pb.update("Generating revsion data", i, i_max)
187
173
rev = self.source.get_revision(rev_id)
188
174
if rev_id == last_rev_id:
189
175
rev_tree = last_rev_tree
207
193
base_tree = self.source.revision_tree(base_id)
208
194
force_binary = (i != 0)
209
self._write_revision(rev, rev_tree, base_id, base_tree,
195
self._write_revision(rev, rev_tree, base_id, base_tree,
210
196
explicit_base, force_binary)
212
198
last_rev_id = base_id
213
199
last_rev_tree = base_tree
215
201
def _testament_sha1(self, revision_id):
216
return StrictTestament.from_revision(self.source,
202
return StrictTestament.from_revision(self.source,
217
203
revision_id).as_sha1()
219
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
205
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
220
206
explicit_base, force_binary):
221
207
"""Write out the information for a revision."""
222
208
def w(key, value):
238
224
w('base id', base_rev)
239
225
if rev.properties:
240
226
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)
227
for name, value in rev.properties.items():
228
self._write(name, value, indent=3)
245
230
# Add an extra blank space at the end
246
231
self.to_file.write('\n')
254
239
self.to_file.write(' // '.join(p_texts).encode('utf-8'))
255
240
self.to_file.write('\n')
257
def _write_delta(self, new_tree, old_tree, default_revision_id,
242
def _write_delta(self, new_tree, old_tree, default_revision_id,
259
244
"""Write out the changes between the trees."""
260
245
DEVNULL = '/dev/null'
277
262
old_lines = tree_lines(old_tree, require_text=True)
278
263
new_lines = tree_lines(new_tree, require_text=True)
279
264
action.write(self.to_file)
280
internal_diff(old_path, old_lines, new_path, new_lines,
265
internal_diff(old_path, old_lines, new_path, new_lines,
282
267
except errors.BinaryFile:
283
268
old_lines = tree_lines(old_tree, require_text=False)
284
269
new_lines = tree_lines(new_tree, require_text=False)
285
270
action.add_property('encoding', 'base64')
286
271
action.write(self.to_file)
287
binary_diff(old_path, old_lines, new_path, new_lines,
272
binary_diff(old_path, old_lines, new_path, new_lines,
290
275
def finish_action(action, file_id, kind, meta_modified, text_modified,
309
294
for path, file_id, kind in delta.added:
310
295
action = Action('added', [kind, path], [('file-id', file_id)])
311
meta_modified = (kind=='file' and
296
meta_modified = (kind=='file' and
312
297
new_tree.is_executable(file_id))
313
298
finish_action(action, file_id, kind, meta_modified, True,
333
318
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
334
319
if new_rev != old_rev:
335
action = Action('modified', [ie.kind,
320
action = Action('modified', [ie.kind,
336
321
new_tree.id2path(ie.file_id)])
337
322
action.add_utf8_property('last-changed', ie.revision)
338
323
action.write(self.to_file)
350
335
object.__init__(self)
351
336
self.from_file = iter(from_file)
352
337
self._next_line = None
354
339
self.info = self._get_info()
355
340
# We put the actual inventory ids in the footer, so that the patch
356
341
# is easier to read for humans.
462
447
# What do we do with a key we don't recognize
463
448
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
465
450
def _read_many(self, indent):
466
451
"""If a line ends with no entry, that means that it should be
467
452
followed with multiple lines of values.
504
489
elif line.startswith('... '):
505
490
action += line[len('... '):-1].decode('utf-8')
507
if (self._next_line is not None and
492
if (self._next_line is not None and
508
493
self._next_line.startswith('===')):
509
494
return action, lines, True
510
495
elif self._next_line is None or self._next_line.startswith('#'):
524
509
action, lines, do_continue = self._read_one_patch()
525
510
if action is not None:
526
511
revision_actions.append((action, lines))
527
if self.info.revisions[-1].tree_actions is not None:
528
raise AssertionError()
512
assert self.info.revisions[-1].tree_actions is None
529
513
self.info.revisions[-1].tree_actions = revision_actions
531
515
def _read_footer(self):