1
# (C) 2005 Canonical Development 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
22
22
from bzrlib import errors
23
23
from bzrlib.bundle.serializer import (BundleSerializer,
28
26
from bzrlib.bundle.serializer import binary_diff
29
27
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
31
29
from bzrlib.osutils import pathjoin
32
30
from bzrlib.progress import DummyProgress
33
31
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
38
from bzrlib.textfile import text_file
38
39
from bzrlib.trace import mutter
55
56
self.properties = properties
58
def add_utf8_property(self, name, value):
59
"""Add a property whose value is currently utf8 to the action."""
60
self.properties.append((name, value.decode('utf8')))
57
62
def add_property(self, name, value):
58
63
"""Add a property to the action"""
59
64
self.properties.append((name, value))
95
100
return BundleReader(f).info
102
def check_compatible(self):
103
if self.source.supports_rich_root():
104
raise errors.IncompatibleBundleFormat('0.8', repr(self.source))
97
106
def write(self, source, revision_ids, forced_bases, f):
98
107
"""Write the bundless to the supplied files.
131
def write_bundle(self, repository, target, base, fileobj):
132
return self._write_bundle(repository, target, base, fileobj)
121
134
def _write_main_header(self):
122
135
"""Write the header for the changes"""
124
f.write(BUNDLE_HEADER)
137
f.write(_get_bundle_header('0.8'))
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'
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')
132
152
f.write('#' + (' ' * indent))
133
153
f.write(key.encode('utf-8'))
136
elif isinstance(value, basestring):
155
if trailing_space_when_empty and value == '':
159
elif isinstance(value, str):
163
elif isinstance(value, unicode):
138
165
f.write(value.encode('utf-8'))
142
169
for entry in value:
143
170
f.write('#' + (' ' * (indent+2)))
144
f.write(entry.encode('utf-8'))
171
if isinstance(entry, str):
174
f.write(entry.encode('utf-8'))
147
177
def _write_revisions(self, pb):
151
181
last_rev_id = None
152
182
last_rev_tree = None
154
i_max = len(self.revision_ids)
184
i_max = len(self.revision_ids)
155
185
for i, rev_id in enumerate(self.revision_ids):
156
186
pb.update("Generating revsion data", i, i_max)
157
187
rev = self.source.get_revision(rev_id)
177
207
base_tree = self.source.revision_tree(base_id)
178
208
force_binary = (i != 0)
179
self._write_revision(rev, rev_tree, base_id, base_tree,
209
self._write_revision(rev, rev_tree, base_id, base_tree,
180
210
explicit_base, force_binary)
182
212
last_rev_id = base_id
183
213
last_rev_tree = base_tree
185
def _write_revision(self, rev, rev_tree, base_rev, 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
220
explicit_base, force_binary):
187
221
"""Write out the information for a revision."""
188
222
def w(key, value):
196
230
self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
198
232
w('revision id', rev.revision_id)
199
w('sha1', StrictTestament.from_revision(self.source,
200
rev.revision_id).as_sha1())
233
w('sha1', self._testament_sha1(rev.revision_id))
201
234
w('inventory sha1', rev.inventory_sha1)
202
235
if rev.parent_ids:
203
236
w('parent ids', rev.parent_ids)
205
238
w('base id', base_rev)
206
239
if rev.properties:
207
240
self._write('properties', None, indent=1)
208
for name, value in rev.properties.items():
209
self._write(name, value, indent=3)
241
for name, value in sorted(rev.properties.items()):
242
self._write(name, value, indent=3,
243
trailing_space_when_empty=True)
211
245
# Add an extra blank space at the end
212
246
self.to_file.write('\n')
220
254
self.to_file.write(' // '.join(p_texts).encode('utf-8'))
221
255
self.to_file.write('\n')
223
def _write_delta(self, new_tree, old_tree, default_revision_id,
257
def _write_delta(self, new_tree, old_tree, default_revision_id,
225
259
"""Write out the changes between the trees."""
226
260
DEVNULL = '/dev/null'
243
277
old_lines = tree_lines(old_tree, require_text=True)
244
278
new_lines = tree_lines(new_tree, require_text=True)
245
279
action.write(self.to_file)
246
internal_diff(old_path, old_lines, new_path, new_lines,
280
internal_diff(old_path, old_lines, new_path, new_lines,
248
282
except errors.BinaryFile:
249
283
old_lines = tree_lines(old_tree, require_text=False)
250
284
new_lines = tree_lines(new_tree, require_text=False)
251
285
action.add_property('encoding', 'base64')
252
286
action.write(self.to_file)
253
binary_diff(old_path, old_lines, new_path, new_lines,
287
binary_diff(old_path, old_lines, new_path, new_lines,
256
290
def finish_action(action, file_id, kind, meta_modified, text_modified,
257
291
old_path, new_path):
258
292
entry = new_tree.inventory[file_id]
259
293
if entry.revision != default_revision_id:
260
action.add_property('last-changed', entry.revision)
294
action.add_utf8_property('last-changed', entry.revision)
261
295
if meta_modified:
262
296
action.add_bool_property('executable', entry.executable)
263
297
if text_modified and kind == "symlink":
268
302
action.write(self.to_file)
270
delta = new_tree.changes_from(old_tree, want_unchanged=True)
304
delta = new_tree.changes_from(old_tree, want_unchanged=True,
271
306
for path, file_id, kind in delta.removed:
272
307
action = Action('removed', [kind, path]).write(self.to_file)
274
309
for path, file_id, kind in delta.added:
275
310
action = Action('added', [kind, path], [('file-id', file_id)])
276
meta_modified = (kind=='file' and
311
meta_modified = (kind=='file' and
277
312
new_tree.is_executable(file_id))
278
313
finish_action(action, file_id, kind, meta_modified, True,
298
333
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
299
334
if new_rev != old_rev:
300
action = Action('modified', [ie.kind,
335
action = Action('modified', [ie.kind,
301
336
new_tree.id2path(ie.file_id)])
302
action.add_property('last-changed', ie.revision)
337
action.add_utf8_property('last-changed', ie.revision)
303
338
action.write(self.to_file)
315
350
object.__init__(self)
316
351
self.from_file = iter(from_file)
317
352
self._next_line = None
319
self.info = BundleInfo08()
354
self.info = self._get_info()
320
355
# We put the actual inventory ids in the footer, so that the patch
321
356
# is easier to read for humans.
322
357
# Unfortunately, that means we need to read everything before we
413
451
revision_info = self.info.revisions[-1]
414
452
if key in revision_info.__dict__:
415
453
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]
416
458
setattr(revision_info, key, value)
418
460
raise errors.MalformedHeader('Duplicated Key: %s' % key)
420
462
# What do we do with a key we don't recognize
421
463
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
423
465
def _read_many(self, indent):
424
466
"""If a line ends with no entry, that means that it should be
425
467
followed with multiple lines of values.
462
504
elif line.startswith('... '):
463
505
action += line[len('... '):-1].decode('utf-8')
465
if (self._next_line is not None and
507
if (self._next_line is not None and
466
508
self._next_line.startswith('===')):
467
509
return action, lines, True
468
510
elif self._next_line is None or self._next_line.startswith('#'):
482
524
action, lines, do_continue = self._read_one_patch()
483
525
if action is not None:
484
526
revision_actions.append((action, lines))
485
assert self.info.revisions[-1].tree_actions is None
527
if self.info.revisions[-1].tree_actions is not None:
528
raise AssertionError()
486
529
self.info.revisions[-1].tree_actions = revision_actions
488
531
def _read_footer(self):
500
543
self._next().next()
504
546
class BundleInfo08(BundleInfo):
505
548
def _update_tree(self, bundle_tree, revision_id):
506
549
bundle_tree.note_last_changed('', revision_id)
507
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()