1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2009 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Serializer factory for reading and writing bundles.
20
from __future__ import absolute_import
22
from bzrlib import errors
23
26
from bzrlib.bundle.serializer import (BundleSerializer,
28
29
from bzrlib.bundle.serializer import binary_diff
29
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
30
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo)
30
31
from bzrlib.diff import internal_diff
31
from bzrlib.osutils import pathjoin
32
from bzrlib.progress import DummyProgress
33
32
from bzrlib.revision import NULL_REVISION
34
from bzrlib.rio import RioWriter, read_stanzas
36
33
from bzrlib.testament import StrictTestament
34
from bzrlib.timestamp import (
37
37
from bzrlib.textfile import text_file
38
38
from bzrlib.trace import mutter
55
55
self.properties = properties
57
def add_utf8_property(self, name, value):
58
"""Add a property whose value is currently utf8 to the action."""
59
self.properties.append((name, value.decode('utf8')))
57
61
def add_property(self, name, value):
58
62
"""Add a property to the action"""
59
63
self.properties.append((name, value))
114
118
source.lock_read()
116
120
self._write_main_header()
121
pb = ui.ui_factory.nested_progress_bar()
119
123
self._write_revisions(pb)
129
def write_bundle(self, repository, target, base, fileobj):
130
return self._write_bundle(repository, target, base, fileobj)
126
132
def _write_main_header(self):
127
133
"""Write the header for the changes"""
129
f.write(BUNDLE_HEADER)
135
f.write(_get_bundle_header('0.8'))
133
def _write(self, key, value, indent=1):
134
"""Write out meta information, with proper indenting, etc"""
135
assert indent > 0, 'indentation must be greater than 0'
138
def _write(self, key, value, indent=1, trailing_space_when_empty=False):
139
"""Write out meta information, with proper indenting, etc.
141
:param trailing_space_when_empty: To work around a bug in earlier
142
bundle readers, when writing an empty property, we use "prop: \n"
143
rather than writing "prop:\n".
144
If this parameter is True, and value is the empty string, we will
145
write an extra space.
148
raise ValueError('indentation must be greater than 0')
137
150
f.write('#' + (' ' * indent))
138
151
f.write(key.encode('utf-8'))
141
elif isinstance(value, basestring):
153
if trailing_space_when_empty and value == '':
157
elif isinstance(value, str):
161
elif isinstance(value, unicode):
143
163
f.write(value.encode('utf-8'))
147
167
for entry in value:
148
168
f.write('#' + (' ' * (indent+2)))
149
f.write(entry.encode('utf-8'))
169
if isinstance(entry, str):
172
f.write(entry.encode('utf-8'))
152
175
def _write_revisions(self, pb):
156
179
last_rev_id = None
157
180
last_rev_tree = None
159
i_max = len(self.revision_ids)
182
i_max = len(self.revision_ids)
160
183
for i, rev_id in enumerate(self.revision_ids):
161
pb.update("Generating revsion data", i, i_max)
184
pb.update("Generating revision data", i, i_max)
162
185
rev = self.source.get_revision(rev_id)
163
186
if rev_id == last_rev_id:
164
187
rev_tree = last_rev_tree
182
205
base_tree = self.source.revision_tree(base_id)
183
206
force_binary = (i != 0)
184
self._write_revision(rev, rev_tree, base_id, base_tree,
207
self._write_revision(rev, rev_tree, base_id, base_tree,
185
208
explicit_base, force_binary)
187
210
last_rev_id = base_id
188
211
last_rev_tree = base_tree
190
213
def _testament_sha1(self, revision_id):
191
return StrictTestament.from_revision(self.source,
214
return StrictTestament.from_revision(self.source,
192
215
revision_id).as_sha1()
194
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
217
def _write_revision(self, rev, rev_tree, base_rev, base_tree,
195
218
explicit_base, force_binary):
196
219
"""Write out the information for a revision."""
197
220
def w(key, value):
213
236
w('base id', base_rev)
214
237
if rev.properties:
215
238
self._write('properties', None, indent=1)
216
for name, value in rev.properties.items():
217
self._write(name, value, indent=3)
239
for name, value in sorted(rev.properties.items()):
240
self._write(name, value, indent=3,
241
trailing_space_when_empty=True)
219
243
# Add an extra blank space at the end
220
244
self.to_file.write('\n')
228
252
self.to_file.write(' // '.join(p_texts).encode('utf-8'))
229
253
self.to_file.write('\n')
231
def _write_delta(self, new_tree, old_tree, default_revision_id,
255
def _write_delta(self, new_tree, old_tree, default_revision_id,
233
257
"""Write out the changes between the trees."""
234
258
DEVNULL = '/dev/null'
238
262
def do_diff(file_id, old_path, new_path, action, force_binary):
239
263
def tree_lines(tree, require_text=False):
264
if tree.has_id(file_id):
241
265
tree_file = tree.get_file(file_id)
242
266
if require_text is True:
243
267
tree_file = text_file(tree_file)
251
275
old_lines = tree_lines(old_tree, require_text=True)
252
276
new_lines = tree_lines(new_tree, require_text=True)
253
277
action.write(self.to_file)
254
internal_diff(old_path, old_lines, new_path, new_lines,
278
internal_diff(old_path, old_lines, new_path, new_lines,
256
280
except errors.BinaryFile:
257
281
old_lines = tree_lines(old_tree, require_text=False)
258
282
new_lines = tree_lines(new_tree, require_text=False)
259
283
action.add_property('encoding', 'base64')
260
284
action.write(self.to_file)
261
binary_diff(old_path, old_lines, new_path, new_lines,
285
binary_diff(old_path, old_lines, new_path, new_lines,
264
288
def finish_action(action, file_id, kind, meta_modified, text_modified,
265
289
old_path, new_path):
266
entry = new_tree.inventory[file_id]
290
entry = new_tree.root_inventory[file_id]
267
291
if entry.revision != default_revision_id:
268
action.add_property('last-changed', entry.revision)
292
action.add_utf8_property('last-changed', entry.revision)
269
293
if meta_modified:
270
294
action.add_bool_property('executable', entry.executable)
271
295
if text_modified and kind == "symlink":
283
307
for path, file_id, kind in delta.added:
284
308
action = Action('added', [kind, path], [('file-id', file_id)])
285
meta_modified = (kind=='file' and
309
meta_modified = (kind=='file' and
286
310
new_tree.is_executable(file_id))
287
311
finish_action(action, file_id, kind, meta_modified, True,
302
326
for path, file_id, kind in delta.unchanged:
303
ie = new_tree.inventory[file_id]
304
new_rev = getattr(ie, 'revision', None)
327
new_rev = new_tree.get_file_revision(file_id)
305
328
if new_rev is None:
307
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
330
old_rev = old_tree.get_file_revision(file_id)
308
331
if new_rev != old_rev:
309
action = Action('modified', [ie.kind,
310
new_tree.id2path(ie.file_id)])
311
action.add_property('last-changed', ie.revision)
332
action = Action('modified', [new_tree.kind(file_id),
333
new_tree.id2path(file_id)])
334
action.add_utf8_property('last-changed', new_rev)
312
335
action.write(self.to_file)
324
347
object.__init__(self)
325
348
self.from_file = iter(from_file)
326
349
self._next_line = None
328
351
self.info = self._get_info()
329
352
# We put the actual inventory ids in the footer, so that the patch
330
353
# is easier to read for humans.
425
448
revision_info = self.info.revisions[-1]
426
449
if key in revision_info.__dict__:
427
450
if getattr(revision_info, key) is None:
451
if key in ('file_id', 'revision_id', 'base_id'):
452
value = value.encode('utf8')
453
elif key in ('parent_ids'):
454
value = [v.encode('utf8') for v in value]
428
455
setattr(revision_info, key, value)
430
457
raise errors.MalformedHeader('Duplicated Key: %s' % key)
432
459
# What do we do with a key we don't recognize
433
460
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
435
462
def _read_many(self, indent):
436
463
"""If a line ends with no entry, that means that it should be
437
464
followed with multiple lines of values.
474
501
elif line.startswith('... '):
475
502
action += line[len('... '):-1].decode('utf-8')
477
if (self._next_line is not None and
504
if (self._next_line is not None and
478
505
self._next_line.startswith('===')):
479
506
return action, lines, True
480
507
elif self._next_line is None or self._next_line.startswith('#'):
494
521
action, lines, do_continue = self._read_one_patch()
495
522
if action is not None:
496
523
revision_actions.append((action, lines))
497
assert self.info.revisions[-1].tree_actions is None
524
if self.info.revisions[-1].tree_actions is not None:
525
raise AssertionError()
498
526
self.info.revisions[-1].tree_actions = revision_actions
500
528
def _read_footer(self):
523
550
testament = StrictTestament.from_revision(repository, revision_id)
524
551
return testament.as_sha1()
526
def _testament_sha1(self, revision, inventory):
527
return StrictTestament(revision, inventory).as_sha1()
553
def _testament_sha1(self, revision, tree):
554
return StrictTestament(revision, tree).as_sha1()